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.
Author |
Topic |
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2007-09-26 : 16:28:04
|
Hi there,I get the tableB by doing a select from TableA (TableB is a subset of TableA)How do I delete the rows from TableA corresponding to the rows in TableBI kind of want to delete the subset TableB from teh original TableATableA and TableB have of course the same columns (col1, Col2, Col3, Col4, Col5)Thanks a lot. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-26 : 16:29:58
|
delete afrom tablea as ainner join tableb as b on b.pkcol = a.pkcol E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-26 : 16:33:59
|
Or why not do the both thing at once?DECLARE @TableA TABLE (i INT)INSERT @TableASELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3DECLARE @TableB TABLE (i INT)SELECT * FROM @TableASELECT * FROM @TableBDELETE aOUTPUT deleted.iINTO @TableBFROM @TableA AS aWHERE i = 2SELECT * FROM @TableASELECT * FROM @TableB E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-26 : 16:37:07
|
Or insert into two tables with ONE insert statement?DECLARE @TableA TABLE (i INT)DECLARE @TableB TABLE (i INT)SELECT * FROM @TableASELECT * FROM @TableBINSERT @TableAOUTPUT inserted.iINTO @TableBSELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3SELECT * FROM @TableASELECT * FROM @TableB E 12°55'05.25"N 56°04'39.16" |
|
|
rtutus
Aged Yak Warrior
522 Posts |
Posted - 2007-09-26 : 16:51:34
|
Thank you I will try it.My table has more than one column so I guess I replace:on b.pkcol = a.pkcolbyon b.pkcol1 = a.pkcol1 and b.pkcol2 = a.pkcol2 and b.pkcol3 = a.pkcol3Right?Thank you very muchP.S: i didn t understand the last two suggestions but I will try them. |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-09-27 : 07:29:44
|
pkcol means your Primary Key column.If your Primary Key is multi column, then yes, you need to join on each one.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
|
|
|
|
|