0

我从这段代码中收到错误“第 5 行缺少逗号”:

declare
  AGG_COLUMNS VARCHAR2(2000);


begin
   AGG_COLUMNS := '(''S'' || chr(38) ||''P (LT Rating)'' as SP,''CreditSights CSR'' as CSR,''FITCH (LT Rating)'' as  Fitch,''Moody||''''''||''s (LT Rating or CFR)'' as MOODY,''DBRS (LT Rating)'' as DBRS )';




execute immediate'

CREATE Global TEMPORARY TABLE temp_extRating

ON COMMIT PRESERVE ROWS
AS

(select * from 
(
  select EXTRAT.Rating_ID  ,EXTRAT.AGENCY_NM  ,EXTRAT.EXT_RATING  ,EXTRAT.RBC_EQ_RATING  ,EXTRAT.EFFECTIVE_DATE 
    from RBC_P_EXT_RATING EXTRAT 
    join RBC_P_RATING Rat on 
  Rat.ID=EXTRAT.Rating_ID
  )

pivot (

  MIN(EXT_RATING) ER, 

  MIN(RBC_EQ_RATING) RER, 

  MIN(EFFECTIVE_DATE) ED

  FOR AGENCY_NM IN ('''||AGG_COLUMNS||''')

) 

order by Rating_ID) ';

end;

为什么我会收到这个错误?

4

1 回答 1

4

如果您显示您尝试运行的动态 SQL,您最终会得到以下语句:

...
  FOR AGENCY_NM IN ('('S' || chr(38) ||'P (LT Rating)' as SP,'CreditSights CSR' as CSR,'FITCH (LT Rating)' as  Fitch,'Moody||'''||'s (LT Rating or CFR)' as MOODY,'DBRS (LT Rating)' as DBRS )')
) 
order by Rating_ID) 

并且那个 IN 子句只是格式错误。您的agg_column构造存在引用问题,围绕chr(38)您最终想要的两个单引号的连接和转义Moody''s;它应该是:

   AGG_COLUMNS := '''S' || chr(38) ||'P (LT Rating)'' as SP,''CreditSights CSR'' as CSR,''FITCH (LT Rating)'' as  Fitch,''Moody''''s (LT Rating or CFR)'' as MOODY,''DBRS (LT Rating)'' as DBRS';

然后添加额外的引号,因此应该是:

  FOR AGENCY_NM IN ('||AGG_COLUMNS||')

整个选择部分周围的最外面的一组括号不应该在那里。

declare
  AGG_COLUMNS VARCHAR2(2000);
begin
   AGG_COLUMNS := '''S' || chr(38) ||'P (LT Rating)'' as SP,''CreditSights CSR'' as CSR,''FITCH (LT Rating)'' as  Fitch,''Moody''''s (LT Rating or CFR)'' as MOODY,''DBRS (LT Rating)'' as DBRS';
   execute immediate '
CREATE Global TEMPORARY TABLE temp_extRating
ON COMMIT PRESERVE ROWS
AS
select * from 
(
  select EXTRAT.Rating_ID  ,EXTRAT.AGENCY_NM  ,EXTRAT.EXT_RATING  ,EXTRAT.RBC_EQ_RATING  ,EXTRAT.EFFECTIVE_DATE 
    from RBC_P_EXT_RATING EXTRAT 
    join RBC_P_RATING Rat on 
  Rat.ID=EXTRAT.Rating_ID
  )
pivot (
  MIN(EXT_RATING) ER, 
  MIN(RBC_EQ_RATING) RER, 
  MIN(EFFECTIVE_DATE) ED
  FOR AGENCY_NM IN ('||AGG_COLUMNS||')
) 
order by Rating_ID';
end;
/

我不确定你为什么将它作为一个单独的变量,但使用替代引用机制来减少单引号混乱会更简单。

   AGG_COLUMNS := q'['S]' || chr(38) || q'[P (LT Rating)' as SP,'CreditSights CSR' as CSR,'FITCH (LT Rating)' as  Fitch,'Moody''s (LT Rating or CFR)' as MOODY,'DBRS (LT Rating)' as DBRS]';

您还可以set define off在 SQL*Plus 或 SQL Developer 中使用以避免将 & 符号连接为chr(38)

set define off
declare
  AGG_COLUMNS VARCHAR2(2000);
begin
   AGG_COLUMNS := q'['S&P (LT Rating)' as SP,'CreditSights CSR' as CSR,'FITCH (LT Rating)' as  Fitch,'Moody''s (LT Rating or CFR)' as MOODY,'DBRS (LT Rating)' as DBRS]';
...

但是您根本不需要使用动态 SQL;您似乎只能避免使用&符号:

set define off
CREATE Global TEMPORARY TABLE temp_extRating
ON COMMIT PRESERVE ROWS
AS
select * from 
(
  select EXTRAT.Rating_ID  ,EXTRAT.AGENCY_NM  ,EXTRAT.EXT_RATING  ,EXTRAT.RBC_EQ_RATING  ,EXTRAT.EFFECTIVE_DATE 
    from RBC_P_EXT_RATING EXTRAT 
    join RBC_P_RATING Rat on 
  Rat.ID=EXTRAT.Rating_ID
  )
pivot (
  MIN(EXT_RATING) ER, 
  MIN(RBC_EQ_RATING) RER, 
  MIN(EFFECTIVE_DATE) ED
  FOR AGENCY_NM IN ('S&P (LT Rating)' as SP,'CreditSights CSR' as CSR,'FITCH (LT Rating)' as  Fitch,q'[Moody's (LT Rating or CFR)]' as MOODY,'DBRS (LT Rating)' as DBRS)
) 
order by Rating_ID;
set define on
于 2016-01-12T12:12:20.863 回答