SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need Help with insert and update using sql server 2005


Need Help with insert and update using sql server 2005

Author
Message
hemagkv
hemagkv
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10425 Visits: 13559
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
hemagkv
hemagkv
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 9
i get an error at from with the update statment.

can't we do this with a single query? stored procedure?
WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)

Group: General Forum Members
Points: 9948 Visits: 10573
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
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

hemagkv
hemagkv
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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
rameshnagineni
rameshnagineni
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search