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. How to Pass column names to query dynamically

How to Pass column names to query dynamically

Scheduled Pinned Locked Moved Database
databasehelptutorial
14 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.
  • V Offline
    V Offline
    veereshIndia
    wrote on last edited by
    #1

    Hi all, Here iam getting RoleId and iam storing it in a variable @str2.Now i want to use this variable data as a column name in third query i.e @str3.Please help me on this. alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) select @str1=AstId from UserTable where UserId=@UserId and Password = @Password; select @str2=RoleID from AstProf where AstId =@str1 ; select @str3=MenuId from MenuTable where @str2='y'; select @str3; End exec LogProc 'veeresh','veeresh'

    i want to join this group

    I 1 Reply Last reply
    0
    • V veereshIndia

      Hi all, Here iam getting RoleId and iam storing it in a variable @str2.Now i want to use this variable data as a column name in third query i.e @str3.Please help me on this. alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) select @str1=AstId from UserTable where UserId=@UserId and Password = @Password; select @str2=RoleID from AstProf where AstId =@str1 ; select @str3=MenuId from MenuTable where @str2='y'; select @str3; End exec LogProc 'veeresh','veeresh'

      i want to join this group

      I Offline
      I Offline
      IMQ
      wrote on last edited by
      #2

      you cannot pass the Column dynamically unless using Dynamic SQL. Now the question is that how to use dynamic sql declare a variable assign it TSQL with any dynamic values execute it .

      ________________ Tehmina Qureshi ****************

      V 1 Reply Last reply
      0
      • I IMQ

        you cannot pass the Column dynamically unless using Dynamic SQL. Now the question is that how to use dynamic sql declare a variable assign it TSQL with any dynamic values execute it .

        ________________ Tehmina Qureshi ****************

        V Offline
        V Offline
        veereshIndia
        wrote on last edited by
        #3

        Hi Sir, Iam new to sql.Can you tel me how to use dynamic sql in my query. Veeresh

        i want to join this group

        I 1 Reply Last reply
        0
        • V veereshIndia

          Hi Sir, Iam new to sql.Can you tel me how to use dynamic sql in my query. Veeresh

          i want to join this group

          I Offline
          I Offline
          IMQ
          wrote on last edited by
          #4

          you may declare and use dynamic sql like this;

          CREATE PROCEDURE GetRecords ( @DynamicColumnName varchar(100) ) AS

          /* Create a variable @SQLStatement */
          DECLARE @dynamicsql varchar(255)

          /* Enter the dynamic SQL statement into the*/
          /* variable @SQLStatement*/
          SELECT @dynamicsql = 'SELECT Column1, Column2, Column3 ' +
          'FROM TableName ORDER BY ' + @DynamicColumnName

          /* Execute the SQL statement*/
          EXEC(@dynamicsql)

          Hope this will help.

          ________________ Tehmina Qureshi ****************

          V R 2 Replies Last reply
          0
          • I IMQ

            you may declare and use dynamic sql like this;

            CREATE PROCEDURE GetRecords ( @DynamicColumnName varchar(100) ) AS

            /* Create a variable @SQLStatement */
            DECLARE @dynamicsql varchar(255)

            /* Enter the dynamic SQL statement into the*/
            /* variable @SQLStatement*/
            SELECT @dynamicsql = 'SELECT Column1, Column2, Column3 ' +
            'FROM TableName ORDER BY ' + @DynamicColumnName

            /* Execute the SQL statement*/
            EXEC(@dynamicsql)

            Hope this will help.

            ________________ Tehmina Qureshi ****************

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

            Dear Sir, Thanks for your help.But it's not comming here.Can you tel me how to use that in this query.Here is my query alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) declare @str4 varchar(500) select @str1=AstId from UserTable where UserId=@UserId and Password = @Password; select @str2=RoleID from AstProf where AstId =@str1 ; set @str4=@str2; select @str3=MenuId from MenuTable where @str4='y'; exec(@str4); select @str3; End exec LogProc 'veeresh','veeresh' But its giving error Like this (1 row(s) affected) Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'R1'.

            i want to join this group

            I V 2 Replies Last reply
            0
            • V veereshIndia

              Dear Sir, Thanks for your help.But it's not comming here.Can you tel me how to use that in this query.Here is my query alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) declare @str4 varchar(500) select @str1=AstId from UserTable where UserId=@UserId and Password = @Password; select @str2=RoleID from AstProf where AstId =@str1 ; set @str4=@str2; select @str3=MenuId from MenuTable where @str4='y'; exec(@str4); select @str3; End exec LogProc 'veeresh','veeresh' But its giving error Like this (1 row(s) affected) Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'R1'.

              i want to join this group

              I Offline
              I Offline
              IMQ
              wrote on last edited by
              #6

              Veer, you did not fully understood the concept of dynamic sql. so 1st you have to understand what the dynamic sql is; Dynamic SQL is actually the complete sql statement, including all the necessory clauses required for executing the sql statemnt like as "select * from tablename" so if u hav a variable declared for dynamic sql u must assign a complete statement in it. here in ur sp u did wrong. Example:Declaration of dynamic sql

              Declare @DynamicSql varchar(2000)
              set @DynamicSql = 'SELECT * FROM MyTable'

              now you may execute the like this

              exec(@DynamicSql) /* See ! Here in execute i've given the variable name nott any procedure */

              You may do aything which you want to with Dynamic SQL. I did not understand what you want to select in you stored procedure (as i dont hav enough time to understand the code coz im at my job. May be after few time i'll correct your code and post it back to u.) If you have got an idea about Dynamic SQL you can do whatever you want. Still hav problem? You are welcome to query. Thanks

              ________________ Tehmina Qureshi ****************

              1 Reply Last reply
              0
              • V veereshIndia

                Dear Sir, Thanks for your help.But it's not comming here.Can you tel me how to use that in this query.Here is my query alter procedure LogProc ( @UserId varchar(500), @Password varchar(500) ) as Begin declare @str1 varchar(500) declare @str2 varchar(500) declare @str3 varchar(500) declare @str4 varchar(500) select @str1=AstId from UserTable where UserId=@UserId and Password = @Password; select @str2=RoleID from AstProf where AstId =@str1 ; set @str4=@str2; select @str3=MenuId from MenuTable where @str4='y'; exec(@str4); select @str3; End exec LogProc 'veeresh','veeresh' But its giving error Like this (1 row(s) affected) Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'R1'.

                i want to join this group

                V Offline
                V Offline
                veereshIndia
                wrote on last edited by
                #7

                Hi, Thank you very much sir.I wil follow the rules. Veeresh.

                i want to join this group

                1 Reply Last reply
                0
                • I IMQ

                  you may declare and use dynamic sql like this;

                  CREATE PROCEDURE GetRecords ( @DynamicColumnName varchar(100) ) AS

                  /* Create a variable @SQLStatement */
                  DECLARE @dynamicsql varchar(255)

                  /* Enter the dynamic SQL statement into the*/
                  /* variable @SQLStatement*/
                  SELECT @dynamicsql = 'SELECT Column1, Column2, Column3 ' +
                  'FROM TableName ORDER BY ' + @DynamicColumnName

                  /* Execute the SQL statement*/
                  EXEC(@dynamicsql)

                  Hope this will help.

                  ________________ Tehmina Qureshi ****************

                  R Offline
                  R Offline
                  Rami Said Abd Alhalim
                  wrote on last edited by
                  #8

                  hi IsrarMuhammadQ i have error to execute dynamic sql in function

                  I 1 Reply Last reply
                  0
                  • R Rami Said Abd Alhalim

                    hi IsrarMuhammadQ i have error to execute dynamic sql in function

                    I Offline
                    I Offline
                    IMQ
                    wrote on last edited by
                    #9

                    Send the code and error msg i'll try to figure it out Can u describe me in words what you want to do? just describe it clearly i'll try to make you the actual understanding about what u are asking.

                    ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                    R 1 Reply Last reply
                    0
                    • I IMQ

                      Send the code and error msg i'll try to figure it out Can u describe me in words what you want to do? just describe it clearly i'll try to make you the actual understanding about what u are asking.

                      ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                      R Offline
                      R Offline
                      Rami Said Abd Alhalim
                      wrote on last edited by
                      #10

                      this example : -------------------------------------- CREATE FUNCTION test_fn ( @x int ) RETURNS @table TABLE ( c1 int ) AS BEGIN EXEC('select * from @table') RETURN END ----------------------------- Msg 443, Level 16, State 2, Procedure _fn__Get_All_References, Line 35 Invalid use of 'EXECUTE' within a function.

                      I 1 Reply Last reply
                      0
                      • R Rami Said Abd Alhalim

                        this example : -------------------------------------- CREATE FUNCTION test_fn ( @x int ) RETURNS @table TABLE ( c1 int ) AS BEGIN EXEC('select * from @table') RETURN END ----------------------------- Msg 443, Level 16, State 2, Procedure _fn__Get_All_References, Line 35 Invalid use of 'EXECUTE' within a function.

                        I Offline
                        I Offline
                        IMQ
                        wrote on last edited by
                        #11

                        Hi, Dont make it complicated. just try this in Northwind database.

                        Create PROCEDURE test_GetRecords ( @DynamicColumnName varchar(100) ) AS

                        /* Create a variable @dynamicsql */
                        DECLARE @dynamicsql varchar(255)

                        /* Enter the dynamic SQL statement into the*/
                        /* variable @dynamicsql*/
                        SELECT @dynamicsql = 'SELECT * ' +
                        'FROM [Categories] ORDER BY ' + @DynamicColumnName + ' desc'

                        /* Execute the SQL statement*/
                        EXEC(@dynamicsql)

                        exec test_GetRecords '[CategoryName]'

                        ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                        R 1 Reply Last reply
                        0
                        • I IMQ

                          Hi, Dont make it complicated. just try this in Northwind database.

                          Create PROCEDURE test_GetRecords ( @DynamicColumnName varchar(100) ) AS

                          /* Create a variable @dynamicsql */
                          DECLARE @dynamicsql varchar(255)

                          /* Enter the dynamic SQL statement into the*/
                          /* variable @dynamicsql*/
                          SELECT @dynamicsql = 'SELECT * ' +
                          'FROM [Categories] ORDER BY ' + @DynamicColumnName + ' desc'

                          /* Execute the SQL statement*/
                          EXEC(@dynamicsql)

                          exec test_GetRecords '[CategoryName]'

                          ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                          R Offline
                          R Offline
                          Rami Said Abd Alhalim
                          wrote on last edited by
                          #12

                          i want use dynamic sql in function not in stored procedure

                          I 1 Reply Last reply
                          0
                          • R Rami Said Abd Alhalim

                            i want use dynamic sql in function not in stored procedure

                            I Offline
                            I Offline
                            IMQ
                            wrote on last edited by
                            #13

                            Oops, sorry - Basically, no, you cannot use dynamic SQL in a function, and you probably shouldn't use it anyway. Writing to a temporary table is prohibited by functions.

                            ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                            R 1 Reply Last reply
                            0
                            • I IMQ

                              Oops, sorry - Basically, no, you cannot use dynamic SQL in a function, and you probably shouldn't use it anyway. Writing to a temporary table is prohibited by functions.

                              ________________ Tehmina Qureshi We can have facts without thinking but we cannot have thinking without facts. ****************

                              R Offline
                              R Offline
                              Rami Said Abd Alhalim
                              wrote on last edited by
                              #14

                              ok Thanks where r u from?

                              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