Insert/Update using Tsql from one table to other
-
Friday, February 01, 2013 7:36 PM
Hi Guys,
I would like to insert/Update the record from one table to other. I have attached a screenshot for more info.
All blue color columns can be used for checking whether that record is exists in Table B.
Please advise. Thank you in advance.
All Replies
-
Friday, February 01, 2013 7:58 PMNot sure I understand the correction. If you do not insert on existence, how is it that Mark's COMPANY became Microsoft from Oracle?
Please mark as answer if this answers your question. Please mark as helpful if you found this post helpful.
-
Friday, February 01, 2013 8:04 PM
Have you tried with MERGE JOIN statement ???
Check the next post in the forum, if you have any question you can ask us :)
What is your SQL SERVER version ??
SQL Server 2008 : Merge T-SQL Command
Sergio Sánchez Arias
- Proposed As Answer by pituach Friday, February 01, 2013 8:23 PM
- Marked As Answer by Kalman TothMicrosoft Community Contributor, Moderator Sunday, February 10, 2013 10:11 AM
-
Friday, February 01, 2013 8:16 PMThe Table B should update a Mark's record from Oracle to Microsoft.
-
Friday, February 01, 2013 8:56 PM
Here you go:
MERGE TABLE_B AS TARGET USING (SELECT * FROM TABLE_A O) Source (ID_A, ADDRESS_A, ZIP_A, PHONE_A, COMPANY_A) ON (TARGET.ID = SOURCE.ID_A AND TARGET.ADDRESS = SOURCE.ADDRESS_A AND TARGET.ZIP = SOURCE.ZIP_A) WHEN MATCHED THEN UPDATE SET TARGET.PHONE = SOURCE.PHONE_A, TARGET.COMPANY = SOURCE.COMPANY_A WHEN NOT MATCHED THEN INSERT(ID, ADDRESS, ZIP, PHONE, COMPANY) VALUES(ID_A, ADDRESS_A, ZIP_A, PHONE_A, COMPANY_A) ;
The aliases are just for your understanding, you can change them.
Please mark as answer if this answers your question. Please mark as helpful if you found this post helpful.
- Edited by SQL Novice 01 Friday, February 01, 2013 8:57 PM
-
Friday, February 01, 2013 9:02 PM
Try - this Inserts / Updates Table B
DECLARE @tableA TABLE(Id INT IDENTITY(1,1) ,NAME VARCHAR(15) ,Adress VARCHAR(20) ,Zip VARCHAR(15) ,Phone VARCHAR(15) ,Company VARCHAR(20)) INSERT INTO @tableA(NAME, Adress, Zip, Phone, Company ) VALUES('John','12 HighStreet','54453','333-333-3333','Apple') ,('Mark','23 5thStreet','34356','444-444-4444','Microsoft'), ('Adam','16 park Street','45332','555-555-5555','IBM') ,('Sam','36 8thStreet','34553','222-222-2222','Java') SELECT *FROM @tableA DECLARE @tableB TABLE(Id INT IDENTITY(1,1) ,NAME VARCHAR(15) ,Adress VARCHAR(20) ,Zip VARCHAR(15) ,Phone VARCHAR(15) ,Company VARCHAR(20) ) INSERT INTO @tableB(NAME, Adress, Zip, Phone, Company ) VALUES('John','12 HighStreet','54453','777-777-7777','Apple') ,('Mark','23 5thStreet','34356','444-444-444','Oracle'), ('Adam','16 park Street','45332','555-555-5555','IBM') --Before Inserting / Updating SELECT * FROM @tableB INSERT INTO @tableB(NAME, Adress, Zip, Phone, Company ) SELECT a.NAME, a.Adress, a.Zip, a.Phone, a.Company FROM @tableA a WHERE 0= (SELECT COUNT(*) FROM @tableB b WHERE a.Id=b.Id AND a.NAME=b.NAME AND a.Adress=b.Adress AND a.Zip=b.Zip ) --After inserting new rows SELECT * FROM @tableB UPDATE b SET b.Phone=a.Phone ,b.Company=a.Company FROM @tableA a INNER JOIN @tableB b ON a.Id=b.Id AND a.NAME=b.NAME AND a.Adress=b.Adress AND a.Zip=b.Zip AND ((CASE WHEN ((a.Phone IS NULL AND b.Phone IS NULL OR a.Phone = b.Phone)) THEN 1 ELSE 0 END) = 0 OR (CASE WHEN ((a.Adress IS NULL and b.Adress is NULL OR a.Adress = b.Adress)) THEN 1 ELSE 0 END) = 0) --After updating SELECT * FROM @tableB
Narsimha

