OPEN emp_refcur FOR {plsql variable} 给出错误
OPEN emp_refcur FOR {plsql variable} giving error
我是plsql开发的新手。
我在 plsql 变量(有我的动态 SQL 查询)上打开游标时遇到问题。我相信我在使用 ORACLE 12。
详情如下:
emp_refcur SYS_REFCURSOR; -- Cursor declaration
component_sql:= 'select * from emp'; -- Base SQL
-- dynamic sql based on business logic
IF SOME_CONDITION THEN
filters:= ' where DEP IN (''ABC'',''DEF'')';
ELSE
filters:= ' where age>50';
component_sql:= component_sql||filters; -- appending filter to my base sql
OPEN emp_refcur FOR component_sql; -- opening the cursor
LOOP
FETCH emp_refcur INTO result;
EXIT WHEN emp_refcur%NOTFOUND;
dbms_output.put_line(result);
END LOOP;
CLOSE emp_refcur;
编译此程序在第 'OPEN emp_refcur FOR component_sql' 行显示错误,错误是
Error(90,9): PL/SQL: Statement ignored
Error(90,29): PLS-00382: expression is of wrong type
我不想使用
这样的绑定值
'OPEN emp_refcur FOR p_query_string USING p_deptno, p_sal;'
我尝试了多种方法
OPEN emp_refcur FOR ''||component_sql||''; -- This approach is not causing compilation error, but running the procedure resulting
ORA-00900: invalid SQL statement
和
OPEN emp_refcur FOR 'select * from emp'||filters; -- This is resulting the error
ORA-00900: invalid SQL statement
不确定我在这里遗漏了什么。请帮忙。
注意:如果有任何 SQL 查询语法错误,请忽略。因为我在附加过滤器后打印的 SQL 执行正常并在我单独 运行 时得到结果。
我试过你的代码,没有问题。 PLS-00382: 表达式类型错误 --> 这是语法错误。它与绑定变量无关。结果声明是什么?我没有在片段中看到。
PLS-00382: expression is of wrong type
Cause: An expression has the wrong datatype for the context in which it was found.
Action: Change the datatype of the expression. You might want to use datatype conversion functions.
Declare
emp_refcur SYS_REFCURSOR;
filters varchar2(100);
component_sql varchar2(100);
result varchar2(20);
Begin
component_sql:= 'select 1 from dual'; -- Base SQL
-- dynamic sql based on business logic
IF 1=1 THEN
filters:= ' UNION ALL SELECT 2 from dual';
ELSE
filters:= ' where age>50';
End if;
component_sql:= component_sql||filters; -- appending filter to my base sql
OPEN emp_refcur FOR component_sql ; -- opening the cursor
LOOP
FETCH emp_refcur INTO result;
EXIT WHEN emp_refcur%NOTFOUND;
dbms_output.put_line(result);
END LOOP;
CLOSE emp_refcur;
END;
我是plsql开发的新手。
我在 plsql 变量(有我的动态 SQL 查询)上打开游标时遇到问题。我相信我在使用 ORACLE 12。
详情如下:
emp_refcur SYS_REFCURSOR; -- Cursor declaration
component_sql:= 'select * from emp'; -- Base SQL
-- dynamic sql based on business logic
IF SOME_CONDITION THEN
filters:= ' where DEP IN (''ABC'',''DEF'')';
ELSE
filters:= ' where age>50';
component_sql:= component_sql||filters; -- appending filter to my base sql
OPEN emp_refcur FOR component_sql; -- opening the cursor
LOOP
FETCH emp_refcur INTO result;
EXIT WHEN emp_refcur%NOTFOUND;
dbms_output.put_line(result);
END LOOP;
CLOSE emp_refcur;
编译此程序在第 'OPEN emp_refcur FOR component_sql' 行显示错误,错误是
Error(90,9): PL/SQL: Statement ignored
Error(90,29): PLS-00382: expression is of wrong type
我不想使用
这样的绑定值'OPEN emp_refcur FOR p_query_string USING p_deptno, p_sal;'
我尝试了多种方法
OPEN emp_refcur FOR ''||component_sql||''; -- This approach is not causing compilation error, but running the procedure resulting
ORA-00900: invalid SQL statement
和
OPEN emp_refcur FOR 'select * from emp'||filters; -- This is resulting the error
ORA-00900: invalid SQL statement
不确定我在这里遗漏了什么。请帮忙。
注意:如果有任何 SQL 查询语法错误,请忽略。因为我在附加过滤器后打印的 SQL 执行正常并在我单独 运行 时得到结果。
我试过你的代码,没有问题。 PLS-00382: 表达式类型错误 --> 这是语法错误。它与绑定变量无关。结果声明是什么?我没有在片段中看到。
PLS-00382: expression is of wrong type
Cause: An expression has the wrong datatype for the context in which it was found.
Action: Change the datatype of the expression. You might want to use datatype conversion functions.
Declare
emp_refcur SYS_REFCURSOR;
filters varchar2(100);
component_sql varchar2(100);
result varchar2(20);
Begin
component_sql:= 'select 1 from dual'; -- Base SQL
-- dynamic sql based on business logic
IF 1=1 THEN
filters:= ' UNION ALL SELECT 2 from dual';
ELSE
filters:= ' where age>50';
End if;
component_sql:= component_sql||filters; -- appending filter to my base sql
OPEN emp_refcur FOR component_sql ; -- opening the cursor
LOOP
FETCH emp_refcur INTO result;
EXIT WHEN emp_refcur%NOTFOUND;
dbms_output.put_line(result);
END LOOP;
CLOSE emp_refcur;
END;