?
Solved

SQL Server 2005 Mass update/insert help

Posted on 2009-05-13
4
Medium Priority
?
542 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 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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