如何使用存储过程结果进行连接?

时间:2013-01-12 14:50:52

标签: sql stored-procedures join informix

我想在查询和存储过程的结果之间建立连接(出于某种原因)。

我的查询是:

SELECT COUNT(a.values_key) AS docscount,b.main_code,b.year  
FROM SP_name_should_be_here a  
INNER JOIN  dmr23req  b 
  ON a.values_key = b.req_year || ',' || b.req_ser  
  AND a.newstateserial = 17  
  AND a.taskcode = 35  
GROUP BY b.main_code,b.year 

我的存储过程dmr_get_full_in需要两个参数


1 个答案:

答案 0 :(得分:1)

答案是你应该在FROM子句中使用'iterator function'

FROM TABLE(FUNCTION stored_procedure(arg1, arg2)) AS alias(col1, col2, ....)

'alias'并不总是必要的,但要仔细查看直接执行过程的输出中的列名:

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');
…