Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:18:22+00:00 2026-05-29T19:18:22+00:00

I have a SQL function that takes a variable called attribute , which is

  • 0

I have a SQL function that takes a variable called attribute, which is the xml attribute I want to get the value from. xmlPath is the full XML string.

My xml looks like this:

<EventSpecificData>
  <Keyword>
    <Word>myWord</Word>
    <Occurences>1</Occurences>
    <Context>context</Context>
  </Keyword>
</EventSpecificData>

I want to get the value for <Word>, so I pass in /Keyword/Word and set a variable to:

set @value = @xmlPath.value('(/EventSpecificData/@attribute)[1]', 'varchar(max)')

However, I don’t think @attribute is actually inserting the variables string. Is there another way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:18:24+00:00Added an answer on May 29, 2026 at 7:18 pm

    Here are a couple of solutions for you.

    Sample data:

    declare @xml xml
    set @xml = 
    '<EventSpecificData>
      <Keyword>
        <Word>myWord</Word>
        <Occurences>1</Occurences>
        <Context>context</Context>
      </Keyword>
    </EventSpecificData>'
    

    Get the first value from node named Word regardless of parents. Use // to do a deep search and use local-name() to match node name.

    declare @Attribute varchar(max)
    
    set @Attribute = 'Word'
    select @xml.value('(//*[local-name() = sql:variable("@Attribute")])[1]',  'varchar(max)')
    

    Provide parent node name and attribute in separate variables using local-name() in two levels.

    declare @Node varchar(max)
    declare @Attribute varchar(max)
    
    set @Attribute = 'Word'
    set @Node = 'Keyword'
    select @xml.value('(/EventSpecificData
                        /*[local-name() = sql:variable("@Node")]
                        /*[local-name() = sql:variable("@Attribute")])[1]',  'varchar(max)')
    

    Since the parameter to nodes have to be a string literal it invites to use dynamic sql to solve this. It could look something like this to make it work with your original variable content.

    set @Attribute = 'Keyword/Word'
    declare @SQL nvarchar(max)
    set @SQL = 'select @xml.value(''(/EventSpecificData/'+@Attribute+')[1]'', ''varchar(max)'')'
    exec sp_executesql @SQL, N'@xml xml', @xml
    

    But you should be aware of that if you use this you are wide open to SQL Injection attacks. Some devious end-user might come up with a attribute string that looks like this:

    set @Attribute = 'Keyword/Word)[1]'', ''varchar(max)'') select @@version --'
    

    Executing the dynamic SQL with that will give you two result sets. The select @@version is just there to show some harmless code but it might be much worse stuff in there.

    You can use quotename() to prevent the SQL injection attack. It will at least prevent the attempt made by me.

    set @Attribute = 'Keyword/Word'
    set @SQL = 'select @xml.value('+quotename('(/EventSpecificData/'+@Attribute+')[1]', '''')+', ''varchar(max)'')'
    exec sp_executesql @SQL, N'@xml xml', @xml
    

    Is the last version using quotename()safe? Have a look at this article by Erland Sommarskog The Curse and Blessings of Dynamic SQL.

    Quote:

    So with quotename() and quotestring(), do we have as good protection
    against SQL injection as we have with parameterised commands? Maybe. I
    don’t know of any way to inject SQL that slips through quotename() or
    quotestring(). Nevertheless, you are interpolating user input into the
    SQL string, whereas with parameterised commands, you don’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and
I have created a function that takes a SQL command and produces output that
I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and
I have an MS SQL function that is called with the following syntax: SELECT
I have an user defined table function in SQL Server that aggregate data from
I have a user defined function in SQL called getBuisnessDays it takes @startdate and
I have to create a pl/sql function which takes seven input parameters. Problem is
I have an SQL Function that takes a String, a DATE and another string.
I have a function, parseQuery, that parses a SQL query into an abstract representation
I have a PL/SQL function (running on Oracle 10g) in which I update some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.