两个表有相同的字段,现在要删除一个表的内容另一个表的内容也跟着删除,SQL语句怎么写
2个回答
展开全部
如果你非要同时删除,那你可以:
1、创建一个触发器(适用于每次删除A表,都要删除B表):
CREATE TRIGGER [tr_del_pk] ON [表A]
FOR DELETE
AS
delete 表A from deleted
where 字段1= deleted.字段1
delete 表2 from deleted
where 字段2 = deleted.字段2
2、写个循环(如果你删除带有条件,并且条件可能随时变动):
declare @count int
set @count = 1
begin
delete from 表A where 字段1 <= @count
delete from 表B where 字段2 <= @count
end
3、直接删除(一次执行,条件基本固定)
delete from 表A where .....
delete from 表B where .....
1、创建一个触发器(适用于每次删除A表,都要删除B表):
CREATE TRIGGER [tr_del_pk] ON [表A]
FOR DELETE
AS
delete 表A from deleted
where 字段1= deleted.字段1
delete 表2 from deleted
where 字段2 = deleted.字段2
2、写个循环(如果你删除带有条件,并且条件可能随时变动):
declare @count int
set @count = 1
begin
delete from 表A where 字段1 <= @count
delete from 表B where 字段2 <= @count
end
3、直接删除(一次执行,条件基本固定)
delete from 表A where .....
delete from 表B where .....
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询