日期:2014-05-17 浏览次数:20573 次
create table #tbl_year_review
(xq_build nvarchar(50) collate Chinese_PRC_CI_AS,
f_1 decimal(18,2) default 0.00 null,
f_2 decimal(18,2) default 0.00 null,
f_3 decimal(18,2) default 0.00 null,
f_4 decimal(18,2) default 0.00 null,
f_5 decimal(18,2) default 0.00 null,
f_6 decimal(18,2) default 0.00 null,
f_7 decimal(18,2) default 0.00 null,
f_8 decimal(18,2) default 0.00 null,
f_9 decimal(18,2) default 0.00 null,
f_10 decimal(18,2) default 0.00 null,
f_11 decimal(18,2) default 0.00 null,
f_12 decimal(18,2) default 0.00 null)
insert into #tbl_year_review(xq_build) values('2')
insert into #tbl_year_review(xq_build) values('5')
insert into #tbl_year_review(xq_build) values('1213')
select distinct * from #tbl_year_review where xq_build in('2','5','1213')
select a.total into #tbl_year_review b from other_tab a where a.xq_build=b.xq_build
------解决方案--------------------
create table #tt ( xq_build nvarchar(10), hz_month int, total decimal(8,2) ) insert into #tt select '1213', 8, 150.00 union select '1213', 7, 69.00 union select '2',8,120.00 insert into #tbl_year_review(xq_build,f_10) select xq_build,total from #tt
------解决方案--------------------
DECLARE @Sql NVARCHAR(MAX)
SELECT distinct
@Sql=ISNULL(@Sql+NCHAR(13)+NCHAR(10),'')+'INSERT INTO #tbl_year_review(xq_build,f_'+ltrim(hz_month)+') VALUES(N'''+xq_build+''','+CONVERT(NVARCHAR(50),total,0)+')'
from #tbl_year_review where xq_build in('2','5','1213')
EXEC (@Sql)