Unix Technical Forum

Assigning values to multiple variables (via subqueries) for use in an update

This is a discussion on Assigning values to multiple variables (via subqueries) for use in an update within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, figured out where I was going wrong in my post just prior, but is there ANY way I ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:11 PM
KathyB
 
Posts: n/a
Default Assigning values to multiple variables (via subqueries) for use in an update

Hi, figured out where I was going wrong in my post just prior, but is
there ANY way I can assign several variables to then use them in an
Update statement, for example (this does not work):

ALTER PROCEDURE dbo.UpdateXmlWF
(
@varWO varchar(50)
)
AS
DECLARE @varCust VARCHAR(50)
SELECT @varCust = (SELECT Customer FROM tblWorkOrders
WHERE WorkOrder=@varWO)

DECLARE @varAssy VARCHAR(50)
SELECT @varAssy=(SELECT Assy FROM tblWorkOrders
WHERE WorkOrder=@varWO)

UPDATE statement here using declared variables...

I can set one @variable but not multiple. Any clues? kinda new to
this.

Thanks,
Kathy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:11 PM
Chuck Urwiler
 
Posts: n/a
Default Re: Assigning values to multiple variables (via subqueries) for use in an update

Kathy,

See my response to your previous post.

I think you want to try this instead:

DECLARE @varCust varchar(50), varAssy varchar(50)
SELECT @varCust = Customer,
@varAssy = Assy
FROM tblWorkOrders
WHERE WorkOrder = @varWO

UPDATE table
SET field = @varCust,
field2 = @varAssy
where somefield = somevalue

In fact, you could condense this to the following:

UPDATE table
SET field = tblWorkOrders.Customer,
field2 = tblWorkOrders.Assy
FROM tblWorkOrders
JOIN table ON tblWorkOrders.PK = table.FK
WHERE tblWorkOrders.WorkOrder = @varWO
AND otherconditions...

Hope this helps.

--
-Chuck Urwiler, MCSD, MCDBA
http://www.eps-software.com


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:11 PM
Shervin Shapourian
 
Posts: n/a
Default Re: Assigning values to multiple variables (via subqueries) for use in an update

Kathy,

You don't need two separate SELECT statements to assign values to your
variables. You can do it like this:

DECLARE @varCust VARCHAR(50),
@varAssy VARCHAR(50)

SELECT @varCust = Customer,
@varAssy = Assy
FROM tblWorkOrders
WHERE WorkOrder = @varWO

But I don't understand your problem with inserting. You can modify as many
columns as you want in UPDATE statement. What's your problem here?
By the way, if these variables have been declared just to use in your UPDATE
statement, then you don't really need them. You can join tblWorkOrders table
with whatever table you want update and fetch values from tblWorkOrders
directly into your destination table.

Shervin



"KathyB" <KathyBurke40@attbi.com> wrote in message
news:75e8d381.0310030718.82cad7f@posting.google.co m...
> Hi, figured out where I was going wrong in my post just prior, but is
> there ANY way I can assign several variables to then use them in an
> Update statement, for example (this does not work):
>
> ALTER PROCEDURE dbo.UpdateXmlWF
> (
> @varWO varchar(50)
> )
> AS
> DECLARE @varCust VARCHAR(50)
> SELECT @varCust = (SELECT Customer FROM tblWorkOrders
> WHERE WorkOrder=@varWO)
>
> DECLARE @varAssy VARCHAR(50)
> SELECT @varAssy=(SELECT Assy FROM tblWorkOrders
> WHERE WorkOrder=@varWO)
>
> UPDATE statement here using declared variables...
>
> I can set one @variable but not multiple. Any clues? kinda new to
> this.
>
> Thanks,
> Kathy



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com