我有以下查询,它接受 XML 输入并将它们存储到临时表中。我需要使查询动态或动态调整任何表或列号修改的函数。我该怎么做:
DECLARE @sXML NVARCHAR(4000)
DECLARE @hDoc INT
SET @sXML = '
<Root>
<Item>
<Key>1</Key>
<Test1>A</Test1>
<Test2>A2</Test2>
</Item>
<Item>
<Key>2</Key>
<Test1>B</Test1>
<Test2>B3</Test2>
</Item>
</Root>
'
CREATE TABLE #tabletest
(
[Key] INT,
Test1 NVARCHAR(50),
Test2 NVARCHAR(50)
)
EXEC sp_xml_preparedocument @hDoc OUTPUT, @sXML
INSERT INTO #tabletest SELECT * FROM OPENXML (@hDoc, '/Root/Item', 2) WITH #tabletest
EXEC sp_xml_removedocument @hDoc
select * from #tabletest
DROP TABLE #tabletest