0
我只想維護當前1個月的記錄日誌細節。需要刪除過去的記錄日誌細節了嘗試這種代碼,但不能工作這一點,觸發器刪除postgresql中的過去記錄
create sequence userseq1;
CREATE TABLE users
(id integer NOT NULL DEFAULT nextval('userseq1'::regclass)
);
INSERT INTO users VALUES(126);
CREATE TABLE History
(userid integer
, createdate timestamp
);
CREATE OR REPLACE FUNCTION recordcreatetime() RETURNS trigger language plpgsql
AS $$
DECLARE
BEGIN
INSERT INTO History values(new.id,NOW());
RETURN NEW;
END;
$$;
CREATE TRIGGER user_hist
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE procedure recordcreatetime();
但是,它正在由一個adding.I插入值測序一個要刪除以前1月曆史日誌詳細信息。我想這下面的代碼,它不工作
CREATE OR REPLACE FUNCTION trf_keep_row_number_steady()
RETURNS TRIGGER AS
$body$
DECLARE
BEGIN
IF (SELECT count(createdate) FROM history) > rownum_limit
THEN
DELETE FROM history
WHERE createdate = (SELECT min(createdate) FROM history);
END IF;
END;
$body$
LANGUAGE plpgsql;
CREATE TRIGGER tr_keep_row_number_steady
AFTER INSERT ON history
FOR EACH ROW EXECUTE PROCEDURE trf_keep_row_number_steady();
感謝您的回覆。我試過,但它不工作。你可以提供任何文檔或示例代碼....... – Patrick 2013-03-26 11:43:15
其實postgresql允許這樣做。但@Patrick應該提供一個錯誤信息,而不是「它不工作」。乍一看,函數在語法上不正確,無論如何因爲這個'rownum_limit'沒有定義。 – 2013-03-26 17:03:55