2011-07-12 77 views
2

我正在將現有SQL Server 2005數據庫轉換爲PostgreSQL 9.0數據庫。將SQL Server觸發器轉換爲PostgreSQL使用觸發器功能觸發問題

一切正常,直到現在。我想將SQL觸發器轉換爲PostgreSQL,但是觸發器功能有問題。

我不知道如何在PostgreSQL語法中實現臨時表inserted。在SQL Server中,inserted表存在,但在PostgreSQL中不存在。有任何想法嗎?

我的代碼(PostgreSQL的):

CREATE OR REPLACE FUNCTION func_co_insert() 

    RETURNS trigger AS 

$BODY$begin 

    declare 

    aa bigint; 

begin 

    select aa = co_id from inserted; 

    update com03 set co_creationdate = CURRENT_TIMESTAMP, 

    co_creationby = USER where co_id = aa; 

end; 

end; 

這裏的SQL Server觸發器主體2005代碼的代碼

begin 

    declare @aa bigint; 

    select @aa = se_id from inserted; 

    update server set se_creationdate = CURRENT_TIMESTAMP , se_creationby = USER where se_id = @aa; 

end; 

感謝

克里斯

回答

3

中的默認值PostgreSQL是一個行級觸發器(與SQL Server中它是一個語句級觸發器相反),因此不需要「插入」表進行選擇。

可以使用關鍵字newold(插入觸發器不存在舊的值)訪問新值和舊值。

在你的情況的聲明,簡直是:

update com03 
     set co_creationdate = CURRENT_TIMESTAMP, 
     co_creationby = CURRENT_USER 
    where co_id = new.co_id; 

無需「從插入的選擇」。

這裏假定觸發器是而不是爲表com03點火。如果您com03觸發器觸發(你沒有告訴我們),那麼它更容易:

new.co_creationdate := current_timestamp; 
    new.co_creationby := current_user; 

有關詳細信息,請參閱手冊:http://www.postgresql.org/docs/current/static/plpgsql-trigger.html

該頁面還包含做一個例子正是你想要實現的

+0

thx正常工作! – user840167