Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have to update one table in sql
i am using the following query:
SQL
UPDATE rdc_lb
 set
 rdc_lb.mrop=(select makeready.mrop from makeready where makeready.date=rdc_lb.date and makeready.shift=rdc_lb.shift and makeready.ns=rdc_lb.ns)

but it is not working fine.

How can I update the table?

Thank You
Posted
Updated 13-Jun-12 2:45am
v2

Your syntax is correct (as long as the tables actually have the fields you specified). I replicated the query you have here on my own system and it ran as expected. My assumption, then, is that the issue is with the data. Most likely your data doesn't quite match or you have a duplicate. Let me explain each possibility.

One way your data might not match is if the date fields are storing actual dates that weren't copied from one to the other. Date fields usually store the entire date/time value, which means that two values might be on the same day but won't be equal because they are one second different in time (or less depending on the precision of your date type). You should check to see how the dates are stored. It would be an expensive query, but you might have to parse out just the date portion of each value and compare them (or create a calculated field).

Duplicate rows that match your criteria will also be a problem. If you have two rows that match your criteria then the sub-query will return two or more rows. That isn't ok. SQL will crash. To fix this, you could limit your sub-query to the TOP 1 rows like so:

SQL
UPDATE rdc_lb
SET rdc_lb.mrop=(SELECT TOP 1 makeready.mrop FROM makeready WHERE makeready.date=rdc_lb.date and makeready.shift=rdc_lb.shift AND makeready.ns=rdc_lb.ns)


That only works if it is ok to choose only the first row (if the second row would have the same data, for instance). Otherwise, you are going to need to find a different way to figure out which row to choose (maybe order them in descending order based upon creation date and then select the top one so you get the latest value).
 
Share this answer
 
Comments
Zukiari 14-Jun-12 1:11am    
Actually, the makeready table contains only one row wich is having the same date,shift and ns values. But in rdc_lb table there are multiple rows with same date,shift and ns values. I want to update the mrop value in each row of rdc_lb table with the value from makeready table.

Thank you.
Tim Corey 14-Jun-12 7:50am    
Then this query will work. If it does not, then your issue is that the data does not exactly match. My guess is that the makeready date field has just a date (which then implies a midnight value for the time) whereas your rdc_lb table has actual date/time values (which will cause a mismatch).
Zukiari 15-Jun-12 0:20am    
Thanks a lot.... There was some mismatch on the data.

Thank you
Dear Friend,

Hope this sample query will be helping you.

SQL
update BLL set status='CLOSE' from
--select * from
BLL a (nolock), BML b (nolock) where a.ID=b.ID


Regards,
AP
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900