PL / SQL Trigger中的SELECT语句返回null

时间:2011-07-22 13:30:30

标签: sql plsql triggers oracle11g

一位同事(具有与我的匹配的PL / SQL技能有限!)正试图在(基于Oracle的)ERP系统的后端创建触发器。他用以下样本为我简化了问题。

简而言之,他有一个可以呈现预期结果的select语句:

SELECT TO_CHAR(MAX(
RESULT_KEY))
FROM IFSINFO.QUOTATION_REPORTS
WHERE QUOTATION_NO = 'G1002387'

...但是当包含在触发器中时,似乎返回null:

CREATE OR REPLACE TRIGGER VMO_QUOTATION_LINK
BEFORE INSERT OR UPDATE
ON VMO_OPPORTUNITY_LINE
FOR EACH ROW

DECLARE
  QuotationLink VARCHAR2(255);

BEGIN
  SELECT TO_CHAR(MAX(RESULT_KEY))
  INTO QuotationLink
  FROM IFSINFO.QUOTATION_REPORTS
  WHERE QUOTATION_NO = 'G1002387';

  :NEW.URL5 := QuotationLink;
END;

我不是专家,但从表面上看,似乎没问题。有人可以建议吗?

我可以尝试另一种方法吗?关于如何调试这个的进一步建议?

1 个答案:

答案 0 :(得分:3)

create table quotation_reports (quotation_no varchar2(30), result_key number);

insert into quotation_reports values ('G1002387', 1);
insert into quotation_reports values ('G1002387', 10);

SQL> select to_char(max(result_key)) from quotation_reports where quotation_no = 'G1002387';

TO_CHAR(MAX(RESULT_KEY))
----------------------------------------
10

create table vmo_opportunity_line(url5 varchar2(300), test_column varchar2(100));

CREATE OR REPLACE TRIGGER VMO_QUOTATION_LINK
BEFORE INSERT OR UPDATE ON VMO_OPPORTUNITY_LINE
FOR EACH ROW
   DECLARE
QuotationLink VARCHAR2(255);
BEGIN
    SELECT TO_CHAR(MAX(RESULT_KEY))   INTO QuotationLink
    FROM QUOTATION_REPORTS
WHERE QUOTATION_NO = 'G1002387';
    :NEW.URL5 := QuotationLink;
END;

insert into vmo_opportunity_line(test_column) values ('A');

select * from vmo_opportunity_line;

URL5        TEST
-----       ------
10          'A'

此示例工作正常。我没有看到任何错误。