Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向SQL Server生成的XML输出添加命名空间_Sql_Sql Server_Xml_Sql Server 2008_Xml Namespaces - Fatal编程技术网

如何向SQL Server生成的XML输出添加命名空间

如何向SQL Server生成的XML输出添加命名空间,sql,sql-server,xml,sql-server-2008,xml-namespaces,Sql,Sql Server,Xml,Sql Server 2008,Xml Namespaces,我使用SQL Server查询(添加在rextester链接中)生成了以下XML输出: 测试仪链接: 有人帮忙吗 您需要使用该子句,例如: ---Fake tables in Stored procedure1 create table #Cdetails(cid int, name varchar(5), age int) insert into #Cdetails values(1001,'John',12), (1002,'Rick',19), (1003,'Diane',25), (10

我使用SQL Server查询(添加在rextester链接中)生成了以下XML输出:

测试仪链接:

有人帮忙吗

您需要使用该子句,例如:

---Fake tables in Stored procedure1
create table #Cdetails(cid int, name varchar(5), age int)
insert into #Cdetails
values(1001,'John',12),
(1002,'Rick',19),
(1003,'Diane',25),
(1004,'Kippy',26)

--Output of Stored procedure 

create table #final(xml_data xml)
insert into #final
select
XML_data =  
    (select ID = cd1.cid,
    details =
        (
        select cd.name,
        cd.age
        from #Cdetails cd
        where cd.cid = cd1.cid
        For XML Path(''), Type)
    from #Cdetails cd1
    For XML Path('Main'));


WITH XMLNAMESPACES ('http://www.samplenamespace.com/json' as json)  
select * from #final
For XML Path('Main')

drop table #Cdetails,#final
注意额外的
语句一起使用时需要的code>


Rextester链接:

谢谢。如何向节点添加类似“json:ValueType=“Number”的属性?我想加12。你也能尝尝吗?明白。我怀疑这个问题是否会因为它是多余的而结束
<Main xmlns:json="http://www.samplenamespace.com/json">
    <ID>1001</ID>
    <details>
        <name>John</name>
        <age>12</age>
    </details>
</Main>
---Fake tables in Stored procedure1
create table #Cdetails(cid int, name varchar(5), age int)
insert into #Cdetails
values(1001,'John',12),
(1002,'Rick',19),
(1003,'Diane',25),
(1004,'Kippy',26)

--Output of Stored procedure 

create table #final(xml_data xml)
insert into #final
select
XML_data =  
    (select ID = cd1.cid,
    details =
        (
        select cd.name,
        cd.age
        from #Cdetails cd
        where cd.cid = cd1.cid
        For XML Path(''), Type)
    from #Cdetails cd1
    For XML Path('Main'));


WITH XMLNAMESPACES ('http://www.samplenamespace.com/json' as json)  
select * from #final
For XML Path('Main')

drop table #Cdetails,#final