删除数据库所有用户表

USE DATABASENAME 
--DATABASENAME 是待清理的数据库  
  
GO  
  
DECLARE @au_lname VARCHAR(40),@SQLString NVARCHAR(500)  
  
DECLARE tb CURSOR FOR  
SELECT [name] FROM sysobjects WHERE xtype='U'  
  
OPEN tb  
  
-- Perform the FIRST FETCH.  
FETCH NEXT FROM tb INTO @au_lname  
  
-- CHECK @@FETCH_STATUS TO see IF there ARE ANY more ROWS TO FETCH.  
WHILE @@FETCH_STATUS = 0  
BEGIN  
-- This IS executed AS long AS the previous FETCH succeeds.  
FETCH NEXT FROM tb INTO @au_lname  
  
SET @SQLString='DROP TABLE '+ 'dbo.['+@au_lname+']'  
EXEC sp_executesql @SQLString  
--DROP TABLE @au_lname  
  
  
END  
  
CLOSE tb  
DEALLOCATE tb  
  
GO  
posted @ 2011-12-12 11:20  编程笔记  阅读(630)  评论(0编辑  收藏  举报