Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update statement

Author  Topic 

lcastillo83
Starting Member

2 Posts

Posted - 2010-09-29 : 18:51:09
Hello Guys

I've been working with SQL for a couple of months so I'm basicaly new on this... I have a question related to update statement...

I have one temp table

declare @TempTable table(id bigint, order bigint)

with values
id | order
180 | 10
151 | 20
172 | 30
130 | 40

I would like to Update table "request" field rqorder with order field in @TempTable.

Request table before update

rqid | rqorder
130 | 10
180 | 20
151 | 30
172 | 40
161 | 120
162 | 130
163 | 140

Request table after desired update
rqid | rqorder
180 | 10
151 | 20
172 | 30
130 | 40
161 | 120
162 | 130
163 | 140

I tried doing this but it does not do anything (no error) at all...

update request set rqorder= (select order from @TempTable t1 , request where t1.id=request.rqorder) where exists (select id from @TempTable t1, rqs where t1.id=request.rqorder)

db is MS SQL Express 2005

I appreciate any help...

LCastillo

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-29 : 18:55:26
Perhaps this:

UPDATE r
SET rqorder = t.order
FROM request r
JOIN @TempTable t
ON r.rqid = t.id

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -