SQL select的别名能否用参数,如何使用?
问题描述:
SQL select的别名能否用参数???如下事例
declare @n1 int
set @n1=month(getdate())
select 当前月份发货数 as @n1+月份发货数,第二月份发货数 as (@n1+1)+月份发货数 from 发货表
答
declare @n1 int ,@n2 int
set @n1=month(getdate())
set @n2 = @n1+1;
--动态SQL语句执行
exec('select 当前月份发货数 as m' + @n1 +'月份发货数,第二月份发货数 as m'+@n2+'月份发货数 from 发货表')--列名不能以数字开头
答
declare @s nvarchar(100)
set @s='bd_user'
set @s='select * from '+@s
exec(@s)