T-SQL 2005 Stored procedure - optional param question


I've got a stored procedure in t-sql running on ms sql server 2005. I need to add an optional parameter in there so that if I pass in the parameter, then the stored proc filters on that value. If the value is not passed in, then the stored proc does not filter on that value.

The value passed in should be an integer. What is the syntax to accomplish this?
roger vAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

default the value to NULL then in your where clause do this

   (yourValue = @yourValue or @yourValue IS NULL)
SharathData EngineerCommented:
As tim stated, default the value to NULL. In the WHERE clause, you can pass the filter like this also.

WHERE yourValue = ISNULL(@yourValue,yourValue)
WHERE yourValue = COALESCE(@yourValue,yourValue)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
roger vAuthor Commented:
How do I declare the param at the begining? Right now I have:

ALTER PROCEDURE [dbo].[getSchoolsInfo] (@schoolid      INT,
                                                   @district VARCHAR(20),
                                    -- this is where i need my integer variable @idnum
ALTER PROCEDURE [dbo].[getSchoolsInfo] (@schoolid      INT,
                                                   @district VARCHAR(20),
                                    @idnum int = NULL )
roger vAuthor Commented:
got it working, thanks guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.