我想创建一个触发器,但是我在某个方面遇到了麻烦。我想要的触发器是检查正在插入的产品的库存水平,如果库存水平= 0,则触发器将阻止插入,如果水平介于两个值之间,则它将允许插入但显示警告并且最后如果级别高于某个值,则不会插入任何问题。插入触发器之前的SQL
我的问题是从插入中提取信息,以获得触发变量@stock_level
的正确值。
下面我有一个样本插入语句,将从GUI中检索,下面是我计划使用的触发器。我已经手动输入了触发器中所需的信息,但是可以提取这些信息而无需在值中进行硬编码?
插入语句
--sample insert
insert into sales_table values (1,2,3,4, '12/31/2015', '12:30:21');
触发
create or replace trigger inv_check
before insert on SALES_TABLE
--declaring variable that contains the stock number of the product being inserted
--4 is the product code and 1 is the outlet_number taken from the above
DECLARE @stock_level = (select quantity from inventory_table where product_code = 4 and outlet_number = 1);
BEGIN
IF @stock_level = 0 then
DBMS_OUTPUT.PUT_LINE("Sorry, out of stock");
ROLLBACK;
ELSIF @stock_level > 0 and @stcok_level < 15 then
DBMS_OUTPUT.PUT_LINE("Running low order more");
--continue with insert
ELSE
DBMS_OUTPUT.PUT_LINE("Inserting row...");
END;