Sunday, November 20, 2011 2:20 PM
Please guide me how to achieve Merge equivalent in Sql Server 2008.
If the records are matching in both the tables then i want to update the records in target table.
If the records are not matched then insert the records in to target table.
If records are not exists in source table then need to delete from target table.
Sunday, November 20, 2011 8:51 PM
SQL Server 2008 actually has a MERGE statement that accomplishes this. Details, syntax and worked examples here:
Hope this helps.
Brent Greenwood, MS, MCITP, CBIP
Monday, November 21, 2011 12:39 AM
Thanks for your reply.
Here I dont want to use Merge because in my table i am having morethan 1.6 million of records when i use merge statement the query is taking long time and then throwing error as disk space is not enough.
So i want to implement insert,update delete without using merge,
Please suggest me.
Monday, November 21, 2011 7:47 AM
Best way to do this is to JOIN between source and target tables based on the key items and UPDATE the data which is already existing in the table. Further write an INSERT statement with NOT EXISTS option.
- UPDATE option being executed first will ensure that the no. of rows on which UPDATE runs is lesser (If we write insert first, then Update had to be performed on larger no. of rows while JOINing the table).
- INSERT with EXISTS option would ensure that the existance check stops at the first occurance of the key item.
Secondly, have index created on Key item so that the JOINs are faster.
Remember that if this is an operation with HUGE data, you may need to rebuild the statistics (index maintenance). Let me know if you have any questions further.
Phani Note: Please vote/mark the post as answered if it answers your question/helps to solve your problem.
Tuesday, November 22, 2011 3:18 PM
Could you please provide me the queries with two tables,Because I am new to data base side.
What ever you have explained in above please put it in queries so that i can use same logic for my requirement.
Santosh Kumar Y