简体   繁体   中英

SELECT statement in PL/SQL Trigger returns null

A colleague (with limited PL/SQL skills to match mine.) is trying to create a trigger the backend of an (Oracle-based) ERP system. He's simplified the problem for me with the following samples.

In short, he has a select statement that renders the expected result:

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

...but when included in a trigger is appears to return 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;

I'm no expert, but on the face of it, it seems OK. Can anyone advise?

Is there an alternative approach I can try? And further suggestions on how to debug this?

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'

This sample works fine. I don't see anything wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM