从存储过程生成表结构的XML文件

时间:2014-03-04 08:19:24

标签: xml oracle stored-procedures plsqldeveloper

我想创建存储过程,根据给定的表名生成表结构的XML文件作为输入参数

我使用下面的查询

 select  
               distinct   column_name,
                  data_type,
                   data_length  
                   from all_tab_columns  
                   where table_name =UPPER('Project')
                   order by column_id;

但它显示错误 ORA-00904:“PROJECT”:标识符无效 PROJECT是我的表名

我创建了一个Store过程,但它显示错误

create or replace 
procedure table_str (
   V_TABLE_NAME IN varchar2,
    v_FLAG OUT NUMBER
)
AS

BEGIN
    DECLARE
    xt_data xmltype;
    v_ctx dbms_xmlgen.ctxHandle;
    rc_data sys_refcursor;
    v_file  UTL_FILE.file_type;

    BEGIN 
   -- v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME ||'.xml', 'A');       
     --  v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME || '.xml', 'A');


      v_file := UTL_FILE.fopen('MYXML', V_TABLE_NAME || 'Layout.xml', 'W');


    OPEN rc_data FOR 
    'select distinct column_name, data_type ,column_id
            from all_tab_columns
            where table_name ='||V_TABLE_NAME ||' order by column_id';

    v_ctx := dbms_xmlgen.newContext (rc_data);
    DBMS_XMLGEN.SETNULLHANDLING(v_ctx,null);

    DBMS_XMLGEN.setrowsettag(v_ctx, V_TABLE_NAME); 
   DBMS_XMLGEN.setrowtag(v_ctx, null);

     xt_data := dbms_xmlgen.getXMLType (v_ctx);
     dbms_xmlgen.closeContext (v_ctx);

    dbms_xslprocessor.clob2file(xt_data.getclobval( ), 'MYXML', V_TABLE_NAME || 'Layout.xml');
    v_FLAG := 1;


      EXCEPTION
      WHEN OTHERS THEN
           DBMS_OUTPUT.PUT_LINE(SQLERRM);
           dbms_xmlgen.closeContext (v_ctx);
            v_FLAG := 0;

      end ;

END table_str;

请在这里帮忙

1 个答案:

答案 0 :(得分:1)

程序中的查询与您显示的示例不同:

OPEN rc_data FOR 
'select distinct column_name, data_type ,column_id
        from all_tab_columns
        where table_name ='||V_TABLE_NAME ||' order by column_id';

...没有将表名括在引号中,并且没有upper转换,因此如果您将其称为table_str('Project', :flag),则无法找到匹配项无论如何。它目前运行的查询将是:

select distinct column_name, data_type ,column_id from all_tab_columns
where table_name=Project order by column_id

...所以它使用Project作为标识符(因此是错误),而不是'Project'或更有用的'PROJECT'作为值。

你需要:

OPEN rc_data FOR 
  'select distinct column_name, data_type ,column_id
    from all_tab_columns
    where table_name = ''' || UPPER(V_TABLE_NAME) || ''' order by column_id';

甚至更好,使用绑定变量:

OPEN rc_data FOR 
  'select distinct column_name, data_type ,column_id
    from all_tab_columns
    where table_name = :table_name order by column_id'
  USING UPPER(V_TABLE_NAME);

这假设您没有为表名使用带引号的标识符。