gpt4 book ai didi

sql-server - 将 SQL NVARCHAR(MAX) 转换为 XML 并从 XML 字符串中获取值

转载 作者:行者123 更新时间:2023-12-03 16:44:44 29 4
gpt4 key购买 nike

我正在尝试从存储 XML 字符串的列的节点读取。该列的类型为 NVARCHAR(MAX)。

下面是创建表的脚本—— 设置 ANSI_NULLS 开始

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[XML_Dummy](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[XMLValue] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

以下是向其输入值的脚本 -

INSERT INTO [dbo].[XML_Dummy]
([Name]
,[XMLValue])
VALUES
('abcd'
,'<?xml version="1.0" encoding="UTF-8"?>
<STAFFv xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EMPLOYEE_NUMBER>123456</EMPLOYEE_NUMBER>
<TITLE>Mr</TITLE>
<INITIALS>J</INITIALS>
<FORENAME>PEARL</FORENAME>
<SURNAME>HOFFMAN</SURNAME>
<GENDER>MALE</GENDER>
<DATE_OF_BIRTH>1992-01-01</DATE_OF_BIRTH>
<DEPARTMENT_DESC>SUPER SUPPORT TEAM</DEPARTMENT_DESC>
<JOB_TITLE_DESC>GENERAL DOGSBODY</JOB_TITLE_DESC>
<ORIGINAL_DATE_JOINED>2014-05-01</ORIGINAL_DATE_JOINED>
<CURRENT_EMPLOYEE financialyear="2014">Y</CURRENT_EMPLOYEE>
<INTERNAL_EMAIL xsi:nil="true" />
<CHANGE_TYPE>INSERT</CHANGE_TYPE>
<CHANGE_DATE>2014-03-27</CHANGE_DATE>
</STAFF>')
GO

我的目标是从此字符串中获取 GENDER。但是,当我编写以下查询时开始 -

     declare @XMLVALUECAST_ xml
declare @XMLVALUECONVERT_ xml

SELECT
@XMLVALUECAST_ = CAST(XMLValue AS XML),
@XMLVALUECONVERT_ = CONVERT(XML, XMLValue)

--CAST(CAST(CAST(XMLValue AS NTEXT) AS XML).query('data(/STAFF/GENDER)') AS VARCHAR(10)) AS Gender
FROM
[dbo].[XML_Dummy]

print @XMLVALUECAST_
PRINT @XMLVALUECONVERT_

我得到错误 -消息 9402,级别 16,状态 1,第 3 行XML解析:第1行,第38个字符,无法切换编码。

我正在使用 SQL Server 2012 (11.0.5058.0)

最佳答案

首先,为什么不以 XML 格式保存该列?另一件事是您在标签中有错误。我相信它应该是 STAFF,而不是 STAFFv。最后一件事是使用 UTF-8 而不是 UTF-16 进行 XML 编码。所以最终的代码是:

CREATE TABLE #XML_Dummy
(
[ID] [INT] IDENTITY(1, 1)
NOT NULL ,
[Name] [NVARCHAR](50) NULL ,
[XMLValue] [NVARCHAR](MAX) NULL
);

INSERT INTO #XML_Dummy
( [Name] ,
[XMLValue]
)
VALUES
( 'abcd' ,
'<?xml version="1.0" encoding="UTF-8"?>
<STAFF xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EMPLOYEE_NUMBER>123456</EMPLOYEE_NUMBER>
<TITLE>Mr</TITLE>
<INITIALS>J</INITIALS>
<FORENAME>PEARL</FORENAME>
<SURNAME>HOFFMAN</SURNAME>
<GENDER>MALE</GENDER>
<DATE_OF_BIRTH>1992-01-01</DATE_OF_BIRTH>
<DEPARTMENT_DESC>SUPER SUPPORT TEAM</DEPARTMENT_DESC>
<JOB_TITLE_DESC>GENERAL DOGSBODY</JOB_TITLE_DESC>
<ORIGINAL_DATE_JOINED>2014-05-01</ORIGINAL_DATE_JOINED>
<CURRENT_EMPLOYEE financialyear="2014">Y</CURRENT_EMPLOYEE>
<INTERNAL_EMAIL xsi:nil="true" />
<CHANGE_TYPE>INSERT</CHANGE_TYPE>
<CHANGE_DATE>2014-03-27</CHANGE_DATE>
</STAFF>'
);

SELECT
b.x.value('/STAFF[1]/GENDER[1]', 'varchar(100)')
FROM
#XML_Dummy a
CROSS APPLY (
SELECT
CAST(CAST ([XMLValue] AS VARCHAR(MAX)) AS XML) x
) b;
DROP TABLE #XML_Dummy;

关于sql-server - 将 SQL NVARCHAR(MAX) 转换为 XML 并从 XML 字符串中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29946777/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com