sql server - SQL UPDATE SELECT with WHERE -


i want update column in table, in each row there must go value dependend on where of row.

this how table looks like.

businessunitguid | classname | defaultguid

    5        | pricelist | 349fdafd34m     5        | footer1   | 987ioxg376l     5        | header1   | 12wqx954mio     7        | pricelist | null     7        | footer1   | null     7        | header1   | null 

results should this.

businessunitguid | classname | defaultguid

    5        | pricelist | 349fdafd34m     5        | footer1   | 987ioxg376l     5        | header1   | 12wqx954mio     7        | pricelist | 349fdafd34m     7        | footer1   | 987ioxg376l     7        | header1   | 12wqx954mio 

but shown query doesn't work, because returns many rows , it's not precise.

update csc_businessunit set defaultguid =     (     select defaultguid     csc_businessunit      businessunitguid = 5     ) businessunitguid = 7 

you need check on classname well:

update b1 set b1.defaultguid =     (     select b2.defaultguid     csc_businessunit b2     b2.businessunitguid = 5         , b2.classname = b1.classname     ) csc_businessunit b1 b1.businessunitguid = 7 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -