SQL Server 脚本:ALTER PROCEDURE - 在一个脚本中执行多个 ALTER PROCEDURE,而不必依次选择每个 ALTER
我知道这不是什么大问题,但它还是让我很高兴。
- 我有一个 SQL Server 2005 脚本来创建新的数据表、约束、更改某些表以添加列、更改过程以考虑表更改等。
- 一切都运行良好,直到脚本遇到我的 ALTER PROCEDURE 语句。
- 错误信息如下:
“消息 156,级别 15,状态 1,过程 cpromo_Get_ConsultDetails_PromotionBan, 第 59 行 附近的语法不正确 关键字“PROCEDURE”。
这是我的脚本的示例:
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
错误指向双击时遇到的第一个“end”关键字。我完全不明白的是,当选择一个又一个 ALTER 语句时,它运行得很好而且很顺利!当我尝试通过按 [F5] 而不选择来运行它们时,它给了我错误。
我尝试将 ALTER 语句嵌入到另一个 BEGIN...END 中,但没有运气,它说关键字 ALTER... 附近存在语法错误
编辑:可能是因为我注释了开始语句之后执行的修改吗?
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
感谢您的任何帮助或任何提示。
I know this is not a big issue, but it tickles me anyway.
- I have a SQL Server 2005 script to create new data tables, constraints, altering some tables to add columns, altering procedures to take the table changes into account, etc.
- Everything runs fine until the script encounters my ALTER PROCEDURE statements.
- The error message is as follows:
"Msg 156, Level 15, State 1, Procedure
cpromo_Get_ConsultDetails_PromotionBan,
Line 59 Incorrect syntax near the
keyword 'PROCEDURE'.
Here's a sample 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 toward the first 'end' keyword encountered when double-clicked. What I don't get at all is when selecting one ALTER statement after another, it runs just fine and smooth! When I try to run them all by pressing [F5] with no selection, it gives me the error.
I tried to embed the ALTER statements into another BEGIN...END, but no luck, it says that there's a syntax error near the keyword ALTER...
EDIT: Can it be because I comment the modifications performed 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
Thank's for any help or any cue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这个答案不是我的,因为它是我得到的所有答案的结果。每个答案都有解决方案的一部分,所以我想给出一个包含解决方案所有要点的答案。
希望这有一天会对某人有所帮助。
感谢大家,功劳归于你们所有人!
This answer is not mine as it is the result of all the answers I have gotten. Each answer has a part of the solution, so I wanted to put an answer with all the points to the solution.
Hope this will help someone someday.
Thanks to everyone, credits go to all of you!
在 alter 语句之间插入“go”
insert "go" between alter statemnts
在第一个 ALTER PROCEDURE BEGIN ... END 之后输入 GO
Put a GO after your first ALTER PROCEDURE BEGIN ... END
在每个
end
过程语句之后添加一个go
语句。go
“向 SQL Server 实用程序发出一批 Transact-SQL 语句结束的信号。” (http://msdn.microsoft.com/en-us/library/ms188037 .aspx)。Add a
go
statement after everyend
procedure statement.go
"Signals the end of a batch of Transact-SQL statements to the SQL Server utilities." (http://msdn.microsoft.com/en-us/library/ms188037.aspx).我同意 go 语句 - 但也许参数周围的括号导致语法错误?这是我的团队使用的...
I agree with the go statements - but maybe the parentheses around your parameters are causing the syntax errors? Here's what my team uses...
在第一个和第二个过程之间的空白区域中可能有一些不可见的字符(例如 nbspace)。删除 end 和后续 alter 之间的所有内容(包括换行符 - 导致 endALTER),然后放回一些换行符并在某些行上写入 GO。
在从网上复制一些示例代码后,我亲自看到了这一点:)
You may have some invisible characters (nbspace for example) in white space area between first and second procedure. Remove everything between end and subsequent alter (including newlines - resulting in endALTER), then put some line breaks back and write GO on some line.
I've seen that personally, after copying some sample code from net :)