在sqlserver2005中通过单个查询一次从多个表中删除记录 [英] deleting records from multiple tables at a time with a single query in sqlserver2005
问题描述
我想用一个查询从子表和父表中删除记录。请找到下面给出的查询。
I wanna delete records from child tables as well as parent table with in a single query. please find the query given below. here response header is the primary table and responseid is the primary key.
DELETE FROM responseheader
FROM responseheader
INNER JOIN responsepromotion ON responseheader.responseid = responsepromotion.ResponseID
INNER JOIN responseext ON responsepromotion.ResponseID=responseext.ResponseID
WHERE responseheader.responseid In ('67D8B9E8-BAD2-42E6-BAEA-000025D56253')
但其抛出错误。谁能帮助我找出正确的查询
but its throwing error . can any one help me to find out the correct query
推荐答案
除非您使用某种级联删除,否则一条delete语句将
Unless you use some kind of cascading delete, a single delete statement will delete rows from a single table.
在您的示例中,如果语法正确,您将从 responseheader $ c $中删除行。仅c>,其余表仅用于确定要从
responseheader
删除哪些行。
In your example, if the syntax is correct, you will be deleting rows from responseheader
only, the rest of the tables are only used to determine which rows to delete from responseheader
.
到直言不讳,您确实不想使用级联删除,因此您应该执行多个删除语句,每个表中一个。
To be blunt, you really don't want to use a cascading delete, so you should execute multiple delete statements, one in each table.
这篇关于在sqlserver2005中通过单个查询一次从多个表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!