视图 viewemployee
ename wdate wwtid
aa 2006年10月17日 1
aa 2006年10月18日 1
aa 2006年10月20日 1
bb 2006年10月17日 2
bb 2006年10月19日 2
cc NULL NULL
dd NULL NULL
ee NULL NULL
ff NULL NULL 执行存储过程
create proc kqgl
as
declare @sql varchar(8000)
set @sql = 'select ename'
select @sql = @sql + ',max(case wdate when '''+wdate+''' then wwtid end) ['+wdate+']'
from (select distinct wdate from viewemployee) as a
select @sql = @sql+' from viewemployee group by ename'
exec(@sql)执行后没有任何数据显示,只显示“命令已成功完成”。为什么呀,就算没数据,也应该有个表头吧!
ename wdate wwtid
aa 2006年10月17日 1
aa 2006年10月18日 1
aa 2006年10月20日 1
bb 2006年10月17日 2
bb 2006年10月19日 2
cc NULL NULL
dd NULL NULL
ee NULL NULL
ff NULL NULL 执行存储过程
create proc kqgl
as
declare @sql varchar(8000)
set @sql = 'select ename'
select @sql = @sql + ',max(case wdate when '''+wdate+''' then wwtid end) ['+wdate+']'
from (select distinct wdate from viewemployee) as a
select @sql = @sql+' from viewemployee group by ename'
exec(@sql)执行后没有任何数据显示,只显示“命令已成功完成”。为什么呀,就算没数据,也应该有个表头吧!
如果你已经建立了
在查询分析器中直接
exec kqgl
就可以
或者在查询分析器中执行declare @sql varchar(8000)
set @sql = 'select ename'
select @sql = @sql + ',max(case wdate when '''+wdate+''' then wwtid end) ['+wdate+']'
from (select distinct wdate from viewemployee) as a
select @sql = @sql+' from viewemployee group by ename'
exec(@sql)
create table ttttt(编号 varchar(20),序号 int,价格 int,数量 int)
insert ttttt values('1234',1,40,20)
insert ttttt values('1234',3,43,25)
insert ttttt values('5678',1,40,30)
insert ttttt values('5678',3,43,20)
insert ttttt values('5678',1,40,32)
insert ttttt values('5678',2,40,32)--查看数据
select * from ttttt--查看结果
declare @sql varchar(2000)
set @sql='select 编号'
select @sql=@sql+',[序号'+convert(varchar(20),序号)+']=isnull(sum(case when 序号='+convert(varchar(20),序号)+' then 数量 end),0)'
from ttttt
group by 序号select @sql=@sql+',[价格合计]=sum(价格*数量) from ttttt group by 编号'
print @sqlexec(@sql)drop table ttttt--参考,建议先将@sql 先print出来看看是什么SQL语句