Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server conditional select depending on input parameter

Tags:

sql-server

I have a stored procedure in MS SQL, that takes 2 parameteres, for example

CREATE PROCEDURE MyProcedure
    @A      INT,
    @B      INT
AS
BEGIN
    SELECT  ...

    FROM    ...

    WHERE   [A] = @A AND [B] = @B 

END

My question is. if i have @B = 0 i want in the select row like this where [A]=@A,wthout the [B]=@B.. how can i achieve this with a minimum code? thanks alot

like image 613
Grace Avatar asked Nov 30 '25 23:11

Grace


1 Answers

Try the following:

SELECT  ...    
FROM    ...    
WHERE   [A] = @A AND 
        (@B = 0 OR [B] = @B)
like image 170
Akram Shahda Avatar answered Dec 02 '25 15:12

Akram Shahda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!