Skip to main content
Home
Blog

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Forums
  3. Knowledge Base
  4. How-To

MSSQL store procedure to convert from XML to table

By ongetc , 26 February, 2017

Below is the actual code so you can just copy and paste to create the store procedure.


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[spXML2Table]
(
-- output from for xml raw
@XmlDocument XML = '',
@Schema varchar(max) = 'ID INT, InterfaceID int, FldPos varchar(20), FldLen varchar(20), FldName varchar(20), RecType varchar(20)'
)
AS
BEGIN
declare @var_query nvarchar(max)
--create table #T (ID INT Identity(1,1),'+@Schema+')
SET @var_query = '
create table #T ('+@Schema+')
DECLARE @DocHandle int;
DECLARE @XmlDocument XML;
SET @XmlDocument = ''' +CAST(@XmlDocument AS NVARCHAR(max)) + ''';
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument;
insert into #T
SELECT *
FROM OPENXML (@DocHandle, ''/doc/row'',1)
WITH ( '+ @Schema +' );
select * from #T

exec dbo.sp_executesql @statement = @var_query

return
END

Forums
How-To

Reseller hosting (free)

Hosting24 (w/ ssh)

A2 Hosting

Monthly archive

  • September 2009 (14)
  • October 2009 (11)
  • January 2010 (2)
  • June 2010 (3)
  • October 2011 (1)
  • March 2012 (1)
  • July 2012 (1)
  • February 2017 (564)
  • March 2017 (5)
  • April 2017 (2)

Pagination

  • 1
  • Next page

Popular content

Last viewed:

  • Wiki Type FrameWork
  • The Regex Coach
  • Contenido
  • Simple Machines Forum
  • Apache OFBiz®
  • DotNetNuke
  • NPDS
  • Exponent
  • How to install PHP on Windows 2008
  • How to use Drupal multiple sites feature?

User login

  • Reset your password