How to set Oracle bind variables when using SQLPlus?

早过忘川 提交于 2020-04-16 02:32:12

问题


How do I set Oracle bind variables when using SQLPlus?

Example:

SELECT orders.order_no FROM orders WHERE orders.order_date BETWEEN :v1 AND :v2

How do I set the dates of :v1 and :v2?


回答1:


Notice the following:

  • VARIABLE is a SQLPlus command. You don't end it with a semicolon (;).

  • In the VARIABLE command, you do not precede the variable name with colon (:).

  • Bind variable can't be of data type "date" - they are some sort of character value.

  • For that reason, IN YOUR CODE you must use to_date() with the proper format model, or some other mechanism, to convert string to date. That is currently missing in your code. NEVER compare dates to strings!

Brief demo below.

SQL> variable v1 varchar2(20)

SQL> exec :v1 := '2015/12/22';
PL/SQL procedure successfully completed.

SQL> select 1 as result from dual where to_date(:v1, 'yyyy/mm/dd') < sysdate;

    RESULT
----------
         1



回答2:


In common you may use define and use variable with &

define x = 12 ;
select &x from dual;

Or varable

variable x refcursor;
begin
 open :x for select * from dual connect by level < 11;
end;
/
print x


来源:https://stackoverflow.com/questions/39993497/how-to-set-oracle-bind-variables-when-using-sqlplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!