SQL Server 2000 多个 IF 语句

发布于 2024-10-09 06:59:30 字数 3569 浏览 2 评论 0原文

当我尝试使用多个 IF 语句时出现错误。这是错误...

“消息 156,级别 15,状态 1,过程 fnTNAccidentIndicator,第 81 行 关键字附近的语法不正确 ‘结束’。”

这是我的代码的结构......

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

I get a error when I try to use multiple IF statements. This is the error...

"Msg 156, Level 15, State 1, Procedure
fnTNAccidentIndicator, Line 81
Incorrect syntax near the keyword
'END'."

This is the structure of my 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

锦上情书 2024-10-16 06:59:30

我在这里看到了很多问题。

  1. 您有一个 nvarchar 类型的参数,但未声明大小。发生这种情况时,SQL Server 默认为 1 个字符,这可能不是您想要的。我鼓励您指定大小。

  2. 您的函数返回一个 nvarchar,但您没有声明大小。

  3. 您已声明 nvarchar 类型的局部变量,但未声明大小。

  4. 您在不使用 set 关键字的情况下分配变量。 SET @ReturnValue = 'Y'

编辑

  1. 您将 @inIndicatorMode 作为整数传递,然后将其与 nvarchar 变量进行比较。这将导致 SQL Server 在比较之前进行类型转换。类型转换通常非常快,但最好尽可能避免它们。

  2. 您似乎只是为了比较的目的而对值进行硬编码。每个声明和变量赋值都需要少量的时间来执行。我认为这种方法的唯一优点是自记录代码。我建议您将比较更改为硬编码值,然后在代码中使用注释,而不是声明变量、分配变量,然后比较这些变量。


IF (@inIndicatorMode = 1) --Alcohol Indicator
 BEGIN
  --select statment   
  IF (@@ROWCOUNT > 0)
  BEGIN
   SET @ReturnValue = 'Y'
  END
 END
  1. 此过程似乎只有 2 个有效返回:“N”或“Y”。我鼓励您稍微更改返回数据类型。这允许您编写其他代码,将此函数的输出视为布尔值而不是字符串。

  2. 在您的帖子中,您展示了 3 个代码块。在代码中,您有“select 语句”的注释。我假设每个 select 语句都是不同的。之后您似乎还检查了 @@RowCount,如果行计数大于 0,则将函​​数的输出设置为“Y”。相反,我鼓励您使用 Exists 函数。使用exists 时会稍微提高性能,因为一旦SQL 找到满足查询的单行,它就会返回true。例如:


IF (@inIndicatorMode = 1) -- Alcohol Indicator
 BEGIN
  If Exists(-- Your select statement Here)
  BEGIN
   SET @ReturnValue = 'Y'
  END
 END

There are quite a few issues I see here.

  1. You have a parameter of type nvarchar without a size declared. When this happens, SQL Server defaults to 1 character, which is probably not what you want. I encourage you to specify the size.

  2. Your function returns an nvarchar, but you did not declare the size.

  3. You have declared local variables of type nvarchar without declaring the size.

  4. You are assigning variables without using the set key word. SET @ReturnValue = 'Y'

EDIT

  1. You pass in @inIndicatorMode as an integer, but then you compare it to an nvarchar variable. This will cause SQL Server to do a type conversion prior to the compare. Type conversions are generally pretty fast, but it's best to avoid them when you can.

  2. You appear to be hard coding values just for the purpose of comparisons. Each declare and variable assignment will take a small amount of time to execute. The only advantage I see to this method is self documenting code. Instead of declaring variables, assigning variables, and then comparing those variables, I would suggest that you change the comparison to a hard coded value and then use comments in the code.


IF (@inIndicatorMode = 1) --Alcohol Indicator
 BEGIN
  --select statment   
  IF (@@ROWCOUNT > 0)
  BEGIN
   SET @ReturnValue = 'Y'
  END
 END
  1. It appears as though there are only 2 valid returns from this procedure, 'N' or 'Y'. I would encourage you to change the return data type to a bit. This allows you to write other code that treats the output of this function as a boolean instead of a string.

  2. In your post, you show 3 blocks of code. In the code you have a comment of "select statement". I assume each select statement is different. You also appear to be checking the @@RowCount afterwards, and if the row count is greater than 0, you set the output of the function to 'Y'. Instead, I would encourage you to use the Exists function instead. There is a slight performance gain when you use exists because it returns true as soon as SQL finds a single row that satisfies the query. For example:


IF (@inIndicatorMode = 1) -- Alcohol Indicator
 BEGIN
  If Exists(-- Your select statement Here)
  BEGIN
   SET @ReturnValue = 'Y'
  END
 END
云醉月微眠 2024-10-16 06:59:30

根据您的编辑。不能有空的 begin..end 块。它需要包含一些东西,即使只是一个打印语句或冗余赋值。

Following your edit. You cannot have an empty begin..end block. It needs to contain something even if just a print statement or redundant assignment.

生寂 2024-10-16 06:59:30

只要看看你的函数,我认为问题不在于多个 if 语句。看起来问题是您在每个 if 语句中执行 @ReturnValue = 'Y' (忘记 SET)。它应该是:

IF (@@ROWCOUNT > 0)     
BEGIN         
   SET @ReturnValue = 'Y'     
END 

另外,看起来你在每个 if 中都做了同样的事情,你可以将 if 语句组合成一个带有 OR 的 if 。

IF (@inIndicatorMode = @AlcoholInd OR @inIndicatorMode = @DrugInd OR @inIndicatorMode = @SpeedInd) 
BEGIN     --select statment        
  IF (@@ROWCOUNT > 0)     
   BEGIN         
     SET @ReturnValue = 'Y'     
   END
END 

Just looking at your function, I don't think the problem is with the multiple if statements. It looks like the problem is that you are doing @ReturnValue = 'Y' inside each if statement (forgetting SET). It should be:

IF (@@ROWCOUNT > 0)     
BEGIN         
   SET @ReturnValue = 'Y'     
END 

Also, it looks like you are doing the same thing inside of each if, you could combine the if statements into a single if with ORs.

IF (@inIndicatorMode = @AlcoholInd OR @inIndicatorMode = @DrugInd OR @inIndicatorMode = @SpeedInd) 
BEGIN     --select statment        
  IF (@@ROWCOUNT > 0)     
   BEGIN         
     SET @ReturnValue = 'Y'     
   END
END 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文