August 11, 2010 at 3:06 pm
Hi,
I have two tables namely result and temp. Both have the same fields. The primary key is Emp_ID. Based on Emp_ID I need to insert or update result table from temp table. Please anyone could help me with the code in doing this? I am using SQL Server 2005.
Thanks for your help.
Regards,
Hema
August 11, 2010 at 3:46 pm
Two basic concepts I use:
1) if you want to insert just one row:
IF EXISTS (SELECT 1 FROM targetTable t WHERE t.ID = @varId)
UPDATE
ELSE
INSERT
2) If you want to upsert (update or insert) multiple rows from another table:
UPDATE target
SET columns
FROM target t
INNER JOIN source s
ON t.id = s.id
--followed by
INSERT INTO target (column list)
SELECT source list
FROM source s
LEFT OUTER JOIN target t
ON s.id = t.id
WHERE t.id IS NULL
(Side note: don't do it the other way around -INSERT then UPDATE- since you would update the rows you just inserted...)
With SS2K8 the MERGE statement seems to make that task more efficient since you won't have to use two statements...
August 11, 2010 at 4:48 pm
i get an error at from with the update statment.
can't we do this with a single query? stored procedure?
August 11, 2010 at 5:33 pm
hemagkv (8/11/2010)
i get an error at from with the update statment.can't we do this with a single query? stored procedure?
You can do this with a single query only if you're on SQL 2008. Then you can use the MERGE statement.
Until then, you need to do the UPDATE, followed by the INSERT, to do this. Lutz gave a great example; use it.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 11, 2010 at 10:48 pm
Thanks a lot!
It worked. However I didn't understand the logic with insert. Why did you use left outer join and why did u compare with is null?
Once again thank you!
Regards,
Hema
February 8, 2011 at 5:23 am
drop table Products --Target Table)
drop table UpdatedProducts--Source Table --Update records
--Create a target(Destination) table
CREATE TABLE Products
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Rate MONEY
)
GO
--Insert records into target(Destination) table
INSERT INTO Products VALUES
(1, 'Tea', 10.00),
(2, 'Coffee', 20.00),
(3, 'Muffin', 30.00),
(4, 'Biscuit', 40.00)
GO
--Create source table
CREATE TABLE UpdatedProducts
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Rate MONEY
)
GO
--Insert records into source table
INSERT INTO UpdatedProducts VALUES
(1, 'Tea', 10.00),
(2, 'Coffee', 25.00),
(3, 'Muffin', 35.00),
(5, 'Pizza', 60.00)
GO
SELECT * FROM Products--Destination table
SELECT * FROM UpdatedProducts--Source Table
SELECT * FROM Products left outer join UpdatedProducts
on Products.ProductID=UpdatedProducts.ProductID
GO
/******************************************************************************/
/********************************** Note:-*************************************/
/******************************************************************************/
/* After executing above tables and Records then unComment following Script */
/*Update records in Destination table*/
--UPDATE Products --Target (Destination table)
--SET Products.ProductName =UpdatedProducts.ProductName,
--Products.Rate=UpdatedProducts.Rate
--FROM Products
--INNER JOIN UpdatedProducts
--ON Products.ProductID = UpdatedProducts.ProductID
--/* Insert New records in destination table */
--INSERT INTO Products (ProductID,ProductName,Rate)--New records
--SELECT UpdatedProducts.ProductID,UpdatedProducts.ProductName,UpdatedProducts.Rate
--FROM UpdatedProducts
--LEFT OUTER JOIN Products
--ON UpdatedProducts.ProductID = Products.ProductID
--WHERE Products.ProductID IS NULL
/******************************************************************************/
/********************************** Note:-*************************************/
/******************************************************************************/
Thanks & Regards
Ramesh Nagineni
Infosys Technologies
Hyderabad
Viewing 0 posts
You must be logged in to reply to this topic. Login to reply