SQL 2005 - If Exists then insert else update?

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?
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
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
Faiga DiegelSr Database EngineerCommented:
IF EXISTS (SLECT 1 FROM TableName WHERE ConditionHere)
   BEGIN
            INSERT STATEMENT
   END
ELSE
   BEGIN
           UPDATE STATEMENT
   END
0
sbornstein2Author Commented:
Is the If Exists faster or slower?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

chapmandewCommented:
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
SharathData EngineerCommented:
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
chapmandewCommented:
MERGE is only available in 2008.
0
sbornstein2Author Commented:
right and I am using 2005 chap the insert statement is pulling back all the records and should be pulling back none
0
chapmandewCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author Commented:
absolutly perfect thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.