create table tb(DepartID INT,DepartName VARCHAR(20),ParentID INT)
insert tb select 1,'dname',2
union all select 2,'ename',3
union all select 3,'fname',3
union all select 4,'gname',3
union all select 5,'hname',3
go
--输出为xml文件,文件名为: c:\a.xml
exec master..xp_cmdshell 'bcp "select ''<root>'' union all select '' <Deprat DepartID=''+char(34)+''''+cast(DepartID as varchar)+''''+char(34)+'' DepartName=''+char(34)+''''+rtrim(DepartName)+''''+char(34)+'' ParentID=''+char(34)+''''+cast(ParentID as varchar)+''''+char(34)+'' />'' from tempdb.dbo.tb union all select ''</root>''" queryout "c:\a.xml" /U"sa" /P"" /c'
go