How to pass column name dynamically in Sql| Sql select statement assign dynamic column names|Sql select without hardcode column names

Here i am using Sql select statement using dynamic column name,without hard code the caption name. 

Example: 

Here Pass the year name dynamically..
declare @Squery  nvarchar(4000)
set @Squery='select countryName '+ 'hi' + 
convert(varchar,year(getdate())) +' from Country'
EXEC SP_EXECUTESQL @Squery
Screen shot:

Include View..
declare @Squery  nvarchar(4000)
set @Squery='Create view tt as select dbname '+ 'hi' + 
convert(varchar,year(getdate())) +' from accountyears'
EXEC SP_EXECUTESQL @Squery

Post a Comment

0 Comments