1

这是存储过程的部分脚本。我希望能够自动更新(下面的数据结果)下一个“P4”(等等)列数:

/*
RESULTS:
66,
221,
0
*/


DECLARE
@APP_TOTAL INT
, @DB_TOTAL INT
, @OS_TOTAL INT
, @YEAR INT

SET @APP_TOTAL = (SELECT COUNT(*) 
  FROM [db_eCAM].[dbo].[tbl_SecChecks]
WHERE LAYER = 'APP' AND [ENABLE] = 1)
PRINT @APP_TOTAL

SET @DB_TOTAL = (SELECT COUNT(*) 
  FROM [db_eCAM].[dbo].[tbl_SecChecks]
WHERE LAYER = 'DB' AND [ENABLE] = 1)
PRINT @DB_TOTAL

SET @OS_TOTAL = (SELECT COUNT(*) 
  FROM [db_eCAM].[dbo].[tbl_SecChecks]
WHERE LAYER = 'OS' AND [ENABLE] = 1)
PRINT @OS_TOTAL

SET @YEAR = 2013

在此处输入图像描述

4

1 回答 1

2

也许是这样的:

update mytable
    set p4 = case
        when description = 'APP_TOTAL' then @APP_TOTAL
        when description = 'DB_TOTAL' then @DB_TOTAL
        when description = 'OS' then @OS_TOTAL
    end
where description in ('APP_TOTAL', 'DB_TOTAL', 'OS');

演示:http ://www.sqlfiddle.com/#!3/f575c/1

要使句点 # 动态化,您可以像这样使用动态 SQL:http ://www.sqlfiddle.com/#!3/f575c/5

于 2012-04-25T20:17:52.177 回答