Monday 23 September 2013

How to Dynamically pass columnname in select query?

declare @var varchar(10);
set @var='';
select @var='Column1,column2,column3,column4,column5';
select @var = @var + COLUMN_NAME + ','
        from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = ' T_TableName ' and column_name=@var
exec ('select top 10 '+@var+' from T_TableName order by '+ @var+' desc ')

No comments:

Post a Comment

C# LINQ Joins With SQL

There are  Different Types of SQL Joins  which are used to query data from more than one database tables. In this article, you will learn a...