0

这是什么,我正在尝试

  1  declare
  2  stmt VARCHAR2(200):='&query';
  3  emprec emp%ROWTYPE;
  4  BEGIN
  5    EXECUTE IMMEDIATE stmt USING out emprec,in &id;
  6    DBMS_OUTPUT.PUT_LINE('Emp: '||emprec.ename);
  7* END;
SQL> /
Enter value for query: select * into :emprec from emp where empno=:no
old   2: stmt VARCHAR2(200):='&query';
new   2: stmt VARCHAR2(200):='select * into :emprec from emp where empno=:no';
Enter value for id: 7369
old   5:   EXECUTE IMMEDIATE stmt USING out emprec,in &id;
new   5:   EXECUTE IMMEDIATE stmt USING out emprec,in 7369;
  EXECUTE IMMEDIATE stmt USING out emprec,in 7369;
                                   *
ERROR at line 5:
ORA-06550: line 5, column 36:
PLS-00457: expressions have to be of SQL types
ORA-06550: line 5, column 3:
PL/SQL: Statement ignored

但是,当我将第5行更改为

EXECUTE IMMEDIATE stmt into emprec USING in &id;

并查询select * from emp where empno=:no

然后它工作正常并显示正确的输出。任何人请帮助我,如何解决这个问题

4

1 回答 1

0

要存储执行查询的结果集,您必须使用 INTO 子句指定兼容变量。

INTO 子句仅用于单行查询,该子句指定检索列值的变量或记录。对于查询检索到的每个值,INTO 子句中必须有一个对应的、类型兼容的变量或字段。

DECLARE
  stmt VARCHAR2(200):='select * from emp where empno=:no';
  emprec emp%ROWTYPE;
BEGIN
  EXECUTE IMMEDIATE stmt INTO emprec USING &id;
  DBMS_OUTPUT.PUT_LINE('Emp: '||emprec.ename);
END;
于 2013-03-31T12:41:54.307 回答