在azure中一次从多个表中删除

时间:2018-05-03 13:14:34

标签: sql azure-sql-database

我正在尝试这个SQL语句一次从两个表中删除,但我发现它只是从一个表中删除并给我一个错误

  

DELETE语句与REFERENCE约束冲突" FK__cc_custom__custo__02084FDA"。冲突发生在数据库" CurtainClub",table" dbo.cc_customer_address",column' customer_number' .`

我的sql语句如下:

BEGIN
SELECT * from cc_customer 
inner join cc_customer_address on customer_id=customer_number

delete from cc_customer where customer_id=3
delete from cc_customer_address where customer_number=3
END

另请注意,customer_numbercustomer_id

的外键

1 个答案:

答案 0 :(得分:1)

您应该交换DELETE语句:

BEGIN
  SELECT * 
  from cc_customer 
  inner join cc_customer_address on customer_id=customer_number;

  delete from cc_customer_address where customer_number=3;  -- first child
  delete from cc_customer where customer_id=3;              -- then parent
END