Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Assuming i have more data in to 2 database table.My problem is How to delete the row/data in Table A if the row/data found in Table B? How to compare them and how to delete the duplicate data in table A to Table B.
Posted
Updated 29-Jul-13 21:19pm
v2
Comments
Maciej Los 30-Jul-13 3:25am    
Please, be more specific and provide more details (example data). We can't read in your mind ;(
What have you tried till now?
Gishisoft 30-Jul-13 3:34am    
Just like what Winston Madiano did. thats what i want.
Maciej Los 30-Jul-13 3:35am    
Who?
Gishisoft 30-Jul-13 3:35am    
It's just like a recursive time of search and delete from one table to another table.
Gishisoft 30-Jul-13 3:38am    
check below. the one who give his solution.

SQL
Delete from #T1 Where exists(Select Id From #T2 where #T2.Id=#T1.Id)


I suggest Using exists Instead of In because EXISTS will be faster because once the engine has found a hit, it will quit looking as the condition has proved true. With IN it will collect all the results from the subquery before further processing

Tested:
VB
Create table #T1 (Id int)
Create table #T2 (Id int)
Insert into #T1
Select 1 Union all
Select 2 Union all
Select 3 Union all
Select 4 Union all
Select 5

Insert into #T2
Select 1 Union all
Select 2 Union all
Select 3 

Delete from #T1 Where exists(Select Id From #T2 where #T2.Id=#T1.Id)

Select * From #T1 -- Returns 4,5

Drop Table #T1
drop Table #T2

http://www.techonthenet.com/sql/delete.php[^]
 
Share this answer
 
v5
Comments
Maciej Los 30-Jul-13 3:52am    
+5Good answer!
Raja Sekhar S 30-Jul-13 4:08am    
Thanks Maciej Los!....
Gishisoft 30-Jul-13 4:02am    
sorry it won't work. it will not search the data and delete it.
Gishisoft 30-Jul-13 4:06am    
i used variable table for this. but nothings happen. :(
Raja Sekhar S 30-Jul-13 4:08am    
Can u Post the Query Which u Used..?
HI,

I would suggest you for the IsDeleted column in table A for the items to delete. I am suggesting you this solution because if the data from the table A is not referred as foreign key in any other table then the data can be deleted but if the data is mapped in other table as foreign key then that will never be deleted and you will be in trouble.

So make a habit of adding the IsDeleted (boolean) column in each table and set the value there as deleted if you want to delete the records. In this way you can manage the foreign key conflicts as well in the delete query.

Thanks
 
Share this answer
 
SQL
DELETE FROM [Table A] WHERE [ID] IN (SELECT [ID] FROM [Table B]);
 
Share this answer
 
v2
Comments
Gishisoft 30-Jul-13 3:27am    
Ok i will try this one.
Gishisoft 30-Jul-13 3:46am    
oopps. is there any another option? it doesn't work. :( some peace is missing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900