SQL 存储过程:叠瓦式 IF 中的语法不正确
我在以下代码中的每个嵌套 IF 和 ELSE 语句中收到“语法不正确”错误...出了什么问题?
ALTER PROCEDURE [WTFAULT].[usp_WTFault_GetPartFaultStatus]
(
@linxPartId int = -1,
@faultStatus varchar(10) output
)
AS
BEGIN
DECLARE @NbFaultsInParts int,
@NbPartsReturned int
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'ALLOCATED')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'ALLOCATED'
END
ELSE BEGIN
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'DRAFT')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'DRAFT'
END
ELSE BEGIN
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'CLOSED')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'CLOSED'
END
ELSE BEGIN
SET @faultStatus = ''
END
END
END
END
GO
I get "Incorrect syntax" errors on every nested IF and ELSE statements in the following code... what's wrong ?
ALTER PROCEDURE [WTFAULT].[usp_WTFault_GetPartFaultStatus]
(
@linxPartId int = -1,
@faultStatus varchar(10) output
)
AS
BEGIN
DECLARE @NbFaultsInParts int,
@NbPartsReturned int
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'ALLOCATED')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'ALLOCATED'
END
ELSE BEGIN
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'DRAFT')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'DRAFT'
END
ELSE BEGIN
SET @NbPartsReturned = (SELECT COUNT(*) FROM WTFAULT.PART, WTFAULT.FAULT WHERE WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID
AND WTFAULT.PART.LINX_PARTID = @linxPartId AND WTFAULT.FAULT.MATURITY = 'CLOSED')
IF @NbPartsReturned > 0 BEGIN
SET @faultStatus = 'CLOSED'
END
ELSE BEGIN
SET @faultStatus = ''
END
END
END
END
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这种不同的方法:
我还更改了您的查询以使用 ANSI 标准连接。 从现在开始你也应该使用它们。 它们更清晰、更易于维护,并且在使用外部联接时不会给出错误的结果,并且意外导致交叉联接的可能性要小得多。
Try this different method:
I also changed your query to use ANSI standard joins. You should use them too from now on. They are clearer, easier to maintain and will not give wrong results when you use outer joins and are far less likely to result in a cross join by accident.
尝试这个:
Try this:
尽量不要将
begins
和ends
与其他语句放在同一行。另外,正如一个不相关的提示,请尝试使用“
select @NbPartsReturned = count(*) from WTFAULT.PART...
”之类的语句,而不是“set @NbPartsReturned = (select count( *) from WTFAULT.PART...
”,因为您将把两个语句变成一个。如果这不能解决您的问题,请尝试放置整个错误的文本。
Try not putting your
begins
andends
on the same lines as other statements.Also, just as an unrelated tip, try using statements like "
select @NbPartsReturned = count(*) from WTFAULT.PART...
" rather than "set @NbPartsReturned = (select count(*) from WTFAULT.PART...
", as you'll turn two statements into one.If this doesn't take care of your problem, try putting up the text of the entire error.
消息 102,级别 15,状态 1,过程 usp_WTFault_GetPartFaultStatus,第 25 行
“ ”附近的语法不正确。
消息 156,级别 15,状态 1,过程 usp_WTFault_GetPartFaultStatus,第 29 行
关键字“ELSE”附近的语法不正确。
消息 102,级别 15,状态 1,过程 usp_WTFault_GetPartFaultStatus,第 33 行
“ ”附近的语法不正确。
消息 156,级别 15,状态 1,过程 usp_WTFault_GetPartFaultStatus,第 37 行
关键字“ELSE”附近的语法不正确。
Msg 102, Level 15, State 1, Procedure usp_WTFault_GetPartFaultStatus, Line 25
Incorrect syntax near ' '.
Msg 156, Level 15, State 1, Procedure usp_WTFault_GetPartFaultStatus, Line 29
Incorrect syntax near the keyword 'ELSE'.
Msg 102, Level 15, State 1, Procedure usp_WTFault_GetPartFaultStatus, Line 33
Incorrect syntax near ' '.
Msg 156, Level 15, State 1, Procedure usp_WTFault_GetPartFaultStatus, Line 37
Incorrect syntax near the keyword 'ELSE'.