Solved

SQL Server 2005 Mass update/insert help

Posted on 2009-05-13
4
521 Views
Last Modified: 2012-05-06
Hello,
I'm working with SQL Server 2005 and I have to develop a process that would compare records in a stagging table with that in production. The process would update the records if they exist, but insert them if they don't. I'm struggling in deciding wether the best method is to use a stored procedure or trigger. The staging table will contain tens of thousands of records that will be used to update/insert into multiple production tables. Does anyone have a best method approach or recommendation as well as some examples to get me pointed in the right direction? I really appreciate any help. Thanks.
Claude
0
Comment
Question by:cbads
4 Comments
 
LVL 13

Expert Comment

by:St3veMax
ID: 24375978
I would do stored procedure and schedule it as a SQL Agent job on a periodic basic.

You could simply do a Insert into X where Y NOT IN (SELECT Y FROM Destination) where Y is an ID field or some form of PK.

Then you could do your comparisons. How many columns are we talking about ?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24375995
You should DEFINITELY use a proc and not a trigger.  This will allow you to queue up large amounts of data and do set based operations for transferring data.  Otherwise, in a trigger you would have to cursor or loop across all inserted records.
0
 
LVL 25

Accepted Solution

by:
reb73 earned 500 total points
ID: 24376070
It would be advisable to handle this in a stored procedure and not in trigger as such.. Triggers are ideally used for validation / logging for data-entry activities and not for synchronization of tables..

A single update and a single insert statement per table should be sufficient (template code below). You can either code  all the Update/Insert statements in a stored procedure or execute the update/insert statements as a SQL statement batch..


UPDATE TT

SET   <Fields to be updated>

FROM <TargetTable> TT

INNER JOIN <SourceTable> ST ON ST.<Keyfield1> = TT.<Keyfield1>  [ AND ST.<Keyfieldn> = TT.<KeyFieldn>]
 

INSERT <TargetTable>

         (<FieldList>)

SELECT  <FieldList>

FROM <TargetTable> TT

WHERE NOT EXISTS (SELECT NULL FROM <SourceTable> ST 

                                 WHERE ON ST.<Keyfield1> = TT.<Keyfield1>  [ AND ST.<Keyfieldn> = TT.<KeyFieldn>]

                                )

Open in new window

0
 

Author Closing Comment

by:cbads
ID: 31581018
Thank you to all who responded.
claude
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now