从表中删除已知值列表的行的基本查询(无法通过查询找到值)可能是:
DELETE FROM table1 WHERE id IN(1,4,6,7)
在这种情况下,table1具有依赖关系,因此我想根据列表(1,4,6,7)将查询中需要首先删除的其他关系写入查询。所以,我可能有类似的东西:
DELETE FROM table2 WHERE id_table2 in(1,4,6,7)
DELETE FROM table1 WHERE id IN(1,4,6,7)
但是我想要一个声明固定列表的查询并为每个值执行查询? (伪代码)
[fixed list] =(1,4,6,7 ...)(允许任何#静态条目)
DELETE FROM table2 WHERE id_table2 in([fixed list])
DELETE FROM table1 WHERE id IN([fixed list])
答案 0 :(得分:0)
WITH FixedList (ID) AS
(SELECT 1 UNION ALL
SELECT 4 UNION ALL
SELECT 6 UNION ALL
SELECT 7)
DELETE FROM table1 where id in
(SELECT ID FROM FixedList)
DELETE FROM table2 where id in
(select ID FROM FixedList)
-- OPTION 2
CREATE TABLE #FixedList
(ID As integer)
INSERT INTO #Fixedlist
VALUES (1),
(4),
(6),
(7)
DELETE FROM table1 where id in
(SELECT ID FROM #FixedList)
DELETE FROM table2 where id in
(select ID FROM #FixedList)
--