Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jscot
Posting Yak Master
106 Posts |
Posted - 2011-04-04 : 21:14:54
|
| Create Table Table1 (ID VARCHAR(20),ORDER_ID VARCHAR(20))CREATE TABLE TABLE2(ID VARCHAR(20),LOC_ID VARCHAR(20))INSERT INTO Table1 VALUES ('1','CRB-123')GOINSERT INTO Table1VALUES ('2','CRB-345')GOINSERT INTO Table1 VALUES ('1','CRB-678') GOINSERT INTO TABLE2VALUES ('1','57')GOINSERT INTO TABLE2VALUES('2','57')GOINSERT INTO TABLE2 VALUES('1','57')GO--USING UPDATE STATMENTUPDATE TABLE2 SET LOC_ID = Table1.ORDER_ID FROM TABLE2 INNER JOIN TABLE1 ON TABLE2.ID = Table1.IDI am getting this resultID LOC_ID1 CRB-1232 CRB-3451 CRB-123I want to be result like this after i run update statementID LOC_ID1 CRB-1232 CRB-3451 CRB-678Please let me know how i can fix this problem. |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2011-04-05 : 01:32:34
|
| hi,i don't know if you have a huge database behind this problem, but i would suggest you to have database normalized in terms to perform "correct" update. your update statement is okey, but your data lack some general consistency.So add another Unique ID on table1 and table2 to match all the IDs together to support your model and then perform update.if this is the case only of three update statements, just hard-code the new values for LOC_ID in table2. |
 |
|
|
|
|
|