2014-08-30 3 views
0

내가 새로운 레코드를 삽입하기 전에 삽입하기 전에 오래된 레코드를 고스트 (일부는 비활성화 된 것)로 업데이트하고 마침내이 새로운 레코드를 추가하고 싶습니다. 그래서 내가 새로운 레코드를 삽입했을 때 내가 정보새로운 레코드를 삽입하기 전에 모든 레코드를 업데이트하는 방법은 무엇입니까?

SQL statement "UPDATE dice_game_seed SET ghost = true WHERE ghost = false" 
PL/pgSQL function "trg_ghost_game_seed_ins_bef" line 3 at SQL statement 

이 간단한 트리거 기능

CREATE OR REPLACE FUNCTION trg_ghost_game_seed_ins_bef() 
    RETURNS trigger AS 
$$ 
BEGIN 
    UPDATE dice_game_seed SET ghost = true WHERE ghost = false; 
RETURN NEW; 
END 
$$ LANGUAGE plpgsql; 

CREATE TRIGGER ins_up_bef 
BEFORE INSERT OR UPDATE ON dice_game_seed 
FOR EACH ROW 
EXECUTE PROCEDURE trg_ghost_game_seed_ins_bef(); 

을 준비하지만 무슨 잘못 라인 3 ???

+0

당신은 트리거로 말하고있다 : 당신이 거래의 어떤 종류를 사용하는 경우 업데이트 업데이트, 그것은 무한 루프 – Houari

+0

같은 경우 무의미이 접근하지 않습니다. 그냥 추측 해. – gorn

답변

1

당신은 무한 재귀를 해결하려면 pg_trigger_depth() 기능을 사용할 수 있습니다 :

create or replace function trg_ghost_game_seed_ins_bef() 
    returns trigger as 
$$ 
begin 
    if (pg_trigger_depth() = 1) then 
     update dice_game_seed set ghost = true where ghost = false; 
    end if; 
    return null; 
end 
$$ language plpgsql; 

create trigger ins_up_bef 
before insert or update on dice_game_seed 
for each statement 
execute procedure trg_ghost_game_seed_ins_bef(); 

당신은 대신 행 트리거의 문 트리거를 사용할 수 있습니다.

Example SQLFiddle

관련 문제