Sunday, February 19, 2012

Get Nth column in a table

HI,

Can any one please help me to solve this....

if we have N number of columns in a table.I need to get 4th column of that table directly.

Thanks
SRI

Use the following query...

Code Snippet

--If you want to Fetch the nth column name (only name)

Declare @.TableName as nVarchar(100);

Declare @.NthCol as Int

Select

@.TableName =N'Sysobjects',

@.NthCol=2

select Col_name(object_id(@.TableName),@.NthCol) ColumnName

Code Snippet

--If you want to select Record for given Column (with data)

Declare @.TableName as nVarchar(100);

Declare @.NthCol as Int

Select

@.TableName =N'Sysobjects',

@.NthCol=2

Declare @.ColName as varchar(100);

select @.ColName = Col_name(object_id(@.TableName),@.NthCol)

Exec ('Select ' + @.ColName + ' From ' + @.TableName)

|||Thanks..a lot.....i execute query and it works

No comments:

Post a Comment