tsql - Using XML with Transact-SQL -



tsql - Using XML with Transact-SQL -

i'm working in sql server 2008 r2. have 20 databases same structure. i'm not exclusively sure went wrong code. if point out error or point me few resources on subject appreciated.

the code i'm running is:

create table #temp ([client] varchar(100), [language_code] char(100)) exec sp_msforeachdb ' if ''?'' ''%'' , exists(select * ?.sys.tables t t.name = ''licenceinfoes'') begin declare @myxml xml declare @lang_code char(100) set @lang_code = ''(select a.b.value''(''laguages[1]'',''char(100)'')'' @myxml.nodes(''licence'') a(b))'' insert #temp select ''?'', @lang_code end ' select * #temp drop table #temp

the end result error:

msg 102, level 15, state 1, line 7 wrong syntax near 'laguages[1]'.

the desired output table 2 columns this:

<db_name> <language_codes> name1 en-gb, en-us

the original query works fine when executed on own , gives me info i'm looking run against of dbs @ 1 time save me time , allow automation of process.

declare @myxml xml set @myxml = (select licencedata licenceinfoes id='1001') select a.b.value('laguages[1]', 'char(100)') language_codes @myxml.nodes('licence') a(b)

and sample xml stored within 1 of dbs

<licence xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <clients /> <lastupdated>2014-06-03t15:59:46.9831585z</lastupdated> <customerid>9999999</customerid> <customername>xxxxx</customername> <contactemail>someone@somewhere.com</contactemail> <type>commercial</type> <languages> <language>en-gb</language> <language>en-us</language> </languages> <defaultlanguagepack>en-gb</defaultlanguagepack>

any help here appreciated.

edit

after trying out few things, found error located.

the error in @myxml variable:

set @myxml = ''(select licencedata t.licenceinfoes)''

changing t.licenceinfoes ?..licenceinfoes fixed issue having.

does help?

declare @tbl table (id int not null, xmlcontent xml) insert @tbl values(1, '<licence xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <clients /> <lastupdated>2014-06-03t15:59:46.9831585z</lastupdated> <customerid>9999999</customerid> <customername>xxxxx</customername> <contactemail>someone@somewhere.com</contactemail> <type>commercial</type> <languages> <language>en-gb</language> <language>en-us</language> </languages> <defaultlanguagepack>en-gb</defaultlanguagepack></licence>') select id, xt.xc.value('(.)[1]', 'varchar(50)') @tbl cross apply xmlcontent.nodes('/licence/languages/language') xt(xc)

this produces output of:

id (no column name) 1 en-gb 1 en-us

tsql xml-parsing sql-server-2008-r2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -