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. Select Column (as Parameter) from Table

Select Column (as Parameter) from Table

Scheduled Pinned Locked Moved Database
databasehelp
6 Posts 4 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.
  • C Offline
    C Offline
    CandyMe
    wrote on last edited by
    #1

    Sorry, I have this sql statement where I supply the column as parameter and then use it to retrieve the values of the specified column. Instead result is column name =( declare @sql varchar(200) SELECT @sql = 'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (Column_name = ''Column1'')' exec ('select (' + @sql + ') from MyTable') RESULT: Columns ------- Column1 Column1 Column1 Column1 Column1 Help Please! Thanks! Gerri

    _ A M 3 Replies Last reply
    0
    • C CandyMe

      Sorry, I have this sql statement where I supply the column as parameter and then use it to retrieve the values of the specified column. Instead result is column name =( declare @sql varchar(200) SELECT @sql = 'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (Column_name = ''Column1'')' exec ('select (' + @sql + ') from MyTable') RESULT: Columns ------- Column1 Column1 Column1 Column1 Column1 Help Please! Thanks! Gerri

      _ Offline
      _ Offline
      _AK_
      wrote on last edited by
      #2

      If you are having complete sql statement in a variable then by using sp_executesql you can execute sql statement in that variable. :)

      Best Regards, Apurva Kaushal

      1 Reply Last reply
      0
      • C CandyMe

        Sorry, I have this sql statement where I supply the column as parameter and then use it to retrieve the values of the specified column. Instead result is column name =( declare @sql varchar(200) SELECT @sql = 'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (Column_name = ''Column1'')' exec ('select (' + @sql + ') from MyTable') RESULT: Columns ------- Column1 Column1 Column1 Column1 Column1 Help Please! Thanks! Gerri

        A Offline
        A Offline
        albCode
        wrote on last edited by
        #3

        -- stored procedure CREATE PROCEDURE returncolname @tablename varchar(255), @colname varchar(255) AS declare @tblname as varchar(255) set @tblname = @tablename declare @clnName as varchar(255) set @clnName = @colname SELECT @clnName AS [Columns] FROM information_schema.columns WHERE (TABLE_NAME = @tablename) and (Column_name =@colname ) GO -- execution of returncolname procedure exec returncolname 'tablename','columname'

        C 1 Reply Last reply
        0
        • A albCode

          -- stored procedure CREATE PROCEDURE returncolname @tablename varchar(255), @colname varchar(255) AS declare @tblname as varchar(255) set @tblname = @tablename declare @clnName as varchar(255) set @clnName = @colname SELECT @clnName AS [Columns] FROM information_schema.columns WHERE (TABLE_NAME = @tablename) and (Column_name =@colname ) GO -- execution of returncolname procedure exec returncolname 'tablename','columname'

          C Offline
          C Offline
          CandyMe
          wrote on last edited by
          #4

          Sorry, I tried this and it returns Column instead of Values (of the Column). Thank you. Help please G

          1 Reply Last reply
          0
          • C CandyMe

            Sorry, I have this sql statement where I supply the column as parameter and then use it to retrieve the values of the specified column. Instead result is column name =( declare @sql varchar(200) SELECT @sql = 'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (Column_name = ''Column1'')' exec ('select (' + @sql + ') from MyTable') RESULT: Columns ------- Column1 Column1 Column1 Column1 Column1 Help Please! Thanks! Gerri

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

            I usually write out the SQL Statement using place holders. In this case ?COLUMN_NAME? fills in for the variable that will be passed in. I then replace the place holders with using REPLACE. ** Be sure you read up on SQL Injection Attacks if you allow free form entry from the users. This may not be as optimal as building the string on the fly but, it is much less error prone and much more maintainable. CREATE PROCEDURE DynamicSqlTest ( @columnName NVARCHAR(100) ) AS DECLARE @sql NVARCHAR(1000) SET @sql = N'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (COLUMN_NAME = ''?COLUMN_NAME?'')' SET @sql = REPLACE(@sql,'?COLUMN_NAME?',@columnName) EXEC(@sql)

            C 1 Reply Last reply
            0
            • M Michael Potter

              I usually write out the SQL Statement using place holders. In this case ?COLUMN_NAME? fills in for the variable that will be passed in. I then replace the place holders with using REPLACE. ** Be sure you read up on SQL Injection Attacks if you allow free form entry from the users. This may not be as optimal as building the string on the fly but, it is much less error prone and much more maintainable. CREATE PROCEDURE DynamicSqlTest ( @columnName NVARCHAR(100) ) AS DECLARE @sql NVARCHAR(1000) SET @sql = N'SELECT COLUMN_NAME AS Columns FROM information_schema.columns WHERE (TABLE_NAME = ''MyTable'') and (COLUMN_NAME = ''?COLUMN_NAME?'')' SET @sql = REPLACE(@sql,'?COLUMN_NAME?',@columnName) EXEC(@sql)

              C Offline
              C Offline
              CandyMe
              wrote on last edited by
              #6

              Thank you so mcuh

              Gerri

              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