答案是您应该在 FROM 子句中使用“迭代器函数” :
FROM TABLE(FUNCTION stored_procedure(arg1, arg2)) AS alias(col1, col2, ....)
“别名”并不总是必需的,但请仔细查看直接执行过程的输出中的列名:
EXECUTE stored_procedure(arg1, arg2)
如果它包含唯一的列名,你会没事的;如果没有,你不会,你需要AS alias(col1, col2, ...)
符号。
这是您查询的类似物(或多或少)。
可以以不同方式编写但有效的琐碎函数:
CREATE FUNCTION SimpleElements(lo INTEGER, hi INTEGER) RETURNING INTEGER AS Atomic_Number;
DEFINE an INTEGER;
FOREACH SELECT Atomic_Number
INTO an
FROM Elements
WHERE Atomic_Number BETWEEN lo AND hi
ORDER BY Atomic_Number
RETURN an WITH RESUME;
END FOREACH;
END FUNCTION;
使用查询TABLE(FUNCTION stored_procedure(arg1, arg2))
:
MOD 函数用作生成一些要计数的组的简单方法,但它说明了作为练习要点的符号。
SELECT COUNT(*) AS group_count, MOD(b.atomic_number, 3) AS mod_num
FROM TABLE(FUNCTION SimpleElements(1, 10)) AS a(Atomic_Number)
JOIN Elements AS b
ON a.atomic_number = b.atomic_number
GROUP BY mod_num
ORDER BY mod_num;
输出:
3 0
4 1
3 2
元素表
CREATE TABLE elements
(
atomic_number INTEGER NOT NULL UNIQUE CONSTRAINT c1_elements
CHECK (atomic_number > 0 AND atomic_number < 120),
symbol CHAR(3) NOT NULL UNIQUE CONSTRAINT c2_elements,
name CHAR(20) NOT NULL UNIQUE CONSTRAINT c3_elements,
atomic_weight DECIMAL(8,4) NOT NULL,
stable CHAR(1) DEFAULT 'Y' NOT NULL
CHECK (stable IN ('Y', 'N'))
);
INSERT INTO elements VALUES( 1, 'H', 'Hydrogen', 1.0079, 'Y');
INSERT INTO elements VALUES( 2, 'He', 'Helium', 4.0026, 'Y');
INSERT INTO elements VALUES( 3, 'Li', 'Lithium', 6.9410, 'Y');
INSERT INTO elements VALUES( 4, 'Be', 'Beryllium', 9.0122, 'Y');
INSERT INTO elements VALUES( 5, 'B', 'Boron', 10.8110, 'Y');
INSERT INTO elements VALUES( 6, 'C', 'Carbon', 12.0110, 'Y');
INSERT INTO elements VALUES( 7, 'N', 'Nitrogen', 14.0070, 'Y');
INSERT INTO elements VALUES( 8, 'O', 'Oxygen', 15.9990, 'Y');
INSERT INTO elements VALUES( 9, 'F', 'Fluorine', 18.9980, 'Y');
INSERT INTO elements VALUES( 10, 'Ne', 'Neon', 20.1800, 'Y');
INSERT INTO elements VALUES( 11, 'Na', 'Sodium', 22.9900, 'Y');
INSERT INTO elements VALUES( 12, 'Mg', 'Magnesium', 24.3050, 'Y');
INSERT INTO elements VALUES( 13, 'Al', 'Aluminium', 26.9820, 'Y');
INSERT INTO elements VALUES( 14, 'Si', 'Silicon', 28.0860, 'Y');
INSERT INTO elements VALUES( 15, 'P', 'Phosphorus', 30.9740, 'Y');
INSERT INTO elements VALUES( 16, 'S', 'Sulphur', 32.0650, 'Y');
INSERT INTO elements VALUES( 17, 'Cl', 'Chlorine', 35.4530, 'Y');
INSERT INTO elements VALUES( 18, 'Ar', 'Argon', 39.9480, 'Y');
…