我收到错误"在第5行和第34行丢失了逗号;从这段代码:
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;
为什么我会收到错误?
答案 0 :(得分: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)
的concatenatin周围有引用问题,并且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;
/
我不确定为什么你将它作为一个单独的变量,但使用the alternative quoting mechanism来减少单引号混乱会更简单。
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]';
您还可以在SQL * Plus或SQL Developer中使用set define off
,以避免将&符号连接为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