0

我正在尝试编写这样的代码

CREATE OR REPLACE PROCEDURE EMAIL_TABLE (errbuf         OUT      VARCHAR2,
                       retcode        OUT      NUMBER
                      ) is
cursor head is
select column_name
from all_tab_columns a
where table_name= 'EMP'
order by column_id;         

cursor line is
select column_name
from all_tab_columns a
where table_name= 'EMP'
order by column_id; 

count3 number;
count4 number;
lines1 varchar2(3000) := '';  
lines2 varchar2(3000) := '';            
count1 number;
count2 number;
header1 varchar2(3000) := '';
header2 varchar2(3000) := '';
BEGIN
count2 := 0;
select count(1)
into count1
from all_tab_columns a
where table_name= 'EMP';
FOR I IN head LOOP
 count2 := count2+1;
 IF count2 != count1 THEN
 header1 := header1||'"'||i.column_name||'"'||',' ;
 ELSIF count1 = count2 THEN
 header1 := header1||'"'||i.column_name||'"' ;
 END IF;
END LOOP;
header2 := header1;

count4 := 0;
select count(1)
into count3
from all_tab_columns a
where table_name= 'EMP';
FOR I IN line LOOP
count4 := count4+1;
 IF count4 = 1 THEN
  lines1 := lines1||''''||'"'||''''||'||'||i.column_name ;
 ELSIF count4 != count3 THEN
  lines1 := lines1||'||'||''''||'"'||','||'"'||''''||'||'||i.column_name;
 ELSIF count3 = count4 THEN
  lines1 :=        lines1||'||'||''''||'"'||','||'"'||''''||'||'||i.column_name||'||'||''''||'"'||'''' ;
 END IF;
END LOOP;
 lines2 := lines1;

FND_FILE.PUT_LINE(fnd_file.output, header2);
EMAIL_TABLE2(lines2);
END;

/

CREATE OR REPLACE PROCEDURE EMAIL_TABLE2(abc in varchar2) is
 xyz varchar2(1000);
 cursor lines is
 select abc a from EMP;  -- This is not working
BEGIN
  for i in lines loop
    FND_FILE.PUT_LINE(fnd_file.output,i.a);
  end loop;
END; 

/

以上将注册为并发程序,fnd_file.pu_line 将在 o||req_numebr||.out 中创建。我将把这个输出文件附加到电子邮件中并邮寄。

Outfile 应如下所示:

 "NAME","EMP_ID","DEPT","SAL"

 "JOHN","101","IT","10000"
 "ROB","102","SALES","8000"

第一行是来自 all_tab_columns 的标题,行来自作为参数传递的表。

请让我知道上述方法是否有效,或者是他们的任何方式。谢谢

4

1 回答 1

0

首先,删除最后一个逗号而不是先计算结果更容易:

FOR I IN head LOOP
 header1 := header1||'"'||i.column_name||'"'||',';
END LOOP;

header1 := SUBSTR(header1, 1, LENGTH(header1)-1);

接下来,如果您想动态构建和使用列列表,您的EMAIL_TABLE2过程需要使用引用游标 ( ):SYS_REFCURSOR

CREATE OR REPLACE PROCEDURE EMAIL_TABLE2(abc in varchar2) is
 xyz varchar2(1000);
 lines SYS_REFCURSOR;
 l_query varchar2(4000);
BEGIN
  l_query := 'select '||abc||' a from EMP';  
  open lines for l_query;
  loop
      fetch lines INTO xyz;
      exit when lines%NOTFOUND;
      FND_FILE.PUT_LINE(fnd_file.output,xyz);
  end loop;
END; 

更多关于参考游标的信息在这里:http ://www.orafaq.com/node/63

于 2012-06-06T07:17:14.377 回答