我的输入是 SQL Server 表中的一个 XML 列,如下所示:
<word Entry="Ketab" Affix="miyanvand" Pos="esm" Derv="Jamed" />
期望的输出:这样的表:
Entry | Affix | Pos | Derv
Ketab | miyanvand | esm | Jamed
我的输入是 SQL Server 表中的一个 XML 列,如下所示:
<word Entry="Ketab" Affix="miyanvand" Pos="esm" Derv="Jamed" />
期望的输出:这样的表:
Entry | Affix | Pos | Derv
Ketab | miyanvand | esm | Jamed
如果您使用的是 sql-server 2005+。然后可能是这样的:
DECLARE @xml XML=
'<word Entry="Ketab" Affix="miyanvand" Pos="esm" Derv="Jamed" />'
SELECT
Y.ID.value('(@Entry)[1]', 'Varchar(100)') as [Entry],
Y.ID.value('(@Affix)[1]', 'Varchar(100)') as [Affix],
Y.ID.value('(@Pos)[1]', 'Varchar(100)') as [Pos],
Y.ID.value('(@Derv)[1]', 'Varchar(100)') as [Derv]
FROM @xml.nodes('/word') as Y(ID)
或者你也可以这样做:
DECLARE @xml XML=
'<word Entry="Ketab" Affix="miyanvand" Pos="esm" Derv="Jamed" />'
SELECT
@xml.value('(/word/@Entry)[1]', 'varchar(50)') as Entry,
@xml.value('(/word/@Affix)[1]', 'varchar(50)') as Affix,
@xml.value('(/word/@Pos)[1]', 'varchar(50)') as Pos,
@xml.value('(/word/@Derv)[1]', 'varchar(50)') as Derv
或者,如果你有一张桌子。然后是这样的:
DECLARE @tbl TABLE(ID INT,someXML XML)
INSERT INTO @tbl
VALUES
(1,'<word Entry="Ketab" Affix="miyanvand" Pos="esm" Derv="Jamed" />')
SELECT
tbl.ID,
Y.ID.value('(@Entry)[1]', 'Varchar(100)') as [Entry],
Y.ID.value('(@Affix)[1]', 'Varchar(100)') as [Affix],
Y.ID.value('(@Pos)[1]', 'Varchar(100)') as [Pos],
Y.ID.value('(@Derv)[1]', 'Varchar(100)') as [Derv]
FROM
@tbl AS tbl
CROSS APPLY someXML.nodes('/word') as Y(ID)
您可以value
在 XML 列上使用该函数:
declare @t table (col1 xml)
insert @t values ('<word Entry="Ketab" Affix="miyanvand" />')
select t.col1.value('(/word/@Entry)[1]', 'varchar(50)') as Entry
, t.col1.value('(/word/@Affix)[1]', 'varchar(50)') as Affix
from @t t