[英]How to filter your sql query using declared parameters in Oracle SQL
I have a Oracle SQL script and would like to pass input parameter to the script for execution. 我有一个Oracle SQL脚本,想将输入参数传递给脚本以执行。 How would I achieve this?
我将如何实现?
Say I have date parameter testdate. 说我有日期参数testdate。 This is how we do it in sql server
这是我们在sql server中的做法
declare @testdate as date
select * from test where testdate=@testdate
Basically I have set of sql statements in a block which needs to be executed. 基本上,我在需要执行的块中有一组sql语句。
You can pass it as an argument to the SQL script and invoke it in SQL*Plus. 您可以将其作为参数传递给SQL脚本,然后在SQL * Plus中调用它。
For example, 例如,
I have a script emp.sql which looks like - 我有一个脚本emp.sql ,看起来像-
select ename from emp where empno=&1
Now, I will invoke it in SQL*Plus passing the empno as an argument. 现在,我将在SQL * Plus中调用empno作为参数来调用它。
SQL> @D:\emp.sql 7369
old 1: select ename from emp where empno=&1
new 1: select ename from emp where empno=7369
ENAME
----------
SMITH
SQL>
Similarly, for a date argument - 同样,对于日期参数-
SQL> @D:\emp.sql sysdate
old 1: select ename from emp where hiredate<=&1
new 1: select ename from emp where hiredate<=sysdate
ENAME
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
ENAME
----------
JAMES
FORD
MILLER
14 rows selected.
SQL>
For string type, you need to have single-quotation marks. 对于字符串类型,您需要使用单引号。 For example -
例如 -
SQL> @D:\emp.sql SCOTT
old 1: select empno from emp where ename='&1'
new 1: select empno from emp where ename='SCOTT'
EMPNO
----------
7788
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.