问题
Is there any way i can change the "Alter" statements for tables and procs to "Drop if exists" when the build spits out the scripts?? Using Visual Studio 2010 Ultimate. TFS 2010.
回答1:
I believe the code you are referring to is generated from a template.
Look here...
C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\Templates\Database Project Items
and the modify these two files.
- New Table Script.sql
- New Stored Procedure Script.sql
回答2:
After the procedure has been created for the first time what you will see is the code the tool gets/reads from the database, no longer an script that you could edit; of course you could copy/paste whatever it shows you into a text editor and save it as an SQL file (.sql extension).
If you try this sample code
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'usp_test_proc')
BEGIN
DROP Procedure usp_test_proc
END
ALTER PROCEDURE dbo.usp_test_proc
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
/* SET NOCOUNT ON */
select name, comment from test_table
RETURN
you will get this message:
"Unable to save object because the statement type is not supported. It must begin with CREATE or ALTER."
I suggest you create your own SQL procedure files and add the exists statment at the top, for example:
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'usp_test_proc')
BEGIN
DROP Procedure usp_test_proc
END
CREATE PROCEDURE usp_test_proc
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
/* SET NOCOUNT ON */
select name, comment from test_table
RETURN
That way you can edit/change your SQL code file at your convenience then just recreate the procedure connecting to your database by opening a new query connection through the Visual Studio menu 'Data/Transact-SQL Editor/New Query Connection', opening the SQL file and clicking on the Execute SQL toolbar button (green arrow).
来源:https://stackoverflow.com/questions/7519445/visual-studio-2010-database-builds-drop-if-exists