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;
/

给出:

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 

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

谢谢。

4

3 回答 3

3

Another way (in Oracle 10g and later) is to use the alternative string literal notation - this means you don't need to worry about correctly escaping all the single quotes in the string, e.g. 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;
/
于 2013-03-22T06:42:46.827 回答
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 之前有效。

于 2013-03-21T10:48:50.450 回答
-1

你不能single quotes直接select statement使用Execute Immediate它需要使用编码CHR(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;
于 2013-03-21T11:02:00.713 回答