oracle ORA-01027:尝试使用if elseif时,数据定义不允许绑定变量

bvpmtnay  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(183)

ORA-01027:bind variables not allowed for data definition(数据定义不允许绑定变量)

procedure create_dates_testing  (dummy_variable varchar2 default 
 to_char(sysdate,'YYYYMMDD')) is

begin
DECLARE
day_of_month varchar2(255) := extract(day from sysdate);
today varchar2(255) := to_char(sysdate, 'DAY');
start_date date;
next_start_date date;

BEGIN
IF today='SUNDAY' THEN
-- Select yesterday
start_date      := trunc(sysdate) - interval '1' day;
next_start_date := trunc(sysdate);

ELSE IF day_of_month=3 then
-- Select the whole of last month
start_date      := runc(sysdate, 'MM') - interval '1' month;
next_start_date := runc(sysdate, 'MM') - interval '1' month

END IF;
END;

execute immediate 'drop table new_customers';
execute immediate 'create table new_customers as 
select id, client_name, invoice_date 
from clients table
where transactiondate >= :start_date
and transactiondate <  :next_start_date;';

end;

字符串
我该如何解决这个错误?我哪里出错了?我需要把这个过程放在一个pl/sql包中。

mbjcgjjk

mbjcgjjk1#

正如错误所说,你不能在这里使用绑定变量,所以你必须连接:

create or replace procedure create_dates_testing 
    ( dummy_variable varchar2 default to_char(sysdate,'YYYYMMDD') )
as
    day_of_month varchar2(255) := extract(day from sysdate);
    today varchar2(255) := to_char(sysdate +1, 'fmDAY', 'nls_date_language = English');
    start_date date;
    next_start_date date;
begin
    if today = 'SUNDAY' then
        -- select yesterday
        start_date      := trunc(sysdate) - interval '1' day;
        next_start_date := trunc(sysdate);

    elsif day_of_month = 3 then
        -- select the whole of last month
        start_date      := trunc(sysdate, 'MM') - interval '1' month;
        next_start_date := trunc(sysdate, 'MM') - interval '1' month;
    else
        return;
    end if;

    execute immediate 'drop table new_customers';

    execute immediate 'create table new_customers as 
    select id, client_name, invoice_date 
    from clients table
    where transactiondate >= date '''  || to_char(start_date,'YYYY-MM-DD') ||
    ''' and transactiondate < date ''' || to_char(next_start_date,'YYYY-MM-DD') ||'''';

end create_dates_testing;

字符串
可能会有更多的代码来处理既不是星期天也不是每月的第三天,或者new_customers表不存在的情况。
编辑:添加了else条件,以在两个日期条件都不满足时结束处理。

相关问题