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
*/

1 comment:

  1. Thank you for this useful tutorial on SQ. I need to create table automatically when user clicks on an HTML button. Can you please give me any ideas to accomplish such task?

    Regards,
    Crish Watson
    Pass Microsoft Certification Without Taking Exam

    ReplyDelete