Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have stored tablename and column name as rows in Lkup table and want to use that table
to retrieve data set.I mean column value from corresponding table as mentioned in Lkup table.

How can it be done? Ae cursors required??
Posted
Updated 5-Sep-13 21:49pm
v2
Comments
Mohan Gopi 6-Sep-13 3:21am    
Can you share your query.
Member 10256460 6-Sep-13 3:38am    
I have stored tablename and column name as rows in one table and want to use that table
to retrieve data set.I mean column value from corresponding table.
Raja Sekhar S 6-Sep-13 3:52am    
Can u specify the table structure.. u saved column names as rows or comma seperated value...?
Member 10256460 6-Sep-13 3:54am    
table structure is like Select Tablename, Columnname from Lkuptable
and now to get the values of Columnname from corresponding Tablename on be one.
Raja Sekhar S 6-Sep-13 4:02am    
In the solution which i have given,in the cursor u can use the select statement...
Check the links which i provided, you will get an idea on how to do it... your answer to my question states that you are entirely new to sql...

1 solution

If you are asking how to retrive Data from that table u can simply use a select statement...
SQL
Select Column1,Column2,Column3 From TableName Where Column1='ReqdTableName'

There is a View Called INFORMATION_SCHEMA.COLUMNS in Sql Server.. You can use that view to select the ColumnNames, Datatype of the columns of a Particular Table...
SQL
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_NAME='TableName'

Updated:
U can use a CURSOR[^] or WHILE[^] to loop through the records...Cursor Example:
SQL
Declare @CurTest Cursor,@Tablename varchar(40),@Columnname Varchar(40)

Set @CurTest = Cursor Fast_Forward
For
Select Tablename, Columnname from Lkuptable-- Order by ID

Open @CurTest
Fetch Next From @CurTest into @Tablename ,@Columnname 
While @@FETCH_STATUS=0
Begin
    -- Logic Here
    Print @Tablename ,@Columnname 
    Fetch Next From @CurTest into @Tablename ,@Columnname 
End

the above cursor will simply loop through all the tablename,columname one by one and displays the TableName and ColumnName
SQL
Declare @Flag int,@Id int,@PrevId Int,@Data varchar(40)
Set @Flag=1
While @Flag =1
Begin
    Set @Data=''
    Select Top(1) @Data=Column ,@Id=id from tableName where ID>@PrevId

    If @Data=''
      Break;

    -- Logic Here
    Set @PrevId=@Id
End
 
Share this answer
 
v5
Comments
Member 10256460 6-Sep-13 3:34am    
Thanks Mohan for this Info, However i have a Lkup table with Tablename and Columnname and using that Lkpup table i need to fetch values row by row using that table and Column, That was the purpose actually..

Can you please give me some idea using code snippet

It would be helpful
Raja Sekhar S 6-Sep-13 3:37am    
Check the Updated Solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900