1 --模拟数据 2 IF OBJECT_ID(‘tempdb..#t‘)>0 DROP TABLE #t 3 SELECT * INTO #t 4 FROM ( 5 SELECT ‘1‘ id,2030 g,265 h, 830 k,‘g*h+h*k‘ gs,0 tt 6 UNION ALL 7 SELECT ‘2‘ id,2030 g,0 h, 0 k,‘g*4‘ gs,0 tt 8 UNION ALL 9 SELECT ‘3‘ id,2030 g,265 h, 0 k,‘(g+h)*2‘ gs,0 tt 10 UNION ALL 11 SELECT ‘4‘ id,2030 g,265 h, 0 k,‘(g+h)*2‘ gs,0 tt)t 12 --原始数据 13 SELECT * FROM #t 14 15 --变量 16 DECLARE @i int,@n INT 17 DECLARE @gs VARCHAR(50) 18 DECLARE @sql VARCHAR(MAX) 19 20 --按公式类别分类 21 IF OBJECT_ID(‘tempdb..#tt‘)>0 DROP TABLE #tt 22 SELECT ROW_NUMBER()OVER(ORDER BY gs)rowid 23 ,gs 24 INTO #tt 25 FROM #t 26 GROUP BY gs 27 28 --按公式类别遍历 29 SELECT @i=MIN(rowid),@n=MAX(rowid) FROM #tt 30 WHILE(@i<=@n) 31 BEGIN 32 SELECT @gs=gs FROM #tt WHERE rowid=@i 33 SET @sql=‘update #t set tt=‘+@gs+‘ where gs=‘‘‘+@gs+‘‘‘‘--生成脚本 34 EXEC(@sql)--执行脚本,可以print看效果 35 SET @i=@i+1 36 END 37 --处理后效果 38 SELECT * FROM #t
效果预览:
时间: 2024-10-27 12:12:18