How to Retrieve Dynamic Columns?

Posted by user2514925 on Stack Overflow See other posts from Stack Overflow or by user2514925
Published on 2013-06-27T04:14:46Z Indexed on 2013/06/27 4:21 UTC
Read the original article Hit count: 309

Filed under:
|

I have a table:

EmpId FirstName  LastName     Domain    Vertical    Account City
345   Priya      Palanisamy   DotNet    LS  Abbott  Chennai
346   Kavitha    Amirtharaj   DotNet    CG  Diageo  Chennai
647   Kala       Haribabu     DotNet    IME IMS     Chennai

I want to take the particular columns dynamically. I am using the following procedure but I can take only one column at a time:

create PROC columnlist
(
    @COLUMNS VARCHAR(1000)
)
AS
BEGIN
    DECLARE @sql nvarchar(max) = 'SELECT '+ @COLUMNS + ' FROM Table';
    exec sp_executesql @sql,N''
END

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql