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 2000 Forums
 Transact-SQL (2000)
 UPDATE .. FROM .. ORDER BY problem

Author  Topic 

MarkyBoy666
Starting Member

14 Posts

Posted - 2005-06-30 : 08:55:02
Hi,

I was hoping someone could help me with what I'm sure is a very simple problem...I just can't seem to find the syntax!

I'm wanting to update the rows in 'tbl_consolidate' from 'tbl_hold', but working through the records in 'tbl_hold' in the order of dates in a date field, rather than the order that the rows are necessarily in.

I came up with the following code to do this:


update tbl_consolidate
set field1 = b.field1, field2 = b.field2, field3 = b.field3
from
(select * from tbl_hold order by datefield1) b
where tbl_consolidate.ID1 = b.ID1


(I originally tried to use an alias 'a' for tbl_consolidate but this threw an error)

In tbl_consolidate, ID1 is unique, but in tbl_hold there can be many records with the same value in ID1. Using my code, I'd expect the UPDATE to work its way through the records in tbl_hold in order of the datefield1 column, but it doesn't seem to do it in this order. Can anyone help?

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-30 : 09:24:05
Why do you care about the order in which the records are updated? This is a set based update, meaning they will be batched together and updated at once.

It would help if you posted create scripts for the tables involved and included desired outcomes (resultset, rows to-be-updated, etc)

see:
[url]http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx[/url]
Go to Top of Page

MarkyBoy666
Starting Member

14 Posts

Posted - 2005-06-30 : 09:27:58
Please ignore this question...I had a momentary lapse and forgot how UPDATE works...
Go to Top of Page
   

- Advertisement -