您好我是使用XML文件的新手,
我尝试将文件ICD.xml插入到sql表
中
XML文件格式,例如
<letter>
<title>#</title>
<mainTerm>
<title>1-propanol</title>
<cell col="2">T51.3X1</cell>
<cell col="3">T51.3X2</cell>
<cell col="4">T51.3X3</cell>
<cell col="5">T51.3X4</cell>
<cell col="6">--</cell>
<cell col="7">--</cell>
</mainTerm>
<mainTerm>
<title>2-propanol</title>
<cell col="2">T51.2X1</cell>
<cell col="3">T51.2X2</cell>
<cell col="4">T51.2X3</cell>
<cell col="5">T51.2X4</cell>
<cell col="6">--</cell>
<cell col="7">--</cell>
</mainTerm>
我使用此代码
Declare @xml XML
Select @xml = CONVERT(XML,bulkcolumn,2) FROM OPENROWSET(BULK 'File Path',SINGLE_BLOB) AS X
SET ARITHABORT ON
Insert into dbo.TestXML
(
C1,C3,C4,C5,C6,C7
)
Select
p.value('C1[1]','VARCHAR(100)') AS C1,
P.value('C2[1]','VARCHAR(100)') AS C2,
P.value('C3[1]','VARCHAR(100)') AS C3,
P.value('C4[1]','VARCHAR(100)') AS C4,
P.value('C5[1]','VARCHAR(100)') AS C5,
P.value('C6[1]','VARCHAR(100)') AS C6,
P.value('C7[1]','VARCHAR(100)') AS C7,
From @xml.nodes('/letter/title') PropertyFeed(P)
但我的语法错误 你可以帮助我,如果有另一种方式 日Thnx
答案 0 :(得分:1)
-
IF OBJECT_ID('dbo.TestXML') IS NOT NULL DROP TABLE dbo.TestXML
CREATE TABLE dbo.TestXML
(
C1 varchar(10),
C2 varchar(10),
C3 varchar(10),
C4 varchar(10),
C5 varchar(10),
C6 varchar(10),
C7 varchar(10)
)
DECLARE @xml xml =
'<letter>
<title>#</title>
<mainTerm>
<title>1-propanol</title>
<cell col="2">T51.3X1</cell>
<cell col="3">T51.3X2</cell>
<cell col="4">T51.3X3</cell>
<cell col="5">T51.3X4</cell>
<cell col="6">--</cell>
<cell col="7">--</cell>
</mainTerm>
<mainTerm>
<title>2-propanol</title>
<cell col="2">T51.2X1</cell>
<cell col="3">T51.2X2</cell>
<cell col="4">T51.2X3</cell>
<cell col="5">T51.2X4</cell>
<cell col="6">--</cell>
<cell col="7">--</cell>
</mainTerm>
</letter>'
INSERT dbo.TestXML(C1, C2, C3, C4, C5, C6, C7)
SELECT p.value('(title)[1]', 'VARCHAR(100)') AS C1,
p.value('(cell)[@col=2][1]','VARCHAR(100)') AS C2,
p.value('(cell)[@col=3][1]','VARCHAR(100)') AS C3,
p.value('(cell)[@col=4][1]','VARCHAR(100)') AS C4,
p.value('(cell)[@col=5][1]','VARCHAR(100)') AS C5,
p.value('(cell)[@col=6][1]','VARCHAR(100)') AS C6,
p.value('(cell)[@col=7][1]','VARCHAR(100)') AS C7
FROM @xml.nodes('/letter/mainTerm') PropertyFeed(p)
SELECT *
FROM dbo.TestXML
请参阅SQLFiddle
上的示例