清空数据库中所有表数据的SQL

某些虚似主机就是讨厌,申请好数据库后,数据库名就自动给你无情地建好了,改名也没有权限。如果要换数据库,只能一个一个的去删表,太繁琐,所以需要一次性地把表给删除了!

declare crsr insensitive cursor
for SELECT [name] FROM DBO.SYSOBJECTS
WHERE OBJECTPROPERTY(ID,N'IsTable')=1 and type = 'U' and [name] <> 'dtproperties' --and crdate...
for read only
open crsr
declare @tblName sysname
fetch next from crsr into @tblName 

while (@@fetch_status<>-1)
begin
EXEC('drop table '+@tblName)
fetch next from crsr into @tblName
end
close crsr
deallocate crsr --删除以释放游标

 

如果只是清空数据,把11行的drop改成truncate就行了。

Leave a Reply