))大家好,目前我陷入了一个问题,希望一些优秀的PostgreSQL同事程序员可以帮我一把。这是我的桌子......
我只想选择一个"时间"行, WHERE" time_type" ='开始'或" time_type" =' Break',但只有一个,位于底行(降序)的那个(ORDER BY" fn_serial" DESC LIMIT 1)。
我成功地使用此触发功能...
CREATE OR REPLACE FUNCTION timediff()
RETURNS trigger AS
$BODY$
DECLARE
prevtime character varying;
BEGIN
SELECT t.time FROM table_ebscb_spa_log04 t WHERE t.fn_name = NEW.fn_name AND (t.time_type = 'Start' OR time_type = 'Break') ORDER BY t.fn_serial DESC LIMIT 1 INTO prevtime;
IF NOT FOUND THEN
RAISE EXCEPTION USING MESSAGE = 'NOT FOUNDED';
ELSE
NEW.time_elapse := prevtime
END IF;
return NEW;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION timediff()
OWNER TO postgres;
但是在我的脚本中,我想根据是否" fn_type"执行不同的操作。 ='开始'或者" fn_type ='休息',我的意思是" prevtime"变量来自,例如:
IF "prevtime" came from "fn_type" = 'Start' THEN
RAISE EXCEPTION USING MESSAGE = 'PREVTIME CAME FROM START';
ELSIF "prevtime" came from "fn_type" = 'BREAK' THEN
RAISE EXCEPTION USING MESSAGE = 'PREVTIME CAME FROM BREAK';
我很难想象有办法做到这一点,所以我想提出建议。
我想有一种方法可以实现这一点,创建一个子IF,检查哪一个('开始' OR' Break')位于底行(降序)。我怎么能这样做?或者什么是更好的方法?
谢谢高级。
答案 0 :(得分:0)
使用多个变量。
CREATE OR REPLACE FUNCTION timediff()
RETURNS trigger AS
$BODY$
DECLARE
prevtime character varying;
time_type character varying;
BEGIN
SELECT t.time, t.time_type FROM table_ebscb_spa_log04 t WHERE t.fn_name = NEW.fn_name AND (t.time_type = 'Start' OR time_type = 'Break') ORDER BY t.fn_serial DESC LIMIT 1 INTO prevtime,time_type;
IF NOT FOUND THEN
RAISE EXCEPTION USING MESSAGE = 'NOT FOUND';
ELSE
NEW.time_elapse := prevtime;
RAISE NOTICE "THE TIME CAME FROM %", time_type;
END IF;
return NEW;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION timediff()
OWNER TO postgres;