1. 程式人生 > >除錯經驗——使用替換變數實現動態查詢

除錯經驗——使用替換變數實現動態查詢

問題描述:

現實中,需求是千遍萬化的,所以程式碼的靈活性越高越好。

今天發現,SQLPLUS/Oracle中使用替換變數也可實現動態查詢。這種方式,程式碼不變,而由於使用者根據需求指定具體的變數值而具有更多的靈活性。

SQL> select &a from dual;
Enter value for a: sysdate
old   1: select &a from dual
new   1: select sysdate from dual

SYSDATE
---------
21-NOV-18
SQL> select &a from dual;
Enter value for a: to_char(sysdate,'yyyy-mm-dd')
old   1: select &a from dual
new   1: select to_char(sysdate,'yyyy-mm-dd') from dual

TO_CHAR(SY
----------
2018-11-21