SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 replace rows with a table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

zhshqzyc
Posting Yak Master

240 Posts

Posted - 06/21/2012 :  08:27:30  Show Profile  Reply with Quote
Hello,
I have a datatable generate from an application. All rows in the table have the same vlaues in the second column.(Not Primary Key). I want to replace the rows in a database table in SQL server 2008 R2 which have the same column values.

That means:
1. If rows are not in the database table, insertion.
2. If rows are in the database table, updatation.
3. If rows are not in the new table but in Sql Server, deletion.

Thanks for help.

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/21/2012 :  08:30:52  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
not really sure what yoou want but maybe

merge table1 t1
using table2 t2
on t1.id = t2.id
when matched then update set colb = t2.colb
when not matched by target then insert
(id, colb)
values (t2.id,t2.colb)
when not matched by source then delete;


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Edited by - nigelrivett on 06/21/2012 08:31:15
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 06/21/2012 :  08:43:14  Show Profile  Reply with Quote
Yes. That is what I want.
For the delete part, do I need where clause?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/21/2012 :  18:19:01  Show Profile  Reply with Quote
nope..thats what when not matched by source means

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000