| Author |
Topic |
|
Darrenmf
Starting Member
5 Posts |
Posted - 2010-02-01 : 06:30:08
|
| I am currently trying to run an update and getting the error message - Msg 512, Level 16, State 1, Line 1Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The statement has been terminated.I am assuming this means i have to do 1 to 1 updates? Is there any way i can do a bulk update?The code i am using at current is:UPDATE Table1SET Table1.Column1 = (SELECT max(Table2.Column1) FROM Table2 ,Table1 WHERE Table1.Column2 = table2.column2group by Table1.Column2,Table1.Column1having Table1.Column1 <> max(Table2.Column1)) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-01 : 06:36:40
|
| this means the subquery returns more than one value so you might need to join to subquery to do the update |
 |
|
|
Darrenmf
Starting Member
5 Posts |
Posted - 2010-02-01 : 06:38:51
|
| Right ok, Can you define how within the query this would be done?I have played with joins unsuccessfully on this query |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-01 : 06:48:55
|
something like:-UPDATE t1SET t1.Column1 = t2.MaxValFROM Table1 t1JOIN (SELECT Table2.Column2,max(Table2.Column1) AS MaxValFROM Table2 group by Table2.Column2) t2ON t1.Column2 = t2.column2WHERE t1.Column1 <> t2.MaxVal |
 |
|
|
Darrenmf
Starting Member
5 Posts |
Posted - 2010-02-01 : 06:55:22
|
| That is genius.....Much appreciated..Worked blindingly |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-01 : 06:59:05
|
| Better you do it as SELECT statement as you dont need to run UPDATEs whenever data are addedMadhivananFailing to plan is Planning to fail |
 |
|
|
Darrenmf
Starting Member
5 Posts |
Posted - 2010-02-01 : 07:01:34
|
| This was to fix a defect as the data is automatically populated based on a trigger when one column changes in one table it should update value into the other.. We had some rows where this had not occurred so this is a one of update to bring both tables into line with one another, So a select statement would not have helped. Thanks anyways |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-02 : 05:15:46
|
quote: Originally posted by Darrenmf That is genius.....Much appreciated..Worked blindingly
welcome |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-02 : 05:16:34
|
quote: Originally posted by Darrenmf This was to fix a defect as the data is automatically populated based on a trigger when one column changes in one table it should update value into the other.. We had some rows where this had not occurred so this is a one of update to bring both tables into line with one another, So a select statement would not have helped. Thanks anyways
ok great. so is this logic now included in trigger to do update automatically in future? |
 |
|
|
|