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. Query columns by name in stored procedures (SQL Server 2005)

Query columns by name in stored procedures (SQL Server 2005)

Scheduled Pinned Locked Moved Database
databasequestionsql-serverdesignsysadmin
7 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.
  • T Offline
    T Offline
    Tobias Schoenig
    wrote on last edited by
    #1

    Hi folks, I got a question concerning stored procedures and I didn't find anything helpful on the comment search or via google: I need to query a single column in a stored procedure but I want to identify this column with a VarChar-parameter, so that I can use one stored procedure for all columns in a table. Maybe there's also a way to design such a stored procedure even with whole tables. What I'm looking for is a stored procedure like this: CREATE PROCEDURE [dbo].[GetValueFromMyTable] ( @Column varchar ) AS BEGIN SELECT @Column FROM MyTable END The code seems to be correct, but the statement doesn't work the way I want it to work. Does anyone have an idea about my problem? Thanks in advance, Tobias

    P A 2 Replies Last reply
    0
    • T Tobias Schoenig

      Hi folks, I got a question concerning stored procedures and I didn't find anything helpful on the comment search or via google: I need to query a single column in a stored procedure but I want to identify this column with a VarChar-parameter, so that I can use one stored procedure for all columns in a table. Maybe there's also a way to design such a stored procedure even with whole tables. What I'm looking for is a stored procedure like this: CREATE PROCEDURE [dbo].[GetValueFromMyTable] ( @Column varchar ) AS BEGIN SELECT @Column FROM MyTable END The code seems to be correct, but the statement doesn't work the way I want it to work. Does anyone have an idea about my problem? Thanks in advance, Tobias

      P Offline
      P Offline
      Puneet Sri
      wrote on last edited by
      #2

      As @Column is an input parameter and you have taken varchar as data type you have to specifies the length of input parameter. for example:- @Column varchar(20) I hope this will help you. Puneet Srivastava

      T 1 Reply Last reply
      0
      • P Puneet Sri

        As @Column is an input parameter and you have taken varchar as data type you have to specifies the length of input parameter. for example:- @Column varchar(20) I hope this will help you. Puneet Srivastava

        T Offline
        T Offline
        Tobias Schoenig
        wrote on last edited by
        #3

        Thank you for your answer, but it still doesn't work. I only get the column name as a result-set. I think the statement awaits a column-object to identify the right column, doesn't it?! Someone got an idea?

        V 1 Reply Last reply
        0
        • T Tobias Schoenig

          Hi folks, I got a question concerning stored procedures and I didn't find anything helpful on the comment search or via google: I need to query a single column in a stored procedure but I want to identify this column with a VarChar-parameter, so that I can use one stored procedure for all columns in a table. Maybe there's also a way to design such a stored procedure even with whole tables. What I'm looking for is a stored procedure like this: CREATE PROCEDURE [dbo].[GetValueFromMyTable] ( @Column varchar ) AS BEGIN SELECT @Column FROM MyTable END The code seems to be correct, but the statement doesn't work the way I want it to work. Does anyone have an idea about my problem? Thanks in advance, Tobias

          A Offline
          A Offline
          Arun Immanuel
          wrote on last edited by
          #4

          CREATE PROCEDURE [dbo].[GetValueFromMyTable] ( @ColID INT) AS BEGIN SELECT ColName = CASE @ColID WHEN @ColID=1 THEN Col1 WHEN @ColID=2 THEN Col2 ELSE col3 END, FROM tableName

          Regards, Arun Kumar.A

          1 Reply Last reply
          0
          • T Tobias Schoenig

            Thank you for your answer, but it still doesn't work. I only get the column name as a result-set. I think the statement awaits a column-object to identify the right column, doesn't it?! Someone got an idea?

            V Offline
            V Offline
            vimal_yet
            wrote on last edited by
            #5

            try building dynamic query with the variable name

            If U Get Errors U Will Learn If U Don't Get Errors U Have Learnt

            T 1 Reply Last reply
            0
            • V vimal_yet

              try building dynamic query with the variable name

              If U Get Errors U Will Learn If U Don't Get Errors U Have Learnt

              T Offline
              T Offline
              Tobias Schoenig
              wrote on last edited by
              #6

              I know the possiblity of dynamic queries, but the problem is, that I've got to use one stored procedure per table. So I simply have to get the mapping between the name and the column. Is there a fast way to achieve this?

              T 1 Reply Last reply
              0
              • T Tobias Schoenig

                I know the possiblity of dynamic queries, but the problem is, that I've got to use one stored procedure per table. So I simply have to get the mapping between the name and the column. Is there a fast way to achieve this?

                T Offline
                T Offline
                Tobias Schoenig
                wrote on last edited by
                #7

                Ok, finally I made it work - here's the simplified procedure's code that's working fine: ALTER PROCEDURE [dbo].[GetData]( @Column varchar(100), @Begin datetime, @End datetime ) AS BEGIN DECLARE @query nvarchar(1000) SET @query = N'SELECT ' + quotename(@Column) + ' FROM MyTable' EXEC sp_executesql @query END Thanks for your hint vimal_yet!

                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