Is there a way to make Visual Studio 2010 database projects use DROP and CREATE instead of ALTER for DML

心已入冬 提交于 2020-01-06 19:35:16

问题


When building a deploy script for a Visual Studio 2010 SQL database project, is there any way to instruct the process to use DROP and CREATE for stored procedures and other DML instead of always ALTERing them?

As an example, if I change a stored procedure, the deploy script will generate something like this...

ALTER PROCEDURE MyProc
AS
SELECT yadda...

I want the deploy script to create something like this instead

IF EXISTS MyProc
  DROP MyProc

CREATE PROCEDURE MyProc
AS 
SELECT yadda....

It would make version controlled upgrade scripts a bit easier to manage, and deployed changes would perform better. Also if this is not possible, a way to at least issue a RECOMPILE with the ALTER would help some.

This question asks something that seems similar, but I do not want this behavior for tables, just DML.


回答1:


I'm not familiar enough with database projects to give an answer about whether it's possible to do a DROP and CREATE. However, in general I find that CREATE and ALTER is better than DROP and CREATE.

By CREATE and ALTER I mean something like:

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES 
                WHERE ROUTINE_NAME = 'MyProc'
                AND ROUTINE_TYPE = 'PROCEDURE')
BEGIN;
    -- CREATE PROC has to be the first statement in a batch so 
    --  cannot appear within a conditional block.  To get around 
    --  this, make the statement a string and use sp_ExecuteSql.
    DECLARE @DummyCreateText NVARCHAR(100);
    SET @DummyCreateText = 'CREATE PROC dbo.MyProc AS SELECT 0;';
    EXEC sp_ExecuteSql @DummyCreateText;
END;
GO

ALTER PROCEDURE dbo.MyProc  
AS
    SELECT yadda...

The advantage of CREATE and ALTER over DROP and CREATE is that the stored proc is only created once. Once created it is never dropped so there is no chance of permissions getting dropped and not recreated.

In a perfect world the permissions on the stored proc would be applied via a database role so it would be easy to reapply them after dropping and recreating the stored proc. In reality, however, I often find that after a few years other applications may start using the same stored proc or well-meaning DBAs may apply new permissions for some reason. So I've found that DROP and CREATE tend to cause applications to break after a few years (and it's always worse when it's someone else's application that you know nothing about). CREATE and ALTER avoids these problems.

By the way, the dummy create statement, "CREATE PROC dbo.MyProc AS SELECT 0" , works with any stored procedure. If the real stored procedure is going to have parameters or return a recordset with multiple columns that can all be specified in the ALTER PROC statement. The CREATE PROC statement just has to create the simplest stored procedure possible. (of course, the name of the stored proc in the CREATE PROC statement will need to change to match the name of your stored proc)



来源:https://stackoverflow.com/questions/12221005/is-there-a-way-to-make-visual-studio-2010-database-projects-use-drop-and-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!