{"id":275,"date":"2013-09-28T10:46:07","date_gmt":"2013-09-28T10:46:07","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2013\/09\/28\/how-to-convert-table-data-to-xml-and-vice-versa\/"},"modified":"2013-09-28T10:46:07","modified_gmt":"2013-09-28T10:46:07","slug":"how-to-convert-table-data-to-xml-and-vice-versa","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2013\/09\/28\/how-to-convert-table-data-to-xml-and-vice-versa\/","title":{"rendered":"HOW TO CONVERT TABLE DATA TO XML AND XML TO TABLE"},"content":{"rendered":"

HOW TO CONVERT TABLE DATA TO XML AND VICEVERSA.<\/p>\n

\"Table<\/a><\/p>\n

The below code explains how to convert the data in a table to xml form and then convert the xml back into table data.<\/p>\n

Creating sample table with data:
\n
\nCREATE TABLE tmpEmployee(ID INT,NAME VARCHAR(100))
\nGO<\/code><\/p>\n

INSERT INTO tmpEmployee
\nSELECT 1,'Devi'
\nunion
\nSELECT 2,'Prasad'
\nGO<\/code><\/p>\n


\n<\/code><\/code>SELECT * FROM tmpEmployee
\nGO
\n<\/code><\/p>\n

Below code converts data and schema of table tmpEmployee into XML, Then it uses the XML to convert it back into the table:
\n
\n\/*Below code converts Table SCHEMA & Data to XML*\/<\/code><\/p>\n

DECLARE @TableData XML,@TableSchema XML<\/code><\/p>\n

SELECT @TableSchema = (
\nselect column_name,
\ndata_type,
\ncase(is_nullable)
\nwhen 'YES' then 'true'
\nelse 'false'
\nend as is_nullable,
\nCHARACTER_MAXIMUM_LENGTH as Charlen
\nfrom information_schema.columns [column]
\nwhere table_name = 'tmpEmployee'
\nfor xml auto,root('Table')
\n)<\/code><\/p>\n

SELECT @TableData = (
\nSELECT *
\nFROM tmpEmployee Row
\nFOR XML AUTO, BINARY BASE64,root('TableData')
\n)<\/code><\/p>\n

SELECT @TableSchema,@TableData<\/p>\n

\/*Below code converts XML to Table*\/<\/p>\n

if object_id('tempdb..#XMLColumns') is not null
\ndrop table #XMLColumns<\/p>\n

SELECT x.value('@column_name', 'sysname') AS column_name
\n,x.value('@data_type', 'sysname') AS data_type
\n,x.value('@is_nullable', 'VARCHAR(20)') AS is_nullable
\n,x.value('@Charlen', 'VARCHAR(20)') AS Charlen
\ninto #XMLColumns
\nFROM @TableSchema.nodes('\/Table\/column') TempXML (x)<\/p>\n

select * from #XMLColumns<\/p>\n

DECLARE @SQL nVARCHAR(MAX) = 'SELECT '<\/p>\n

SELECT @SQL = @SQL + '
\nx.value(''@'+column_name+''', '''+data_type+case when Charlen is null then '' else '('+Charlen+')' end + ''''+') AS ['+column_name+'],'
\nfrom #XMLColumns<\/p>\n

SET @SQL = LEFT(@SQL, LEN(@SQL) - 1)<\/p>\n

SELECT @SQL = @SQL + ' FROM @TableData.nodes(''\/TableData\/Row'') TempXML (x)'<\/p>\n

<\/code><\/code><\/code><\/code><\/code><\/code>EXEC sp_executeSQl @SQL,N'@TableData xml',@TableData=@TableData
\n<\/code><\/p>\n

Output:<\/p>\n

tmpEmployee:
\n
\nID NAME
\n----------- ----------------------------------------------------------------------------------------------------
\n1 Devi
\n2 Prasad<\/code><\/p>\n


\n<\/code>(2 row(s) affected)
\n<\/code><\/p>\n

@TableSchema:
\n
\n<Table>
\n<column column_name=\"ID\" data_type=\"int\" is_nullable=\"true\" \/>
\n<column column_name=\"NAME\" data_type=\"varchar\" is_nullable=\"true\" Charlen=\"100\" \/>
\n<\/Table>
\n<\/code><\/p>\n

@TableData:
\n
\n<TableData>
\n<Row ID=\"1\" NAME=\"Devi\" \/>
\n<Row ID=\"2\" NAME=\"Prasad\" \/>
\n<\/TableData>
\n<\/code><\/p>\n

Table generated from XML:
\n
\nID NAME
\n----------- ----------------------------------------------------------------------------------------------------
\n1 Devi
\n2 Prasad<\/code><\/p>\n


\n<\/code>(2 row(s) affected)
\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"

HOW TO CONVERT TABLE DATA TO XML AND VICEVERSA. The below code explains how to convert the data in a table to xml form and then convert the xml back into table data. Creating sample table with data: CREATE TABLE tmpEmployee(ID INT,NAME VARCHAR(100)) GO INSERT INTO tmpEmployee SELECT 1,’Devi’ union SELECT 2,’Prasad’ GO SELECT *…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1789],"tags":[],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/275"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}