是否可以执行以下操作?
declare @start_date int
declare @end_date int
set @start_date = 202003
set @end_date = 202004
select
'Total_Units' as Metric_Description
,cast(count(*) as varchar(max)) as @end_date
from table
我正在尝试使用@end_date作为查询结果中列的名称,换句话说,我试图做到这一点,而每个月所有多个查询中的202004都没有更改
select
'Total_Units' as Metric_Description
,cast(count(*) as varchar(max)) as '202004'
from table
declare @start_date int
declare @end_date varchar(20)
set @start_date = 202003
set @end_date = '202004'
declare @query varchar(max)
set @query = 'select
''Total_Units'' as Metric_Description
,cast(count(*) as varchar(max)) as [' + @end_date + ']
from table'
exec sp_sqlexec @query