sqlserverToXML
--raw模式 以属性的形式展示
select * from goods for xml raw
select *from goods for xml raw('goods') --修改节点名称
select *from goods for xml raw('goods') ,root('root'); --增加根节点
--****
select *from goods for xml raw('goods') ,root('root'),elements;--以自节点的形式呈现
--****
--auto模式 不允许修改名称(默认为表名称)
select * from goods for xml auto --默认的以子节点形式展示
select * from goods for xml auto,root('root'),elements --增加根节点 ***
--EXPLICIT
--path
select * from goods for xml path
select * from goods for xml path('goods'),root('root') --修改名称,添加根节点
select gno as num ,gname as name from goods for xml auto,elements,root('root')
select
1 as tag,--强类型
null as parent, --强类型
null as[node!1],--根节点
gno as [node!1!gno!element], --子节点
gname as [node!1!name!element]
from goods for xml explicit
--JSON
select '{"gno":"'+CAST(gno as varchar)+'","gname":"'+CAST(gname as varchar)+'"}'
from goods
for xml path('')
--XMLToSql
declare @doc varchar(1000)
declare @idoc int
set @doc='<root><student id="2" name="admin" age="12" /></root>'
exec sp_xml_preparedocument @idoc output ,@doc
select * from openxml(@idoc,'root/student',1) --1是属性,2是节点
with(id varchar,name varchar,age int)
insert student
select * from openxml(@idoc,'root/student')
with student
exec sp_xml_removedocument @idoc