日期:2014-05-18  浏览次数:20429 次

|M| 如果根据一个数组删除数据库记录 强
如A表有字段DeleteID
A
DeleteID
2,4,5,6

B
ID     Name
1       Age
2       Apple
3       Orange
4       Egg
5       Banana
6       Tomato

写出SQL语句根据A表中的DeleteID里面的ID号删除B表的相应记录

谢谢

------解决方案--------------------
delete from B where charindex(DeleteID,(select DeleteID from A) )> 0
------解决方案--------------------
create table A(DeleteID varchar(20))
insert A select '2,4,5,6 '

create table B(ID int,Name varchar(20))
insert B select 1, 'Age '
union all select 2, 'Apple '
union all select 3, 'Orange '
union all select 4, 'Egg '
union all select 5, 'Banana '
union all select 6, 'Tomato '

delete from B where charindex( ', '+rtrim(ID)+ ', ', ', '+(select DeleteID from A)+ ', ')> 0

select * from B

drop table A,B
------解决方案--------------------
delete from b
from b inner join a on a.detailId=b.id