SQL实现删除所有表数据

来源:互联网 发布:vmware虚拟机安装linux 编辑:程序博客网 时间:2024/05/16 08:46
DECLARE tables_cursor CURSOR
FOR
SELECT name FROM sysobjects WHERE type = 'U'--选择用户表名
OPEN tables_cursor --打开游标连接  
DECLARE @tablename sysname   -- 定义变量
FETCH NEXT FROM tables_cursor INTO @tablename 
   --结果集中一行一行读取表名
WHILE (@@FETCH_STATUS <> -1) --判断游标状态 
   BEGIN EXEC ('delete from ' + '['+@tablename+']')  --清空表中的数据 
   FETCH NEXT FROM tables_cursor INTO @tablename --下一行数据
END   DEALLOCATE tables_cursor --关闭游标
0 0
原创粉丝点击