用SQL SERVER 2008的for xml可以读取数据并转为XML,下面例程使用了pubs数据库
DECLARE @idoc intDECLARE @doc varchar(1000) set @doc=''exec sp_xml_preparedocument @idoc OUTPUT,@docselect a.title,a.au_lname,p.pub_name,p.city from openxml(@idoc,'/ROOT/authors/books',2) WITH(title varchar(20) './title', au_id varchar(11) '../au_id', au_lname varchar(40) '../au_lname', au_fname varchar(20) '../au_fname', phone varchar(12) '../phone', city varchar(20) '../city') As a inner join publishers as p ON a.city=p.city use pubsgoselect * from authors for xml raw select * from authors for xml autoselect publishers.pub_name,titles.title,titles.price from titles,publishers where publishers.pub_id=titles.pub_id for xml autoselect 1 As Tag,null as parent, authors.au_fname as [Authors!1!au_fname!element], authors.au_lname as [Authors!1!au_lname!element], NULL AS [Titleauthor!2!Royaltyper], NULL AS [Titles!3!au_fname!element] FROM authorsUNION ALLselect 2 As Tag,1 as parent, au_fname,au_lname,royaltyper,NULL from authors inner join titleauthor on authors.au_id=titleauthor.au_id union all select 3 As Tag,2 as parent, au_fname,au_lname,royaltyper,title from authors inner join titleauthor on authors.au_id=titleauthor.au_id inner join titles on titles.title_id=titleauthor.title_id order by [Authors!1!au_fname!element],[Authors!1!au_lname!element], [Titleauthor!2!Royaltyper],tag for xml explicit 172-32-1176 White Johnson 408 496-7223 1010932 Bigger Rd.Menlo Park CA 94025 1 My Book1 My Book2 213-46-8915 Green Marjorie 415 986-7020 309 St. #31Boston CA 94618 1 My Book3 My Book4