完整版测试····用游标简便···create table t_dwq(d1 smalldatetime,d2 int,d3 money,d4 varchar(20))insert into t_dwq(d1,d2,d3,d4)select '2009-11-03',1,480,'晚餐'unionselect '2009-11-04',1,530,'晚餐'unionselect '2009-11-04',4,2360,'午餐'declare @Sql varchar(8000),@tmpfeild varchar(20),@UpSql varchar(8000)select @Sql = '',@tmpfeild = '',@UpSql = ''create table #tmp(日期 varchar(10))declare cur_tt cursor for select distinct d4 from t_dwqopen cur_ttFETCH NEXT FROM cur_tt into @tmpfeildwhile @@FETCH_STATUS = 0 begin if @Sql = '' Set @Sql = 'Alter table #tmp add '+@tmpfeild+'金额 money,'+@tmpfeild+'次数 int' else Set @Sql = @Sql + ','+@tmpfeild+'金额 money,'+@tmpfeild+'次数 int' FETCH NEXT FROM cur_tt into @tmpfeild endCLOSE cur_ttDEALLOCATE cur_ttexec(@Sql)insert into #tmp(日期) select distinct convert(varchar(10),d1,120) from t_dwq order by convert(varchar(10),d1,120)declare cur_tt cursor for select distinct d4 from t_dwqopen cur_ttFETCH NEXT FROM cur_tt into @tmpfeildwhile @@FETCH_STATUS = 0 begin Set @UpSql = 'Update a Set a.'+@tmpfeild+'金额 = isnull(b.d3,0),a.'+@tmpfeild+'次数 = isnull(b.d2,0)' + ' from #tmp a left join t_dwq b On a.日期 = b.d1 and b.d4 = '''+@tmpfeild+'''' exec(@UpSql) FETCH NEXT FROM cur_tt into @tmpfeild endCLOSE cur_ttDEALLOCATE cur_ttselect * from #tmpdrop table #tmp