我编写了一个将表数据导出到 XML 文件的 CLR 程序集。现在我想将此数据导入另一个实例的临时表中。 XML文件结构是这样的:
<row>
<SystemInformationID>1</SystemInformationID>
<Database_x0020_Version>10.00.80404.00</Database_x0020_Version>
<VersionDate>2008-04-04T00:00:00</VersionDate>
<ModifiedDate>2008-04-04T00:00:00</ModifiedDate>
</row>
我希望在目标位置解析 XML 并将其导入到临时表中。我也有主表,所以我可以从那里得到表结构。 有办法吗?我使用 OPENXML,但它似乎无法正常工作。我可以将 XML 文件读入表中,该表将存储在 XML 数据类型的列中。我的问题是解析该列中的数据。 这是一个临时尝试:
CREATE TABLE ##T (IntCol int, XmlCol xml)
GO
INSERT INTO ##T(XmlCol)
SELECT * FROM OPENROWSET(
BULK 'c:\HISOutput.xml',
SINGLE_CLOB) AS x
--works correctly up to this point
DECLARE @x xml
DECLARE @id int
SELECT @x=XmlCol FROM ##T
EXEC sp_xml_preparedocument @id OUTPUT, @x
SELECT *
FROM OPENXML (@id,'/row',2)
WITH
dbo.awbuildversion
--I used dbo.awbuildversion table from AdventureWorks DB for testing
this doesn't show the first column no matter how I change the OPENXML instruction.
提前交易
最佳答案
我不太确定您想要什么,因为您对 OMG Ponies 的回答的评论与您的问题不同。临时表/表结构有什么问题?
无论如何,由于存在内存泄漏风险,我不会在 SQL Server 2050 及更高版本上使用 OPENXML
或 sp_xml_preparedocument
(您之所以假设是因为您提到了 CLR) .
此外,如果您需要表结构,则可以使用 INTO #tempTable
DECLARE @foo xml
SET @foo = '<row>
<SystemInformationID>1</SystemInformationID>
<Database_x0020_Version>10.00.80404.00</Database_x0020_Version>
<VersionDate>2008-04-04T00:00:00</VersionDate>
<ModifiedDate>2008-04-04T00:00:00</ModifiedDate>
</row>'
SELECT
bar.value('./SystemInformationID[1]','INT') AS 'SystemInformationID',
bar.value('./Database_x0020_Version[1]','VARCHAR(14)') AS 'Database_x0020_Version',
bar.value('./VersionDate[1]','DATETIME') AS 'VersionDate',
bar.value('./ModifiedDate[1]','DATETIME') AS 'ModifiedDate'
INTO #tempTable -- This?
FROM
@foo.nodes('/row') AS foo(bar) --use nodes not OPENXML
关于sql - 在 SQL Server 中解析 XML 并将其导入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1814761/