Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql query

sql query

Scheduled Pinned Locked Moved Database
databasehelpcsharp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Unknown Ajanabi
    wrote on last edited by
    #1

    hi, i m using this store procedure in my project ALTER procedure getkeyname(@city sysname,@key varchar(50)) as exec ('select distinct a.keywordname,a.keywordid from keyword As a INNER JOIN ' + @city+ ' As b on a.keywordid=b.keywordid and a.keywordname like ' + @key) but i got an error like.. invalid column name that i entered in @key parameter i want to pass my table name dynamically and parameter name but it accept only @city as a table name parameter and it treat @key as Colunm name like @city but i want to pass @key as parameter. please help me

    no knowledge in .net

    C M 2 Replies Last reply
    0
    • U Unknown Ajanabi

      hi, i m using this store procedure in my project ALTER procedure getkeyname(@city sysname,@key varchar(50)) as exec ('select distinct a.keywordname,a.keywordid from keyword As a INNER JOIN ' + @city+ ' As b on a.keywordid=b.keywordid and a.keywordname like ' + @key) but i got an error like.. invalid column name that i entered in @key parameter i want to pass my table name dynamically and parameter name but it accept only @city as a table name parameter and it treat @key as Colunm name like @city but i want to pass @key as parameter. please help me

      no knowledge in .net

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Dyamic SQL - Something you have to be very careful in constructing because it is so easy to make a mistake and punch a big massive security hole in your database. The way you have constructed the SQL make SQL Server interpret @key as a column name. You have to pass @key as a parameter. Actually, you should look up sp_executesql as it provides more protection against attack. Now, as a further defence against attack you must check that @city relates to a real table first. You can do this by checking against INFORMATION_SCHEMA.TABLES like this:

      IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @city)
      BEGIN
      -- The table exists, we can run the dynamic SQL
      END
      ELSE
      BEGIN
      -- The table does not exist. Perform error processing.
      END

      Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

      1 Reply Last reply
      0
      • U Unknown Ajanabi

        hi, i m using this store procedure in my project ALTER procedure getkeyname(@city sysname,@key varchar(50)) as exec ('select distinct a.keywordname,a.keywordid from keyword As a INNER JOIN ' + @city+ ' As b on a.keywordid=b.keywordid and a.keywordname like ' + @key) but i got an error like.. invalid column name that i entered in @key parameter i want to pass my table name dynamically and parameter name but it accept only @city as a table name parameter and it treat @key as Colunm name like @city but i want to pass @key as parameter. please help me

        no knowledge in .net

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        @key must be in quotes. ALTER procedure getkeyname(@city sysname,@key varchar(50)) as exec ('select distinct a.keywordname,a.keywordid from keyword As a INNER JOIN ' + @city + ' As b on a.keywordid=b.keywordid and a.keywordname like ''' + @key + '''')

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups