views:

21

answers:

1

I have a stored proc with a parameter which I want to have contain an XPath expression which can then be used to retrieve a value in a select, but the r.value(...) exception message says that I can only use literals.

Is this true or is there a way around this?

create proc MySproc
    @myxml    xml,
    @xpath    nvarchar(50)
as
begin
    select r.value(@xpath, 'nvarchar(100)') as 'demofield'
    from @myxml.nodes('/*') as records(r)
end

I have also tried things like r.value('sql:column("@xpath")', 'nvarchar(100)')

A: 

Ok, well ultimately I've put it all in a dynamic statement. Which, of course, has caused it's own troubles...

Matt W
Redesigning the query eventually got the problem solved. The answer is Yes, it must be a literal, but as with all my other queries, the best solution is to have a better design. Ultimately, it was better to perform a join on a table produced from the parsed content of the XML.
Matt W