oracle SQL查询使用多个参数

jc3wubiy  于 2023-11-17  发布在  Oracle
关注(0)|答案(3)|浏览(174)

我想知道如何重写这个查询,以便只传递一次日期参数-

Select a.recorded_hours,
and (((a.employee_hours+b.Quantity)/( ('&To_date' - '&from_date')*24))
From table1 a, table2 b
Where a.code=b.code
and a.site = b.site
and a.reg_date between '&from_date' and '&to_date'

字符串

vd2z7a6w

vd2z7a6w1#

您可以在CTE(WITH ... as)中从DUAL接收参数到单行中,并将该表连接到您的查询:

with Parms as (
   select  '&from_date' as FROM_DATE, '&To_date' as TO_DATE from DUAL
   )
Select a.recorded_hours,
       ((  (a.employee_hours+b.Quantity) / ((P.TO_DATE - P.FROM_DATE)*24)))
From table1 a
     
     inner join
     table2 b
     on a.code=b.code
     and a.site = b.site

     inner join
     Parms P
     on a.reg_date between P.FROM_DATE and P.TO_DATE

字符串
我还将表a和b的连接语法更改为更现代的INNER JOIN语法。

3npbholx

3npbholx2#

只需将&字符重复两次:

--  This will ask you to enter parameter twice:
Select SubStr('ABCDEFGHI', &Pos, 1) || SubStr('123456789', &Pos, 1) "CELL" From Dual;

--  ... and this just once
Select SubStr('ABCDEFGHI', &&Pos, 1) || SubStr('123456789', &&Pos, 1) "CELL" From Dual;

字符串
结果是一样的:

/*
CELL
----
C3      */

yptwkmov

yptwkmov3#

使用两个号代替:

SELECT a.recorded_hours,
       ((a.employee_hours + b.Quantity) / (('&&To_date' - '&&from_date') * 24))
  FROM table1 a, table2 b
 WHERE     a.code = b.code
       AND a.site = b.site
       AND a.reg_date BETWEEN '&&from_date' AND '&&to_date';

字符串
怎么做?像这样:

SQL> select '&&par_name' from dual where '&&par_name' = 'ABC';
Enter value for par_name: ABC

'AB
---
ABC


正如你所看到的,只有一个替换变量的提示。如果你重新执行相同的(最后一个)语句会怎么样?查询工作,但它使用以前输入的值:

SQL> /

'AB
---
ABC


如果你想使用一个新的值,undefine变量:

SQL> undefine par_name
SQL> /
Enter value for par_name: XYZ

no rows selected

SQL>

相关问题