将表格数据作为附件发送,将表格名称和电子邮件ID作为参数

时间:2012-06-06 02:37:02

标签: oracle plsql smtp plsqldeveloper

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

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的标题,而行来自作为参数传递的表。

请告诉我上述是否有效,或者是他们的任何方式。 感谢

1 个答案:

答案 0 :(得分: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