Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update and Insert combination

Author  Topic 

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-06-21 : 07:13:12
Hi

I need to create an query where i compare two tables
and update change in records and also insert new records if it isn't exists yet.

An combination with UPDATE and INSERT, or is the best way to split it up in two separated querys?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-21 : 08:09:58
In 2005 it is not possible but in 2008.

In 2005 do an update first and after that an insert with where not exists()


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-21 : 09:39:27
In 2008 you can do it using Merge statement.

http://technet.microsoft.com/en-us/library/bb510625.aspx

If I am not mistaken Oracle supported it from 9i .Dont know why Microsoft waited till 2008 release to implement it.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-06-21 : 13:58:26
Thanks WebFred and Idera. You are right both
Merge is one way, unfortunate i dont have 2008, and it came to my conclusion also that
it was best to do an update-query first and second make an insert-query.

Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-21 : 14:04:58
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -