473,583 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

T-SQL UPDATE .. FROM .. ORDER BY problem

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_consolidat e' 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?

Jul 23 '05 #1
2 54454
(ch************ *@gmail.com) writes:
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_consolidat e' 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?


I'm afraid that this does not make any sense at all. A table is an
unordered set of data, and an UPDATE statement will access rows in
order that the optimizer estimates to be the most effecient.

If I am to make a complete guess, this may be what you want:

UPDATE tbl_consolidate
SET field1 = h.field1, field2 = h.field2, field3 = h.field3
FROM tbl_consolidate c
JOIN tbl_hold h ON c.ID1 = h.ID1
JOIN (SELECT ID1, datefield1 = MAX(datefield1)
FROM tbl_hold
GRUOP BY ID1) AS m ON h.ID1 = m.ID1
AND h.datefield1 = m.datefield1

This will set tbl_consolidate to the latest values for each ID.

If this does not meet your requirements, please post the following:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The desired result given the sample.
o A short narrative of your business problem.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Sorry, you're right. I've just realised what I'm doing wrong...

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3525
by: Yannick Turgeon | last post by:
Hello, I'm using SS2000 on NT4. If I run the following query, is it garanteed that the field "OldField1Value" will hold the value Field1 had before the query was run? UPDATE MyTable SET Field1 = 42, OldField1Value = Field1
1
1863
by: Chris Michael | last post by:
I've only just started using update queries and have a problem with the following query in that it comes back with this error: "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression." Query: UPDATE tbl_customerpassword SET ...
4
2476
by: Gernot Frisch | last post by:
Hi, when porint apps to powerpc the byte order get's swapped to low-endian. Now, what problems can occur and what should I take care of when porting to mac e.g.? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
10
8775
by: sqlgoogle | last post by:
Hi I'm trying to update a db based on the select statement which has ORDER BY in it. And due to that I'm getting error which states that Server: Msg 1033, Level 15, State 1, Line 13 The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified Here is my sql statement:
3
6304
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field new field table 2 has: key field (autonumber)
4
2606
by: mr_wizard | last post by:
I haven't written ASP code to update an Access DB in years. Having a permission problem. Been working on it for hours, time to get some help. Running IIS 5 on an XP Pro system. ASP code to update an Access 2002 table via ODBC. Receive the following error: ADODB.Recordset (0x800A0CB3) Current Recordset does not support updating. This may be a...
1
1716
by: BW | last post by:
Hello, I am attempting to to take edited values from a datagrid control and update the underlying database. Problem is that some of the edited values don quite seem to make it to my update sub. My code is shown below. The values that are not appearing are Cell(4) - sCompletion and Cell(5) - sNote. The other values show up just fine. What...
2
3227
by: Agnes | last post by:
Binding Manager (update problem) by agnescheng I use binding Manager to bind the textbox, I got a problem on update record. E.g Me.txtUserId.Text = "PETER" bm.current.item("userid") = Me.txtUserId.Text. if ds.haschanges() then .................... .....acceptchanges() messagebox.show("Save is completed") end if................ I found...
3
5412
by: mrutyunjaya | last post by:
if (Cache==null) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings.ConnectionString); SqlDataAdapter mydataadapter = new SqlDataAdapter("select intproductid,vchproductname,vchproductacctype,vchprodacclogoimg from productmaster order by intproductid desc", con); ...
1
1721
by: 125a29 | last post by:
hi i'm trying to update total order cost in order table , basically what i want when i edit order item table such as changing quantity then it automatically update the total cost in order table i mean if there are two order items in orderitemtable such as for item 1 and the hire cost is £20 and quantity is 2 for each item and these two...
0
7829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7940
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6587
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5705
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5379
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.