34,838
社区成员




if object_id('test') is not null
drop table test
go
create table test(id int, t_name nchar(1))
go
insert into test
select 1, 'a' union all
select 2, 'b' union all
select 3, 'c' union all
select 4, 'd' union all
select 5, 'e'
go
select *
from test
for xml path('test_record'), root
go
drop table test
go
<root>
<test_record>
<id>1</id>
<t_name>a</t_name>
</test_record>
<test_record>
<id>2</id>
<t_name>b</t_name>
</test_record>
<test_record>
<id>3</id>
<t_name>c</t_name>
</test_record>
<test_record>
<id>4</id>
<t_name>d</t_name>
</test_record>
<test_record>
<id>5</id>
<t_name>e</t_name>
</test_record>
</root>
<?xml version="1.0" encoding="GBK"?>
declare @xml xml
select @xml=(select *
from test
for xml path('test_record'), root
)
select @xml='<?xml version="1.0" encoding="GBK"?>'+cast(@xml as varchar(max))
select @xml