Having trouble with xml.query

I am using xml. query on the following xml

This node is the child of /PCRDATA/Activity
<Procedures>
      <Procedure>
        <EnteredBy ID="586807">XXXXXXXXXX</EnteredBy>
        <MedicationDosage>100</MedicationDosage>
        <MedicationDosageUnit ID="2835">MG</MedicationDosageUnit>
        <MedicationName ID="92">Ketamine</MedicationName>
        <MedicationRoute ID="2">IV - Push</MedicationRoute>
        <Procedure ID="Medication">Medication</Procedure>
        <ProcedureResponse ID="2">Unchanged</ProcedureResponse>
        <Comments>Ketamine, 100 MG via IV - Push given by xxxxxxxxxxxxxxxxxxxxxx). Pt. Response: Unchanged. </Comments>
        <ConsultEventID>xxxxxxxx</ConsultEventID>
        <EventID>2xxxxxxx</EventID>
        <ProcedureAuthorization>XXXXXXXXXX</ProcedureAuthorization>
        <ProcedureCrew>XXXXXXXXX</ProcedureCrew>
        <ProcedureDate>2018-01-17T19:53:00</ProcedureDate>
      </Procedure>

Open in new window


my query is as follows

value of @MedicationNameID1="92"


exec( 'declare @xml xml 
set @xml=(
  SELECT *
  FROM OPENROWSET(BULK'+ ''''+@FILENAME+''''+', SINGLE_BLOB) AS data
)
select 
@xml.query(''/PCRDATA/General/Outcome[@ID="'+@Disposition+'"]'').value(''.'', ''varchar(50)'') as DispDesc,
@xml.query(''/PCRDATA/General/DispatchedAs[@ID="'+@Nature+'"]'').value(''.'', ''varchar(50)'') as NatureDesc ,
@xml.query(''/PCRDATA/General/MedicalCategory[@ID="'+@MedCat+'"]'').value(''.'', ''varchar(50)'') as MedCatDesc,
@xml.query(''/PCRDATA/Actvity/Procedures/Procedure[1]/MedicationName[@ID="'+@MedicationNameID1+'"]'').value(''.'', ''varchar(50)'') as MedicationName1
' )

Open in new window


The value of MedicationName1 is blank.  I am expecting ketamine

Can someone point me in the right direction,  I am baffled.

Thank you
LVL 5
WillOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
First of all: Post concise and complete examples. This means complete and especially well-formed XML. It took me several minutes to correct your sloppiness.

Then don't use dynamic SQL as long as you don't need to. Thus said, don't parse the XML in dynamic SQL. It's not necessary.

This works:
DECLARE @Sample XML = N'
<PCRDATA>
    <Activity>
        <Procedures>
            <Procedure>
                <EnteredBy ID="586807">XXXXXXXXXX</EnteredBy>
                <MedicationDosage>100</MedicationDosage>
                <MedicationDosageUnit ID="2835">MG</MedicationDosageUnit>
                <MedicationName ID="92">Ketamine</MedicationName>
                <MedicationRoute ID="2">IV - Push</MedicationRoute>
                <Procedure ID="Medication">Medication</Procedure>
                <ProcedureResponse ID="2">Unchanged</ProcedureResponse>
                <Comments>Ketamine, 100 MG via IV - Push given by xxxxxxxxxxxxxxxxxxxxxx). Pt. Response: Unchanged. </Comments>
                <ConsultEventID>xxxxxxxx</ConsultEventID>
                <EventID>2xxxxxxx</EventID>
                <ProcedureAuthorization>XXXXXXXXXX</ProcedureAuthorization>
                <ProcedureCrew>XXXXXXXXX</ProcedureCrew>
                <ProcedureDate>2018-01-17T19:53:00</ProcedureDate>
            </Procedure>
        </Procedures>
    </Activity>
</PCRDATA>
';

SELECT A.MedicationName.value('.', 'NVARCHAR(255)') AS MedicationName ,
       A.MedicationName.value('@ID', 'INT') AS MedicationID
FROM   @Sample.nodes('/PCRDATA/Activity/Procedures/Procedure/MedicationName') A(MedicationName)
WHERE  A.MedicationName.exist('@ID=92') = 1;

Open in new window


But its incomplete due missing context.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WillOwnerAuthor Commented:
Thank you for responding and the constructive criticism.  The reason I was using the approach was that there are thousands of possible permutations. That's why I grab the id first the pull out the useful data.  If I was to stay with dynamic query what is syntacically wrong with my code? Thanks
0
WillOwnerAuthor Commented:
Ok   I can work with your solution,  thank you
0
WillOwnerAuthor Commented:
Thank you
0
ste5anSenior DeveloperCommented:
You need the dynamic SQL only for the BULK import, when your file names vary. Use it to import the files into a table.

For parsing and filtering the XML, you should use sql:variable() function in your XPath expressions.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.