How to display my parameter value as a column name ?

Posted by Jeyjey under Sql Server on 12/30/2010 | Points: 10 | Views : 2787 | Status : [Member] | Replies : 1
Hi,

I have a doubt in sqlserver storedprocedure.

I want to display my parameter value as column name. Is it possible ? How?

Example:

Date - name - phone - product1 - product2

INSTEAD OF i want

Date - name - phone - addida - nokia

Thanks,
JJ




Responses

Posted by: T.saravanan on: 12/30/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi JJ,

Yes its possible.Try the way...


Create proc stp_Test
(
@Prod1 varchar(100),
@Prod2 varchar(100)
)
AS
BEGIN
DECLARE @sQuery NVARCHAR(MAX)
SET @sQuery = N'SELECT sProd1Name as ' + @Prod1 + ', sProd2Name as ' + @Prod2 + ' FROM tbl_Test where nProdID =5 '
EXEC SP_EXECUTESQL @sQuery
END


Cheers :)

Thanks,
T.Saravanan

Jeyjey, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response