Oracle FOR循环脚本未运行
我试图在Oracle中执行一个查询,其中输入是一个日期范围,然后根据两个日期之间的天数执行查询,输入日期也用作查询中的参数。也就是说,如果我的输入日期范围是5月28日至31日,那么查询将在5月28日、29日、30日、31日执行四次,然后返回日期范围内每个日期的结果集,然后将所有结果集合并为一个大结果集。我试着用FOR循环来做这个。到目前为止,我有:Oracle FOR循环脚本未运行,oracle,loops,for-loop,plsql,toad,Oracle,Loops,For Loop,Plsql,Toad,我试图在Oracle中执行一个查询,其中输入是一个日期范围,然后根据两个日期之间的天数执行查询,输入日期也用作查询中的参数。也就是说,如果我的输入日期范围是5月28日至31日,那么查询将在5月28日、29日、30日、31日执行四次,然后返回日期范围内每个日期的结果集,然后将所有结果集合并为一个大结果集。我试着用FOR循环来做这个。到目前为止,我有: DECLARE Bus_Date_1 date := '28-MAY-2013'; Bus_Date_2 date := '31-MAY-2013
DECLARE
Bus_Date_1 date := '28-MAY-2013';
Bus_Date_2 date := '31-MAY-2013';
Date_Iter date;
Date_Diff integer;
BEGIN
Date_Diff := (Bus_Date_2 - Bus_Date_1) + 1;
Date_Iter := Bus_Date_1;
FOR SL_Ctr in 1 .. Date_Diff
LOOP
select *
from (
select
cdav.bank_id,
ent.bank_desc Bank_Description,
cdav.sol_id,
sol.sol_desc SOL_Description,
cdav.gl_sub_head_code GLSH_Code,
decode(cdav.gl_sub_head_code,
'10301',1,
'10403',2, '60403',2, '10501',2, '60501',2, '10502',2,
'10503',2, '10504',2, '10505',2, '10507',2, '10509',2,
'60509',2, '10511',2, '10518',2, '60518',2, '10523',2,
'60523',2, '10551',2, '10552',2, '10553',2, '10554',2,
'10555',2, '10557',2, '10559',2, '10561',2, '10568',2,
'10573',2,
'12336',3, '62336',3, '10401',3, '60402',3, 4 ) GLSH_SET ,
gsh.gl_sub_head_desc GLSH_Name,
case
when (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 1
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 5)
then 'R'
when cast(substr(cdav.gl_sub_head_code,0,1) as int) = 0
or (cast(substr(cdav.gl_sub_head_code,0,1) as int) >= 6
and cast(substr(cdav.gl_sub_head_code,0,1) as int) <= 9)
then 'F'
end book_type,
gam.foracid account_number,
gam.acct_name,
cdav.tran_crncy_code Tran_Currency,
cdav.value_date,
cdav.tran_date Transaction_Date,
cdav.gl_date,
cdav.tran_particular,
rank() over(partition by gam.foracid order by eab.eod_date desc) eod_date_rank,
eab.eod_date,
case when
(select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013') -- current date in the LOOP should be here
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) is not null
then (select tran_date_bal from tbaadm.eab
where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
where cdav.acid = eab.acid and eab.eod_date < '28-MAY-2013') -- current date in the LOOP should be her
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id)
else 0
end beg_tran_date_bal,
(select tran_date_bal from tbaadm.eab eab
where eod_date = (select max(eab.eod_date) from tbaadm.eab eab
where cdav.acid = eab.acid and eab.eod_date <= '28-MAY-2013') -- current date in the LOOP should be her
and cdav.acid = eab.acid
and cdav.bank_id = eab.bank_id) end_tran_date_bal,
ott.ref_num OAP_Ref_No,
trim(cdav.tran_id) Transaction_ID,
--cdav.dth_init_sol_id Initiating_SOL_ID,
'PCC_Code',
cdav.tran_rmks Tran_Remarks,
case
when (cdav.part_tran_type = 'D')
then (cdav.tran_amt)
end dr_amount,
case
when (cdav.part_tran_type = 'C')
then (cdav.tran_amt)
end cr_amount
from tbaadm.ctd_dtd_acli_view cdav
left outer join tbaadm.gam
on cdav.bank_id = gam.bank_id and cdav.acid = gam.acid
left outer join tbaadm.gsh
on gam.bank_id = gsh.bank_id and gam.sol_id = gsh.sol_id
and gam.gl_sub_head_code = gsh.gl_sub_head_code
and gam.acct_crncy_code = gsh.crncy_code
left outer join tbaadm.sol
on cdav.bank_id = sol.bank_id and cdav.sol_id = sol.sol_id
left outer join tbaadm.eab
on gam.bank_id = eab.bank_id and gam.acid = eab.acid
left outer join tbaadm.cnc
on gam.bank_id = cnc.bank_id and gam.acct_crncy_code = cnc.crncy_code
left outer join crmuser.end ent
on cdav.bank_id = ent.bank_id
left outer join tbaadm.gct
on cdav.bank_id = gct.bank_id
left outer join tbaadm.ott
on cdav.tran_id = ott.tran_id and cdav.tran_date = ott.tran_date
and cdav.part_tran_srl_num = ott.part_tran_srl_num
and cdav.bank_id = ott.bank_id and cdav.acid = ott.acid
where
gam.acct_ownership = 'O'
and cdav.bank_id = 'CBC01'
and cdav.gl_date = '28-MAY-2013' -- current date in the LOOP should be her
and (gam.gl_sub_head_code in ('10301','10403','60403',
'10501','60501','10502','10503','10504','10505',
'10507','10509','60509','10511','10518','60518',
'10523','60523','10551','10552','10553','10554',
'10555','10557','10559','10561','10568','10573',
'12336','62336','10401','60402')
or gam.acct_classification_flg in ('I','E')
)
and trim(cdav.del_flg) in ('N', null)
and trim(gam.del_flg) in ('N', null)
and trim(gsh.del_flg) in ('N', null)
and trim(sol.del_flg) in ('N', null)
and trim(cnc.del_flg) in ('N', null)
and trim(gct.del_flg) in ('N', null)
)
where
eod_date_rank = 1
and GLSH_SET = 1
order by
bank_id,
sol_id,
tran_currency,
glsh_code,
book_type desc,
account_number,
transaction_date;
EXIT WHEN SL_Ctr > Date_Diff;
END LOOP;
END;
每当我运行它时,蟾蜍总是抛出一个错误,指向底部的结束循环线,说:
PLS-00103:在预期以下情况之一时遇到文件结尾符号:
begin case为goto if loop mod声明结束异常退出
null pragma raise return select update with with with with with
你写道:
FOR SL_Ctr in 1 .. Date_Diff
LOOP
select *
from (
big select
)
where
eod_date_rank = 1
and GLSH_SET = 1
order by
bank_id,
sol_id,
tran_currency,
glsh_code,
book_type desc,
account_number,
transaction_date;
EXIT WHEN SL_Ctr > Date_Diff;
END LOOP;
END;
在pl/sql中,您不能以这种方式使用sql,您需要将select转换为变量,或者使用类似于select的游标的东西将其用作隐式游标。更改循环内的select以将结果指定给变量
看
另一种写作方式可以是:
FOR SL_Ctr in 1 .. Date_Diff
LOOP
for i in (
select *
from (
big select
)
where
eod_date_rank = 1
and GLSH_SET = 1
order by
bank_id,
sol_id,
tran_currency,
glsh_code,
book_type desc,
account_number,
transaction_date
)
loop
dbms_output.put(line (i.bank_id||' '||i.bank_description);
end loop;
EXIT WHEN SL_Ctr > Date_Diff;
END LOOP;
END;
当SL_Ctr>Date_Diff时,注释退出;线FOR循环中不需要它。@AsfakulIslam我删除了它,我仍然得到相同的错误。为什么你会自愿执行此语句四次,而一次执行所有日期就可以了?@RobvanWijk,因为查询中有一部分会使事情复杂化。也就是说,获取beg_tran_date_bal和end_tran_date_bal。相信我,我问过的每个人都建议我使用到之间,我告诉他们这不适合这个特殊的查询。我明白你的意思。这段代码没有什么问题。将字符串指定给日期变量,EXIT语句是多余的,并且从EAB表中选择了多次。为什么不从头开始,先问一下您需要哪些表中的数据?这样你可能会得到更好的答案。你建议我怎么做?在将结果分配给变量后,如何显示变量中现在的结果?