日期:2014-05-17  浏览次数:20771 次

Oracle中使用触发器
C# code
CREATE OR REPLACE TRIGGER SCOTT.SET_UPDATE 
BEFORE INSERT 
ON SCOTT.Table 
FOR EACH ROW 
DECLARE 
JG VARCHAR2(50); 
BEGIN 
SELECT DIC_XZQH.NAME INTO JG FROM DIC_XZQH WHERE DIC_XZQH.CODE = :NEW.Table_JG_CODE; 
:NEW.Table_JG_NAME := JG; 
END SET_UPDATE; 
/ 
我是通过字典表 更新Table, 字典表DIC_XZQH 两列  Code    Name; 
当在字典表里能找到Table里的Table_JG_CODE的时候即:DIC_XZQH.CODE = Table_JG_CODE成立,上面句子执行顺利, 
当在Table里的Table_JG_CODE在DIC_XZQH得不到映射的时候,就是没有的时候,触发器提示找不到数据, 
这个问题能解决下么? 两个表不能完全映射情况既定,我想的是 能不能找不到的时候就插入空 或什么的?


------解决方案--------------------
CREATE OR REPLACE TRIGGER SCOTT.SET_UPDATE 
BEFORE INSERT 
ON SCOTT.Table 
FOR EACH ROW 
DECLARE 
JG VARCHAR2(50); 
l_count pls_integer;
BEGIN 
select count(*) into l_count from DIC_XZQH where DIC_XZQH.CODE = :NEW.Table_JG_CODE;
if nvl(l_count,0)<>0 then
SELECT DIC_XZQH.NAME INTO JG FROM DIC_XZQH WHERE DIC_XZQH.CODE = :NEW.Table_JG_CODE; 
:NEW.Table_JG_NAME := JG; 
end if;
END SET_UPDATE;
------解决方案--------------------
楼上的可以,也可以这样
CREATE OR REPLACE TRIGGER SCOTT.SET_UPDATE 
BEFORE INSERT 
ON SCOTT.Table 
FOR EACH ROW 
DECLARE 
JG VARCHAR2(50); 
BEGIN 
SELECT NVL(DIC_XZQH.NAME,' ') INTO JG FROM DIC_XZQH WHERE DIC_XZQH.CODE = :NEW.Table_JG_CODE; 
:NEW.Table_JG_NAME := JG; 
END SET_UPDATE; 


------解决方案--------------------
探讨
CREATE OR REPLACE TRIGGER SCOTT.SET_UPDATE
BEFORE INSERT
ON SCOTT.Table
FOR EACH ROW
DECLARE
JG VARCHAR2(50);
l_count pls_integer;
BEGIN
select count(*) into l_count from DIC_XZQH where DIC_XZQH.CODE = :NEW.Table_JG_CODE;
if nvl(l_count,0) <>0 then
SELECT DIC_XZQH.NAME INTO JG FROM DIC_XZQH WHERE DIC_XZQH.CODE = :NEW.Table_JG_CODE;
:NEW.Table_JG_NAME := JG;
end if;
END SET_UPDATE;