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 123»»»

T-SQL 2005 UPSERT help Expand / Collapse
Author
Message
Posted Friday, April 17, 2009 12:21 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 12:53 PM
Points: 21, Visits: 87
Upsert Logic
Post #699688
Posted Friday, April 17, 2009 1:22 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:47 PM
Points: 2,023, Visits: 4,948
Check the small demo bellow. I didn’t fallow the exact way that you asked, but if you want you can modify it to have also a delete statement before the insert statement and then do an insert without where clause.

--Creating the tables and insert some test data
create table tbl (i int not null primary key, c char(5))
go

insert into tbl (i,c)
select 1, 'abc'
union
select 2, 'def'
union
select 3, 'ghi'
go

create table StagingTbl (i int not null primary key, c char(5))
go


insert into StagingTbl (i,c)
select 3, 'ghijk'
union
select 4, 'lmnop'
go

--First step is to update column c according to i
--I do it with an update statement that uses
--from clause. You can read about it in BOL
update tbl
set tbl.c = StagingTbl.c
from tbl inner join StagingTbl on tbl.i = StagingTbl.i

--The insert is done with select that is doing a left
--join and inserts only records that were not found
--in tbl1.
insert into tbl (i,c)
select StagingTbl.i, StagingTbl.c
from StagingTbl left join tbl on StagingTbl.i = tbl.i
where tbl.i is null
go

--Check the results
select * from Tbl

--cleanup
drop table tbl
go
drop table StagingTbl

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #699727
Posted Friday, April 17, 2009 1:41 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, June 10, 2013 12:13 PM
Points: 4,319, Visits: 9,217
A slightly easier, and generally accepted method is to perform a delete/insert instead of an upsert. For example:

DELETE FROM Destination
WHERE key IN (SELECT key FROM Source);

Or, using EXISTS:

DELETE FROM Destination
WHERE EXISTS (SELECT * FROM Source WHERE key = Destination.key);

Then, we perform the insert:

INSERT INTO Destination
SELECT {columns} FROM Source;

Once completed, cleanup the source...

DELETE FROM Source;


Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #699743
Posted Sunday, April 19, 2009 8:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:48 AM
Points: 10,990, Visits: 10,578
Replacing the UPSERT with a DELETE then INSERT doesn't scale very well.

This is a demo of an alternative:

--DROP TABLE #Destination, #Staging

CREATE TABLE #Destination (a INT IDENTITY(1,1) PRIMARY KEY, Data VARCHAR(36) NULL)
CREATE TABLE #Staging (a INT IDENTITY(5,1) PRIMARY KEY, Data VARCHAR(36) NULL)

-- Test data
SET NOCOUNT ON
GO
INSERT #Destination (Data) SELECT '';
INSERT #Staging (Data) SELECT CONVERT(VARCHAR(36), NEWID());
GO 10

-- Show the 'before'
SELECT * FROM #Destination; SELECT * FROM #Staging

DECLARE @KeysInserted TABLE (a INT PRIMARY KEY)

SET IDENTITY_INSERT #Destination ON

-- Insert new rows, remembering the keys
INSERT #Destination (a, Data)
OUTPUT inserted.a INTO @KeysInserted
SELECT S.a, S.Data
FROM #Staging AS S
WHERE NOT EXISTS (SELECT 1 FROM #Destination AS D WHERE S.a = D.a)

SET IDENTITY_INSERT #Destination OFF

-- Update
UPDATE D
SET Data = S.Data
FROM #Staging AS S
JOIN #Destination AS D ON D.a = S.a
WHERE NOT EXISTS (SELECT 1 FROM @KeysInserted AS KI WHERE KI.a = S.a)

-- The 'after'
SELECT *
FROM #Destination

Cheers

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #700204
Posted Sunday, April 19, 2009 5:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 12:53 PM
Points: 21, Visits: 87
Hi ,

Thanks to you all for all your replis.

Post #700297
Posted Sunday, April 19, 2009 5:43 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:48 AM
Points: 10,990, Visits: 10,578
Hi,

Several different solutions have been provided, all of which work - some even use EXISTS as you requested.

If you don't understand them, please try again. If you still don't get it, ask a specific question and you will get a specific answer.

"Plz post teh codez" is not gonna work here

Cheers

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #700307
Posted Monday, April 20, 2009 7:28 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 12:53 PM
Points: 21, Visits: 87
Thanks, for your reply.
Post #701112
Posted Monday, April 20, 2009 7:45 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:48 AM
Points: 10,990, Visits: 10,578
All the information you need to answer question is in this thread, if you look for it.
We've done our bit by providing examples and explaining the techniques.
It's now up to you to put it into practice.

At least have go, based on what we've already given you, and come back after you've put some effort in, ok?

You won't learn new stuff by having it done for you all the way to the end.

Cheers,

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #701115
Posted Monday, April 20, 2009 8:01 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, March 22, 2013 11:49 AM
Points: 945, Visits: 998
rajamohangade (4/20/2009)

How can I write above logic by using EXISTS ?? Any help on this is really appreciated


There are some excellent examples already posted. What have you tried? Let's have a look at the code you've written based on the examples so we can offer more help.
Post #701120
Posted Monday, April 20, 2009 10:01 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:47 PM
Points: 2,023, Visits: 4,948
Can you explain why the SQL Statement has to use the exist operator?

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #701154
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse