PLSQL 在期望以下之一时遇到符号“YES”

Posted

技术标签:

【中文标题】PLSQL 在期望以下之一时遇到符号“YES”【英文标题】:PLSQL Encountered the symbol "YES" when expecting one of the following 【发布时间】:2019-12-14 00:20:56 【问题描述】:

您好,我正在尝试使用 PLSQL 创建一个汇总统计表。我是 PLSQL 的新手。我不断收到此错误。 预期以下情况之一时遇到符号“YES”

下面是我的代码。

DECLARE
AGE NUMBER;
CAMPAIGN NUMBER;
PDAYS NUMBER;
PREVIOUS NUMBER;
POUTCOME NUMBER;
EMP_VAR_RATE NUMBER;
CONS_PRICE_IDX NUMBER;
CONS_CONF_IDX NUMBER;
EURIBOR3M NUMBER;
Y VARCHAR;


BEGIN
 EXECUTE IMMEDIATE ('
CREATE TABLE SUMMARY_STAT
AS

SELECT 
ROUND(AVG(AGE)) AS AVERAGE_AGE
,ROUND(AVG(CAMPAIGN)) AS AVERAGE_CONTACTS_MADE
,ROUND(AVG(PDAYS)) AS NO_DAYS_PASSED
,ROUND(AVG(PREVIOUS)) AS AVERAGE_PREVIOUS_CONTACTS_MADE
,ROUND(AVG(EMP_VAR_RATE)) AS AVERAGE_EMPLOYEE_VARIATION
,ROUND(AVG(CONS_PRICE_IDX)) AS AVERAGE_CONSUMER_PRICE_INDEX
,ROUND(AVG(CONS_CONF_IDX)) AS AVERAGE_CONSUMER_CONFIDENCE_INDEX
,ROUND(AVG(EURIBOR3M)) AS AVERAGE_EURIBOR
,COUNT(CASE WHEN Y = 'yes' THEN 1 end) AS NUMBER_CUSTOMERS_TOOK_DEPOSIT
,COUNT(CASE WHEN Y = 'no' THEN 1 end) AS NUMBER_CUSTOMERS_DIDNT_TAKE_DEPOSIT

FROM BANK_DATA
');

END;

【问题讨论】:

【参考方案1】:

问题是您在较大的字符串中有单引号。不幸的是,从解释器的角度来看,您在 'Create ... when Y = ' 中的字符串是一个字符串,而下一个标记是解释器不理解的单词“yes”。 你有两个选择:

    将较大字符串中所需的引号加倍。 使用替代quote delimiter。

使用引号分隔符你会得到:

BEGIN
 EXECUTE IMMEDIATE (q'[
CREATE TABLE SUMMARY_STAT
AS

SELECT 
ROUND(AVG(AGE)) AS AVERAGE_AGE
,ROUND(AVG(CAMPAIGN)) AS AVERAGE_CONTACTS_MADE
,ROUND(AVG(PDAYS)) AS NO_DAYS_PASSED
,ROUND(AVG(PREVIOUS)) AS AVERAGE_PREVIOUS_CONTACTS_MADE
,ROUND(AVG(EMP_VAR_RATE)) AS AVERAGE_EMPLOYEE_VARIATION
,ROUND(AVG(CONS_PRICE_IDX)) AS AVERAGE_CONSUMER_PRICE_INDEX
,ROUND(AVG(CONS_CONF_IDX)) AS AVERAGE_CONSUMER_CONFIDENCE_INDEX
,ROUND(AVG(EURIBOR3M)) AS AVERAGE_EURIBOR
,COUNT(CASE WHEN Y = 'yes' THEN 1 end) AS NUMBER_CUSTOMERS_TOOK_DEPOSIT
,COUNT(CASE WHEN Y = 'no' THEN 1 end) AS NUMBER_CUSTOMERS_DIDNT_TAKE_DEPOSIT
FROM BANK_DATA
]');

END; 

【讨论】:

以上是关于PLSQL 在期望以下之一时遇到符号“YES”的主要内容,如果未能解决你的问题,请参考以下文章

在期望以下之一时遇到符号:如果在 Pl/SQL 函数中

在预期以下情况之一时遇到符号“文件结尾”

错误:PLS-00103:遇到符号“;”当期望以下之一时:* & = - + < / > at in is mod remaining not rem then

PLS-00103:错误。在期望以下之一时遇到符号“BEGIN”:<an identifier> <a double-quoted delimited-identifier>

遇到符号“文件结尾” - plsql

PLSQL forall 中遇到符号“IF”