We help IT Professionals succeed at work.
Get Started

xquery question

Last Modified: 2014-09-03
I am running the following in sql server 2008 query analyzer ....(I am a newbie with xquery in sql)

declare @xml xml
       set @xml =
                  <organization id="128313" name="GVC" type="Unit" />
                  <question id="360" text="Info: What Prompted Your Visit?" />
                  <participant id="D1CE7DB6CB261B815CD1D3F21BB8E252" orgid="128313">
                        <attribute name="Organization">GV</attribute>
                              <importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</importedattribute>
                              <importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</importedattribute>
                              <importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@test.com</importedattribute>
                              <importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10T00:00:00</importedattribute>
                              <importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</importedattribute>
                              <importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importedattribute>
                              <attribute name="Sent Date">2014-08-12T14:15:59.403</attribute>
                              <attribute name="Submitted Date">2014-08-13T00:00:00</attribute>
                              <attribute name="Survey Score">61.046511627906973</attribute>
                        <response questionid="360" orgid="128313">Pleasure</response>
                        <response questionid="10306" orgid="128352">50</response>


                   tab.col.value('(@LoyaltyLevel)[1]', 'nvarchar(100)') LoyaltyLevel,
             tab1.col1.value('(@questionid)[1]', 'nvarchar(100)') questionid,
             tab1.col1.value('(@orgid)[1]', 'nvarchar(100)') orgId,
                   tab1.col1.value('.', 'nvarchar(100)') response
       from @xml.nodes('/dataextract/participants/participant/responses') as tab(col)
         cross apply col.nodes('response') as tab1(col1)

I am getting

LoyaltyLevel      questionid      orgId      response
NULL              360                     128313      Pleasure
NULL              10306             128352      50

I would like it to make changes so that it includes the loyalty id that the question is associated with

so that the results are

LoyaltyLevel      questionid      orgId      response
7000482              360                     128313      Pleasure
7000482              10306             128352      50
Watch Question
Top Expert 2012
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE