Sqlserver数据库drop全部表

DECLARE C1 CURSOR FOR
SELECT 'ALTER TABLE ['+ OBJECT_NAME(PARENT_OBJ) + '] DROP CONSTRAINT ['+NAME+']; '
FROM SYSOBJECTS
WHERE XTYPE = 'F'
OPEN C1
DECLARE @C1 VARCHAR(8000)
FETCH NEXT FROM C1 INTO @C1
WHILE(@@FETCH_STATUS=0)
BEGIN
EXEC(@C1)
FETCH NEXT FROM C1 INTO @C1
END
CLOSE C1
DEALLOCATE C1


DECLARE @SQL VARCHAR(8000)
WHILE (SELECT COUNT(*) FROM SYSOBJECTS WHERE TYPE='U')>0
BEGIN
SELECT @SQL='DROP TABLE ' + NAME
FROM SYSOBJECTS
WHERE (TYPE = 'U')
ORDER BY 'DROP TABLE ' + NAME
EXEC(@SQL)
END

 

posted on 2024-08-02 10:37  技术高超  阅读(18)  评论(0编辑  收藏  举报