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

Update Table If Record Exists Else Insert ? Expand / Collapse
Author
Message
Posted Friday, December 3, 2010 5:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 4, 2013 2:02 AM
Points: 3, Visits: 38
I have a 'Test1' table (with: 'uid' and 'CatFName' and ''CatLName' and 'CatMName' and 'countID' columns)
and 'Test2' table (with: 'uid' and 'CatFName' and ''CatLName' and 'CatMName' columns).

When the inserting the records from Test2 to Test1. if the existing record is present then update the count
else insert as new record.
Post #1029790
Posted Friday, December 3, 2010 6:25 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
If you're using SQL 2005 (as per the forum you posted in), then you do a two-state "upsert", which just means you do an update on matched records, and then an insert on unmatched records. It's really no more complex than that.

If you're actually in 2008 by any chance, you can use Merge to do it all at once.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1029814
Posted Friday, December 3, 2010 6:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 7,136, Visits: 13,525
The two-stage method mentioned by GSquared is outlined here.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1029815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse