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