473,785 Members | 2,188 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 54465
(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
3534
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
1868
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 tbl_customerpassword.passnumber = '1111' where tbl_customerpassword.customerid = (
4
2486
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
8790
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
6312
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
2611
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 limitation of the provider, or of the selected locktype. I have set the directory permissions on...
1
1726
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 am I missing here?
2
3236
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 that Messagebox is showed, BUT the userid is still the old value.
3
5426
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); mydataadapter.Fill(ds, "Authors");
1
1840
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 different orderitems ID are under one orderID and it calculates the total order cost but doesnot shows in...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.