2014-11-17 16 views
0

我想寫一個比較舊值和新值的sql觸發器。如果這兩個值不同,那麼我需要顯示一個錯誤,說您不能更新名稱。這是我似乎有問題,我不知道如何在PSQL異常顯示一個錯誤我觸發的確切定義是在PL/PgSQL中爲函數創建特定的異常

write a trigger function named disallow_team_name_update that compares the OLD and NEW records 
team fields. If they are different raise an exception that states that changing the team name is 
not allowed. 

,我使用了這個問題的表

Table "table.group_standings" 
    Column |   Type   | Modifiers 
    --------+-----------------------+----------- 
    team | character varying(25) | not null 
    wins | smallint | not null 
    losses | smallint | not null 
    draws | smallint | not null 
    points | smallint| not null 
    Indexes: 
    "group_standings_pkey" PRIMARY KEY, btree (team) 
    Check constraints: 
    "group_standings_draws_check" CHECK (draws >= 0) 
    "group_standings_losses_check" CHECK (losses >= 0) 
    "group_standings_points_check" CHECK (points >= 0) 
    "group_standings_wins_check" CHECK (wins >= 0) 

我現在所擁有的代碼,我需要告訴他們不出聲來改變球隊的名字用戶的幫助,但我有問題這樣做。

CREATE OR REPLACE FUNCTION disallow_team_name_update() RETURNS trigger AS $$ 
    BEGIN 
      if(NEW.team <> OLD.team) 
      /*tell the user to not change team names*/ 

    END; 
$$ LANGUAGE plpgsql; 


CREATE TRIGGER tr_disallow_team_name_update BEFORE INSERT OR UPDATE OF team ON group_standings 
FOR EACH ROW EXECUTE PROCEDURE disallow_team_name_update(); 

回答

0

您需要RAISE statement來引發異常。手冊中有例子。

RAISE EXCEPTION .... 

(這看起來像功課,你以前問功課,所以我故意不給一個完整的答案)

相關問題