功能说明:
1.模拟for Xml explict 写的一个比较复杂的Sql 语句
2.替换几个特殊字符 (&, <, >, char(11), char(20) )
3.去掉不必要的栏位(当omfname = omfdesc,在该文件中只包含omfname这个栏位)
其它说明:
1.之所以不用for xml explict,是因为该语句所用范围的特殊性。
它是用在通过SQL Server自带的DTS(数据传输服务)将该查询的结果转化为txt文件。然后在一个Xml文件中包含该文本,实现数据库到Xml的转化。(具体如何衔接为一个整体,先不在介绍)
2.char(11)和char(20)在繁体系统下都能解析成空格,但在简体系统下无法解析。(它们是显示为特殊字符)当然这和简繁体没必然的直接联系,和其自带的编码方式有关,我们不去深究,直接使用其字符格式即可。
代码:
--Desc: Export acmnet.dbo.acmomf to xml
/*
The output style is:
<Obj> <Id>OS0003000415098442</Id> <Name>IW-WT0400356</objName> <Desc>desc</Desc> <Key>km</objKey> <Rem>test</Rem> <User>Show Zhu</User> <Time>2004/12/01</Time> </eachObj>
<Obj> <Id>OS0003000415098441</Id> <Name>IW-WT0400355</objName> <User>Show Zhu</User> <Time>2004/12/01</Time> </eachObj>
*/
--Desc:
--Char(11),Char(20) is a invalid character for xmlreader, it displays as space in tradtional OS, So we place it as ' '
-------------------------------------------------------------------
SELECT
'<Obj>' as eachobj_start,
'<Id>' + omfid + '</Id>' as objid,
'<Name>' + replace(replace(replace(replace(replace(rtrim(omfname),'&','&'),'<','<'),'>','>'),char(11),' '),char(20),' ') + '</Name>' as objname,
case
when rtrim(omfdesc) = '' then ''
when rtrim(omfdesc) = rtrim(omfname) then ''
else '<Desc>' + replace(replace(replace(replace(replace(rtrim(omfdesc),'&','&'),'<','<'),'>','>'),char(11),' '),char(20),' ') + '</Desc>'
end
as objdesc,
case
when rtrim(omfkeyword) = '' then ''
when rtrim(omfkeyword) = rtrim(omfname) then ''
when rtrim(omfkeyword) = rtrim(omfdesc) then ''
else '<Key>' + replace(replace(replace(replace(replace(rtrim(omfkeyword),'&','&'),'<','<'),'>','>'),char(11),' '),char(20),' ') + '</Key>'
end
as objkey,
case
when rtrim(omfremark) = '' then ''
when rtrim(omfremark) = rtrim(omfname) then ''
when rtrim(omfremark) = rtrim(omfdesc) then ''
when rtrim(omfremark) = rtrim(omfkeyword) then ''
else '<Rem>' + replace(replace(replace(replace(replace(rtrim(omfremark),'&','&'),'<','<'),'>','>'),char(11),' '),char(20),' ') + '</Rem>'
end
as objrem,
'<User>' + isnull((select rtrim(right(usraccount,datalength(usraccount)-charindex('\',usraccount))) from acmusr where usrid = omfmusr),'') + '</User>' as objuser,
'<Time>' + CONVERT(varchar(30),omfmtime,111) + '</Time>'as objtime,
'</Obj>' as eachobj_end
from acmomf
where omfactive !='D' and omfactive !='P' and omfactive!='C' and omfactive!='W'
order by omfmtime desc