SQL Server 2008R2和创建XML文档

时间:2015-01-08 16:41:54

标签: sql xml sql-server-2008-r2

论坛上的第一篇帖子,因为我真的被困在这个帖子上。

以下查询正确地将有效的XML文档分配给@xTempXML变量(xml类型)。注意:文档的长度(转换为varchar(max)= 711

select @xTempXML = (
        select 
            PrescriberFirstName     as "row/prescriber/name/first",
            PrescriberLastName      as "row/prescriber/name/last",
            PrescriberAddress1      as "row/prescriber/address/line1",
            PrescriberAddress2      as "row/prescriber/address/line2",  
            PrescriberCity          as "row/prescriber/address/city",
            PrescriberState         as "row/prescriber/address/state",
            PrescriberZipCode       as "row/prescriber/address/zipcode",
            PatientFirstName        as "row/patient/name/first",
            PatientLastName         as "row/patient/name/last",
            PatientMiddleName       as "row/patient/name/middle",
            PatientAddress1         as "row/patient/address/line1",
            PatientAddress2         as "row/patient/address/line2", 
            PatientCity             as "row/patient/address/city",
            PatientState            as "row/patient/address/state",
            PatientZipCode          as "row/patient/address/zipcode",
            PatientFileID           as "row/patient/fileid",
            PatientSSN              as "row/patient/ssn",
            PatientDOB              as "row/patient/dob",
            DrugDescription         as "row/medicationprescribed/description",
            DrugStrength            as "row/medicationprescribed/strength",
            DrugDEASchedule         as "row/medicationprescribed/deaschedule",
            DrugQty                 as "row/medicationprescribed/qty",
            DrugDirections          as "row/medicationprescribed/directions",
            DrugFormCode            as "row/medicationprescribed/form",
            DrugDateWritten         as "row/medicationprescribed/writtendate",
            DrugEffectiveDate       as "row/medicationprescribed/effectivedate",
            DrugRefillQty           as "row/medicationprescribed/refill/qty",
            DrugRefillQtyQualifier  as "row/medicationprescribed/refill/qualifier",
            DrugNote                as "row/medicationprescribed/note",
            PharmacyStoreName       as "row/pharmacy/storename",
            PharmacyIdentification  as "row/pharmacy/identification",
            PharmacyAddress1        as "row/pharmacy/address/line1",
            PharmacyAddress2        as "row/pharmacy/address/line2",
            PharmacyCity            as "row/pharmacy/address/city",
            pharmacyState           as "row/pharmacy/address/state",
            pharmacyZipCode         as "row/pharmacy/address/zipcode"
        from 
            Rxarchive
         where ArchiveUUID=@ArchiveRefUUID
           and CreatedDT between @RptParamStartDT and  @RptParamStopDT
           and CHARINDEX(',' + PrescriberFID + ',', ',' + @RptParamFID + ',') > 0 
        FOR XML PATH(''), ROOT('result'), TYPE
        )

declare @sXMLVersion varchar(max) = '<?xml version="1.0" encoding="utf-8"?>'
select len(@sXMLVersion + convert(varchar(max),@xTempXML))

注意:连接字符串的长度= 749,这是正确的。

set @xFinalXML = convert(xml,(@sXMLVersion + CAST(@xTempXML as varchar(max))))

select LEN(convert(varchar(max),@xFinalXML))

注意:此变量的长度回到711!

select @xFinalXML

该变量仍然是有效的XML文档,只是没有版本信息

我做错了什么?

任何和所有帮助都非常感谢!

1 个答案:

答案 0 :(得分:0)

您错过了测试中的一个步骤。试试这个:

SELECT CONVERT(XML, '<?xml version="1.0" encoding="utf-8"?>')

它将返回一个空单元格。

根据您正在做的事情(即最终转换为VARCHAR),没有理由从XML数据类型开始。您也可以从, TYPE子句中删除FOR XML,然后只连接@sXMLVersion + @xTempXML

发生这种情况的原因在此处注明:Limitations of the xml Data Type

  

xml 数据类型实例中存储XML数据时,不保留XML声明PI,例如&lt;?xml version =&#39; 1.0&#39;?&gt; 。这是设计的。数据转换为 xml 类型后,XML声明(&lt;?xml ...?&gt;)及其属性(版本/编码/独立)将丢失。 XML声明被视为XML解析器的指令。 XML数据在内部存储为ucs-2。保留XML实例中的所有其他PI。

如何正确处理从XML字段/变量中提取数据,请注意:XML Best Practices(&#34;文本编码&#34;)

  

SQL Server 2005以Unicode(UTF-16)存储XML数据。从服务器检索的XML数据以UTF-16编码形式出现。如果需要不同的编码,则必须对检索到的数据执行所需的转换。有时,XML数据可能采用不同的编码方式。如果是,则必须在数据加载期间小心。例如:

     
      
  • 如果您的文本XML是Unicode(UCS-2,UTF-16),则可以将其分配给XML列,变量或参数而不会出现任何问题。
  •   
  • 如果编码不是Unicode且是隐式的,由于源代码页,数据库中的字符串代码页应与要加载的代码点相同或兼容。如果需要,请使用COLLATE。如果不存在此类服务器代码页,则必须使用正确的编码添加显式XML声明。
  •   
  • 要使用显式编码,请使用 varbinary()类型,该类型与代码页无交互,或使用相应代码页的字符串类型。然后,将数据分配给XML列,变量或参数。
  •   
     

示例:明确指定编码
  假设您有一个XML文档vcdoc,存储为varchar(max),没有显式的XML声明。以下语句添加带有编码&#34; iso8859-1&#34;的XML声明,连接XML文档,将结果转换为 varbinary(max),以便保留字节表示,并且然后最终将其转换为XML。这使XML处理器能够根据指定的编码解析数据&#34; iso8859-1&#34;并为字符串值生成相应的UTF-16表示。

SELECT CAST(
    CAST (('<?xml version="1.0" encoding="iso8859-1"?>'+ vcdoc) AS VARBINARY (MAX))
  AS XML)

以下S.O.问题是相关的: