- SQL code
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')
一个临时表 #tbl_year_review,然后从数据库里其他表里查询得到结果值,如下
xq_build hz_month total
1213 8 150.00
1213 7 69.00
2 8 120.00
现在我想把total这一列的值插入到临时表 #tbl_year_review里去,想得到如下效果,该如何操作?>
xq_build f_1 f_2 f_3 f_4 f_5 f_6 f_7 f_8 f_9 f_10 f_11
1213 0.00 0.00 0.00 0.00 0.00 0.00 0.00 150.00 0.00 0.00 0.00
1213 0.00 0.00 0.00 0.00 0.00 0.00 69.00 0.00 0.00 0.00 0.00
2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 120.00 0.00 0.00 0.00
------解决方案--------------------
- SQL code
select a.total into #tbl_year_review b from other_tab a where a.xq_build=b.xq_build
------解决方案--------------------
- SQL code
create table #tt(xq_build nvarchar(10),hz_month int,total decimal(8,2))insert into #tt select '1213', 8, 150.00 unionselect '1213', 7, 69.00 unionselect '2',8,120.00insert into #tbl_year_review(xq_build,f_10)select xq_build,total from #tt
------解决方案--------------------
- SQL code
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)