1.删除存储过程
Code
BEGIN TRANSACTION
DECLARE drop_p_cursor CURSOR FOR
SELECT 'DROP PROCEDURE ' + [Name] FROM sysobjects WHERE xtype = 'p' AND category = 0
OPEN drop_p_cursor
DECLARE @sql sysname
FETCH NEXT FROM drop_p_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
EXEC(@sql)
FETCH NEXT FROM drop_p_cursor INTO @sql
BEGIN TRANSACTION
DECLARE drop_p_cursor CURSOR FOR
SELECT 'DROP PROCEDURE ' + [Name] FROM sysobjects WHERE xtype = 'p' AND category = 0
OPEN drop_p_cursor
DECLARE @sql sysname
FETCH NEXT FROM drop_p_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
EXEC(@sql)
FETCH NEXT FROM drop_p_cursor INTO @sql
2. 删除表
注意:删除表的时候先删除约束。NOCHECK在此例中不起作用,必须用DROP。
Code
BEGIN TRANSACTION
DECLARE @sql sysname
DECLARE drop_u_cursor CURSOR FOR
SELECT 'DROP TABLE ' + [Name] FROM sysobjects WHERE xtype = 'u' AND category = 0
DECLARE forbiden_f_cursor CURSOR FOR
SELECT 'ALTER TABLE ' + tbl.Name + ' DROP CONSTRAINT ' + cst.Name
FROM sysobjects tbl, sysobjects cst WHERE cst.xtype = 'f' AND cst.parent_obj = tbl.id
OPEN forbiden_f_cursor
FETCH NEXT FROM forbiden_f_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT @sql
EXEC(@sql)
FETCH NEXT FROM forbiden_f_cursor INTO @sql
END
CLOSE forbiden_f_cursor
DEALLOCATE forbiden_f_cursor
OPEN drop_u_cursor
FETCH NEXT FROM drop_u_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT @sql
EXEC(@sql)
FETCH NEXT FROM drop_u_cursor INTO @sql
END
CLOSE drop_u_cursor
DEALLOCATE drop_u_cursor
GO
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END
ELSE
COMMIT TRANSACTION
BEGIN TRANSACTION
DECLARE @sql sysname
DECLARE drop_u_cursor CURSOR FOR
SELECT 'DROP TABLE ' + [Name] FROM sysobjects WHERE xtype = 'u' AND category = 0
DECLARE forbiden_f_cursor CURSOR FOR
SELECT 'ALTER TABLE ' + tbl.Name + ' DROP CONSTRAINT ' + cst.Name
FROM sysobjects tbl, sysobjects cst WHERE cst.xtype = 'f' AND cst.parent_obj = tbl.id
OPEN forbiden_f_cursor
FETCH NEXT FROM forbiden_f_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT @sql
EXEC(@sql)
FETCH NEXT FROM forbiden_f_cursor INTO @sql
END
CLOSE forbiden_f_cursor
DEALLOCATE forbiden_f_cursor
OPEN drop_u_cursor
FETCH NEXT FROM drop_u_cursor INTO @sql
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT @sql
EXEC(@sql)
FETCH NEXT FROM drop_u_cursor INTO @sql
END
CLOSE drop_u_cursor
DEALLOCATE drop_u_cursor
GO
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END
ELSE
COMMIT TRANSACTION