select * from table for xml auto
查询结果为:<action id="1" />select * from table1 for xml auto
查询结果为:<company cid="51" sintime="2006-05-12" spantime="5" />
怎么合并上面的两个查询结果为一个,如:
<action id="1" />
<company cid="51" sintime="2006-05-12" spantime="5" />

解决方案 »

  1.   

    准备
    Create table Tb_ Customer (id int,name nvarchar(10),Sex nvarchar(10),Age int)
    insert into tb_Customer (Name,Sex,Age)(
     select '张三' as name,'男' as Sex,20 as Age union all
     select '李三' as name,'男' as Sex,20 as Age union all
     select '王三' as name,'男' as Sex,20 as Age union all
     select '赵三' as name,'女' as Sex,19 as Age 
    )Create table Tb_contact (id int,custid int,phone nvarchar(30),typeid int)
    insert into Tb_contact (id,custid,phone,typeid)(
     select 1 as id,1,'1376049500' as phone,1 as typeid union all
     select 2 ,2,'1376049500' ,1 union all
     select 3 ,3,'1376049500' ,1 union all
     select 4 ,4,'1376049500' ,1
    类似方案
    SELECT id as "custid",
          (SELECT id as "contid"
           FROM Tb_contact "contact"
           WHERE "contact".custid = Customer.ID
           FOR XML AUTO, TYPE)     
    FROM Tb_Customer Customer
    FOR XML AUTO, TYPE