Я хотел бы создать курсор или таблицу из оператора select, используя в качестве параметра строки другого курсора. В моем примере я получил временную таблицу с одним столбцом и множеством строк. Эти строки должны быть записью следующего выбора и показывать (или сохранять) результаты.
REPLACE PROCEDURE test (IN name_db VARCHAR(25), IN name_table VARCHAR(25), IN n INT)
DYNAMIC RESULT SETS 1
BEGIN
DECLARE a VARCHAR(32000);
DECLARE b VARCHAR(32000);
DECLARE qry VARCHAR(32000);
DECLARE prindx VARCHAR(32000);
DECLARE i INT;
DECLARE p VARCHAR(32000);
DECLARE idx VARCHAR(32000);
DECLARE diff VARCHAR(32000);
DECLARE csr1 CURSOR WITH RETURN FOR stmt1;
SET a = 'SELECT * FROM (SELECT ColumnName AS col1 FROM DBC.Columns WHERE DatabaseName=''' ||name_db || ''' AND TableName=''' || name_table || ''') A1 ';
SET b = 'WHERE ';
SET i=1;
WHILE i <=N DO
BEGIN
CASE i
WHEN 1 THEN
SET qry = a;
SET prindx = 'SELECT (col1';
WHEN 2 THEN
SET a = a || 'CROSS JOIN (SELECT ColumnName AS col' || CAST(i AS VARCHAR(2)) || ' FROM DBC.Columns WHERE DatabaseName='''||name_db || ''' AND TableName=''' || name_table || ''') A' || CAST(i AS VARCHAR(2)) || ' ';
SET b = b || 'A' || CAST((i-1) AS VARCHAR(2)) || '.col' || CAST((i-1) AS VARCHAR(2)) || ' < A' || CAST(i AS VARCHAR(2)) || '.col' || CAST(i AS VARCHAR(2));
SET qry a || b;
SET prindx = prindx || ' || '','' || col' || CAST(i AS VARCHAR(2));
ELSE
SET a = a || 'CROSS JOIN (SELECT ColumnName AS col' || CAST(i AS VARCHAR(2)) || ' FROM DBC.Columns WHERE DatabaseName='''||name_db || ''' AND TableName=''' || name_table || ''') A' || CAST(i AS VARCHAR(2)) || ' ';
SET b = b || ' AND A' || CAST((i-1) AS VARCHAR(2)) || '.col' || CAST((i-1) AS VARCHAR(2)) || ' < A' || CAST(i AS VARCHAR(2)) || '.col' || CAST(i AS VARCHAR(2));
SET qry a || b;
SET prindx = prindx || ' || '','' || col' || CAST(i AS VARCHAR(2));
END CASE;
SET i = i + 1;
END;
END WHILE;
BEGIN
DECLARE exit HANDLER FOR SQLEXCEPTION
BEGIN
IF SQLCODE <> 3807 THEN RESIGNAL; END IF;
END;
SET p = name_db || '.tbl1 ';
DROP TABLE p;
END;
SET p = 'CREATE TABLE ' || name_db || '.tbl1 AS (' || qry || ') WITH DATA';
EXECUTE IMMEDIATE p;
SET prindx = prindx || ') AS PRYINDX FROM ' || name_db || '.tbl1 ';
PREPARE stmt1 FROM prindx;
OPEN csr1;
--------------------------------------------------------------------------------
FETCH csr1 INTO idx;
SET diff = 'SELECT (MAX(A.TOTAL) - MIN(A.TOTAL)) AS DIFF
FROM (SELECT HASHAMP(HASHBUCKET(HASHROW(' || idx || '))) CODHASH, CAST(COUNT(*) AS DECIMAL(18,0) TOTAL
FROM ' || name_db || '.' || name_table || ' GROUP BY 1) A';
Я хотел бы использовать значения "csr1" внутри оператора Select выше и принять все возможные результаты,Сейчас я могу взять последний.