代码改变世界

勤于思考:游标删除数据库所有表数据

2013-07-05 11:46  薛凯凯圆滚滚  阅读(259)  评论(0编辑  收藏  举报

使用游标删除库的所有表

DECLARE @tablename varchar(20) 
DECLARE CUR CURSOR FOR SELECT name FROM sysobjects WHERE type = 'U' 
OPEN CUR 
FETCH NEXT FROM CUR INTO @tablename
WHILE @@FETCH_STATUS=0
BEGIN
EXEC ('TRUNCATE TABLE ' + @tablename) 
FETCH NEXT FROM CUR INTO @tablename
END
CLOSE CUR;
DEALLOCATE CUR;