用一Sql语句能直接实现批量删除吗?
表A
ID NAME
--------
1 DEMO
2 DEMO2
表B
ID AID PRICE
------------
1 1 30
2 1 32
3 1 26
4 1 26
5 2 30
删除后结果:
表B
ID AID PRICE
------------
5 2 30
create table A(ID int, Name varchar(10))
insert A select 1, 'DEMO '
union all select 2, 'DEMO2 '
create table B(ID int, AID int, PRICE int)
insert B select 5, 2, 30
union all select 2, 1, 32
union all select 3, 1, 26
union all select 4, 1, 26
union all select 5, 2, 26
SELECT B.* FROM B ,(SELECT * FROM A WHERE A.ID=1) AS C WHERE C.ID=B.AID
能用一条Sql语句能直接实现批量删除上面选择的那些记录吗?还是需要写触发器?
------解决方案--------------------delete B from B ,(SELECT * FROM A WHERE A.ID=1) AS C WHERE C.ID=B.AID
------解决方案--------------------delete from b where aid=1
------解决方案--------------------declare @A table (ID int, Name varchar(10))
insert @A select 1, 'DEMO '
union all select 2, 'DEMO2 '
declare @B table(ID int, AID int, PRICE int)
insert @B select 1, 1, 30
union all select 2, 1, 32
union all select 3, 1, 26
union all select 4, 1, 26
union all select 5, 2, 26
delete b from @b b where exists(select 1 from @a where id=b.aid and id=1)--这样效率高一点
select * from @b
(所影响的行数为 2 行)
(所影响的行数为 5 行)
(所影响的行数为 4 行)
ID AID PRICE
----------- ----------- -----------
5 2 26
(所影响的行数为 1 行)