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. Passing Column Names in Stored Procedures.

Passing Column Names in Stored Procedures.

Scheduled Pinned Locked Moved Database
databasehelpregexperformancetutorial
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.
  • P Offline
    P Offline
    Pradhip
    wrote on last edited by
    #1

    Hi: I have this challenge that i am sure many of us would have faced. I want to write a stored procedure that has to list out all company names based on whether one of the characteristic has value 1.. For eg: create procedure (proc_name) @paramcharacteristic varchar(40) as select * from tblcompany where @paramCharacteristic=1 Now this @paramcharacteristic will always match one of the column names in the table like Performance,Satisfaction,Profits and so on.But this does not seem to work. When i run the stored procedure from an application i get the error Cannot convert varchar 'Satisfaction' to integer.So i think what it does is try to compare the value of @paramcharacteristic variable and 1.But instead i want SQL to understand the statement as select * from tblcompany where Satisfaction=1 Where do u think should i make the change.In the definition of paramcharacteristic in the stored procedure??Pls give me some suggestions.On how to deal this situation.. Thanks in advance for the help.. Regards Pradhip.S If a Building is Completed then why do they call it BUILDING ??

    S 1 Reply Last reply
    0
    • P Pradhip

      Hi: I have this challenge that i am sure many of us would have faced. I want to write a stored procedure that has to list out all company names based on whether one of the characteristic has value 1.. For eg: create procedure (proc_name) @paramcharacteristic varchar(40) as select * from tblcompany where @paramCharacteristic=1 Now this @paramcharacteristic will always match one of the column names in the table like Performance,Satisfaction,Profits and so on.But this does not seem to work. When i run the stored procedure from an application i get the error Cannot convert varchar 'Satisfaction' to integer.So i think what it does is try to compare the value of @paramcharacteristic variable and 1.But instead i want SQL to understand the statement as select * from tblcompany where Satisfaction=1 Where do u think should i make the change.In the definition of paramcharacteristic in the stored procedure??Pls give me some suggestions.On how to deal this situation.. Thanks in advance for the help.. Regards Pradhip.S If a Building is Completed then why do they call it BUILDING ??

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

      One option is to build up the T-SQL statement dynamically, like:

      exec('select * from sysobjects where ' + @myColumnName + ' >5')

      Just watch out for SQL injection problems. Cheers, Simon "The day I swan around in expensive suits is the day I hope someone puts a bullet in my head.", Chris Carter.
      my svg article

      B 1 Reply Last reply
      0
      • S SimonS

        One option is to build up the T-SQL statement dynamically, like:

        exec('select * from sysobjects where ' + @myColumnName + ' >5')

        Just watch out for SQL injection problems. Cheers, Simon "The day I swan around in expensive suits is the day I hope someone puts a bullet in my head.", Chris Carter.
        my svg article

        B Offline
        B Offline
        Bernhard Hofmann
        wrote on last edited by
        #3

        You just saved me at least 3 days work. Cheers mate! :-D

        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