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 values from a table dyunamically in Stored procedure

select values from a table dyunamically in Stored procedure

Scheduled Pinned Locked Moved Database
databasehelp
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
    Pranav Thakur
    wrote on last edited by
    #1

    Hi, I have created a "test" table having 4 column Q1,Q2,Q3 and Q4 with values one,two,three,and four respectively. Please refer the code below:- Declare @loopId int set @loopId=1 Declare @vr varchar(100) declare @variable varchar(100) while @loopId<=4 begin set @vr='Q'+''+Convert(varchar,@loopId)+'' Select @variable=@vr from test print(@variable ) set @loopId=@loopId+1 end In the above code I am printing the values of each column of table "test" one by one.But the out put of the above code is Q1 Q2 Q3 Q4 but i want to have the values not column name.This is a sample code which I will be using in one of my stored proc.What wrong I am doing.Please help.

    D N 2 Replies Last reply
    0
    • P Pranav Thakur

      Hi, I have created a "test" table having 4 column Q1,Q2,Q3 and Q4 with values one,two,three,and four respectively. Please refer the code below:- Declare @loopId int set @loopId=1 Declare @vr varchar(100) declare @variable varchar(100) while @loopId<=4 begin set @vr='Q'+''+Convert(varchar,@loopId)+'' Select @variable=@vr from test print(@variable ) set @loopId=@loopId+1 end In the above code I am printing the values of each column of table "test" one by one.But the out put of the above code is Q1 Q2 Q3 Q4 but i want to have the values not column name.This is a sample code which I will be using in one of my stored proc.What wrong I am doing.Please help.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Pranav Thakur wrote:

      Select @variable=@vr from test

      This line holds the problem. You wil need to create a dynamic query to display values.

      Avoid Google. Use Blackle[^]

      1 Reply Last reply
      0
      • P Pranav Thakur

        Hi, I have created a "test" table having 4 column Q1,Q2,Q3 and Q4 with values one,two,three,and four respectively. Please refer the code below:- Declare @loopId int set @loopId=1 Declare @vr varchar(100) declare @variable varchar(100) while @loopId<=4 begin set @vr='Q'+''+Convert(varchar,@loopId)+'' Select @variable=@vr from test print(@variable ) set @loopId=@loopId+1 end In the above code I am printing the values of each column of table "test" one by one.But the out put of the above code is Q1 Q2 Q3 Q4 but i want to have the values not column name.This is a sample code which I will be using in one of my stored proc.What wrong I am doing.Please help.

        N Offline
        N Offline
        Niraj_Silver
        wrote on last edited by
        #3

        hi... Try This one.... Declare @loopId int set @loopId=1 Declare @vr varchar(100) declare @variable varchar(100) Declare @qry VarChar(1000) while @loopId<=4 begin set @vr='Q'+''+Convert(varchar,@loopId)+'' Select @variable=@vr from test print(@variable ) Set @qry = (Select @variable=@vr from test) set @loopId=@loopId+1 Exec(@qry) end Take Care .... sure there will not be any prob now if still having prob..then reply.. take care... By :-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