PL / SQL在Adhoc SELECT中使用变量

时间:2016-01-18 20:11:44

标签: sql variables select plsql

我(尝试)编写一个查询,该查询将在查询中的多个点使用相同的日期。我想使用变量来更轻松地运行不同日期的查询。我该怎么办?当我尝试按原样执行查询时,我收到了各种错误。

ORA-06550:第25行第5栏: PLS-00103:遇到符号" END"期待以下之一:

DECLARE 
    CalendarDate Date := TO_DATE('1/20/2015','MM/DD/YYYY');
BEGIN

SELECT 
    TIME_OF_DAY, COUNT(TIME_OF_DAY) ISSUE_ACTIVITY_COUNT
FROM
    (SELECT
        case 
            when to_char(CREATED_DT,'mi') between '00' and '14' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '15' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '15' and '29' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '30' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '30' and '44' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '45' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '45' and '59' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '60' minute,'HH24:MI')
        end as TIME_OF_DAY
    FROM
        CE.ISSUE_ITEM i
    WHERE
        (TRUNC(i.CREATED_DT) = CalendarDate))
    GROUP BY
        TIME_OF_DAY
    ORDER BY
        TIME_OF_DAY;

EXECUTE IMMEDIATE     
END;

2 个答案:

答案 0 :(得分:0)

要在查询中多次使用日期,请使用'iron-node' is not recognized as an internal or external command, operable program or batch file.子句:

WITH

我希望我猜测你需要的是正确的。

答案 1 :(得分:0)

您所犯的错误

...与$(document).ready(function() { $("#btnSubmit").click(function() { $('.stashText').prepend("<br><br>Line_No_1"); }); $("#btn2Submit").click(function() { $('.stashText').prepend("<br><br>Line_No_2"); }); }); 声明相关,这在任何情况下都没有任何意义。我不知道你想做什么,所以我不会解决你的问题,但会向你展示其他选择:

重用绑定变量

您不需要使用PL / SQL。使用普通的命名绑定变量:

EXECUTE IMMEDIATE

然后,您可以在整个SQL语句中重用该绑定变量。

使用公用表表达式(在Oracle中说的子查询因子)

原则上,您可以使用以下类型的查询来引入单个语句范围的变量:

SELECT 
    TIME_OF_DAY, COUNT(TIME_OF_DAY) ISSUE_ACTIVITY_COUNT
FROM
    (SELECT
        case 
            when to_char(CREATED_DT,'mi') between '00' and '14' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '15' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '15' and '29' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '30' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '30' and '44' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '45' minute,'HH24:MI')
            when to_char(CREATED_DT,'mi') between '45' and '59' then TO_CHAR(trunc(CREATED_DT,'HH24') + interVal '60' minute,'HH24:MI')
        end as TIME_OF_DAY
    FROM
        CE.ISSUE_ITEM i
    WHERE
        (TRUNC(i.CREATED_DT) = :CalendarDate)) -- Bind variable here
    GROUP BY
        TIME_OF_DAY
    ORDER BY
        TIME_OF_DAY;

但我个人认为这有点模糊不清。为了访问变量,您必须交叉连接它们,这不是非常易读。