Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 XML to Temp Table

Author  Topic 

Sql_forum
Yak Posting Veteran

50 Posts

Posted - 2012-06-29 : 02:19:43
StringXML><
<Color>
<Color1>RED</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>GREEN</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>BLUE</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>WHITE</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>BLACK</Color1>
<Color2>P</Color2>
</Color>
</StringXML>


I have the above xml, how can i fetch data into temp table ?
my table looks like Color1 Color2

and my Color1,Color2 tags are dynamic, they may be 2 at one tym, may be 4 some oter tym..


Pls help me how to oachieve this

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-29 : 07:42:31
[code]INSERT INTO #YourTempTable
( Color1, Color2 )
SELECT
c.value('Color1[1]','varchar(32)') AS Color1,
c.value('Color2[1]','varchar(32)') AS Color2
FROM
@YourXMLVariable.nodes('//Color') T(c);
[/code] If you have more Color nodes, add more columns to the select list.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-29 : 23:16:47
quote:
Originally posted by Sql_forum

StringXML><
<Color>
<Color1>RED</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>GREEN</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>BLUE</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>WHITE</Color1>
<Color2>P</Color2>
</Color>
<Color>
<Color1>BLACK</Color1>
<Color2>P</Color2>
</Color>
</StringXML>


I have the above xml, how can i fetch data into temp table ?
my table looks like Color1 Color2

and my Color1,Color2 tags are dynamic, they may be 2 at one tym, may be 4 some oter tym..


Pls help me how to oachieve this


what can be maximum number of colors that can be present in color nodE?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -