Showing posts with label xml To SQL.. Show all posts
Showing posts with label xml To SQL.. Show all posts

Monday, July 27, 2009

Create sql table from XML.

/*If you want to Create a temporary table from XML in Sql server then
refet the following code

*/
DECLARE @hDoc int
declare @XML xml

/*Following is the sample XML*/
set @XML =
'<root>'+
'<RestaurantBookingInfo>'+
'<dBookingDate>11-11-2009</dBookingDate>'+
'<nRestaurantId>119</nRestaurantId>'+
'<cRestaurantName>The Red Launge</cRestaurantName>'+
'</RestaurantBookingInfo>'+
'</root>'


EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT
*
FROM
OPENXML (@hDoc, 'root/RestaurantBookingInfo',2)
WITH
(
dBookingDate DATETIME,
nRestaurantId INT,
cRestaurantName NVARCHAR(100)
)

EXEC sp_xml_removedocument @hDoc

/*It will generate table like this

dBookingDate nRestaurantId cRestaurantName
----------------------- ------------- ------------------------
2009-11-11 00:00:00.000 119 The Red Launge
*/