Я получаю ошибку, когда пытаюсь использовать несколько операторов IF.Это ошибка ...
"Сообщение 156, уровень 15, состояние 1, процедура fnTNAccidentIndicator, строка 81 Неверный синтаксис рядом с ключевым словом" END "."
Это структура моего кода ...
USE SS_TNRecords_Accident
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.fnTNAccidentIndicator
(
@inAccidentNumber nvarchar(100),
@inIndicatorMode int
)
RETURNS nvarchar
AS
BEGIN
DECLARE @AlcoholInd nvarchar(1)
DECLARE @DrugInd nvarchar(1)
DECLARE @SpeedInd nvarchar(1)
DECLARE @ReturnValue nvarchar(1)
SET @AlcoholInd = '1'
SET @DrugInd = '2'
SET @SpeedInd = '3'
SET @ReturnValue = 'N'
IF (@inIndicatorMode = @AlcoholInd)
BEGIN
SELECT AccidentNumber, AlcoholTestResult FROM tblAccidentUnit
WHERE AccidentNumber = @inAccidentNumber AND AlcoholTestResult NOT IN('00', '95', '96', '97', '98', '99')
UNION
SELECT AccidentNumber, AlcoholTestResult FROM tblAccidentOccupant
WHERE AccidentNumber = @inAccidentNumber AND AlcoholTestResult NOT IN('00', '95', '96', '97', '98', '99')
UNION
SELECT AccidentNumber, AlcoholTestResult FROM tblAccidentNonMotorist
WHERE AccidentNumber = @inAccidentNumber AND AlcoholTestResult NOT IN('00', '95', '96', '97', '98', '99')
IF (@@ROWCOUNT > 0)
BEGIN
SET @ReturnValue = 'Y'
END
END
IF (@inIndicatorMode = @DrugInd)
BEGIN
SELECT a.AccidentNumber,'AccidentUnit' AS TableFound, c.PrimaryKey AS TableKeyValue
FROM tblAccident a INNER JOIN tblAccidentUnit b
ON a.AccidentNumber = b.AccidentNumber INNER JOIN tblAccidentUnitDrug c
ON b.PrimaryKey = c.ForeignKey
AND a.AccidentNumber = '001'
WHERE c.DrugTestResult IN('02', '03', '04', '05', '06', '07', '08', '97', '98')
UNION
SELECT a.AccidentNumber, 'AccidentOccupant' AS TableFound, c.PrimaryKey AS TableKeyValue
FROM tblAccident a INNER JOIN tblAccidentOccupant b
ON a.AccidentNumber = b.AccidentNumber INNER JOIN tblAccidentOccupantDrug c
ON b.PrimaryKey = c.ForeignKey
AND a.AccidentNumber = '001'
WHERE c.DrugTestResult IN('02', '03', '04', '05', '06', '07', '08', '97', '98')
UNION
SELECT a.AccidentNumber, 'AccidentNonMotorist' AS TableFound, c.PrimaryKey AS TableKeyValue
FROM tblAccident a INNER JOIN tblAccidentNonMotorist b
ON a.AccidentNumber = b.AccidentNumber INNER JOIN tblAccidentNonMotoristDrug c
ON b.PrimaryKey = c.ForeignKey
AND a.AccidentNumber = '001'
WHERE c.DrugTestResult IN('02', '03', '04', '05', '06', '07', '08', '97', '98')
IF (@@ROWCOUNT > 0)
BEGIN
SET @ReturnValue = 'Y'
END
END
IF (@inIndicatorMode = @SpeedInd)
BEGIN
SELECT a.AccidentNumber,'AccidentUnit' AS TableFound, c.PrimaryKey AS TableKeyValue
FROM tblAccident a INNER JOIN tblAccidentUnit b
ON a.AccidentNumber = b.AccidentNumber INNER JOIN tblAccidentUnitDriverAction c
ON b.PrimaryKey = c.ForeignKey
AND a.AccidentNumber = '001'
WHERE c.DriverAction IN('28', '29')
IF (@@ROWCOUNT > 0)
BEGIN
SET @ReturnValue = 'Y'
END
END
Return @ReturnValue
END
GO