?
Solved

SQL 2005 - If Exists then insert else update?

Posted on 2008-11-18
9
Medium Priority
?
1,537 Views
Last Modified: 2013-11-05
I have a staging table with the same fields, I need to either insert new data into the non staging table or update if exists.  Can anyone provide me some example syntax that will run quickly?
0
Comment
Question by:sbornstein2
9 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22987169
you'll need to do both if you're using multiple records:

--those that exist
update t2
set field = t1.field, field2 = t1.field2
from table1 t1
join table2 t2 on t1.fieldno = t2.fieldno

--those that do not

insert into t2(fieldlist)
select t1.fieldlist
from table1 t1
left outer join table2 t2 on t1.fieldno = t2.fieldno

note that fieldno is the common field(s) between the two tables.
0
 
LVL 15

Expert Comment

by:Faiga Diegel
ID: 22987177
IF EXISTS (SLECT 1 FROM TableName WHERE ConditionHere)
   BEGIN
            INSERT STATEMENT
   END
ELSE
   BEGIN
           UPDATE STATEMENT
   END
0
 

Author Comment

by:sbornstein2
ID: 22987348
Is the If Exists faster or slower?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 60

Expert Comment

by:chapmandew
ID: 22987363
It would be faster...but will only work if you're dealing with a single record...otherwise, you'll have to do operations on both tables because you may have records that do exist and dont' exist in the same recordset.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 22987637
You can use MERGE statement.
http://msdn.microsoft.com/en-us/library/bb510625.aspx
MERGE Table2 AS T
USING (SELECT col2,col3,col4 FROM Table1) AS S
   ON T.col1 = S.col1 -- col1 is the Primary key in your table 
 WHEN MATCHED THEN UPDATE SET col2 = S.col2, col3 = S.col3, col4 = S.col4
 WHEN NOT MATCHED THEN INSERT (col2,col3,col4) VALUES (S.col2,S.col3,S.col4) 

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22987712
MERGE is only available in 2008.
0
 

Author Comment

by:sbornstein2
ID: 22989633
right and I am using 2005 chap the insert statement is pulling back all the records and should be pulling back none
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 1000 total points
ID: 22989864
my fault...forgot to add the where

insert into t2(fieldlist)
select t1.fieldlist
from table1 t1
left outer join table2 t2 on t1.fieldno = t2.fieldno
where t2.fieldno is null
0
 

Author Closing Comment

by:sbornstein2
ID: 31517955
absolutly perfect thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

840 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