Simple SQL Query

4

I have a proc that gets the ID of a product. If it gets NULL, then it should list all products. If you get the specific ID, you should only list that product.

Is there any way to do this without having to create a dynamic query?

    
asked by anonymous 06.11.2015 / 14:16

2 answers

5

Try:

SELECT * FROM tabela WHERE (id = @varid OR @varid IS NULL)
    
06.11.2015 / 14:19
1

Just to complement the friend's solution there: You can also use the isnull

SELECT * FROM tabela WHERE id = isnull(@varid, id)
    
07.06.2016 / 23:17