November 9, 2016 at 1:18 am
Hi friends ,
i woud like to make display the header and value using parameter
eg : (below code is working perfectly)
declare @a varchar(25) = 'Anand' ,
select (@a) as 'Anand'
but if i give code like below there is an column name as variable :
declare @a varchar(25) = 'Anand'
declare @b-2 varchar(25) = 'bala'
select (@a) as [@a] , (@b) as [@b]
select (@a) as '@a' , (@b) as '@b'
if i run this code i dont get column name :
declare @a varchar(25) = 'Anand'
declare @b-2 varchar(25) = 'bala'
select (@a) as @a , (@b) as @b-2
please some one helpme how can i get below output ..?
Anand Bala --> [column name as parameter value]
Anand Bala --> [parameter Values]
November 9, 2016 at 2:04 am
You will have to use dynamic SQL, here is a quick example
😎
USE TEEST;
GO
SET NOCOUNT ON;
DECLARE @COL01 NVARCHAR(MAX) = N'COLUMN_01';
DECLARE @COL02 NVARCHAR(MAX) = N'COLUMN_02';
DECLARE @PARAM_STR NVARCHAR(MAX) = N'@PARAM01 VARCHAR(50),@PARAM02 VARCHAR(50)';
DECLARE @PARAM01 VARCHAR(50) = 'Anand Bala 1';
DECLARE @PARAM02 VARCHAR(50) = 'Anand Bala 2';
DECLARE @SQL_SELECT NVARCHAR(MAX) = REPLACE(REPLACE(N'
SELECT
@PARAM01 AS [{{@COL01}}]
,@PARAM02 AS [{{@COL02}}]
;',N'{{@COL01}}',@COL01),N'{{@COL02}}',@COL02);
EXEC SP_EXECUTESQL @SQL_SELECT
,@PARAM_STR
,@PARAM01
,@PARAM02
;
Output
COLUMN_01 COLUMN_02
-------------------------------------------------- --------------------------------------------------
Anand Bala 1 Anand Bala 2
November 9, 2016 at 8:02 am
Dear Eirikur Eiriksson ,
Thank you verymuch for your valuable time. its working.
November 9, 2016 at 8:56 am
Anandkumar-SQL_Developer (11/9/2016)
Dear Eirikur Eiriksson ,Thank you verymuch for your valuable time. its working.
You are very welcome.
😎
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply