2012-03-25 204 views
2

我一直在研究,但我似乎無法做到正確。 我有以下表格:觸發器在sql server 2008中

create table school_tb 
(idSchool int identity primary key, 
nameSchool varchar(100), 
schoolPopulation int 
) 

create table career_tb 
(idCareer int identity primary key, 
nameCareer  varchar(100), 
carrerPopulation int, 
numberClasses int, 
idSchool int foreign key references school_tb(idSchool) 
) 

找出在第一個表我要在同一所學校從事業SUM()人口populatuon。 我需要創建一個觸發器,當我更新career_tb中的人口時,將更新表school_tb中的列人數。請幫幫我。 我有這樣的事情,但我不能得到它的工作。

--create trigger updatePopulation 
--on career_tb 
--for update as 
--if UPDATE(carrerPopulation) 
--update school_tb set schoolPopulation =(SELECT add(carrerPopulation) 
--         from career_tb 
--         where idSchool=(SELECT idSchool 
--         from career_tb 
--         where [email protected]) 
--         ) 
--go 

我很感激任何幫助。謝謝

+0

順便說一句,我使用SQL Server 2008 R2 – itsmedavid 2012-03-25 13:45:10

回答

3

這應該會幫助你。請參閱觸發器正文內的註釋。

create trigger updatePopulation 
on career_tb 
-- to update sum even if carreer gets deleted or inserted 
after insert, update, delete 
as 
-- to avoid trigger messing up rows affected 
    set nocount on 

    if UPDATE(carrerPopulation) 
    begin 
    -- update sum by difference between previous and current state of one record in career 
     update school_tb 
     set schoolPopulation = schoolPopulation + difference 
     from school_tb 
     -- derived table sums all the careers changed in one go 
     inner join 
     (
     -- sum all values from careers by school 
      select idSchool, sum (carrerPopulation) difference 
      from 
      (
       -- change sign of previous values 
       select deleted.idSchool, -deleted.carrerPopulation carrerPopulation 
        from deleted 
       union all 
       -- + current values 
       select inserted.idSchool, inserted.carrerPopulation 
        from inserted 
      ) a 
       group by idSchool 
      -- Skip update in case of no change 
       having sum (carrerPopulation) <> 0 
     ) a 
      on school_tb.idSchool = a.idSchool 
    end 
+0

打招呼。謝謝。爲了快速回答,一直在嘗試你的答案,但在選擇刪除和​​插入的值之前仍然無法在選擇中工作,正在說無效的列名:S我真的很感謝你的回答。謝謝 – itsmedavid 2012-03-25 14:03:16

+0

我已經改變了我的迴應,請再試一次。 – 2012-03-25 14:36:01

+0

我收到了此消息8155,級別16,狀態2,過程updatePopulation,第30行 未對'a'的列2指定列名稱。 消息207,級別16,狀態1,過程updatePopulation,第32行 無效的列名稱'carrerPopulation'。 消息207,級別16,狀態1,過程updatePopulation,第19行 無效的列名稱'carrerPopulation'。 – itsmedavid 2012-03-25 15:00:24

2
CREATE TRIGGER name ON career_tb 
AFTER INSERT, UPDATE, DELETE 
AS 
BEGIN 
    SET NOCOUNT ON; 

    MERGE school_tb T 
    USING 
    (
     SELECT idSchool, SUM(carrerPopulation) res 
     FROM 
     (
      SELECT idSchool, carrerPopulation 
      FROM INSERTED 
      UNION ALL 
      SELECT idSchool, -carrerPopulation 
      FROM DELETED 
     ) t 
     GROUP BY idSchool 
    ) S 
    ON T.idSchool = S.idSchool 
    WHEN MATCHED THEN UPDATE SET 
     schoolPopulation = T.schoolPopulation +S.res 
    ; 


END 
+0

我試過編碼,沒有工作,要麼說'後'試圖把它關閉不正確的語法,它告訴我'對象career_tb不存在或是無效的(它確實存在)感謝您的快速答案I欣賞它 – itsmedavid 2012-03-25 14:11:21

+0

嗨。我將下面的代碼添加到您的答案'如果更新(poblacion)',它允許我創建觸發器,但它正在用一個瘋狂的數字更新schoolPopulation值。例如-9990當我將職業生涯更新爲1000人時人口人口 – itsmedavid 2012-03-25 14:24:26

+0

@itsmedavid更正了,我認爲你應該刪除'If update(population)' - 或者檢查它是否可以刪除。 – 2012-03-26 09:16:02