在SQL函数中正确嵌套if语句

时间:2014-02-04 19:32:57

标签: sql sql-server

我正在处理一个行为不符合预期的函数。在我看来,这是因为我的IF语句没有像我期望的那样嵌套:

CREATE FUNCTION primaryTheme (
    @StoryNumber NVARCHAR(10)
    ,@ProductType NVARCHAR(10)
    )
RETURNS VARCHAR(15)
AS
BEGIN
    DECLARE @return VARCHAR(15)
    DECLARE @count INT

    SELECT @count = COUNT(THM.NAME)
    FROM Theme THM
    LEFT JOIN ProductTheme PDT ON THM.PK_Theme = PDT.ThemeId
    LEFT JOIN StoryProductTheme SPT ON PDT.PK_ProductTheme = SPT.ProductThemeId
    LEFT JOIN Story STY ON SPT.StoryId = STY.PK_Story
    WHERE STY.Number = @StoryNumber

IF (@count = 0)
    IF (@ProductType = 'Sprint')
        SELECT @return = 'New Development'
    ELSE
        SELECT @return = NULL
ELSE
    IF (@count = 1)
        IF (@ProductType = 'Sprint')
            SELECT @return = THM.NAME
            FROM Theme THM
            LEFT JOIN ProductTheme PDT ON THM.PK_Theme = PDT.ThemeId
            LEFT JOIN StoryProductTheme SPT ON PDT.PK_ProductTheme = SPT.ProductThemeId
            LEFT JOIN Story STY ON SPT.StoryId = STY.PK_Story
            WHERE STY.Number = @StoryNumber

IF (
        @return != 'Adaptive'
        OR @return != 'Corrective'
        OR @return != 'Perfective'
        OR @return != 'Preventative'
        )
    SELECT @return = 'New Development'
ELSE
    SELECT @return = THM.NAME
    FROM Theme THM
    LEFT JOIN ProductTheme PDT ON THM.PK_Theme = PDT.ThemeId
    LEFT JOIN StoryProductTheme SPT ON PDT.PK_ProductTheme = SPT.ProductThemeId
    LEFT JOIN Story STY ON SPT.StoryId = STY.PK_Story
    WHERE STY.Number = @StoryNumber

IF (
        @return != 'Adaptive'
        OR @return != 'Corrective'
        OR @return != 'Perfective'
        OR @return != 'Preventative'
        OR @return != 'New Development'
        )
    SELECT @return = NULL
ELSE
    IF (@count > 1)
        IF (@ProductType = 'Sprint')
            SELECT @return = THM.NAME
            FROM Theme THM
            LEFT JOIN ProductTheme PDT ON THM.PK_Theme = PDT.ThemeId
            LEFT JOIN StoryProductTheme SPT ON PDT.PK_ProductTheme = SPT.ProductThemeId
            LEFT JOIN Story STY ON SPT.StoryId = STY.PK_Story
            WHERE STY.Number = @StoryNumber
                AND THM.NAME != 'New Development'
                AND (
                    THM.NAME = 'Adaptive'
                    OR THM.NAME = 'Corrective'
                    OR THM.NAME = 'Perfective'
                    OR THM.NAME = 'Preventative'
                    )

IF (@return IS NULL)
    SELECT @return = 'New Development'
ELSE
    SELECT @return = THM.NAME
    FROM Theme THM
    LEFT JOIN ProductTheme PDT ON THM.PK_Theme = PDT.ThemeId
    LEFT JOIN StoryProductTheme SPT ON PDT.PK_ProductTheme = SPT.ProductThemeId
    LEFT JOIN Story STY ON SPT.StoryId = STY.PK_Story
    WHERE STY.Number = @StoryNumber
        AND THM.NAME = 'New Development'
        AND (
            THM.NAME = 'Adaptive'
            OR THM.NAME = 'Corrective'
            OR THM.NAME = 'Perfective'
            OR THM.NAME = 'Preventative'
            )

IF (@return IS NULL)
    SELECT @return = 'New Development'

RETURN @return
END

这些IF声明:

IF (
        @return != 'Adaptive'
        OR @return != 'Corrective'
        OR @return != 'Perfective'
        OR @return != 'Preventative'
        )
    SELECT @return = 'New Development'

应该在其他的范围内。我如何确保它们正常工作?

2 个答案:

答案 0 :(得分:1)

使用BEGIN IF / END IF的详细样式会有所帮助(作为评论者已经建议过)。此外,您是否尝试在Management Studio调试器中运行它?否则,你可以通过sproc撒上PRINT语句,看看它到底有多远。

答案 1 :(得分:1)

如果IF未使用BEGIN,则SQL Server会在END后执行一条语句。建议始终使用BEGINEND(我认为MCTS工具包说明了这一点)。

以下是语法:

IF @@TRANCOUNT = 1 
BEGIN
    -- Do Some Things
    print 'Do some things'

    IF @@TRANCOUNT = 1 
    BEGIN
        -- Do Some Things
        print 'Do some more things'
    END
END

here is the BOL link用于纪录片。