-
Notifications
You must be signed in to change notification settings - Fork 0
/
cr-eq_last_sent.sql
46 lines (37 loc) · 1.19 KB
/
cr-eq_last_sent.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- Function: eq_last_sent(character varying, character varying, numeric)
-- DROP FUNCTION eq_last_sent(character varying, character varying, numeric);
CREATE OR REPLACE FUNCTION eq_last_sent(
a_mod_id character varying,
a_time_delivery character varying,
a_qnt numeric)
RETURNS boolean AS
$BODY$
DECLARE
loc_time_delivery VARCHAR;
loc_qnt NUMERIC;
loc_equal BOOLEAN default false;
loc_id INTEGER;
loc_cnt INTEGER;
BEGIN
-- есть ли неотправленные
PERFORM * FROM arc_energo.stock_status_changed WHERE mod_id=a_mod_id AND change_status=0;
if found then
loc_equal := false;
else
SELECT time_delivery, qnt, id, dbl_cnt INTO loc_time_delivery, loc_qnt, loc_id, loc_cnt
FROM arc_energo.stock_status_changed
WHERE mod_id=a_mod_id
AND change_status=1
ORDER BY dt_sent DESC LIMIT 1;
loc_equal = (FOUND AND (loc_qnt=a_qnt) AND (loc_time_delivery=a_time_delivery));
IF loc_equal THEN
UPDATE stock_status_changed SET dbl_cnt = (loc_cnt + 1) WHERE id=loc_id;
END IF;
end if;
RETURN loc_equal;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION eq_last_sent(character varying, character varying, numeric)
OWNER TO arc_energo;