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. Stored Procedure problem >>

Stored Procedure problem >>

Scheduled Pinned Locked Moved Database
helpdatabase
5 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.
  • K Offline
    K Offline
    kindman_nb
    wrote on last edited by
    #1

    Hay , I edit a stored procedure to select a Name column from various tables as the next : Create PROCEDURE [dbo].[SelectName] @table_name sysname, @sName nvarchar(50) AS exec ( ' SELECT * FROM ' + @table_name + ' WHERE Name =' + @sName ) then when i execute it and pass the next valriables @table_name = 'emp' @sName = 'NameTest' the next error message appear Invalid column name 'NameTest' what the error here ,, thanks

    jooooo

    S R 2 Replies Last reply
    0
    • K kindman_nb

      Hay , I edit a stored procedure to select a Name column from various tables as the next : Create PROCEDURE [dbo].[SelectName] @table_name sysname, @sName nvarchar(50) AS exec ( ' SELECT * FROM ' + @table_name + ' WHERE Name =' + @sName ) then when i execute it and pass the next valriables @table_name = 'emp' @sName = 'NameTest' the next error message appear Invalid column name 'NameTest' what the error here ,, thanks

      jooooo

      S Offline
      S Offline
      scottgp
      wrote on last edited by
      #2

      If you add a print statement to your stored procedure, such as: print ' SELECT * FROM ' + @table_name + ' WHERE Name =' + @sName and execute it, you'll see this as output: SELECT * FROM emp WHERE Name =NameTest Msg 207, Level 16, State 1, Line 1 Invalid column name 'NameTest'. From there you can see that it's trying to compare the Name column to what it interprets as another column name - not a text literal. You need to put the literal in quotes, so your procedure would need to look like: exec ( ' SELECT * FROM ' + @table_name + ' WHERE Name =''' + @sName + '''') That being said, that code is dangerous, and you should read up on SQL injection, e.g. SQL Injection Attacks and Some Tips on How to Prevent Them[^] Scott

      K 1 Reply Last reply
      0
      • K kindman_nb

        Hay , I edit a stored procedure to select a Name column from various tables as the next : Create PROCEDURE [dbo].[SelectName] @table_name sysname, @sName nvarchar(50) AS exec ( ' SELECT * FROM ' + @table_name + ' WHERE Name =' + @sName ) then when i execute it and pass the next valriables @table_name = 'emp' @sName = 'NameTest' the next error message appear Invalid column name 'NameTest' what the error here ,, thanks

        jooooo

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        Err, the table 'emp' doesn't have a column 'NameTest'? You want to try and avoid that dynamic sql stuff. No query plans and not safe.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        • S scottgp

          If you add a print statement to your stored procedure, such as: print ' SELECT * FROM ' + @table_name + ' WHERE Name =' + @sName and execute it, you'll see this as output: SELECT * FROM emp WHERE Name =NameTest Msg 207, Level 16, State 1, Line 1 Invalid column name 'NameTest'. From there you can see that it's trying to compare the Name column to what it interprets as another column name - not a text literal. You need to put the literal in quotes, so your procedure would need to look like: exec ( ' SELECT * FROM ' + @table_name + ' WHERE Name =''' + @sName + '''') That being said, that code is dangerous, and you should read up on SQL injection, e.g. SQL Injection Attacks and Some Tips on How to Prevent Them[^] Scott

          K Offline
          K Offline
          kindman_nb
          wrote on last edited by
          #4

          Thanks for reply I use print as you said <code>print ' SELECT * FROM ' + @table_name + ' WHERE Name ="' + @sName + '"' it's ok no errors ,, but no rows are returned ,, for eg @sName = "Jooo" this name is already exit but no rows return ,, I test the same statement but with another column type <code>print ' SELECT * FROM ' + @table_name + ' WHERE ID =' + @iID @iID = 1 it's ok and return the the required row, what the diff between nvarchar and int column to get the int and nvarchar no

          jooooo

          S 1 Reply Last reply
          0
          • K kindman_nb

            Thanks for reply I use print as you said <code>print ' SELECT * FROM ' + @table_name + ' WHERE Name ="' + @sName + '"' it's ok no errors ,, but no rows are returned ,, for eg @sName = "Jooo" this name is already exit but no rows return ,, I test the same statement but with another column type <code>print ' SELECT * FROM ' + @table_name + ' WHERE ID =' + @iID @iID = 1 it's ok and return the the required row, what the diff between nvarchar and int column to get the int and nvarchar no

            jooooo

            S Offline
            S Offline
            scottgp
            wrote on last edited by
            #5

            The print statement that I mentioned was just to help you debug what was happening. Did you also change your exec statement as I recommended? The difference between the nvarchar and int columns is that to find something in the nvarchar column you need to specify a text literal, which needs to be in quotes. If it's not in quotes it's going to interpret it as another column name. To find something in the int column you specify a numeric literal, which doesn't need to be in quotes, and the numerical literal won't be interpreted as a column name. Scott

            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