Solved

SQL Server 2005 Mass update/insert help

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select - Finding chars in a column 2 71
Passing value to a stored procedure 8 112
CROSS APPLY 4 61
SQL parsing XML works but want to do it another way 4 22
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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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