SQL Server script: ALTER PROCEDURE - Execute multiple ALTER PROCEDUREs in one script without having to select each of the ALTERs one by one

I know it's not a big problem, but it tickles me anyway.

  • I have a SQL Server 2005 script to create new data tables, constraints, change some tables to add columns, change procedures to account for table changes, etc.
  • Everything works fine until the script meets my ALTER PROCEDURE statements.
  • The error message looks like this:

"Msg 156, Level 15, State 1, Procedure cpromo_Get_ConsultDetails_PromotionBan, Line 59 Incorrect syntax next to keyword" PROCEDURE ".

Here's an example of my script:

ALTER PROCEDURE [dbo].[cpromo_Get_ConsultDetails_PromotionBan] 
(
 @idPromoBan int, 
 @uid int 
)
AS
begin
 set nocount on;

 /* 1-  detail de la promo */
 SELECT p.[nopromo], p.[StartDate], p.[EndDate], p.[DateText]
 FROM [cpromo_PromotionBanniere] as pb
 INNER JOIN [cpromo_Promotions] as p ON p.[idPromo] = pb.[idPromo]
 WHERE (pb.[idPromoBan] = @idPromoBan)

 /* 2 - cartes de la promo */
 SELECT pis.[idCardText], ct.[nom], ct.[descr], ct.[prix], ct.[prixCoupon], ct.[qtyItem], i.[Impact]
 FROM [cpromo_PromotionsItems] as pis
 INNER JOIN [cpromo_Impacts] as i ON i.[idImpact] = pis.[idImpact] 
 INNER JOIN [cpromo_CardText] as ct ON ct.[idCardText] = pis.[idCardText]
 WHERE (pis.[idPromoBan] = @idPromoBan)
 ORDER BY i.[iorder], ct.[nom];

 /* 3 - pvedettes opti */
 SELECT m.[idCardText], m.[qtyCardL], m.[qtyCardM], m.[qtyCardMG], m.[qtyCardS],
     ISNULL(m.[qtyCardMini], 0) as qtyCardMini,
     ISNULL(m.[qtyCardMiniPTJ], 0) as qtyCardMiniPTJ
 FROM [cpromo_MEMCards] as m
 INNER JOIN [cpromo_CardText] as ct ON ct.[idCardText] = m.[idCardText]
 WHERE (m.[idPromoBan] = @idPromoBan)
 ORDER BY ct.[nom];


 /* 4 - cart */
 SELECT [idCartEl], [idCardText], [qtyL], [qtyM], [qtyMG], [qtyS],
     ISNULL([qtyMini], 0) as qtyMini,
     ISNULL([qtyMiniPTJ], 0) as qtyMiniPTJ
 FROM [cpromo_UserCarts]
 WHERE ([uid] = @uid AND [idPromoBan] = @idPromoBan);
end


ALTER PROCEDURE [dbo].[cpromo_Get_CartItems_ByPromotionBan] 
(
 @uid int,
 @idPromoBan int 
)
AS
begin
 set nocount on;

 SELECT ct.nom, ct.descr, p.DateText, ct.prix, ct.prixCoupon, ct.qtyItem,
           uc.qtyL, uc.qtyM, uc.qtyMG, uc.qtyS,
     isnull(uc.qtyMini, 0) as qtyMini,
     isnull(uc.qtyMiniPTJ, 0) as qtyMiniPTJ, 3 as qteLimite
 FROM cpromo_UserCarts as uc
 INNER JOIN cpromo_CardText as ct ON ct.idCardText = uc.idCardText 
 INNER JOIN cpromo_PromotionBanniere as pb ON pb.idPromoBan = uc.idPromoBan 
 INNER JOIN cpromo_Promotions  as p ON p.idPromo = pb.idPromo
 WHERE (uc.uid = @uid) AND (uc.idPromoBan = @idPromoBan);
end

      

The error points to the first keyword "end" that was encountered on a double click. What I don't get at all is when you select one ALTER statement after another, it works just fine and smooth! When I try to run them all by pressing [F5] with no selection, it gives me an error.

I tried to include ALTER statements in another BEGIN ... END but with no luck, it says there is a syntax error next to the ALTER keyword ...

EDIT: Maybe because I am commenting out modifications made after the begin statement?

ALTER PROCEDURE [dbo].[cpromo_Get_ConsultDetails_PromotionBan] 
    (
     @idPromoBan int, 
     @uid int 
    )
    AS
    begin
------------------
-- Added column to take table changes into account blah blah blah...
------------------
     set nocount on;

     /* 1-  detail de la promo */
     SELECT p.[nopromo], p.[StartDate], p.[EndDate], p.[DateText]
     FROM [cpromo_PromotionBanniere] as pb
     INNER JOIN [cpromo_Promotions] as p ON p.[idPromo] = pb.[idPromo]
     WHERE (pb.[idPromoBan] = @idPromoBan)

     /* 2 - cartes de la promo */
     SELECT pis.[idCardText], ct.[nom], ct.[descr], ct.[prix], ct.[prixCoupon], ct.[qtyItem], i.[Impact]
     FROM [cpromo_PromotionsItems] as pis
     INNER JOIN [cpromo_Impacts] as i ON i.[idImpact] = pis.[idImpact] 
     INNER JOIN [cpromo_CardText] as ct ON ct.[idCardText] = pis.[idCardText]
     WHERE (pis.[idPromoBan] = @idPromoBan)
     ORDER BY i.[iorder], ct.[nom];

     /* 3 - pvedettes opti */
     SELECT m.[idCardText], m.[qtyCardL], m.[qtyCardM], m.[qtyCardMG], m.[qtyCardS],
         ISNULL(m.[qtyCardMini], 0) as qtyCardMini,
         ISNULL(m.[qtyCardMiniPTJ], 0) as qtyCardMiniPTJ
     FROM [cpromo_MEMCards] as m
     INNER JOIN [cpromo_CardText] as ct ON ct.[idCardText] = m.[idCardText]
     WHERE (m.[idPromoBan] = @idPromoBan)
     ORDER BY ct.[nom];


     /* 4 - cart */
     SELECT [idCartEl], [idCardText], [qtyL], [qtyM], [qtyMG], [qtyS],
         ISNULL([qtyMini], 0) as qtyMini,
         ISNULL([qtyMiniPTJ], 0) as qtyMiniPTJ
     FROM [cpromo_UserCarts]
     WHERE ([uid] = @uid AND [idPromoBan] = @idPromoBan);
    end

      

Thanks for any help or any hint.

+2


source to share


6 answers


This answer is not mine as it is the result of all the answers I received. Each answer has a part of the solution, so I would like to answer all questions in the solution.

  • Insert a "GO" statement between each ALTER PROCEDURE statement (everyone who answered)
  • Make sure there are no invisible characters in the white space (Arvo)
  • BEGIN ... END assertions that don't seem to be needed (Mayo)
  • Deleting half-columns in AS ... GO according to the procedural core also caused certain problems (Mayo)
  • The comments in the core of the procedure as described in my edit of the question are irrelevant, this did not cause any error once the above points were checked (Myself)


Hope this helps someone someday.

Thank you all, loans go to all of you!

+3


source


insert "go" between alter statemnts



+4


source


Place GO after the first launch of ALTER PROCEDURE ... END

+2


source


Add a statement go

after each statement in the procedure end

. go

"Batch End Signals for Transact-SQL to SQL Server Utilities." ( http://msdn.microsoft.com/en-us/library/ms188037.aspx ).

+2


source


I agree with go operations - but maybe the parentheses around your parameters are causing syntax errors? This is what my team is using ...

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

ALTER PROCEDURE dbo.proc
@param1 int
AS
   select 1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

-- repeat for additional procedures

      

+2


source


You may have some invisible characters (like nbspace) in the white space between the first and second procedure. Delete everything between the end and the subsequent change (including newlines - the result is endALTER), then flip a few lines and run GO on some line.

I saw this personally, after copying some example code from the net :)

+2


source







All Articles