MS SQLSERVER 一次性删除所有表以及视图等等

DECLARE @tablename varchar(50)
DECLARE @type varchar(50)
DECLARE @truncatesql varchar(255)
DECLARE TrCun_Cursor CURSOR FOR
select [name],[type] from sysobjects where type = 'U' or type='V'
--有条件的清空表 name<>'不想清空的表名'--
OPEN TrCun_Cursor
FETCH TrCun_Cursor INTO @tablename,@type
WHILE(@@fetch_status = 0)
BEGIN
    IF @type='V'
        SET @truncatesql = 'drop view ' + @tablename
        exec(@truncatesql) --当要删除时,就去掉--
        PRINT @truncatesql
    IF @type='U'
        SET @truncatesql = 'drop table ' + @tablename
        exec(@truncatesql) --当要删除时,就去掉--
        PRINT @truncatesql
    FETCH TrCun_Cursor INTO @tablename,@type
END
CLOSE TrCun_Cursor
DEALLOCATE TrCun_Cursor
posted @ 2013-01-02 21:44  CoderNet  阅读(276)  评论(0编辑  收藏  举报