Oracle sql开发人员,在where子句中替换动态运算符,而不是手动运算符
我正在编写一个使用ARGUMENT的过程,该过程有一组运算符。
我只想在where子句中替换那个动态运算符。请指导我如何做到这一点。
Select * from table where field (ARGUMENT) 100.
注意:在上述查询中,ARUMENT具有'+,<,>,<=,>='
。
在纯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.