0

在我正在處理的MVC Web應用程序中,我有4個特定的表,這些表使用並自動生成角色身份驗證。在創建遷移併爲我的數據庫創建種子後,我運行創建這4個表的應用程序。在VS2012中創建存儲過程以一次刪除4個表格?

每當我必須修改我的模型時,由於用戶表上的引用約束,我不得不刪除這4個表。 VS2012不允許我一次選擇所有4個表並刪除,所以我想知道是否有一種方法可以創建一個存儲過程,我可以執行4個刪除腳本一次運行嗎?

根據Server Explorer我右鍵單擊Stored Prcoedures,然後選擇Add New Stored Procedure。我則提出了以下模板:

CREATE PROCEDURE [dbo].[Procedure] 
    @param1 int = 0, 
    @param2 int 
AS 
    SELECT @param1, @param2 
RETURN 0 

通過當我點擊DELETE爲每個表的我抄在下面自動生成的DROP TABLE命令的GUI。

如何在VS2012中創建一個存儲過程(或多個?),以便在一個命令中刪除所有4個表?

/* 
Deployment script for RandomDBdev 

This code was generated by a tool. 
Changes to this file may cause incorrect behavior and will be lost if 
the code is regenerated. 
*/ 

GO 
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; 

SET NUMERIC_ROUNDABORT OFF; 


GO 
:setvar DatabaseName "RandomDBdev" 
:setvar DefaultFilePrefix "RandomDBdev" 
:setvar DefaultDataPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 
:setvar DefaultLogPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 

GO 
:on error exit 
GO 
/* 
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported. 
To re-enable the script after enabling SQLCMD mode, execute the following: 
SET NOEXEC OFF; 
*/ 
:setvar __IsSqlCmdEnabled "True" 
GO 
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True' 
    BEGIN 
     PRINT N'SQLCMD mode must be enabled to successfully execute this script.'; 
     SET NOEXEC ON; 
    END 


GO 
USE [$(DatabaseName)]; 


GO 

IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors 
GO 
CREATE TABLE #tmpErrors (Error int) 
GO 
SET XACT_ABORT ON 
GO 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
GO 
BEGIN TRANSACTION 
GO 
PRINT N'Dropping [dbo].[webpages_Membership]...'; 


GO 
DROP TABLE [dbo].[webpages_Membership]; 


GO 
IF @@ERROR <> 0 
    AND @@TRANCOUNT > 0 
    BEGIN 
     ROLLBACK; 
    END 

IF @@TRANCOUNT = 0 
    BEGIN 
     INSERT INTO #tmpErrors (Error) 
     VALUES     (1); 
     BEGIN TRANSACTION; 
    END 


GO 

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION 
GO 
IF @@TRANCOUNT>0 BEGIN 
PRINT N'The transacted portion of the database update succeeded.' 
COMMIT TRANSACTION 
END 
ELSE PRINT N'The transacted portion of the database update failed.' 
GO 
DROP TABLE #tmpErrors 
GO 
PRINT N'Update complete.'; 


GO 
/*----------------------------------------------------------------------------------------------------------------------*/ 
/* 
Deployment script for RandomDBdev 

This code was generated by a tool. 
Changes to this file may cause incorrect behavior and will be lost if 
the code is regenerated. 
*/ 

GO 
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; 

SET NUMERIC_ROUNDABORT OFF; 


GO 
:setvar DatabaseName "RandomDBdev" 
:setvar DefaultFilePrefix "RandomDBdev" 
:setvar DefaultDataPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 
:setvar DefaultLogPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 

GO 
:on error exit 
GO 
/* 
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported. 
To re-enable the script after enabling SQLCMD mode, execute the following: 
SET NOEXEC OFF; 
*/ 
:setvar __IsSqlCmdEnabled "True" 
GO 
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True' 
    BEGIN 
     PRINT N'SQLCMD mode must be enabled to successfully execute this script.'; 
     SET NOEXEC ON; 
    END 


GO 
USE [$(DatabaseName)]; 


GO 

IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors 
GO 
CREATE TABLE #tmpErrors (Error int) 
GO 
SET XACT_ABORT ON 
GO 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
GO 
BEGIN TRANSACTION 
GO 
PRINT N'Dropping [dbo].[webpages_OAuthMembership]...'; 


GO 
DROP TABLE [dbo].[webpages_OAuthMembership]; 


GO 
IF @@ERROR <> 0 
    AND @@TRANCOUNT > 0 
    BEGIN 
     ROLLBACK; 
    END 

IF @@TRANCOUNT = 0 
    BEGIN 
     INSERT INTO #tmpErrors (Error) 
     VALUES     (1); 
     BEGIN TRANSACTION; 
    END 


GO 

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION 
GO 
IF @@TRANCOUNT>0 BEGIN 
PRINT N'The transacted portion of the database update succeeded.' 
COMMIT TRANSACTION 
END 
ELSE PRINT N'The transacted portion of the database update failed.' 
GO 
DROP TABLE #tmpErrors 
GO 
PRINT N'Update complete.'; 


GO 
/*----------------------------------------------------------------------------------------------------------------------*/ 
/* 
Deployment script for RandomDBdev 

This code was generated by a tool. 
Changes to this file may cause incorrect behavior and will be lost if 
the code is regenerated. 
*/ 

GO 
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; 

SET NUMERIC_ROUNDABORT OFF; 


GO 
:setvar DatabaseName "RandomDBdev" 
:setvar DefaultFilePrefix "RandomDBdev" 
:setvar DefaultDataPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 
:setvar DefaultLogPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 

GO 
:on error exit 
GO 
/* 
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported. 
To re-enable the script after enabling SQLCMD mode, execute the following: 
SET NOEXEC OFF; 
*/ 
:setvar __IsSqlCmdEnabled "True" 
GO 
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True' 
    BEGIN 
     PRINT N'SQLCMD mode must be enabled to successfully execute this script.'; 
     SET NOEXEC ON; 
    END 


GO 
USE [$(DatabaseName)]; 


GO 

IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors 
GO 
CREATE TABLE #tmpErrors (Error int) 
GO 
SET XACT_ABORT ON 
GO 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
GO 
BEGIN TRANSACTION 
GO 
PRINT N'Dropping fk_RoleId...'; 


GO 
ALTER TABLE [dbo].[webpages_UsersInRoles] DROP CONSTRAINT [fk_RoleId]; 


GO 
IF @@ERROR <> 0 
    AND @@TRANCOUNT > 0 
    BEGIN 
     ROLLBACK; 
    END 

IF @@TRANCOUNT = 0 
    BEGIN 
     INSERT INTO #tmpErrors (Error) 
     VALUES     (1); 
     BEGIN TRANSACTION; 
    END 


GO 
PRINT N'Dropping [dbo].[webpages_Roles]...'; 


GO 
DROP TABLE [dbo].[webpages_Roles]; 


GO 
IF @@ERROR <> 0 
    AND @@TRANCOUNT > 0 
    BEGIN 
     ROLLBACK; 
    END 

IF @@TRANCOUNT = 0 
    BEGIN 
     INSERT INTO #tmpErrors (Error) 
     VALUES     (1); 
     BEGIN TRANSACTION; 
    END 


GO 

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION 
GO 
IF @@TRANCOUNT>0 BEGIN 
PRINT N'The transacted portion of the database update succeeded.' 
COMMIT TRANSACTION 
END 
ELSE PRINT N'The transacted portion of the database update failed.' 
GO 
DROP TABLE #tmpErrors 
GO 
PRINT N'Update complete.'; 


GO 
/*----------------------------------------------------------------------------------------------------------------------*/ 
/* 
Deployment script for RandomDBdev 

This code was generated by a tool. 
Changes to this file may cause incorrect behavior and will be lost if 
the code is regenerated. 
*/ 

GO 
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; 

SET NUMERIC_ROUNDABORT OFF; 


GO 
:setvar DatabaseName "RandomDBdev" 
:setvar DefaultFilePrefix "RandomDBdev" 
:setvar DefaultDataPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 
:setvar DefaultLogPath "C:\Users\arandomuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0\" 

GO 
:on error exit 
GO 
/* 
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported. 
To re-enable the script after enabling SQLCMD mode, execute the following: 
SET NOEXEC OFF; 
*/ 
:setvar __IsSqlCmdEnabled "True" 
GO 
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True' 
    BEGIN 
     PRINT N'SQLCMD mode must be enabled to successfully execute this script.'; 
     SET NOEXEC ON; 
    END 


GO 
USE [$(DatabaseName)]; 


GO 

IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors 
GO 
CREATE TABLE #tmpErrors (Error int) 
GO 
SET XACT_ABORT ON 
GO 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED 
GO 
BEGIN TRANSACTION 
GO 
PRINT N'Dropping [dbo].[webpages_UsersInRoles]...'; 


GO 
DROP TABLE [dbo].[webpages_UsersInRoles]; 


GO 
IF @@ERROR <> 0 
    AND @@TRANCOUNT > 0 
    BEGIN 
     ROLLBACK; 
    END 

IF @@TRANCOUNT = 0 
    BEGIN 
     INSERT INTO #tmpErrors (Error) 
     VALUES     (1); 
     BEGIN TRANSACTION; 
    END 


GO 

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION 
GO 
IF @@TRANCOUNT>0 BEGIN 
PRINT N'The transacted portion of the database update succeeded.' 
COMMIT TRANSACTION 
END 
ELSE PRINT N'The transacted portion of the database update failed.' 
GO 
DROP TABLE #tmpErrors 
GO 
PRINT N'Update complete.'; 


GO 

/*----------------------------------------------------------------------------------------------------------------------*/ 

我對存儲過程沒有太多的經驗,任何幫助或輸入表示讚賞。簡單地嘗試讓重複性任務更容易一點。

回答

0
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE PROCEDURE DropTables 
AS 
BEGIN 
    DROP TABLE Table1 
    DROP TABLE Table2 
    DROP TABLE Table3 
    DROP TABLE Table4 
END 
GO 

EXEC DropTables

叫它
相關問題