I am trying to use a IF THEN loop in a stored procedure as below:

```
@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

```

My goal is that the SP works like what a searching engine would do:
-------------------------------------------------------------------

1. If users specifies both @A and @B (say, @A = alpha, @B = bravo), it returns top 5 results in terms of Y1 and top 5 results in terms of Y2. Those results are in A = alpha and B = bravo.
2. If user specifies only @A (say, @A = alpha) or @B (@B = bravo) (another one will input as 'NULL'), it returns top 5 results in terms of Y1 and top 5 results in terms of Y2. Those results are only in A=alpha and any B or B=bravo and any A.
3. If user specified none of A and B (input as 'NULL' for both), it returns top 5 results in terms of Y1 and top 5 results in terms of Y2. Those results' A and B can be any.

The actual result of the code turns out that:
---------------------------------------------

* If I specify both A and B, the code works fine.
* If I do not specify A or B (input @A = 'NULL' and @B = 'NULL'), the code works fine too.
* But If I only specify one of A and B (say, input @A = 'alpha' and @B = 'NULL'; or input A = 'NULL' and B = 'Bravo'), the code returns a empty table only with the heading.

Is there anyone might help me on this?
Thanks a lot.

===========================================================================================
BTW, I tried using the same code in query but not stored procedure.
I tried

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

```

and

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

```

then followed is exactly the same code I used in stored procedure, and it works exactly what I want! I think they should be equivalent, but somehow only works in normal query but not in stored procedure... I am really confused.