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!

Using SELECT, How to select 2 columns into 2 variables

Status
Not open for further replies.

btturner

Programmer
May 17, 2001
175
US
In a SQL 2K SP, how can I select 2 columns into 2 variables w/out specifying a DECLARE/OPEN/FETCH/CLOSE Cursor.

Example: (this SQL is not working)

declare @PostStartDate datetime, @PostEndDate datetime

select PostStartDate, PostStartDate
into @PostStartDate, @PostEndDate
from AuditJob

 
i think:

select
@PostStartDate = PostStartDate,
@PostEndDate = PostStartDate
from AuditJob
where ...


this would only work if there's just one row returned though!

HTH
LFCfan
 
Not sure at all if that answers the question you asked - if not, apologies, and put it down to the lateness of the day!
:eek:)
LFCfan
 
select
@PostStartDate = PostStartDate,
@PostEndDate = PostStartDate
from AuditJob
where ...

This will actually work even if multiple rows are returned by the query. The values from the last row processed will be stored in the variables after the statment completes. This is usually not a desirable result. It is best to add WHERE criteria so that only one row is returned by the query.

If you want to get the best answer for your question read faq183-874 and faq183-3179.
Terry L. Broadbent - DBA
SQL Server Page:
 
lfcfan if right, so just think can u make sure the query will return only one row..or else use cursor...to loop through all the rows. And get seperate set of variables..for each row processed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top