2
CREATE TABLE unpivot_test 
(
    unique_id NUMBER(9) PRIMARY key,
    cusip VARCHAR2(30 CHAR),
    sedol VARCHAR2(30 CHAR),
    isin VARCHAR2(30 CHAR),
    ticker VARCHAR2(30 CHAR),
    currency VARCHAR2(3 CHAR)
);
INSERT INTO  unpivot_test VALUES (1, '1234', NULL, NULL, NULL, 'USD');

当我使用 unpivot 函数时,它按预期工作。

SELECT Identifier, scheme,  unique_id, currency
FROM unpivot_test
UNPIVOT (Identifier FOR scheme IN (
    cusip AS '1', sedol AS '2', isin AS '3', ticker AS '4'));

现在用用户定义类型替换表。

CREATE OR REPLACE TYPE obj_unpivot_test AS OBJECT (
    unique_id NUMBER(9) ,
    cusip VARCHAR2(30 CHAR),
    sedol VARCHAR2(30 CHAR),
    isin VARCHAR2(30 CHAR),
    ticker VARCHAR2(30 CHAR),
    currency VARCHAR2(3 CHAR)
);
CREATE OR REPLACE TYPE obj_unpivot_test_array AS TABLE OF obj_unpivot_test;

以下查询将失败,并出现未找到数据错误。

DECLARE
   l_obj obj_unpivot_test_array := obj_unpivot_test_array(obj_unpivot_test(1, '1234',     NULL, NULL, NULL, 'USD'));
   l_IDENTIFIER VARCHAR2(30 CHAR);
   l_SCHEME NUMBER(1);
   l_UNIQUE_ID number(9);
BEGIN
   SELECT identifier, scheme, UNIQUE_ID
   INTO l_IDENTIFIER, l_SCHEME, l_UNIQUE_ID
   FROM
       TABLE(l_obj) o
   UNPIVOT  (IDENTIFIER FOR SCHEME IN (cusip AS 1, sedol AS 2, isin AS 3, ticker AS 4));
   Dbms_Output.put_line('l_IDENTIFIER:'||l_IDENTIFIER);
   Dbms_Output.put_line('l_SCHEME:'||l_SCHEME);
END;

我错过了什么?

4

1 回答 1

1
SET serveroutput ON
DECLARE
     l_IDENTIFIER VARCHAR2(30 CHAR);
     l_SCHEME     NUMBER(1);
     l_UNIQUE_ID  NUMBER(9);
BEGIN
     SELECT identifier,
          scheme,
          UNIQUE_ID
     INTO l_IDENTIFIER,
          l_SCHEME,
          l_UNIQUE_ID
     FROM
          (SELECT *
          FROM TABLE(obj_unpivot_test_array(obj_unpivot_test(1, '1234',NULL, NULL, NULL, 'USD')))
          ) UNPIVOT (IDENTIFIER FOR scheme IN ( cusip AS '1', sedol AS '2', isin AS '3', ticker AS '4'));
     Dbms_Output.put_line('l_IDENTIFIER:'||l_IDENTIFIER);
     Dbms_Output.put_line('l_SCHEME:'||l_SCHEME);
END;
于 2013-07-31T10:42:07.260 回答