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