PLS-00103:遇到符号“,”

时间:2013-03-21 10:36:57

标签: plsql oracle11g

此过程出现以下错误。

CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE
    'CREATE TABLE COLUMN_NAMES AS (
     SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS
     FROM   
         (SELECT DISTINCT COLUMN_NAME
          FROM BW_COLUMN_ROW_CELL_JOIN)
     )';
END;
/

给出:

PLS-00103: Encountered the symbol "," when expecting one of the following:     
* & = - + ; < / > at in is mod remainder not rem return    
returning <an exponent (**)> <> or != or ~= >= <= <> and or   
like like2 like4 likec between into using || multiset bulk    member submultiset 

任何人都可以说这有什么问题吗?

感谢。

3 个答案:

答案 0 :(得分:3)

另一种方式(在Oracle 10g及更高版本中)是使用替代字符串文字表示法 - 这意味着您无需担心正确转义字符串中的所有单引号,例如q'{my string's got embedded quotes}'

CREATE OR REPLACE PROCEDURE SAMPLE
IS
BEGIN
EXECUTE IMMEDIATE q'[
     CREATE TABLE COLUMN_NAMES AS (
     SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS
     FROM   
         (SELECT DISTINCT COLUMN_NAME
          FROM BW_COLUMN_ROW_CELL_JOIN)
     )]';
END;
/

答案 1 :(得分:1)

我认为问题是你在单引号内有单引号。我目前无法对此进行测试,但我建议您尝试以下操作(注意内部引号是双引号'',它们会逃脱它们:

CREATE OR REPLACE PROCEDURE SAMPLE 
IS
BEGIN 
    EXECUTE IMMEDIATE 'CREATE TABLE COLUMN_NAMES AS ( SELECT LISTAGG(COLUMN_NAME, '','') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS FROM (SELECT DISTINCT COLUMN_NAME FROM BW_COLUMN_ROW_CELL_JOIN) )'; 
END; 
/

我还首先尝试独立代码的create table部分,以确保它在将其包装到proc中之前有效。

答案 2 :(得分:-1)

您无法直接single quotes select statement使用Execute ImmediateCHR(39)需要使用CREATE OR REPLACE PROCEDURE SAMPLE IS BEGIN EXECUTE IMMEDIATE 'CREATE TABLE COLUMN_NAMES AS ( SELECT LISTAGG(COLUMN_NAME,'||chr(39)||','||chr(39)||') WITHIN GROUP (ORDER BY COLUMN_NAME) AS STUDENTS FROM (SELECT DISTINCT COLUMN_NAME FROM BW_COLUMN_ROW_CELL_JOIN))'; END; 进行编码

{{1}}