为什么我的查询会出错? [英] why am getting error for this query?

查看:77
本文介绍了为什么我的查询会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

delete from Tbl_Sales where C_INVOICE='AP-SIV-VS-111' and c_year=2015





收到此错误





am getting error for this

Msg 547, Level 16, State 0, Procedure Trig_Sales_delete, Line 31
The DELETE statement conflicted with the REFERENCE constraint "FK_Tbl_Sales_Details_Tbl_Sales". The conflict occurred in database "Pulse_VIVO", table "dbo.Tbl_Sales_Details".
The statement has been terminated.





请告诉问题在哪里?



please tell where is the issue?

推荐答案

我可以从约束中看出: FK_Tbl_Sales_Details_Tbl_Sales 有两个表链接在一起:Tbl_Sales和Tbl_Sales_Details。



该链接称为外键。 Tbl_Sales_Details中有一个字段,它将引用Tbl_Sales中的id。此链接无法破解。 Tbl_Sales_Details中的任何id都必须在Tbl_Sales中。你无法删除记录,因为这样做会破坏链接。



这就是这个消息的含义

DELETE语句与REFERENCE约束FK_Tbl_Sales_Details_Tbl_Sales冲突

其余的只是告诉您问题发生的位置。它甚至会告诉你冲突表名称:

...表dbo.Tbl_Sales_Details



您需要删除详细信息才能删除销售。



类似于:

I can tell from the constraint: FK_Tbl_Sales_Details_Tbl_Sales that there are two tables linked together: Tbl_Sales and Tbl_Sales_Details.

The link is called a Foreign Key. There is a field in Tbl_Sales_Details that will refer to the id in Tbl_Sales. This link cannot be broken. Any id in Tbl_Sales_Detailsmust already be in Tbl_Sales. You cannot delete the record because doing so would break the link.

That is what this message means
The DELETE statement conflicted with the REFERENCE constraint "FK_Tbl_Sales_Details_Tbl_Sales"
The rest just tells you where the issue occurred. It even tells you the conflict table name:
... table "dbo.Tbl_Sales_Details"

You need to delete the details before you can delete the sale.

something like:
delete sd.* from Pulse_VIVO..Tbl_Sales_Details sd
inner join Tbl_Sales s on ds.sales_id = s.id
 where s.C_INVOICE='AP-SIV-VS-111' and s.c_year=2015


delete from Tbl_Sales where C_INVOICE='AP-SIV-VS-111' and c_year=2015





但要注意:这可能不是行的结束。例如,在Tbl_bill中可能存在到Tbl_Sales和Tbl_Sales_Details的链接。这一切都取决于你的数据结构。



祝你好运^ _ ^

Andy



but beware: this might not be the end of the line. There could be a link to Tbl_Sales and Tbl_Sales_Details in Tbl_bill for example. It all depends on your data structure.

Good luck ^_^
Andy


Tbl_Sales_Details包含引用表Tbl_Sales的数据,对于此引用,定义了外键...但不幸的是(?)没有选项ON DELETE CASCADE。



要么重新定义外键,要么首先从Tbl_Sales_Details删除详细信息,然后在Tbl_Sales中删除。注意:这两个删除应该在一个Transaction中完成。



我跳了它帮助。
"Tbl_Sales_Details" contains data which refer to table "Tbl_Sales" and for this reference a foreign key is defined...but unfortunatelly (?) without the Option "ON DELETE CASCADE".

Either you redefine the foreign key or you delete the Details first from "Tbl_Sales_Details" and than you can delete in "Tbl_Sales". Attention: this two delete should be done inside a Transaction.

I hop it helps.


有一个PrimaryKey和ForeignKey表之间的关系,每当您尝试从父表中删除之前,您应该从子记录中删除。然后只允许你删除父记录。
There is a PrimaryKey and ForeignKey relation between tables, whenever you try to delete from parent table before that you should delete from child record. Then only it's allowing you to delete parent record.


这篇关于为什么我的查询会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆