以下代码获取PLSQL数字或值错误

时间:2017-06-08 09:16:55

标签: oracle plsql

    Declare 
            TYPE cur_type IS REF CURSOR;
            v_owner varchar2(40);
             v_table_name varchar2(40);
             v_column_name varchar2(100);
            ln_col_variable VARCHAR2(20000) := Null;
            sql_fetch       VARCHAR2(30000) := Null;
             l_xmltype XMLTYPE;
             col_var  varchar2(2000);

               enquiry_cur cur_type;

              /* First cursor */
                  CURSOR get_tables IS
                  SELECT DISTINCT  tbl.table_name
                   FROM user_tab_columns tbl
                   WHERE tbl.column_name='ENQUIRY_NO';

              /* Second cursor */
                        CURSOR get_columns IS
                         SELECT DISTINCT col.column_name
                         FROM user_tab_columns col
                          -- WHERE col.owner = v_owner
                        WHERE col.table_name = v_table_name;

                  BEGIN
                      dbms_output.enable(1000000);
                    -- Open first cursor
                          OPEN get_tables;

                   LOOP
                 FETCH get_tables INTO  v_table_name;
                 exit when  get_tables%notfound;
                 DBMS_OUTPUT.PUT_LINE(v_table_name);
                 dbms_output.put_line('----------------------');


                         -- Open second cursor
                  OPEN get_columns;
                  LOOP
               FETCH get_columns INTO v_column_name;
                exit when get_columns%notfound;
               dbms_output.put_line(v_column_name);
               ln_col_variable :=ln_col_variable||','||v_column_name;

               col_var:=replace(substr(ln_col_variable,1,1),',','*');


               --dbms_output.put_line(col_var);
                 /*  Loop
         ln_col_variable :=ln_col_variable||','||v_column_name;


                      End Loop;*/
         -- ln_col_variable :=ln_col_variable||sysdate;
         --  sql_fetch       := 'Select '||ln_col_variable||'           From '||v_table_name;
             -- dbms_output.put_line(sql_fetch);
                          END LOOP;
                sql_fetch := 'Select '||col_var||' From '||v_table_name;

           OPEN enquiry_cur FOR sql_fetch ;


                  l_xmltype := XMLTYPE(enquiry_cur);
                 dbms_output.put_line(l_xmltype.getClobVal);
                  CLOSE get_columns;

   END LOOP;

   CLOSE get_tables;

         EXCEPTION
           WHEN OTHERS THEN  raise_application_error(-20001,'An              encountered  '||SQLCODE||'        -ERROR- '||SQLERRM);
                   end ;

我为第74行获取Plsql数字或值错误。想要获取sql_fetch字符串的xml输出。使用utl_files但无法获得xml输出。这些表只是动态知道的。表结构在编译时也是已知的

1 个答案:

答案 0 :(得分:2)

您的代码有一些错误。在解决实际错误之前,您需要纠正错误。您的游标apm link已参数化,并且您没有向其传递任何值。见下文:

get_columns

更正代码后,您需要确保在光标DECLARE TYPE cur_type IS REF CURSOR; v_owner VARCHAR2 (40); v_table_name VARCHAR2 (40); v_column_name VARCHAR2 (100); ln_col_variable VARCHAR2 (20000) := NULL; sql_fetch VARCHAR2 (30000) := NULL; l_xmltype XMLTYPE; col_var VARCHAR2 (2000); enquiry_cur cur_type; /* First cursor */ CURSOR get_tables IS SELECT DISTINCT tbl.table_name FROM user_tab_columns tbl WHERE tbl.column_name = 'COL1'; /* Second cursor */ CURSOR get_columns(v_table_name varchar) IS SELECT DISTINCT col.column_name FROM user_tab_columns col -- WHERE col.owner = v_owner WHERE col.table_name = v_table_name; --<--using a variable here BEGIN --DBMS_OUTPUT.enable (1000000); -- Open first cursor OPEN get_tables; LOOP FETCH get_tables INTO v_table_name; EXIT WHEN get_tables%NOTFOUND; DBMS_OUTPUT.PUT_LINE (v_table_name); DBMS_OUTPUT.put_line ('----------------------'); -- Open second cursor OPEN get_columns(v_table_name); --<--You need to pass parameter to cursor LOOP FETCH get_columns INTO v_column_name; EXIT WHEN get_columns%NOTFOUND; DBMS_OUTPUT.put_line (v_column_name); ln_col_variable := ln_col_variable || ',' || v_column_name; col_var := REPLACE (SUBSTR (ln_col_variable, 1, 1), ',', '*'); --dbms_output.put_line(col_var); /* Loop ln_col_variable :=ln_col_variable||','||v_column_name; End Loop;*/ -- ln_col_variable :=ln_col_variable||sysdate; -- sql_fetch := 'Select '||ln_col_variable||' From '||v_table_name; -- dbms_output.put_line(sql_fetch); END LOOP; sql_fetch := 'Select ' || col_var || ' From ' || v_table_name; OPEN enquiry_cur FOR sql_fetch; l_xmltype := XMLTYPE (enquiry_cur); DBMS_OUTPUT.put_line (l_xmltype.getClobVal); CLOSE get_columns; END LOOP; CLOSE get_tables; EXCEPTION WHEN OTHERS THEN raise_application_error ( -20001, 'An encountered ' || SQLCODE || ' -ERROR- ' || SQLERRM); END; 中引用的所有表格至少应有一行,否则您将获得get_tables

输出:

PL/SQL: numeric or value error