Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need Help with insert and update using sql server 2005 Expand / Collapse
Author
Message
Posted Wednesday, August 11, 2010 3:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 8, 2010 1:06 PM
Points: 5, Visits: 9
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
Post #967838
Posted Wednesday, August 11, 2010 3:46 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:11 PM
Points: 7,019, Visits: 12,909
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...




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #967858
Posted Wednesday, August 11, 2010 4:48 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 8, 2010 1:06 PM
Points: 5, Visits: 9
i get an error at from with the update statment.

can't we do this with a single query? stored procedure?
Post #967873
Posted Wednesday, August 11, 2010 5:33 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 6,580, Visits: 8,856
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
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #967882
Posted Wednesday, August 11, 2010 10:48 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 8, 2010 1:06 PM
Points: 5, Visits: 9
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
Post #967932
Posted Tuesday, February 8, 2011 5:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 19, 2013 8:10 AM
Points: 1, Visits: 49
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
Post #1060133
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse