我希望删除select语句返回的结果,因为我这样做是因为我在表之间有关系,如果我从最顶层的表中删除,那么其他表中的子行也必须删除。
有人可以帮我纠正这个存储过程吗?
ALTER proc [dbo].[storedprocname]
(@Parameter uniqueidentifier = '00000000-0000-0000-0000-000000000000')
AS BEGIN
DELETE FROM TableOne
WHERE IDOne IN
(SELECT
IDOne,
DescOne, IndexOne,
IDTwo,
QuestionTwo, ControlTypeTwo, IndexTwo,
IDThree,
DescThree, IndexThree,
QuestionFour,
OptionFour
FROM
TableOne
INNER JOIN
TableTwo ON TableTwo.CatID = TableOne.IDOne
INNER JOIN
TableThree ON TableThree.Question = TableTwo.IDTwo
LEFT OUTER JOIN
TableFour ON TableFour.Question = TableThree.IDThree
WHERE
TableOne.IDOne = @Parameter)
END
答案 0 :(得分:1)
ALTER proc [dbo].[storedprocname]
(@Parameter uniqueidentifier = '00000000-0000-0000-0000-000000000000')
AS BEGIN
DELETE FROM TableOne
WHERE IDOne IN
(SELECT
IDOne
FROM
TableOne
INNER JOIN
TableTwo ON TableTwo.CatID = TableOne.IDOne
INNER JOIN
TableThree ON TableThree.Question = TableTwo.IDTwo
LEFT OUTER JOIN
TableFour ON TableFour.Question = TableThree.IDThree
WHERE
TableOne.IDOne = @Parameter)
END
答案 1 :(得分:1)
由于您要删除IDOne
在可能值列表中的所有行 - 然后您需要确保IN (...)
之后的子查询也返回一列可用于比较!毕竟,您无法将单个IDOne
值与您当前返回的整个列列表进行比较....
尝试这样的事情:
ALTER proc [dbo].[storedprocname]
(@Parameter uniqueidentifier = '00000000-0000-0000-0000-000000000000')
AS BEGIN
DELETE FROM TableOne
WHERE IDOne IN
(SELECT
IDOne
FROM
TableOne
INNER JOIN
TableTwo ON TableTwo.CatID = TableOne.IDOne
INNER JOIN
TableThree ON TableThree.Question = TableTwo.IDTwo
LEFT OUTER JOIN
TableFour ON TableFour.Question = TableThree.IDThree
WHERE
TableOne.IDOne = @Parameter)
END
我不知道(你的问题太模糊,不够清楚)是否真的需要子查询中的所有JOIN
....你可能需要根据你的要求进行调整。 / p>