0

Can't figure this out...

Sql stored procedure has an optional argument, say an id(int). For simplicity sake, say, I have to return a result set with extra ...and id=@id check if argument is not null. If not I just return the result set.

How do I avoid writing the 'core' of the query twice? CTE to the rescue?

4

2 に答える 2

2

You could do

where .... and (@id is null or id = @id)
于 2013-03-26T19:31:08.697 に答える
1

you can try:

select * from myTable where id = isnull(@id, id)

If the @id is null the query compare its own id column to itself, which is always true, otherwise is the parameter used.

于 2013-03-26T19:29:53.090 に答える