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!

re-use common paramater in query??

Status
Not open for further replies.

SmokinRR

Technical User
May 26, 2004
56
US
As the subject states, I'm looking for a way to reuse one paramater multiple times in the same query (nested query). For instance, if I want data between certain dates, I need to change the from and to dates on each nested query within the query, is there a way to set these once at the beginning of the query and then just reference them from the nested queries, leaving me one place to modify the dates rather then multiple?

Any help would be greatly appreciated.

Regards,

jay
 
declare @startdt datetime
declare @enddt datetime

put that in the begining of your query or use them as parameters if you are using a stored procedure.
 
So then at every point that I would specify the start and end dates later in the query, I would use '@startdt' and '@enddt'?

I'll try this and see how it works.

Thanks!
 
Yes... once you set the values into those parameters


select *
from table
where col = @startdt

Something like that

 
Nice, thanks for clarifying it. Now does the declare @startdt... go before select? and will this work with nested queries or does it need to be declared in each nested query as well?


 
Yes.. first you must declare a variable with a datatype before you can use it It only needs to be declared once in the sql you are running or a stored procedure..

Then you set a value to it


ex:

declare startdate datetime
set startdate = getdate()

then you can use it in your queries
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top