Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2004, 12:38 PM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Set multiple variables with one Select?

I would like to update several variables with a single select statement, but do not know if this is possible. I can only figure two ways to do this and I don't like either solutions.

Solution 1: Use multiple Set (or Select) statements:

Set @Var1 = (Select Col1 from...... where....)
Set @Var2 = (Select Col2 from...... where....)

The reason I do not like this solution (aside form the extra typing) is that the same select statement is issued over and over again only changing the returned value and the rest join clause, where clause etc. stays the same. It seems innefficent to me.


Solution 2: Use a cursor

Declare tmpcursor Cursor for
Select Col1, Col2, .... from...... where....)
Open tmpcursor
fetch next form tmpcursor into @Var1, @Var2, ...
close tmpcursor
deallocate tmpcursor

The reason I do not like this solution is that a cursor is created but the only reason for the cursor is so I can use the Select...Into clause. The cursor is not really used and I don't think this is too efficient either.

Note: In my particular case I only expect a single record to be returned from the above select statements.

Any comments would be appreciated.


 
Old June 11th, 2004, 08:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Probably you don't have to think about the second solution at all. That is similar to taking the longest route to go to your destination.

I can modify you first solution to help you out.

Code:
DECLARE @Var1 int
DECLARE @Var1 varchar(20)

-- This line assigns column values to variables
Select @Var1=Col1, @Var2=Col2, from...... where....)

Select @Var1, @Var2  -- This line prints the values of your variables.
You dont have to use separate SELECTs with same JOIN and WHERE for assigning values to each of the variables.

Does that helps?
Cheers!

_________________________
-Vijay G
Strive for Perfection
 
Old June 14th, 2004, 08:08 AM
Registered User
 
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. It works great.

Normally I prefer to use Set when assigning to local variables, but in this case Select makes things much easier!








Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to set $_Session Variables in java rinventive Java Basics 1 February 13th, 2006 05:41 AM
Variables in Select seeyem General .NET 1 August 30th, 2004 02:41 PM
Select with 3 variables morpheus SQL Server 2000 3 December 5th, 2003 02:59 PM
Passing variables into the SELECT of Stored Proc kerrj SQL Server 2000 0 October 14th, 2003 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.