我正在将一些过程从PostgreSQL迁移到新的DB2环境。我已经完成了大部分工作,但我找不到一种方法来为内部行集/记录DECLARE变量。
基本上,Postgres的程序是:
DECLARE
counts RECORD;
BEGIN
-- fill "counts" with one row of aggregated data
SELECT
COUNT(....) AS failed_inserts,
COUNT(....) AS failed_updates,
COUNT(....) AS failed_deletes,
INTO counts
FROM (...)
-- check "counts" with some conditionals
IF counts.failed_inserts > 0
(...)
END IF;
(...)
-- return info depending on the data
RETURN (...);
END
我找不到相当于在IBM手册或其他地方在线声明“计数”。我需要的行是静态的(3列聚合数据)。因此,如果可能的话,将该行声明为硬编码就足够了。
是否可以在DB2上的存储过程中DECLARE记录/数据集/“虚拟表”?
我们使用的是DB2 for Linux(V10.5)而不是DB2 for iSeries。
@mustaccio 的答案指向正确的解决方案:
在程序之外创建所需的rowtype:
CREATE TYPE empRow AS ROW (failed_inserts INTEGER, failed_updates INTEGER, failed_deletes INTEGER);
然后您可以在过程中声明新类型
DECLARE newRow empRow;
答案 0 :(得分:0)
我不确定我完全理解您的需求,但您可能正在寻找ROW
数据类型?像这样:
DECLARE
TYPE counts_row AS ROW (
failed_inserts INT,
failed_updates INT,
failed_deletes INT
);
counts counts_row;
BEGIN
-- fill "counts" with one row of aggregated data
SELECT
COUNT(....) AS failed_inserts,
COUNT(....) AS failed_updates,
COUNT(....) AS failed_deletes,
INTO counts
FROM (...);
...
PS。未经测试。
更多信息in the manual。
答案 1 :(得分:0)
代替创建一个或多或少特定于单个查询的永久**用户定义类型,您也可以使用FOR
statement来实现相同的目标:
FOR counts AS c1 CURSOR FOR SELECT COUNT(.....) AS failed_inserts,
COUNT(....) AS failed_updates,
COUNT(....) AS failed_deletes,
FROM (...)
DO
IF counts.failed_inserts > 0 THEN
(....)
END IF;
END FOR;
**永久意味着在系统目录中定义的东西。