2

Oracle sql developer,在 where 子句中替换动态运算符而不是手动运算符

我正在编写带有一组运算符的 ARGUMENT 程序。

我只想在 where 子句中替换那个动态运算符。请指导我如何实现这一目标。

Select * from table where field (ARGUMENT) 100.

注意:在上面的查询中 ARGUMENT 有'+,<,>,<=,>='

4

1 回答 1

0

SQL中不能有动态运算符,它必须是静态的。您需要(ab)在PL/SQL中使用EXECUTE IMMEDIATE来执行动态 SQL

例如,在 SCOTT 模式中使用标准 EMP 表:

SQL> set serveroutput on
SQL> declare
  2  v_empno NUMBER;
  3  v_operator VARCHAR2(2);
  4  v_dyn_sql VARCHAR2(200);
  5  BEGIN
  6  v_operator := '=';
  7  v_dyn_sql := 'SELECT empno FROM emp WHERE ename '||v_operator||' ''SCOTT''';
  8  EXECUTE IMMEDIATE v_dyn_sql INTO v_empno;
  9  DBMS_OUTPUT.PUT_LINE('employee number is '||V_EMPNO);
 10  END;
 11  /
employee number is 7788

PL/SQL procedure successfully completed.

或者,在SQL*Plus中使用绑定变量

SQL> variable v_operator varchar2(2);
SQL> exec :v_operator := '=';

PL/SQL procedure successfully completed.

SQL> print v_operator;

V_OPERATOR
----------------------
=

SQL> set serveroutput on
SQL> declare
  2  v_empno NUMBER;
  3  v_dyn_sql VARCHAR2(200);
  4  v_operator VARCHAR2(2);
  5  BEGIN
  6  v_operator := '=';
  7  v_dyn_sql := 'SELECT empno FROM emp WHERE ename '||:v_operator||' ''SCOTT''';
  8  EXECUTE IMMEDIATE v_dyn_sql INTO v_empno;
  9  DBMS_OUTPUT.PUT_LINE('employee number is '||V_EMPNO);
 10  END;
 11  /
employee number is 7788

PL/SQL procedure successfully completed.
于 2015-11-21T07:35:42.280 回答