使用IN运算符执行Oracle表类型为的动态本机SQL时出现无效标识符错误

使用IN运算符执行Oracle表类型为的动态本机SQL时出现无效标识符错误,oracle,stored-procedures,plsql,dynamicquery,Oracle,Stored Procedures,Plsql,Dynamicquery,在该过程中,在我的控制台中打印的最终sql语句是正确的,当您在过程中以静态sql语句的形式运行它时,它不会出现任何错误。但在动态sql中,它失败了 我尝试使用DYNAMIC_SQL包执行,但结果是相同的错误。此外,我还尝试将其作为“table(testCodes)”的绑定变量。这也失败了。标记oracleplsqldynamicquerystored过程建议标记:oracle无法提交您的问题 create or replace TYPE "INPUTCODE" as object (pc var

在该过程中,在我的控制台中打印的最终sql语句是正确的,当您在过程中以静态sql语句的形式运行它时,它不会出现任何错误。但在动态sql中,它失败了

我尝试使用DYNAMIC_SQL包执行,但结果是相同的错误。此外,我还尝试将其作为“table(testCodes)”的绑定变量。这也失败了。标记oracleplsqldynamicquerystored过程建议标记:oracle无法提交您的问题

create or replace TYPE "INPUTCODE" as object (pc varchar2(100) )

create or replace TYPE "INPUTCODEARR" IS TABLE OF inputcode;

create or replace PROCEDURE "TEST_PROC" (
            testCodes IN inputcodeArr, 
            timeHorizon IN NUMBER, 
            p_recordset OUT SYS_REFCURSOR) 
AS var_sqlStmt VARCHAR2(4096); 
BEGIN 
   var_sqlStmt := 'select t.a,t.b, t.c'; 
   var_sqlStmt := var_sqlStmt || 'from test t';
   if testCodes is not null then 
      var_sqlStmt := var_sqlStmt || ', table(testCodes) tc'; 
      var_sqlStmt := var_sqlStmt || 'where tc.pc = t.name'; 
   end if; 
   dbms_output.put_line('Final SQL Statement::' || var_sqlStmt); 
   open p_recordset for var_sqlStmt; 
END TEST_PROC;

这应该是可行的。

就像kordirko建议的那样,您需要绑定变量;不是作为整个表运算符,而是作为
表(:x)
。下面是SQL*Plus中的完整示例:

SQL> create or replace type inputcode as object (pc varchar2(100));
  2  /

Type created.

SQL> create or replace type inputcodearr is table of inputcode;
  2  /

Type created.

SQL> create table test(a number, b number, c number, name varchar2(100));

Table created.

SQL> insert into test values (1,2,3,'A');

1 row created.

SQL> create or replace procedure test_proc(
  2     testcodes in inputcodearr,
  3     p_recordset in out sys_refcursor
  4  ) is
  5  begin
  6     open p_recordset for
  7     '
  8             select t.a, t.b, t.c
  9             from test t, table(:input) tc
 10             where tc.pc = t.name
 11     '
 12     using testcodes;
 13  end;
 14  /

Procedure created.

SQL> variable my_refcursor refcursor;
SQL> exec test_proc(inputcodeArr(inputcode('A'), inputcode('B')), :my_refcursor);

PL/SQL procedure successfully completed.

SQL> print my_refcursor;

         A          B          C
---------- ---------- ----------
         1          2          3

请使用子句和绑定变量回答我的问题:
var_sqlStmt:=var_sqlStmt||',table(:x)tc'
使用testcode打开var_sqlStmt的p_记录集SQL> create or replace type inputcode as object (pc varchar2(100));
  2  /

Type created.

SQL> create or replace type inputcodearr is table of inputcode;
  2  /

Type created.

SQL> create table test(a number, b number, c number, name varchar2(100));

Table created.

SQL> insert into test values (1,2,3,'A');

1 row created.

SQL> create or replace procedure test_proc(
  2     testcodes in inputcodearr,
  3     p_recordset in out sys_refcursor
  4  ) is
  5  begin
  6     open p_recordset for
  7     '
  8             select t.a, t.b, t.c
  9             from test t, table(:input) tc
 10             where tc.pc = t.name
 11     '
 12     using testcodes;
 13  end;
 14  /

Procedure created.

SQL> variable my_refcursor refcursor;
SQL> exec test_proc(inputcodeArr(inputcode('A'), inputcode('B')), :my_refcursor);

PL/SQL procedure successfully completed.

SQL> print my_refcursor;

         A          B          C
---------- ---------- ----------
         1          2          3