如果那么在存储过程中循环

时间:2014-04-13 15:37:59

标签: sql sql-server stored-procedures if-statement

我试图在存储过程中使用IF THEN循环,如下所示:

@A varchar(100),
@B varchar(100)  
AS
BEGIN 

IF (@A = 'NULL' AND @B = 'NULL')
    BEGIN
    SELECT TOP 5 X1, X2, A, B, Y1, Y2, X3
        FROM mytable
    ORDER BY Y1 DESC
    SELECT TOP 5 X1, X2, A, B, Y1, Y2, X3
    FROM mytable
    ORDER BY Y2 DESC
    END

ELSE IF (@A = 'NULL' AND @B IS NOT NULL) 
    BEGIN
    SELECT TOP 5 Y1Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
            SELECT *,
        ROW_NUMBER() OVER (PARTITION BY B ORDER BY Y1 DESC) AS 'Y1Rank'
        FROM mytable
        WHERE B = @B
        ) b1

    SELECT TOP 5 Y2Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY B ORDER BY Y2 DESC) AS 'Y2Rank'
        FROM mytable
        WHERE B = @B
        ) b2
    END

ELSE IF (@A IS NOT NULL AND @B = 'NULL')
    BEGIN
    SELECT TOP 5 Y1Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY A ORDER BY Y1 DESC) AS 'Y1Rank'
        FROM mytable
        WHERE A = @A
        ) b3

    SELECT TOP 5 Y2Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY A ORDER BY Y2 DESC) AS 'Y2Rank'
        FROM mytable
        WHERE A = @A
        ) b4
    END
ELSE
    BEGIN
    SELECT TOP 5 Y1Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY A, B ORDER BY Y1 DESC) AS 'Y1Rank'
        FROM mytable
        WHERE A = @A AND B = @B
        ) b5

    SELECT TOP 5 Y2Rank, X1, X2, A, B, Y1, Y2, X3
    FROM (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY A, B ORDER BY Y2 DESC) AS 'Y2Rank'
        FROM mytable
        WHERE A = @A AND B = @B
        ) b6
    END
END
GO

我的目标是SP就像搜索引擎一样:

  1. 如果用户同时指定@A和@B(例如,@ A = alpha,@ B = bravo),则根据Y1返回前5个结果,按Y2指示返回前5个结果。这些结果是A = alpha和B = bravo。
  2. 如果用户仅指定@A(例如,@ A = alpha)或@B(@B = bravo)(另一个将输入为'NULL'),则会根据Y1和前5个结果返回前5个结果就Y2而言。这些结果仅在A = alpha和任何B或B = bravo和任何A.
  3. 如果用户没有指定A和B中的任何一个(两者都输入为“NULL”),则按Y1的顺序返回前5个结果,按Y2表示返回前5个结果。那些结果'A和B可以是任何结果。
  4. 代码的实际结果证明:

    • 如果我同时指定A和B,则代码可以正常工作。
    • 如果我没有指定A或B(输入@A ='NULL'和@B ='NULL'),代码也可以正常工作。
    • 但如果我只指定A和B中的一个(比如输入@A ='alpha'和@B ='NULL';或者输入A ='NULL'和B ='Bravo'),则代码返回只有标题的空表。

    有没有人可以帮我这个? 非常感谢。

    =============================================== ============================================ 顺便说一句,我尝试在查询中使用相同的代码但不是存储过程。 我试过了

    DECLARE @A varchar(100),
    @B varchar(100)
    SET @A = 'NULL' 
    SET @B = 'Bravo'
    

    DECLARE @A varchar(100),
    @B varchar(100)
    SET @A = 'Alpha' 
    SET @B = 'NULL'   
    

    然后跟着我在存储过程中使用的代码完全相同,它正是我想要的!我认为它们应该是等价的,但不知何故只能在普通查询中工作,而不能在存储过程中工作......我真的很困惑。

2 个答案:

答案 0 :(得分:0)

尝试使用参数的默认值,如下所示:

@A varchar(100) = 'NULL',
@B varchar(100) = 'NULL'

这样,如果用户只提供一个参数的值,则另一个参数只使用默认值。

答案 1 :(得分:0)

declare @a as varchar(10)=NULL
if @a is null
SELECT @a as [a]

declare @b as varchar(10)='NULL'
if @b is null
SELECT @b as [b]

尝试上面的代码is null='NULL'不是一回事,你似乎已混合并匹配它们。