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



>我得到'ORA-01027:数据定义不允许绑定变量'

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 包中。

正如错误所说,您不能在此处使用绑定变量,因此您必须连接:

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条件,以便在不满足任何日期条件的情况下结束处理。

最新更新