Trying to extract name from xml file

Here is the xml file

<Crew>
    <CrewMembers>
      <CrewMember>
        <CrewName ID="35124">JOHN DOE</CrewName>
        <CrewRole ID="7">Not Applicable</CrewRole>
        <CrewLevel>EMT-Paramedic</CrewLevel>
        <CrewSequence>1</CrewSequence>
      </CrewMember>
    
    </CrewMembers>

Open in new window


Here is the query

DECLARE @PRID_S varchar(10) set @PRID_S='48967264'

DECLARE @FILENAME VARCHAR(30) SET @FILENAME='c:\pcr\archive\'+@PRID_S+'.XML'
Declare @CrewName1 varchar(30)
Declare @Crew1 varchar(10) set @crew1='35124'

IF OBJECT_ID('tempdb..#tempDATA4') IS NOT NULL DROP TABLE #tempDATA4
CREATE TABLE #tempDATA4 (  CrewName1 VARCHAR(20))

INSERT INTO #tempDATA4
exec(
'declare @xml xml 
set @xml=(
  SELECT *
  FROM OPENROWSET(BULK'+ ''''+@FILENAME+''''+', SINGLE_BLOB) AS data
)

select 

@xml.value(''(/PCRDATA/Crew/CrewMembers/CrewMember/CrewName/ID='+ ''''+@Crew1+''''+', ''nvarchar(max)'') as CrewName')

SELECT TOP 1 @CrewName1=CrewName1 FROM #tempDATA4
print(@CrewName1)

Open in new window


The error I am getting is Unclosed quotation mark after the character string ') as CrewName'.  

what am I doing wrong

Thanks
LVL 5
WillOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

kenfcampCommented:
My guess is that

@xml.value(''(/PCRDATA/Crew/CrewMembers/CrewMember/CrewName/ID='+ ''''+@Crew1+''''+', ''nvarchar(max)'') as CrewName')

is the issue

Try changing CrewName' to CrewName
0
WillOwnerAuthor Commented:
Thanks for your reply. Didn't work.   It created errors after CrewName
0
zc2Commented:
I have reformatted a little the string you pass to the exec() :
exec( 'declare @xml xml 
set @xml=(
  SELECT *
  FROM OPENROWSET(BULK'+ ''''+@FILENAME+''''+', SINGLE_BLOB) AS data
)
select 
@xml.value(''/PCRDATA/Crew/CrewMembers/CrewMember/CrewName/ID="'+@Crew1+'"'', ''nvarchar(max)'') as CrewName' )

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

WillOwnerAuthor Commented:
Thank you. That executed but returned a "false" on Print(@Crewname1)
0
zc2Commented:
Please try this:
@xml.query(''/PCRDATA/Crew/CrewMembers/CrewMember/CrewName[@ID="'+@Crew1+'"]'').value(''.'', ''nvarchar(max)'') as CrewName' )

Open in new window

Also, in the the XML you posted, the root element is "Crew". But your XPath expression starts with "PCRDATA". Is "Crew" actually a child of "PCRDATA" ?
1

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. Works Great
0
zc2Commented:
You are welcome.
0
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
Query Syntax

From novice to tech pro — start learning today.