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>\nOutput:<\/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>\nTable 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":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-sqlserverpedia-syndication","category-t-sql-code-examples"],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"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}]}}