Solved

SQL Server 2005 Mass update/insert help

Posted on 2009-05-13
4
518 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Group by correlation 4 43
sql calculate reminders 11 56
PERFORMANCE OF SQL QUERY 13 58
sql help 5 49
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

15 Experts available now in Live!

Get 1:1 Help Now