看看我写代码,我想清楚数据库中所有表中的数据
DECLARE @tbname VARCHAR(50);
DECLARE cur CURSOR FOR SELECT name FROM sys.sysobjects;
OPEN cur;
FETCH NEXT FROM cur INTO @tbname;
WHILE @@FETCH_STATUS=0
BEGIN
TRUNCATE TABLE @tbname;
FETCH NEXT FROM cur INTO @tbname;
END;
CLOSE cur;
DEALLOCATE cur;
TRUNCATE TABLE @tbname; 这句提示有错误。请问想实现我这个功能,应该怎么写???
------解决方案--------------------
楼主,sys.sysobjects这个里面不止包含的是表名,如果是其他对象名称,你的语句会出问题
考虑用
select * from sys.tables
------解决方案--------------------
DECLARE @tbname VARCHAR(50);
DECLARE cur CURSOR FOR SELECT name FROM sys.tables;
OPEN cur;
FETCH NEXT FROM cur INTO @tbname;
WHILE @@FETCH_STATUS=0
BEGIN
exec('TRUNCATE TABLE '+@tbname);
FETCH NEXT FROM cur INTO @tbname;
END;
CLOSE cur;
DEALLOCATE cur;
------解决方案--------------------
有些表有外键 需要级联删除或者从子表开始删起。