[oracle]与 '情况' 的 sql 语句的奇怪行为

标签: sql Oracle
发布时间: 2017/4/15 18:47:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图创建 SQL 报表。这是 select 语句︰

  select
  -- ticket id
  '<tr align=left valign=top><td width=95>'||maspid||'</td>'||

  -- priority
  case when instr(masfld017, '<|lang' ) = 0 then
        '<td width=90>'||masfld017
  else
        '<td width=90>'||substr( masfld017, 1 , instrb(masfld017, '<|lang') - 1)
  end||'</td>'||

  -- customer
  '<td width=150>'||masfld007||'</td>'||

  -- status
    '<td width=100>'||decode(masfld104, '0', 'Collected',
          '1', 'Postponed',
          '2', 'Accepted',
          '3', 'in Progress',
          '5', 'Work Around',
          '7', 'Solved',
          '8', 'Closed')||'</td>'||

  -- subject
  '<td width=400>'||masfld001||'</td>'||

  -- full name
  '<td width=100>' || replace(replace(masfld037, '<||>'), '</||>')||'</td>'||

  -- creation date
  '<td width=150>'||to_char(to_date(masfld022, 'YYYYMMDDHH24MI'), 'DD.MM.YYYY HH24:MM')||'</td>'||

  -- target date
  '<td width=150>'||to_char(to_date(masfld023, 'YYYYMMDDHH24MI'), 'DD.MM.YYYY HH24:MM')||'</td>'||

  -- ticket type
  '<td width=180>'||decode(masfld040, '9', 'HUS',
        '8', 'Project',
        '7', 'Enhancement',
        '6', 'Complaint',
        '5', 'Change Request',
        '4', 'Bug (Do not use)',
        '3', 'Support',
        '2', 'Service Request',
        '1', 'Incident (Problem)',
        null, 'N/A')||'</td></tr>'
from
        k2h.tmaster01 tm inner join k2h.tprofile tp on replace(replace(tm.masfld035, '<||>'), '</||>') = tp.propid
where
        masfld024 is null and
        masfld082 = '&REGION' 

order by case masfld017
        when 'Emergency'         then 1 
        when 'Critical'          then 2
        when 'High'              then 3
        when 'Major Problem'     then 4
        when 'Major defect'      then 5
        when 'Semi-major defect' then 6
        when 'Medium'            then 7
        when 'Minor Problem'     then 8
        when 'Low'               then 9
        end;

如果直接用 sqldeveloper 运行,获取结果列表。如果通过 crontab 启动时报告,它将失败并此错误消息︰

SP2-0734: unknown command beginning "case when ..." - rest of line ignored. SP2-0734: unknown command beginning "'

看起来像案例 command 不承认,但是,为什么?

解决方法 1:

它是可能的在 crontab,你正在运行的 SQL 直接在 shell 中如

sqlplus...<<ESQL
select..
...
order by case end;

? 如果是这样,请删除空行。

否则你会得到一些像︰

SQL> select
  2    -- ticket id
  3    '<tr align=left valign=top><td width=95>'||maspid||'</td>'||
  4
SQL>   -- priority
SQL>   case when instr(masfld017, '<|lang' ) = 0 then
SP2-0734: unknown command beginning "case when ..." - rest of line ignored.
赞助商