获取动态获取列表名称的表列的值 [英] Fetching values for columns of tables getting column table names dynamically

查看:131
本文介绍了获取动态获取列表名称的表列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将表名和列名存储为Lkup表中的行,并希望使用该表

来检索数据集。我的意思是Lkup表中提到的相应表中的列值。



怎么办?需要Ae游标??

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??

推荐答案

如果你问如何从该表中检索数据你只需使用一个select语句......

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



有一个名为 INFORMATION_SCHEMA.COLUMNS 的视图 。您可以使用该视图选择ColumnNames,特定表的列的数据类型......


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...

Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_NAME='TableName'



更新:

您可以使用 CURSOR [ ^ ]或 WHILE [ ^ ]循环记录...光标示例:


Updated:
U can use a CURSOR[^] or WHILE[^] to loop through the records...Cursor Example:

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



上面的光标将循环遍历所有tablename,逐个列名并显示TableName和ColumnName


the above cursor will simply loop through all the tablename,columname one by one and displays the TableName and ColumnName

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


这篇关于获取动态获取列表名称的表列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆