【问题标题】:How to filter your sql query using declared parameters in Oracle SQL如何使用 Oracle SQL 中声明的参数过滤您的 sql 查询
【发布时间】:2015-01-14 09:19:21
【问题描述】:

我有一个 Oracle SQL 脚本,想将输入参数传递给脚本以供执行。 我将如何实现这一目标?

假设我有日期参数 testdate。这就是我们在sql server中的做法

declare @testdate as date

select * from test where testdate=@testdate

基本上我在一个需要执行的块中有一组 sql 语句。

【问题讨论】:

标签: sql oracle plsql oracle11g


【解决方案1】:

您可以将它作为参数传递给 SQL 脚本并在 SQL*Plus 中调用它。

例如,

我有一个脚本 emp.sql,它看起来像 -

select ename from emp where empno=&1

现在,我将在 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>

同样,对于日期参数 -

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>

对于字符串类型,您需要有单引号。比如——

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多