如何使用运行存储过程的数据结果自动更新“期间”列

时间:2012-04-25 20:09:10

标签: sql

这是存储过程的部分脚本。 我希望能够自动更新(下面的数据结果)下一个'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

enter image description here

1 个答案:

答案 0 :(得分: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