all groups > sql server programming > january 2006 >
You're in the

sql server programming

group:

setting a field filter to all records



setting a field filter to all records tshad
1/2/2006 9:36:12 PM
sql server programming: If I have a statement in a Stored procedure such as:

CREATE PROCEDURE GetRecord
@ID Int
As
SELECT Name, Address, Salary, ID
FROM Candidate
WHERE ID = @ID

I want to actually setup this procedure to see all records regardless of @ID
(in which I would pass ID as 0) or only records which match @ID (when <> 0).

I would change this to:

CREATE PROCEDURE GetRecord
@ID Int
As
IF (@ID = 0 )
BEGIN
SELECT Name, Address, Salary, ID
FROM Candidate
END
ELSE
BEGIN
SELECT Name, Address, Salary, ID
FROM Candidate
WHERE ID = @ID
END

Is there another way to do this by using one statement, such as using a Case
Statement?

Thanks,

Tom

Re: setting a field filter to all records tshad
1/2/2006 11:22:33 PM
[quoted text, click to view]

I don't understand how that would work.

If a 0 is passed I want to see all the records.

If a non-zero is passed I only want records of that type. There would never
be an ID of 0.

Thanks,

Tom
[quoted text, click to view]

Re: setting a field filter to all records tshad
1/3/2006 1:05:39 AM

[quoted text, click to view]

You're right.

I was thinking it said
WHERE ID= @ID OR ID = 0

Thanks,

Tom
[quoted text, click to view]

Re: setting a field filter to all records Mike John
1/3/2006 8:22:35 AM
This is a fairly common technique - look at the where clause carefully.

WHERE ID= @ID will evaluate to true for any rows wher ethe ID column is
equal to the @ID parameter.
WHERE @ID = 0 will be true when the supplied parameter is 0, and it will be
true for every row in the table, so all rows will be returned. This should
do exactly what you are describing.

Mike John

[quoted text, click to view]

Re: setting a field filter to all records Roji. P. Thomas
1/3/2006 11:34:56 AM
SELECT Name, Address, Salary, ID
FROM Candidate
WHERE ID = @ID
OR @ID = 0


--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


[quoted text, click to view]

AddThis Social Bookmark Button