Je reçois une erreur lorsque j'essaie d'utiliser plusieurs instructions IF. Ceci est l'erreur ...Instructions IF multiples SQL Server 2000
"Msg 156, Niveau 15, État 1, Procédure fnTNAccidentIndicator, Ligne 81 syntaxe incorrecte près du mot-clé 'END'."
C'est la structure de mon code ...
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
Vous pouvez utiliser la clause IN au lieu de plusieurs OR. À partir de votre troisième requête, utilisez [WHERE c.DrugTestResult IN ('02 ',' 03 ',' 04 ',' 05 ',' 06 ',' 07 ',' 08 ',' 97 ',' 98 ')] – Suirtimed
Je suis assez confus par l'histoire d'édition maintenant. À un moment donné, vous avez mis en place une version avec des instructions DML et un 'begin..end 'vide qui semble avoir disparu. Pouvez-vous trier et mettre en forme le code correctement en utilisant l'icône «{}» et non les balises afin de ne pas avoir à le modifier. –
@Martin - Désolé à ce sujet. Je faisais des ajustements au code et je voulais poster mon code sans toutes les instructions select pour affiner le problème que j'avais. Je suppose que je l'ai rendu plus confus. J'ai posté le code que j'ai dans les déclarations IF cette fois. – Spidermain50