检查记录是否在当月第一

时间:2019-05-15 20:27:38

标签: oracle plsql

我想检查添加的记录是否是该月的第一条记录。我究竟做错了什么?请告诉我如何做。

我像

CREATE TABLE Client_Statistics
(
id number(20),
client_id number(20),
year number(4) check (year between 1900 and 9999),
month number(2) check (month between 1 and 12),
order_count varchar2(10),
order_sum number(10,2)
);


CREATE SEQUENCE cs_seq 
START WITH 1 
INCREMENT BY 1;


CREATE OR REPLACE TRIGGER add_record_with_sequence
AFTER 
INSERT OR DELETE OR UPDATE ON Orders
FOR EACH ROW
DECLARE sel number;
csdate number;

BEGIN
    IF INSERTING OR DELETING OR UPDATING
    THEN
    INSERT INTO Client_Statistics (id, client_id, year, month, order_count, order_sum)
    VALUES (cs_seq.nextval, cs_seq.nextval, 2019, 5, cs_seq.nextval, cs_seq.nextval);
    END IF;
    SELECT COUNT(*) into sel
    FROM Client_Statistics;
IF sel != NULL
THEN 
    INSERT INTO Client_Statistics (id, client_id, year, month, order_count, order_sum)
    VALUES (cs_seq.nextval, cs_seq.nextval,  2019, 5, cs_seq.nextval, cs_seq.nextval);
END IF;
END add_record_with_sequence;

但是没有用

这是我的查询代码:

IF

如果我在第一个const foo = 'bar'; const baz = { foo }; // { foo: 'bar' }; 条件下的代码得到满足,我希望触发器能够起作用。预先感谢。

1 个答案:

答案 0 :(得分:1)

COUNT永远不会NULL,因此-您要检查的内容:

if sel != null

从未相遇。使用

if sel > 0 

代替