现在开发人员有一个需求,想要在前台程序调用一去SP,
SP的参数有    参数有文件名称,路径,表格
功能 把表格中的内容写入指定的路径下的文件中提醒:
    每次调用时,表格的名称可能会不一样,个人觉得要用到动态SQL及动态游标的知识
但因能力有限,请高手作答。

解决方案 »

  1.   

    簡單寫了一個,希望對樓主有所啟發create or replace procedure print_table(table_name in varchar2)
    is
      cursor cur_columns(tn varchar2) is
      select column_name,data_type from user_tab_columns
      where upper(table_name) = upper(tn)
      order by column_id;
      
      statement varchar2(2000);
      print_line varchar2(2000);
    begin
      statement := 'declare ' || chr(10);
      statement := statement || '  cursor cur_main is ' || chr(10);
      statement := statement || '  select ' || chr(10);
      
      print_line := '  dbms_output.put_line(';
      for col in cur_columns(table_name) loop
        if col.data_type = 'DATE' then
          statement := statement || 'to_char(' || col.column_name || ', ''yyyymmdd hh24:mi:ss'') AS ' || col.column_name;
        else
          statement := statement || col.column_name;
        end if;
        statement := statement || ',';
        
        print_line := print_line || 'r.' || col.column_name || ' || '','' || ';
      end loop;
      statement := substr(statement, 1, length(statement) - 1) || chr(10);
      statement := statement || ' from ' || table_name || ';' || chr(10);
      
      print_line := substr(print_line, 1, length(print_line) - 11);
      print_line := print_line || ');' || chr(10);
      
      statement := statement || 'begin ' || chr(10);
      statement := statement || 'for r in cur_main loop ' || chr(10);
      statement := statement || print_line;
      statement := statement || 'end loop;' || chr(10);
      statement := statement || 'end;';
      
      dbms_output.put_line(statement);
      
      execute immediate statement;
    end;-- 測試代碼
    begin
      print_table('employees');
    end;-- 輸出結果
    198,Donald,OConnell,DOCONNEL,650.507.9833,19990621 00:00:00,SH_CLERK,2600,,124,50
    199,Douglas,Grant,DGRANT,650.507.9844,20000113 00:00:00,SH_CLERK,2600,,124,50
    ...
    ...
      

  2.   

    需求太模糊.不过应该会用到cursor和utl_file包.