johnnyg123
asked on
openxml
Trying to run the following in sql server 2008
DECLARE @idoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<runparameters>Daily Data Extract - 2014-08-13 : 2014-08-13</runparameters>
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3 F21BB8E252 " orgid="128313">
<attributes>
<attribute name="Organization">GV</at tribute>
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im portedattr ibute>
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i mportedatt ribute>
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes t.com</imp ortedattri bute>
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10 T00:00:00< /importeda ttribute>
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i mportedatt ribute>
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe dattribute >
<attribute name="Sent Date">2014-08-12T14:15:59. 403</attri bute>
<attribute name="Submitted Date">2014-08-13T00:00:00< /attribute >
<attribute name="Survey Score">61.046511627906973< /attribute >
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r esponse>
</responses>
</participant>
</participants>
</dataextract>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, 'dataextract/responses/res ponse',2)
WITH (questionid varchar(10),
orgid varchar(20),
response varchar(1000));
I was hoping results would be
questionID orgid response
360 128313 Pleasure
unfortunately, I am getting null
Can someone please tell me what I'm missing
DECLARE @idoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<runparameters>Daily Data Extract - 2014-08-13 : 2014-08-13</runparameters>
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3
<attributes>
<attribute name="Organization">GV</at
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe
<attribute name="Sent Date">2014-08-12T14:15:59.
<attribute name="Submitted Date">2014-08-13T00:00:00<
<attribute name="Survey Score">61.046511627906973<
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r
</responses>
</participant>
</participants>
</dataextract>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, 'dataextract/responses/res
WITH (questionid varchar(10),
orgid varchar(20),
response varchar(1000));
I was hoping results would be
questionID orgid response
360 128313 Pleasure
unfortunately, I am getting null
Can someone please tell me what I'm missing
ASKER
I appreciate the answer
I apologize but I am a total xml newbie
I got my desired result
questionID orgid response
360 128313 Pleasure
using
DECLARE @hdoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<runparameters>Daily Data Extract - 2014-08-13 : 2014-08-13</runparameters>
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3 F21BB8E252 " orgid="128313">
<attributes>
<attribute name="Organization">GV</at tribute>
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im portedattr ibute>
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i mportedatt ribute>
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes t.com</imp ortedattri bute>
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10 T00:00:00< /importeda ttribute>
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i mportedatt ribute>
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe dattribute >
<attribute name="Sent Date">2014-08-12T14:15:59. 403</attri bute>
<attribute name="Submitted Date">2014-08-13T00:00:00< /attribute >
<attribute name="Survey Score">61.046511627906973< /attribute >
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r esponse>
</responses>
</participant>
</participants>
</dataextract>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@hdoc, '/dataextract/dataextract/ participan ts/partici pant/respo nses/respo nse',1)
WITH (questionid varchar(10),
orgid varchar(20),
response ntext 'text()')
EXEC sp_xml_removedocument @hDoc
The one thing I forgot to mention is that I need to tie the question response to the participant via the Loyalty ID which in the example is 7000482
So I need the result to be
LoyaltyId questionID orgid response
7000482 360 128313 Pleasure
Not sure how to do this
I apologize but I am a total xml newbie
I got my desired result
questionID orgid response
360 128313 Pleasure
using
DECLARE @hdoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<runparameters>Daily Data Extract - 2014-08-13 : 2014-08-13</runparameters>
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3
<attributes>
<attribute name="Organization">GV</at
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe
<attribute name="Sent Date">2014-08-12T14:15:59.
<attribute name="Submitted Date">2014-08-13T00:00:00<
<attribute name="Survey Score">61.046511627906973<
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r
</responses>
</participant>
</participants>
</dataextract>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@hdoc, '/dataextract/dataextract/
WITH (questionid varchar(10),
orgid varchar(20),
response ntext 'text()')
EXEC sp_xml_removedocument @hDoc
The one thing I forgot to mention is that I need to tie the question response to the participant via the Loyalty ID which in the example is 7000482
So I need the result to be
LoyaltyId questionID orgid response
7000482 360 128313 Pleasure
Not sure how to do this
Use a cte for selecting and do the filtering on the result
;with cteXml as(SELECT *
FROM OPENXML (@hdoc, '/dataextract/dataextract/participants/participant/responses/response',1)
WITH (questionid varchar(10),
orgid varchar(20),
response ntext 'text()')
)
select * from cteXML where orgid = '128313';
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you tell me when I run
DECLARE @hdoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3 F21BB8E252 " orgid="128313">
<attributes>
<attribute name="Organization">GV</at tribute>
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im portedattr ibute>
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i mportedatt ribute>
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes t.com</imp ortedattri bute>
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10 T00:00:00< /importeda ttribute>
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i mportedatt ribute>
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe dattribute >
<attribute name="Sent Date">2014-08-12T14:15:59. 403</attri bute>
<attribute name="Submitted Date">2014-08-13T00:00:00< /attribute >
<attribute name="Survey Score">61.046511627906973< /attribute >
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r esponse>
<response questionid="10306" orgid="128352">50</respons e>
</responses>
</participant>
</participants>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
with cte as(SELECT *
FROM OPENXML (@hdoc, '/dataextract/participants /participa nt/attribu tes/import edattribut e/@name')
WITH (questionid varchar(10) '../../../responses/respon se/@questi onid' ,
orgid varchar(20) '../../../responses/respon se/@orgid' ,
response ntext '../../../responses/respon se/text()'
,attrname varchar(20) '.'
,attrId varchar(20) '../@id'
,attrValue varchar(20) '../text()'
)
)
select * from cte where attrid = 'LoyaltyID'
and attrValue = '7000482'
EXEC sp_xml_removedocument @hDoc
why I only get the first question id
questionid orgid response attrname attrId attrValue
360 128313 Pleasure Loyalty ID LoyaltyID 7000482
I was hoping to get
questionid orgid response attrname attrId attrValue
360 128313 Pleasure Loyalty ID LoyaltyID 7000482
10306 128352 50 Loyalty ID LoyaltyID 7000482
DECLARE @hdoc int, @doc varchar(max);
SET @doc ='
<dataextract>
<organizations>
<organization id="128313" name="GVC" type="Unit" />
</organizations>
<questions>
<question id="360" text="Info: What Prompted Your Visit?" />
</questions>
<participants>
<participant id="D1CE7DB6CB261B815CD1D3
<attributes>
<attribute name="Organization">GV</at
<importedattribute name="First Name" id="FirstName" datatype="Char">VERINO</im
<importedattribute name="Last Name" id="LastName" datatype="Char">MESSERE</i
<importedattribute name="Email Address" id="EmailAddress" datatype="Char">golfvj@tes
<importedattribute name="Date In" id="ServiceStartDate" datatype="Date">2014-08-10
<importedattribute name="Loyalty ID" id="LoyaltyID" datatype="Char">7000482</i
<importedattribute name="Loyalty level" id="LoyaltyLevel" datatype="Char">3</importe
<attribute name="Sent Date">2014-08-12T14:15:59.
<attribute name="Submitted Date">2014-08-13T00:00:00<
<attribute name="Survey Score">61.046511627906973<
</attributes>
<responses>
<response questionid="360" orgid="128313">Pleasure</r
<response questionid="10306" orgid="128352">50</respons
</responses>
</participant>
</participants>
</dataextract>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
with cte as(SELECT *
FROM OPENXML (@hdoc, '/dataextract/participants
WITH (questionid varchar(10) '../../../responses/respon
orgid varchar(20) '../../../responses/respon
response ntext '../../../responses/respon
,attrname varchar(20) '.'
,attrId varchar(20) '../@id'
,attrValue varchar(20) '../text()'
)
)
select * from cte where attrid = 'LoyaltyID'
and attrValue = '7000482'
EXEC sp_xml_removedocument @hDoc
why I only get the first question id
questionid orgid response attrname attrId attrValue
360 128313 Pleasure Loyalty ID LoyaltyID 7000482
I was hoping to get
questionid orgid response attrname attrId attrValue
360 128313 Pleasure Loyalty ID LoyaltyID 7000482
10306 128352 50 Loyalty ID LoyaltyID 7000482
ASKER
I think you are correct that xquery may be more beneficial
I will close this question and open one for xquery question
I will close this question and open one for xquery question
Open in new window
and that you miss some levelsOpen in new window