手头上有一个项目需要用Oracle数据库,而且要在某些的数据项中加入含有段落的内容,即含有换行符。例如
要在TA表中Content字段中加入以下的内容
@#CONTROL_BEGIN#@
@#SECTION#@ ControlType
@#BEGIN#@
DateSegment
@#END#@
@#SECTION#@ caption
@#BEGIN#@
HIS 出院日期'引号'
@#END#@
@#CONTROL_END#@
如要用sqlplus的 pl/sql的话,那么应要以下的方式进行插入
insert into ta(content)values(
'@#CONTROL_BEGIN#@'||Chr(13)||Chr(10)||''||Chr(13)||Chr(10)||'@#SECTION#@ ControlType'||Chr(13)||Chr(10)||'@#BEGIN#@'||Chr(13)||Chr(10)||'DateSegment'||Chr(13)||Chr(10)||'@#END#@'||Chr(13)||Chr(10)||''||Chr(13)||Chr(10)||'@#SECTION#@ caption'||Chr(13)||Chr(10)||'@#BEGIN#@'||Chr(13)||Chr(10)||'HIS 出院日期''引号'''||Chr(13)||Chr(10)||'@#END#@'||Chr(13)||Chr(10)||''||Chr(13)||Chr(10)||'@#CONTROL_END#@'
);
其中换行转换为: ||Chr(13)||Chr(10)||
而内容中的引号就需要多加一个进行转义: '引号' 变成 ''引号''
按上面的规则,要手工转换一段的内容十分容易出错,有鉴及此,随手就写了这个程序出来。
程序是用Delphi写的,其中关键的地方只有几行代码:
function OracleRTLineTrans(slContent: TStrings): string;
var
i : integer;
s : string;
begin
result := '';
for i := 0 to slContent.Count - 1 do
begin
s := StrUtils.AnsiReplaceStr(slContent.Strings[i], '''', '''''');
if i = 0 then
result := '''' + s + ''''
else
result := result + '||Chr(13)||Chr(10)||' + '''' + s + '''';
end;
end;
Written by flexitime
2008.5.3