select * into from中能否使用子查询的问题?SQL codeset @sqlstrselect 部品品番,供应商名称,sum(case
select * into from中能否使用子查询的问题?
SQL code
set @sqlstr='select 部品品番,供应商名称,sum(case 月份 when '''+ @yf1 + '''then 部品数量 else 0 end) '''+ @yf1 +''' ,sum(case 月份 when '''+ @yf2 + '''then 部品数量 else 0 end) '''+ @yf2 +''' ,sum(case 月份 when '''+ @yf3 + '''then 部品数量 else 0 end) '''+ @yf3 +''' from dest group by 部品品番,供应商名称'
想把以上的查询存进新表
SQL code
declare @sqlstr varchar(8000)--这种方法,varchar的内容一定不够,数据有2万行.set @sqlstr='select ,部品品番,供应商名称'select @sqlstr=@sqlstr+', sum(case 月份 when ''' + cast(月份 as varchar )+ ''' then 部品数量 else 0 end) ['+ 月份 +']'from destset @sqlstr='select 部品品番,供应商名称,'+ substring(@sqlstr,20,len(@sqlstr))+' into dest1 from dest group by 部品品番,供应商名称,月份'exec(@sqlstr)