Simple SQL query

Asked

Viewed 81 times

4

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

There is some way to do this without having to create a dynamic query?

  • Can you change the proc? It would be easier if you post her body.

2 answers

5


Tries:

SELECT * FROM tabela WHERE (id = @varid OR @varid IS NULL)
  • Of course! That’s right.

1

Just to complement the solution of the friend there: You can also use the function isnull

SELECT * FROM tabela WHERE id = isnull(@varid, id)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.