0

我正在尝试获取基本语句的结果。SQL 语句可以正常工作。但是,为了打印结果,我想使用 refcursor。我收到以下错误:

ORA-00900:无效的 SQL 语句 ORA-01008:并非所有变量都绑定 ORA-00900:无效的 SQL 语句

VARIABLE reader refcursor;
DECLARE
  line varchar2(32767);
BEGIN
  line := 'SELECT role_id,';
  FOR n IN (SELECT name
              FROM (SELECT competence.skill_role.role_id,
                           competence.skill_label.name,
                           competence.skill_role.target_value
                      FROM competence.skill_role,
                           competence.skill_label
                     WHERE competence.skill_label.skill_id =
                           competence.skill_role.skill_id
                       AND competence.skill_label.language_id = 1)
                       matrix_result ) LOOP
    line := line || '(decode(name,''' || n.name ||
            ''',target_value)) "' || n.name || '",';
  END LOOP;
  line := RTRIM(line, ',') ||
    ' FROM (SELECT competence.skill_role.role_id,
                   competence.skill_label.name,
                   competence.skill_role.target_value
              FROM competence.skill_role, competence.skill_label
             WHERE competence.skill_label.skill_id =
                   competence.skill_role.skill_id
               AND competence.skill_label.language_id = 1) matrix_result';
--dbms_output.put_line(line);
--execute immediate line;
 OPEN :reader FOR line;
END;
/
PRINT :reader;

表数据

CREATE TABLE competence.skill_role
  (skill_id  NUMBER,
   role_id NUMBER,
   target_value NUMBER)
/
INSERT ALL 
INTO competence.skill_role VALUES (3432030, 1421866, 2)
INTO competence.skill_role VALUES (3434962, 1421866, 2)
INTO competence.skill_role VALUES (3488025, 3488804, 4)


SELECT * FROM competence.skill_role

SKILL_ID ROLE_ID     target_value
---------- -------   -----------
3432030 1421866       2
3434962 1421866       2
3488025 3488804       4


CREATE TABLE competence.skill_label
  (skill_id  NUMBER,
   name vchar2 (30))
/
INSERT ALL 
INTO competence.skill_label VALUES (3432030, 'Alueen projektipätevyys')
INTO competence.skill_label VALUES (3434962, 'Moottorin koekäyttö')
INTO competence.skill_label VALUES (3488025, 'Etähallintajärjestelmät')


SELECT * FROM arc_competence.skill_label

SKILL_ID NAME
---------- -------
3432030, Alueen projektipätevyys
3434962, Moottorin koekäyttö
3488025, Etähallintajärjestelmät

我想从第一个查询中得到以下结果。从您的回答(如果我理解正确的话)看来,我需要手动运行结果查询才能得到答案。我想在不运行结果查询的情况下获得结果 :-) 我目前无法访问客户端计算机,但我现在要去那里。

ROLE_ID     Alueen projektipätevyys  Moottorin koekäyttö    Etähallintajärjestelmät          
1421866          2                      2
3488804                                                            4       
4

1 回答 1

1

如果我更正您的代码以便编译

  • 您的INSERT ALL陈述缺少一个SELECT
  • 中的nameskill_label被定义为 avchar2(30)而不是 avarchar2(30)
  • 您的匿名块引用了language_id您的 DDL 不包含的列

代码运行没有错误。如果唯一的问题是您想将前两行合并为一行,则只需将 a 添加MAX到所有列中,而不是ROLE_ID将 a 添加GROUP BY role_id到查询中。

SQL> ed
Wrote file afiedt.buf

  1  CREATE TABLE skill_role
  2    (skill_id  NUMBER,
  3     role_id NUMBER,
  4*    target_value NUMBER)
SQL> /

Table created.

SQL> ed
Wrote file afiedt.buf

  1  INSERT ALL
  2  INTO skill_role VALUES (3432030, 1421866, 2)
  3  INTO skill_role VALUES (3434962, 1421866, 2)
  4  INTO skill_role VALUES (3488025, 3488804, 4)
  5* select * from dual
SQL> /

3 rows created.


SQL> ed
Wrote file afiedt.buf

  1  CREATE TABLE skill_label
  2    (skill_id  NUMBER,
  3*    name varchar2 (30))
SQL> /

Table created.

SQL> ed
Wrote file afiedt.buf

  1  INSERT ALL
  2  INTO skill_label VALUES (3432030, 'Alueen projektipΣtevyys')
  3  INTO skill_label VALUES (3434962, 'Moottorin koekΣytt÷')
  4  INTO skill_label VALUES (3488025, 'EtΣhallintajΣrjestelmΣt')
  5* select * from dual
SQL> /

3 rows created.

SQL> commit;

Commit complete.

SQL> variable reader refcursor;

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2    line varchar2(32767);
  3  BEGIN
  4    line := 'SELECT role_id,';
  5    FOR n IN (SELECT name
  6                FROM (SELECT skill_role.role_id,
  7                             skill_label.name,
  8                             skill_role.target_value
  9                        FROM skill_role,
 10                             skill_label
 11                       WHERE skill_label.skill_id =
 12                             skill_role.skill_id
 13                         )
 14                         matrix_result ) LOOP
 15      line := line || 'max(decode(name,''' || n.name ||
 16              ''',target_value)) "' || n.name || '",';
 17    END LOOP;
 18    line := RTRIM(line, ',') ||
 19      ' FROM (SELECT skill_role.role_id,
 20                     skill_label.name,
 21                     skill_role.target_value
 22                FROM skill_role, skill_label
 23               WHERE skill_label.skill_id =
 24                     skill_role.skill_id
 25                 ) matrix_result ' ||
 26       ' GROUP BY role_id' ;
 27    dbms_output.put_line(line);
 28    --execute immediate line;
 29    OPEN :reader FOR line;
 30* END;
 31  /

PL/SQL procedure successfully completed.

SQL> print reader

   ROLE_ID Alueen projektipΣtevyys Moottorin koekΣytt÷ EtΣhallintajΣrjestelmΣt
---------- ----------------------- ------------------- -----------------------
   1421866                       2                   2
   3488804                                                                   4
于 2012-04-24T15:54:27.053 回答