触发器

今天写了一个相对复杂的触发器(根据一个表自动生成另一个表的ID)

http://www.cnblogs.com/sekihin/archive/2009/08/14/1545867.html

http://blog.itpub.net/30010989/viewspace-1378060/

http://www.jsjtt.com/shujuku/postgresql/14.html

http://blog.csdn.net/o0wufan0o/article/details/9239201

http://www.cnblogs.com/stephen-liu74/archive/2012/05/02/2294071.html

 

test(id_user,id_rst,roles)  t2(id_user,id_order,num)

 

--SELECT('100000001'+(SELECT cast(random()*100000) as INTEGER))
---
--创建店铺的触发器 生成员工
CREATE OR REPLACE FUNCTION func_example() RETURNS TRIGGER AS $func_example$
BEGIN
INSERT INTO
t2(id_rst,id_user,roles)
SELECT (SELECT (cast(NEW.id_rst as VARCHAR)||
case when extract(month from now())<10 then '0'|| cast(extract(month from now()) as VARCHAR)
ELSE cast(extract(month from now()) as VARCHAR)
end ||
(select
case when extract(day from now())<10
then '0'|| cast(extract(day from now()) as VARCHAR)
ELSE cast(extract(day from now()) as VARCHAR)
end )||
cast(random()*99 as INTEGER))),NEW.id_user,'32';
RETURN NEW;
END;
$func_example$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS t_test ON test;
CREATE TRIGGER t_test AFTER INSERT ON test
FOR EACH ROW EXECUTE PROCEDURE func_example()

你可能感兴趣的:(触发器)