解析xml时进行整理

时间:2017-01-11 13:39:37

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

在同一台服务器上,我有很少的数据库具有不同的排序规则。 我正在将xml解析为

CREATE TABLE #tblDocuments
(
    Document varchar(5) COLLATE DATABASE_DEFAULT
   ,DocCode varchar(15) COLLATE DATABASE_DEFAULT
   ,Ammount decimal(18,2)
   );

IF @@ERROR <> 0
    GOTO ErrorHandl;

INSERT INTO #tblDocuments
(
    Document
   ,DocCode
   ,Ammount
)

SELECT 
    Tbl.Col.value('@DocType','varchar(3)') AS Document
   ,Tbl.Col.value('@DocCode','varchar(15)') AS DocCode
   ,Tbl.Col.value('@Ammount','decimal(18,2)') AS Ammount
FROM 
    @xmlData.nodes('/Documents/document') AS Tbl(Col);

有没有办法在不先创建具有预定义排序规则的表的情况下解析xml。像(我知道这段代码不起作用)

SELECT 
    Tbl.Col.value('@DocType','varchar(3) COLLATE DATABASE_DEFAULT') AS Document
   ,Tbl.Col.value('@DocCode','varchar(15) COLLATE DATABASE_DEFAULT') AS DocCode
   ,Tbl.Col.value('@Ammount','decimal(18,2) COLLATE DATABASE_DEFAULT') AS Ammount
INTO #tblDocuments
FROM 
    @xmlData.nodes('/Documents/document') AS Tbl(Col);enter code here

提前谢谢

1 个答案:

答案 0 :(得分:1)

COLLATE放在.value()函数之外:

SELECT 
    Tbl.Col.value('@DocType','varchar(3)') COLLATE DATABASE_DEFAULT AS [Document]
   ,Tbl.Col.value('@DocCode','varchar(15)') COLLATE DATABASE_DEFAULT AS [DocCode]
   ,Tbl.Col.value('@Ammount','decimal(18,2)') AS Ammount
INTO #tblDocuments
FROM 
    @xmlData.nodes('/Documents/document') AS Tbl(Col);
    enter code here

此外,除了字符串数据类型(COLLATE / CHAR / VARCHAR / NCHAR - 之外,您不会对任何内容使用NVARCHAR - 甚至已弃用TEXTNTEXT数据类型。)