0

我如何在动态 SQL 中编写查询,我收到下面给出的错误。

 EXECUTE IMMEDIATE 
 'CREATE TABLE SAMPLE
  SELECT EXTRACT (VALUE (d), '''//row/text()''').getstringval () 
  FROM (SELECT XMLTYPE (   '''<rows><row>'''
  || REPLACE ('''venkat,vijay,bylla,12''', ''',''', '''</row><row>''')
   || '''</row></rows>'''
    ) AS xmlval
     FROM DUAL) x,
   TABLE (XMLSEQUENCE (EXTRACT (x.xmlval, '''/rows/row'''))) d';

错误是:

Error(118,34): PLS-00103: Encountered the symbol "/" when expecting
one of the following:     ( - + case mod new null <an identifier>    
<a double-quoted delimited-identifier> <a bind variable>
4

1 回答 1

1

你的引号是错误的,你需要给列加上别名..例如:

SQL> begin
  2  EXECUTE IMMEDIATE
  3   'CREATE TABLE SAMPLE AS
  4    SELECT EXTRACT (VALUE (d), ''//row/text()'').getstringval () a
  5    FROM (SELECT XMLTYPE (   ''<rows><row>''
  6    || REPLACE (''venkat,vijay,bylla,12'', '','', ''</row><row>'')
  7     || ''</row></rows>''
  8      ) AS xmlval
  9       FROM DUAL) x,
 10     TABLE (XMLSEQUENCE (EXTRACT (x.xmlval, ''/rows/row''))) d';
 11  end;
 12  /

PL/SQL procedure successfully completed.

SQL> select * from sample;

A
--------------------------------------------------------------------------------
venkat
vijay
bylla
12

但是你为什么要在 oracle 中动态创建表?如果您正在考虑在代码中动态创建表,这是一种不好的做法,在 oracle 中应避免使用(如果需要,请使用全局临时表)。

于 2013-03-26T11:32:29.443 回答