Oracle中触发器的问题
--库存信息CREATETABLESTORAGE(WARE_IDVARCHAR2(10),STORAGE_COUNTSNUMBER(10),CONSTRAINTSSTOR...
--库存信息
CREATE TABLE STORAGE (
WARE_ID VARCHAR2(10),
STORAGE_COUNTS NUMBER(10),
CONSTRAINTS STOR_PK_ID PRIMARY KEY(WARE_ID)
);
--进货数量
CREATE TABLE STOCK (
WARE_ID VARCHAR2(10),
STOCK_COUNTS NUMBER(10) NOT NULL,
CONSTRAINTS STO_PK_ID PRIMARY KEY(WARE_ID)
);
ALTER TABLE STOCK
ADD CONSTRAINTS STO_FK_COUNTS FOREIGN KEY(WARE_ID) REFERENCES STORAGE(WARE_ID);
CREATE OR REPLACE TRIGGER BI_STO
BEFORE INSERT OR UPDATE
ON STOCK
FOR EACH ROW
BEGIN
IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
WHERE STO.WARE_ID = :NEW.WARE_ID))
THEN
UPDATE STORAGE SET STORAGE.STORAGE_COUNTS =
(STORAGE.STORAGE_COUNTS + :NEW.STOCK_COUNTS)
WHERE STORAGE.WARE_ID = :NEW.WARE_ID;
ELSE IF(NOT EXISTS(SELECT STO.WARE_ID FROM STORAGE STO, STOCK
WHERE STO.WARE_ID = :NEW.WARE_ID))
THEN
INSERT INTO STORAGE VALUES(:NEW.WARE_ID, :NEW.STOCK_COUNTS);
END IF;
END IF;
END;
TRIGGER SGL.BI_STO 编译错误
错误:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用
行:7
文本:IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
错误:PL/SQL: Statement ignored
行:7
文本:IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
请问向上面那中情况怎么才能正确的返回一个布尔值?
时间越快,追加分越高!
一楼、二楼的答案都有一个相同的问题,
错误: ORA-OO922:选项缺失或无效
ORA-01008:并非所有的变量都已绑定 展开
CREATE TABLE STORAGE (
WARE_ID VARCHAR2(10),
STORAGE_COUNTS NUMBER(10),
CONSTRAINTS STOR_PK_ID PRIMARY KEY(WARE_ID)
);
--进货数量
CREATE TABLE STOCK (
WARE_ID VARCHAR2(10),
STOCK_COUNTS NUMBER(10) NOT NULL,
CONSTRAINTS STO_PK_ID PRIMARY KEY(WARE_ID)
);
ALTER TABLE STOCK
ADD CONSTRAINTS STO_FK_COUNTS FOREIGN KEY(WARE_ID) REFERENCES STORAGE(WARE_ID);
CREATE OR REPLACE TRIGGER BI_STO
BEFORE INSERT OR UPDATE
ON STOCK
FOR EACH ROW
BEGIN
IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
WHERE STO.WARE_ID = :NEW.WARE_ID))
THEN
UPDATE STORAGE SET STORAGE.STORAGE_COUNTS =
(STORAGE.STORAGE_COUNTS + :NEW.STOCK_COUNTS)
WHERE STORAGE.WARE_ID = :NEW.WARE_ID;
ELSE IF(NOT EXISTS(SELECT STO.WARE_ID FROM STORAGE STO, STOCK
WHERE STO.WARE_ID = :NEW.WARE_ID))
THEN
INSERT INTO STORAGE VALUES(:NEW.WARE_ID, :NEW.STOCK_COUNTS);
END IF;
END IF;
END;
TRIGGER SGL.BI_STO 编译错误
错误:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用
行:7
文本:IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
错误:PL/SQL: Statement ignored
行:7
文本:IF(EXISTS(SELECT STO.WARE_ID FROM STORAGE STO
请问向上面那中情况怎么才能正确的返回一个布尔值?
时间越快,追加分越高!
一楼、二楼的答案都有一个相同的问题,
错误: ORA-OO922:选项缺失或无效
ORA-01008:并非所有的变量都已绑定 展开
4个回答
展开全部
这样应该就可以了,不过自治事务用在业务比较复杂的数据库里面是要慎用的,很容易引发死锁,
create
or
replace
trigger
stu_upd
after
update
on
score
referencing
old
as
old
new
as
new
for
each
row
declare
pragma
autonomous_transaction
;
t_savg
number(6,2);
begin
select
avg(score)
into
t_savg
from
score
where
sno=:new.sno;
update
student
set
savg=t_savg
where
sno=:new.sno;
commit
;
end;
create
or
replace
trigger
stu_upd
after
update
on
score
referencing
old
as
old
new
as
new
for
each
row
declare
pragma
autonomous_transaction
;
t_savg
number(6,2);
begin
select
avg(score)
into
t_savg
from
score
where
sno=:new.sno;
update
student
set
savg=t_savg
where
sno=:new.sno;
commit
;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主要原因是调用biu_document
触发器时,没有commit,所以执行
select
sum(money)
into
countmoney
from
(select
sto.counts
*
sto.price
money
from
stock
sto
where
sto.document_id
=
:new.document_id)
stock_moeny
时,返回的是null值。
触发器之间也不能传递参数,这里建议写存储过程吧
在biu_stock触发器中调用存储过程分别向document,affairs写数据。
触发器时,没有commit,所以执行
select
sum(money)
into
countmoney
from
(select
sto.counts
*
sto.price
money
from
stock
sto
where
sto.document_id
=
:new.document_id)
stock_moeny
时,返回的是null值。
触发器之间也不能传递参数,这里建议写存储过程吧
在biu_stock触发器中调用存储过程分别向document,affairs写数据。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用select into 如果 查询出来的是null值,应该会报错吧
试试下面这个:
CREATE OR REPLACE TRIGGER BI_STO
BEFORE INSERT OR UPDATE
ON STOCK
FOR EACH ROW
declare
v_count int;
BEGIN
select count(*)
into v_count
from STORAGE STO
WHERE STO.WARE_ID = :NEW.WARE_ID;
if v_count > 0 then
UPDATE STORAGE
SET STORAGE_COUNTS = (STORAGE_COUNTS +:NEW.STOCK_COUNTS)
WHERE WARE_ID = :NEW.WARE_ID;
else
INSERT INTO STORAGE
VALUES(:NEW.WARE_ID,:NEW.STOCK_COUNTS);
end if;
END;
试试下面这个:
CREATE OR REPLACE TRIGGER BI_STO
BEFORE INSERT OR UPDATE
ON STOCK
FOR EACH ROW
declare
v_count int;
BEGIN
select count(*)
into v_count
from STORAGE STO
WHERE STO.WARE_ID = :NEW.WARE_ID;
if v_count > 0 then
UPDATE STORAGE
SET STORAGE_COUNTS = (STORAGE_COUNTS +:NEW.STOCK_COUNTS)
WHERE WARE_ID = :NEW.WARE_ID;
else
INSERT INTO STORAGE
VALUES(:NEW.WARE_ID,:NEW.STOCK_COUNTS);
end if;
END;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用SQL%ROWCOUNT判断SQL语句返回的行数。
你可以这样写:
...
FOR EACH ROW
DECLARE
v_ware_id STO.WARE_ID%type;
BEGIN
SELECT STO.WARE_ID INTO v_ware_id FROM STORAGE STO WHERE STO.WARE_ID = :NEW.WARE_ID)
IF SQL%ROWCOUNT > 0 THEN --即返回一行以上,相当于EXISTS
...
ELSE
SELECT STO.WARE_ID INTO v_ware_id FROM STORAGE STO, STOCK WHERE STO.WARE_ID = :NEW.WARE_ID
IF SQL%ROWCOUNT = 0 THEN --无返回行,相当于NOT EXISTS
...
END IF;
END IF;
END;
你可以这样写:
...
FOR EACH ROW
DECLARE
v_ware_id STO.WARE_ID%type;
BEGIN
SELECT STO.WARE_ID INTO v_ware_id FROM STORAGE STO WHERE STO.WARE_ID = :NEW.WARE_ID)
IF SQL%ROWCOUNT > 0 THEN --即返回一行以上,相当于EXISTS
...
ELSE
SELECT STO.WARE_ID INTO v_ware_id FROM STORAGE STO, STOCK WHERE STO.WARE_ID = :NEW.WARE_ID
IF SQL%ROWCOUNT = 0 THEN --无返回行,相当于NOT EXISTS
...
END IF;
END IF;
END;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询