在PLSQL循环中创建clob

时间:2015-10-07 15:06:22

标签: oracle plsql plsqldeveloper

我已经编写了一个plsql程序来创建一个html页面并将其作为电子邮件附件发送。

在我的程序中,我正在编写for循环,选择大约500条记录,exp代码如下:

declare

table clob;
footer VARCHAR2(1000);

begin

table:='<html><head></head><body><table>';
DBMS_LOB.APPEND(table, '<tr><th>ID</th><th>NAME</th></td>');    

footer:='</table></body></html>'

for rec in (

select o.id from <some tables> group by o.id

)
loop

select 
o.id,
oa.name
into id_object,name
from
objects o,
objectsattributes oa
where
o.id=oa.object_id and
o.id=rec.id;

DBMS_LOB.APPEND(table, '<tr><td>'||id_object||'</td><td>'||name||'</td></td>');

END LOOP;

DBMS_LOB.APPEND(table,footer):


-- send email with table as html attachment

END;

对于较少量的数据,它是工作文件,我将获得html doc作为附件,具有适当的记录和布局但是当数据很大时(在for循环中)我将在html doc中获得重复数据并且布局也被破坏。 / p>

我已调试它,我发现在查询中我没有得到任何重复的ID。

但是在html doc中我得到了重复,而且还有更多的时间。

它似乎有兑现问题或同步问题或问题与for循环或clob写作问题。

任何人都可以告诉我如何循环大量数据并在for循环中选择其他数据库。

因此数据不会丢失并按顺序排列。

注意:上面的代码用于描述问题,实际的select语句更复杂,数据更多。

1 个答案:

答案 0 :(得分:1)

您没有在标题行或正文行中正确关闭表格行</tr>

-- Opening header
table:='<html><head></head><body><table>';
-- Change the closing </td> to a </tr> below to end the header row
DBMS_LOB.APPEND(table, '<tr><th>ID</th><th>NAME</th></tr>');    


-- Body rows
-- Change the closing </td> to  </tr>
DBMS_LOB.APPEND(table, '<tr><td>'||id_object||'</td><td>'||name||'</td></tr>');