Solved

SQL Server 2005 Mass update/insert help

Posted on 2009-05-13
4
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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 …
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

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