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
4

4 回答 4

0

试试这个:

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
于 2009-04-08T13:27:32.060 回答
0

尽量不要把你的beginsand 和ends其他陈述放在同一行。

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

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

于 2009-04-08T13:28:17.330 回答
0

消息 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”附近的语法不正确。

于 2009-04-08T13:40:16.223 回答
0

试试这个不同的方法:

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 标准联接。从现在开始,您也应该使用它们。它们更清晰,更易于维护,并且在您使用外连接时不会给出错误的结果,并且不太可能意外导致交叉连接。

于 2009-04-08T14:02:00.900 回答