【发布时间】:2013-08-16 09:46:36
【问题描述】:
如何将insertdelete 语句中删除的行放入 DB2 SQL 存储过程中的新表中?
DB2 允许以下语法返回已删除的行:
select * from old table (
delete from my_table where foo > 1
)
出于某种原因,您不能只根据该语句返回的结果执行insert。 However, you can use common table expressions as a kludgy workaround:
with deleted as (
select * from old table (delete from my_table where foo > 1)
)
select * from new table (insert into archive_table select * from deleted)
这有一个我不想要的不必要的额外选择语句,但至少它有效。删除的行被插入到另一个表中。
但是,如何在存储过程中做到这一点?
存储过程不允许使用简单的 select 语句。我想把它放在set 声明中:
set my_var = (
with deleted as (
select * from old table (delete from my_table where foo > 1)
)
select * from new table (insert into archive_table select * from deleted)
);
但是,这会失败,因为在这样的语句中不允许使用公用表表达式。
有没有办法在存储过程中做到这一点?
(可以使用其他方法来解决该任务。但我想知道是否可以这样做。如果不可能,这似乎很愚蠢限制。)
更新:我正在使用 DB2 9.7 LUW。
【问题讨论】:
标签: sql stored-procedures db2