Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameterized Stored Proc question

Status
Not open for further replies.
Jun 27, 2001
837
US
I want to create a stored proc that might or might not recieve a value like below.

create proc cmine @vdate datetime = null
as
select * from table where mydate > @vdate

However if the value is null,( no value was supplied) I would not want to use the where clause. Should (or can you) use an if statement to check and see if no value was given to change the where clause?
Thanks
 
You can use an IF statement to control flow in a SQL script.

create proc cmine @vdate datetime = null
as

If @vdate Is Null
select * from table
Else
select * from table
where mydate > @vdate Terry L. Broadbent - Salt Lake City, UT
Home of the 2002 Winter Olympics (Feb 8-24)
 
Or
select * from table
where (@vdate is null or mydate > @vdate)

This might be slightly eachier if you have lots of arguments of this flavour.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top