我要清除一个数据库中所有表的数据,写了个脚本但是报错
看看我写代码,我想清楚数据库中所有表中的数据
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;
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;