我无法在以下查询中使用if then语句:
define start_date = '&START_DATE'
define end_date = '&END_DATE'
define co_cd = '&COMPANY_CODE'
define myacct = '&4_DIGIT_ACCOUNT'
select ivc.pmt_dt, ivc.ivc_cd, gl.seq#, gl.gl_acct_cd, ivc.ve_cd, ve.ve_name, gl.amt
from ivc ivc
left outer join ve ve
on ivc.ve_cd = ve.ve_cd
left outer join ivc$gl_acct gl
on (ivc.ivc_cd = gl.ivc_cd and ivc.ve_cd = gl.ve_cd)
where ivc.post_dt >= '&start_date'
and ivc.post_dt <= '&end_date'
and substr(gl_acct_cd,1,4) = '&myacct'
and if '&co_cd' = 'BSD' then
substr(gl_acct_cd,6,2) in ('04','08','11','31','37')
elseif '&co_cd' = 'JEYE' then
substr(gl_acct_cd,6,2) in ('03','20','21','22','23','24')
elseif '&co_cd' = 'BSF' then
--something
elseif '&co_cd' = 'ALL' then
--something
else
substr(gl_acct_cd,6,2) in '&co_cd'
end if
order by gl.amt desc
我在If Then声明中做错了什么?我一直收到无效的关系运算符错误。我在线搜索了很多,但找不到答案。任何帮助表示赞赏。我知道这个东西很危险......做了很多sql查询,但它们通常不会太复杂。谢谢!
答案 0 :(得分:3)
如果SQL是面向集合的,那么就没有像IF这样的过程方法。
相反,构建查询如下:
and (
('&co_cd' = 'BSD' and substr(gl_acct_cd,6,2) in ('04','08','11','31','37')) or
('&co_cd' = 'JEYE' and substr(gl_acct_cd,6,2) in ('03','20','21','22','23','24')) or
('&co_cd' = 'BSF' and --something) or
('&co_cd' = 'ALL' and --something) or
('&co_cd' not in ('BSD','JEYE','BSF','ALL') and substr(gl_acct_cd,6,2) in '&co_cd'))
答案 1 :(得分:2)
如果那么其他方法在SQL语句中不起作用,你应该使用CASE WHEN
,但在你的情况下,case when
不是一个好的选择试试这个
and (
('&co_cd' = 'BSD' and substr(gl_acct_cd,6,2) in ('04','08','11','31','37'))
OR
('&co_cd' = 'JEYE' and substr(gl_acct_cd,6,2) in ('03','20','21','22','23','24'))
OR
('&co_cd' = 'BSF' and --something)
OR
('&co_cd' = 'ALL' and --something)
OR
('&co_cd' NOT IN ('BSD','JEYE','BSF', 'ALL') and substr(gl_acct_cd,6,2) in '&co_cd')
)