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 |
deha_chopper
Starting Member
5 Posts |
Posted - 2012-12-05 : 03:11:53
|
Hi all,Need some help here, I want to update one table based on another table.For example, I have 3 tablesTABLE ANAME REGION IS_A QUANTITYAndi INN N 0Andi OUT N 0Andi POR N 0TABLE BREGION QUANTITYINN 5 OUT 3 POR 4 TABLE CNAME REG IS_AAndi INN ASo, what i want to do is to update table A based on table B and C.Since there is only Andi in table C, and Andi's REGION is INN, I want to update table A for Andi to be like below and keep the others value.TABLE ANAME REGION IS_A QUANTITYAndi INN A 5Budi OUT N 0Charlie POR N 0i'm sorry if there is any unclear explanation. |
|
deha_chopper
Starting Member
5 Posts |
Posted - 2012-12-05 : 03:26:04
|
Hi all,Need some help here, I want to update one table based on another table.For example, I have 3 tablesTABLE ANAME REGION IS_A QUANTITYAndi INN N 0Andi OUT N 0Andi POR N 0TABLE BREGION QUANTITYINN 5 OUT 3 POR 4 TABLE CNAME REG IS_AAndi INN ABudi OUT ASo, what i want to do is to update table A based on table B and C.Since there is only Andi and Budi in table C, and Andi's REGION is INN and Budi's REGION is OUT, I want to update table A for Andi and Budi to be like below and keep the others value.TABLE ANAME REGION IS_A QUANTITYAndi INN A 5Budi OUT A 3Charlie POR N 0just for information, i have a lot of different names and regionsi'm sorry if there is any unclear explanation. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-05 : 03:27:47
|
UPDATE aSET IS_A = c.IS_A ,QUANTITY = b.quantity, NAME = c.NAME FROM TABLEA a JOIN TABLEB b ON a.REGION= b.REGIONJOIN TABLEC c ON a.NAME = c.NAME AND b.REGION= c.REGEDIT:DECLARE @TABLEA TABLE(NAME varchar(10), REGION VARCHAR(10), IS_A CHAR(1), QUANTITY int)INSERT INTO @TABLEASELECT 'Andi', 'INN', 'N', 0 union all SELECT 'Andi', 'OUT', 'N', 0 union allSELECT 'Andi', 'POR', 'N', 0DECLARE @TABLEB TABLE(REGION VARCHAR(10), QUANTITY int)INSERT INTO @TABLEBSELECT 'INN', 5 union allSELECT 'OUT', 3 union all SELECT 'POR', 4 DECLARE @TABLEC TABLE(NAME varchar(10), REG VARCHAR(10), IS_A CHAR(1))INSERT INTO @TABLECSELECT 'Andi', 'INN', 'A' union allSELECT 'Budi', 'OUT', 'A'UPDATE @TABLEASET IS_A = c.IS_A ,QUANTITY = b.quantity, NAME = c.NAMEFROM @TABLEA a JOIN @TABLEB b ON a.REGION= b.REGIONJOIN @TABLEC c ON b.REGION= c.REGSELECT * FROM @TABLEA--Chandu |
|
|
deha_chopper
Starting Member
5 Posts |
Posted - 2012-12-05 : 03:52:33
|
thanks.. it works |
|
|
deha_chopper
Starting Member
5 Posts |
Posted - 2012-12-05 : 04:02:17
|
can i update the table in database based on the temp table? i try to update but i comes errorMsg 137, Level 16, State 1, Line 27Must declare the scalar variable "@TABLEA". |
|
|
deha_chopper
Starting Member
5 Posts |
Posted - 2012-12-05 : 04:55:57
|
ok.. i already figure it out |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-05 : 05:32:04
|
quote: Originally posted by deha_chopper thanks.. it works
Welcome--Chandu |
|
|
|
|
|
|
|