ImportantHappyCustomer.'--Createaninternalrepresentat" />

sqlserver解析xml

/********************* 导入xml 文件*****************/

DECLARE @idoc int
DECLARE @doc varchar(1000)
--sample XML document
SET @doc ='



Customer was very satisfied




Important
Happy Customer.




'

-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

-- Execute a SELECT statement using OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/root/Customer/Order', 1)
WITH (oid char(5),
amount float,
comment ntext 'text()')
EXEC sp_xml_removedocument @idoc


DECLARE @idoc int
DECLARE @doc varchar(1000),@oldTag varchar(500),@tag varchar(500)
SET @doc ='




'

-- 直接使用 xml 类型的 value 方法就可以了
DECLARE
@xml xml
SELECT
@xml = CONVERT(xml, @doc)
SELECT
@xml.value('(/root/tag/@name)[1]', 'nvarchar(100)'),
@xml.value('(/root/tag/@name)[2]', 'nvarchar(100)')




DECLARE @idoc int
DECLARE @doc varchar(1000),@oldTag varchar(500),@tag varchar(500)
--sample XML document
SET @doc ='





'
set @tag=''
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
declare cur cursor local
for
SELECT [name] FROM OPENXML (@idoc, '/root/tag', 1) WITH ([name] varchar(50))
open cur
fetch cur into @oldTag
while (@@fetch_status=0)
begin
set @tag = @tag + @oldTag +'|'
fetch next from cur into @oldTag
end
deallocate cur
EXEC sp_xml_removedocument @idoc
print @tag
--艾滋病|健康|

相关主题
相关文档
最新文档