SQL 存储过程:叠瓦式 IF 中的语法不正确

发布于 2024-07-16 20:22:12 字数 1541 浏览 6 评论 0原文

我在以下代码中的每个嵌套 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素罗衫 2024-07-23 20:22:12

尝试这种不同的方法:

ALTER PROCEDURE [WTFAULT].[usp_WTFault_GetPartFaultStatus](@linxPartId int = -1,
@faultStatus varchar(10) output)
AS   

SELECT COUNT(*) as MaturityCount,WTFAULT.FAULT.MATURITY  INTO #Temp
FROM WTFAULT.PART JOIN WTFAULT.FAULT 
    ON WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID 
WHERE TFAULT.PART.LINX_PARTID = @linxPartId 
GROUP BY WTFAULT.FAULT.MATURITY

If (select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'ALLOCATED') >0
    BEGIN
        SET @faultStatus = 'ALLOCATED'
    END
ELSE IF
(select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'DRAFT') >0
    BEGIN
        SET @faultStatus = 'DRAFT'
    END
ELSE IF
(select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'CLOSED') >0
    BEGIN
        SET @faultStatus = 'CLOSED'  
    END
ELSE 
    BEGIN                                        
        SET @faultStatus = ''                        
    END  

我还更改了您的查询以使用 ANSI 标准连接。 从现在开始你也应该使用它们。 它们更清晰、更易于维护,并且在使用外部联接时不会给出错误的结果,并且意外导致交叉联接的可能性要小得多。

Try this different method:

ALTER PROCEDURE [WTFAULT].[usp_WTFault_GetPartFaultStatus](@linxPartId int = -1,
@faultStatus varchar(10) output)
AS   

SELECT COUNT(*) as MaturityCount,WTFAULT.FAULT.MATURITY  INTO #Temp
FROM WTFAULT.PART JOIN WTFAULT.FAULT 
    ON WTFAULT.PART.FAULT_COID = WTFAULT.FAULT.COID 
WHERE TFAULT.PART.LINX_PARTID = @linxPartId 
GROUP BY WTFAULT.FAULT.MATURITY

If (select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'ALLOCATED') >0
    BEGIN
        SET @faultStatus = 'ALLOCATED'
    END
ELSE IF
(select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'DRAFT') >0
    BEGIN
        SET @faultStatus = 'DRAFT'
    END
ELSE IF
(select MaturityCount from #temp where WTFAULT.FAULT.MATURITY = 'CLOSED') >0
    BEGIN
        SET @faultStatus = 'CLOSED'  
    END
ELSE 
    BEGIN                                        
        SET @faultStatus = ''                        
    END  

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.

雨后彩虹 2024-07-23 20:22:12

尝试这个:

DECLARE @faultStatus nvarchar(20)
DECLARE @NbFaultsInParts int
DECLARE @NbPartsReturned int

SELECT @NbPartsReturned = 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
                SELECT @NbPartsReturned = 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
                        SELECT @NbPartsReturned = 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

Try this:

DECLARE @faultStatus nvarchar(20)
DECLARE @NbFaultsInParts int
DECLARE @NbPartsReturned int

SELECT @NbPartsReturned = 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
                SELECT @NbPartsReturned = 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
                        SELECT @NbPartsReturned = 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
却一份温柔 2024-07-23 20:22:12

尽量不要将 beginsends 与其他语句放在同一行。

另外,正如一个不相关的提示,请尝试使用“select @NbPartsReturned = count(*) from WTFAULT.PART...”之类的语句,而不是“set @NbPartsReturned = (select count( *) from WTFAULT.PART...”,因为您将把两个语句变成一个。

如果这不能解决您的问题,请尝试放置整个错误的文本。

Try not putting your begins and ends 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.

陪我终i 2024-07-23 20:22:12

消息 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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文