如何写动态的列名
如何写动态的列名?
例如:
select id, pnumb as @syear+'借方',acode from acctable
其中@syear是定义的,值是传进来的,变化的。以上的语句这样写好象有问题,
应如何去写?
------解决方案--------------------exec('select id,pnumb as ['+@syear+'借方],acode from acctable')
------解决方案--------------------exec('select id, pnumb as '+@syear+''+借方'' acode from acctable' )
------解决方案--------------------SQL code
declare @syear int, @sql varchar(1000)
set @syear = 2007
set @sql = 'select id, pnumb as [' + cast(@syear as varchar(100)) + '借方], acode from acctable '
exec (@sql)