You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PL/SQL Web文档输出内容时如何实现换行?

PL/SQL Web开发输出换行解决方案

问题原因

你输出的是HTML内容,浏览器渲染HTML时会默认忽略所有普通换行符(即chr(13)||chr(10)),将连续空白字符合并为单个空格,所以就算你加了操作系统换行符,页面展示还是会挤成一行。

两种可行实现方案

方案1:使用HTML换行标签<br>(最常用,适配普通HTML场景)

在每一行需要换行的内容末尾拼接<br>标签即可,修改后的代码如下:

create or replace procedure aptstaff_information_prn as
begin
htp.print('<html><head>');
htp.print('<title>Staff Apartment Details</title></head><body>');
htp.print('<p>Staff Apartment Details</p>');
for Apt_staff in
(select staff.first_name as sfn, staff.last_name as sln, staff.position as sp, rental.rental_no as srn
 from staff left join rental
 on staff.staff_no = rental.staff_no
 order by staff.first_name, staff.last_name, staff.position)
loop
if Apt_staff.srn is not null then
htp.prn(Apt_staff.sfn||' '||Apt_staff.sln||'      '||Apt_staff.sp || '<br>');
htp.prn('--------------------<br>');
htp.prn('Apartment#<br>');
htp.prn(Apt_staff.srn || '<br>');
else
htp.prn(Apt_staff.sfn||' '||Apt_staff.sln||'      '||Apt_staff.sp || '<br>');
htp.prn('--------------------<br>');
htp.prn('Apartment#<br>');
htp.prn('No Apartment Rented<br>');
end if;
end loop;
htp.print('</body></html>');
end;

方案2:使用<pre>预格式化标签(适合保留纯文本原生格式的场景)

如果不想给每一行都加<br>,可以把所有列表内容放在<pre>标签内,此时chr(13)||chr(10)的换行效果会被保留,修改逻辑如下:

  • 在输出列表内容前加一行htp.print('<pre>');
  • 每一行内容末尾拼接chr(10)(或者chr(13)||chr(10)适配全平台)
  • 在列表输出结束后加一行htp.print('</pre>');

选择建议

  • 只是要实现简单的网页换行效果,选方案1即可
  • 后续需要保留文本的缩进、空格排版,优先选方案2

内容的提问来源于stack exchange,提问作者YilangZhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 10:15:06