避免 SQL Server 中的数据库游标
我有一点困惑(至少对我来说),我希望这主要是因为我还不是 SQL 大师。基本上我有三个表:
表A、表B和表C。
表C有一个到表B的FK(外键),表B有一个到表A的FK。(每个都是多对一)
我需要删除一个表 A 中的条目,当然还有表 B 和表 C 中的所有相应条目。过去,我使用游标来执行此操作,选择表 B 中的所有条目,然后循环浏览每个条目以删除它们在表 C. 现在这可以工作了 - 并且一直工作得很好,但我怀疑/希望有更好的方法来实现这种效果而不使用游标。这就是我的问题 - 我怎样才能在不使用光标的情况下做到这一点,或者可以做到吗?
(如果我不清楚,请告诉我 - 我会尽力解决这个问题)。
I have a bit of a puzzle (at least for me) which I am hoping is mostly because I am not yet an SQL master of the universe. Basically I have three tables:
Table A, Table B, and Table C.
Table C has a FK (Foriegn Key) to Table B, which has FK to Table A. (Each of these is many to one)
I need to remove an entry from Table A and of course all of it's corresponding entries from Tables B and C. In the past I've used a cursor to do this, selecting all the entries in Table B and cycling through each one to delete all their corresponding entries in Table C. Now this works - and has been working fine, but I suspect/hope there is a better way to achieve this effect without the use of cursors. So that's my question - how can I do this without using a cursor, or can it be done?
(Please let me know if I haven't been clear - I'll try to fix up the question).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的
FOREIGN KEY
声明为ON DELETE CASCADE
Declare your
FOREIGN KEY
s asON DELETE CASCADE
您可以通过几种方式做到这一点...
在上述任何一种情况下,您只需从表 A 中删除记录,然后让级联和触发器处理其余的事情。
You could do this a couple ways...
In any of the above cases, you'd just delete the record from table A, and let the cascading and triggers take care of the rest.
已经给出的答案(级联删除和触发器)很好,但您可能在一个无法选择这些的环境中工作。如果是这样,下面是一个纯 SQL 解决方案。该示例仅涉及 DELETE 语法。在现实世界中,您可能会将其包装在事务中并将其实现为存储过程。
The answers already given (Cascading Deletes and Triggers) are great, but you might work in an environment where these are not an option. If so, below is a purely SQL solution. The example is solely concerned with the DELETE syntax. In the real world you'd probably wrap this within a transaction and implement it as a stored procedure.
当您为两个表创建外键关系时,您可以指定 ON DELETE CASCADE,当您删除 A 中的记录时,它会为您处理这个问题。
When you create the foreign key relationship for both tables you can specify ON DELETE CASCADE and it will take care of this for you when you delete a record in A.