使用 If 语句在触发器中分配变量
Using an If statement to assign a variable within a Trigger
我正在尝试根据 if 语句为变量赋值,该 if 语句用于作为触发器
的一部分插入 table
我正在尝试根据计算的另一个变量是否大于零来为变量赋值。
CREATE OR REPLACE TRIGGER transactions_checking_trigger
AFTER INSERT OR UPDATE ON checking
FOR EACH ROW
DECLARE
account_type char(4);
account_num char(4);
trans_date date;
trans_amt number;
trans_type char(4);
trans_comments varchar(25);
BEGIN
account_type := 'CHEK';
trans_date := sysdate;
trans_amt := :new.balance-:old.balance;
if trans_amt > 0
trans_type := 'DEPT'
ELSE
trans_type := 'WDRW'
end if;
IF UPDATE THEN
INSERT INTO transactions (account_type, account_num, trans_date,
trans_amt, trans_type,trans_comments)
VALUES (account_type, :new.account_num,trans_date,trans_amt, trans_type,
new:trans_comments);
END IF;
END;
/
我希望触发器在 trans_amt > 0 时插入 DEPT,在 trans_amt < 0
时插入 WDRW
语法为IF-THEN-ELSE
;你错过了 THEN
。此外,语句必须以分号结束。
if trans_amt > 0
then --> this
trans_type := 'DEPT'; --> semi-colon
ELSE
trans_type := 'WDRW'; --> semi-colon
end if;
更简单的选项:
trans_type := case when trans_amt > 0 then 'DEPT'
else 'WDRW'
end;
这是错误的:
IF UPDATE THEN
您是说 updating
吗?
我正在尝试根据 if 语句为变量赋值,该 if 语句用于作为触发器
的一部分插入 table我正在尝试根据计算的另一个变量是否大于零来为变量赋值。
CREATE OR REPLACE TRIGGER transactions_checking_trigger
AFTER INSERT OR UPDATE ON checking
FOR EACH ROW
DECLARE
account_type char(4);
account_num char(4);
trans_date date;
trans_amt number;
trans_type char(4);
trans_comments varchar(25);
BEGIN
account_type := 'CHEK';
trans_date := sysdate;
trans_amt := :new.balance-:old.balance;
if trans_amt > 0
trans_type := 'DEPT'
ELSE
trans_type := 'WDRW'
end if;
IF UPDATE THEN
INSERT INTO transactions (account_type, account_num, trans_date,
trans_amt, trans_type,trans_comments)
VALUES (account_type, :new.account_num,trans_date,trans_amt, trans_type,
new:trans_comments);
END IF;
END;
/
我希望触发器在 trans_amt > 0 时插入 DEPT,在 trans_amt < 0
时插入 WDRW语法为IF-THEN-ELSE
;你错过了 THEN
。此外,语句必须以分号结束。
if trans_amt > 0
then --> this
trans_type := 'DEPT'; --> semi-colon
ELSE
trans_type := 'WDRW'; --> semi-colon
end if;
更简单的选项:
trans_type := case when trans_amt > 0 then 'DEPT'
else 'WDRW'
end;
这是错误的:
IF UPDATE THEN
您是说 updating
吗?