简体   繁体   中英

Transform xml to sql table

Based on the following link : we can transform a table to xml

Table:

USE AdventureWorks2012;  
  GO  
  SELECT ProductModelID, Name  
  FROM Production.ProductModel  
  WHERE ProductModelID=122 or ProductModelID=119  
  FOR XML RAW, XMLSCHEMA  
  GO 

XML:

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1"         
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" 
elementFormDefault="qualified">  
<xsd:import namespace="http://schemas.microsoft.com/sqlserver /2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />  
 <xsd:element name="row">  
<xsd:complexType>  
  <xsd:attribute name="ProductModelID" type="sqltypes:int" use="required" />  
  <xsd:attribute name="Name" use="required">  
    <xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">  
      <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">  
        <xsd:maxLength value="50" />  
      </xsd:restriction>  
    </xsd:simpleType>  
  </xsd:attribute>  
  </xsd:complexType>  
 </xsd:element>  
</xsd:schema>  
   <row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1" ProductModelID="122" Name="All-Purpose Bike Stand" />  

<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1" ProductModelID="119" Name="Bike Wash" />  

Based on previous xml, how can we create the sql table in order to insert the data back to table: Production.ProductModel ? I need a general way which will work for all tables and not for a specific table.

For example, I need something like the following but more clever (For example, to retrieve the data about the columns names and their types from xmlschema.)

DECLARE   @xml XML
    , @table NVARCHAR(50)

DECLARE   @COLUMNS NVARCHAR(MAX)
    , @COLUMNSBYTYPE NVARCHAR(MAX)

SET @xml = '<row><ProductModelID>1</ProductModelID><Name>Test</Name></row>
        <row><ProductModelID>2</ProductModelID><Name>Test2</Name></row>'

SET @table = 'Production.ProductModel'

-- get columns name and their types
SET @COLUMNSBYTYPE = stuff(
                        (
                            select ',T.X.value(''('+C.Name+'/text())[1]'', ''' + 'nvarchar(max)' + ''') as '+C.Name
                            from @xml.nodes('row[1]/*') as T(X)
                                cross apply (
                                                select T.X.value('local-name(.)', 'nvarchar(max)')
                                            ) as C(Name)
                                cross apply (
                                                select DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME ='' + @table + '' and COLUMN_NAME = C.Name 
                                            ) as CType
                            ORDER BY C.Name
                            for xml path(''), type
                        ).value('text()[1]', 'nvarchar(max)')
                        , 1, 1, '')


-- transfrom xml to sql table
DECLARE @SQL nvarchar(max)
SET @SQL =  'select ' + @COLUMNSBYTYPE +
        ' from @x.nodes(''/row'') as T(X)'
exec sp_executesql @SQL, N'@x xml', @x = @xml

Are you looking for an app which can do this with ease? If Yes, then you can try Total XML Converter which can convert the xml to sql in batches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM