Showing posts with label Create Xml after querying on Table. Show all posts
Showing posts with label Create Xml after querying on Table. Show all posts

Monday, August 3, 2009

Create Xml after querying on Table

--How to Create Xml after querying on Table

declare @Emp xml
declare @EmployeesTable table
(Code int, UserName nvarchar(50))
insert into @EmployeesTable values(1,'Nishant')
insert into @EmployeesTable values(2,'Kavita')
set @Emp = (select * from @EmployeesTable where UserName like '%a%' for xml path('Employee'), root('Employees'), type)

--Select * from @EmployeesTable

print(cast(@Emp as nvarchar(4000)))
--NOw if u will see the varibale @emp's Value u will see the XML Like this
/*<Employees>
<Employee>
<Code>1</Code>
<UserName>Nishant</UserName>
</Employee>
<Employee>
<Code>2</Code>
<UserName>Kavita</UserName>
</Employee>
</Employees>
*/