After Insert触发器中有关“新”值的问题

时间:2019-02-11 20:18:52

标签: sql oracle plsql

我创建了一个After Insert触发器,但是在编译它时出现此错误:

LINE/COL  ERROR
--------- -------------------------------------------------------------
35/8      PL/SQL: SQL Statement ignored
84/18     PL/SQL: ORA-00918: column ambiguously defined
Errors: check compiler log

触发器定义为:

CREATE or replace TRIGGER ai_s1_port
AFTER INSERT ON s1_port
REFERENCING new AS new
FOR EACH ROW
DECLARE
   v_count                             number;
   v_db_session_id                     number(18,0);
BEGIN

   SELECT COUNT(1)
     INTO v_count
     FROM s1_grainsmart_option sgo
    WHERE sgo.advanced_ship_management_flag = 'Y'
       OR sgo.asm_bv_flag = 'Y';

   IF v_count = 1 THEN

       INSERT INTO gtt_possible_port_ids (serial_nbr, 
                                          port_id
                                  )
       SELECT :new.serial_nbr,
              'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,'0')
         FROM s1_name_and_address snaa
        WHERE 'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,'0') = snaa.name_and_address_id
          AND snaa.name_and_address_id IS NULL        
        UNION
       SELECT :new.serial_nbr,
              'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,'-')
         FROM s1_name_and_address snaa
        WHERE 'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,'=') = snaa.name_and_address_id
          AND snaa.name_and_address_id IS NULL
        UNION
       SELECT :new.serial_nbr,
              'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,' ')
         FROM s1_name_and_address snaa
        WHERE 'PORT' || LPAD(TRIM(LEADING '0' FROM :new.serial_nbr),6,' ') = snaa.name_and_address_id
          AND snaa.name_and_address_id IS NULL; 

       INSERT INTO s1_name_and_address (name_and_address_id,
                                        full_name,
                                        city,
                                        state_province_code,
                                        zip_postal_code,
                                        parent_record_id,
                                        ship_to_flag,
                                        short_name,
                                        country_code,
                                        state_province_name,
                                        long_name,
                                        add_by,
                                        add_date,
                                        change_by,
                                        change_date
                                       )
       SELECT MAX(tblPI.port_id),
              :new.port_name,
              :new.city_name,
              :new.state_province_code,
              sc.zip_postal_code,
              MAX(tblPI.port_id),
              'Y',
              SUBSTR(1,:new.port_name,10),
              :new.country_code,
              ssp.name,
              :new.port_name,
              'ANY USER',
              SYSDATE,
              'ANY USER',
              SYSDATE       
         FROM gtt_possible_port_ids tblPI
              JOIN s1_city sc
                ON :new.city_name = sc.city_name
               AND :new.state_province_code = sc.state_province_code
               AND :new.country_code = sc.country_code
              JOIN s1_state_province ssp
                ON :new.state_province_code = ssp.state_province_code
               AND :new.country_code = ssp.country_code
        WHERE tblPI.serial_nbr = :new.serial_nbr
        GROUP BY tblPI.serial_nbr,
                 :new.port_name,
                 :new.city_name,
                 :new.state_province_code,
                 sc.zip_postal_code,
                 :new.port_name,
                 :new.country_code,
                 ssp.name,
                 add_by,
                 change_by; 

   END IF;

END;
/

我使用的全局临时表定义为:

CREATE GLOBAL TEMPORARY TABLE gtt_possible_port_ids
(
   serial_nbr                      number(12,0)                not null,
   port_id                         varchar2(10)                not null,
   CONSTRAINT pk_gtt_possible_port_ids PRIMARY KEY (serial_nbr)
)
ON COMMIT DELETE ROWS
tablespace temp
/

一个示例INSERT SQL是:

insert into s1_port (serial_nbr, port_name, city_name, state_province_code, country_code, un_locode, name_and_address_id) 
values (2,'Test1','Kitchener','ON','CAN','23456',NULL);

我不理解“列的定义不正确的错误;据我所知,该列是由“:new”唯一定义的。

表定义:

  CREATE TABLE s1_name_and_address
  (
     name_and_address_id             varchar2(10)                not null,
     full_name                       varchar2(50 char)           not null,
     city                            varchar2(32 char)           null,
     state_province_code             varchar2(3)                 null,
     zip_postal_code                 varchar2(10)                null,
     parent_record_id                varchar2(10)                not null,
     ship_to_flag                    char(1)       DEFAULT 'N'   not null
         CONSTRAINT ckc_naa_ship_to_flag CHECK (ship_to_flag IN ('Y','N')),
     short_name                      varchar2(10)                null,
     country_code                    varchar2(3)                 not null,
     state_province_name             varchar2(50 char)           null,
     long_name                       varchar2(250 char)          not null,
     add_by                          varchar2(40)  DEFAULT user  not null,
     add_date                        date        DEFAULT sysdate not null,
     change_by                       varchar2(40)  DEFAULT user  not null,
     change_date                     date        DEFAULT sysdate not null,
     CONSTRAINT pk_name_and_address PRIMARY KEY (name_and_address_id)
         using index
         tablespace smartsoft_index
  )
  tablespace smartsoft_data
  /

s1_city表:

  CREATE TABLE s1_city
  (
     city_name                       varchar2(32 char)           not null,
     state_province_code             varchar2(3)                 not null,
     country_code                    varchar2(3)                 not null,
     zip_postal_code                 varchar2(10)                null,
     op_software_interface           varchar2(30)                null,
     add_by                          varchar2(40)                null,
     add_date                        date                        null,
     change_by                       varchar2(40)                null,
     change_date                     date                        null,
     CONSTRAINT pk_city PRIMARY KEY (city_name, state_province_code, country_code)
         using index
         tablespace smartsoft_index
  )
  ORGANIZATION HEAP
  tablespace smartsoft_data
  /

s1_state_province

  CREATE TABLE s1_state_province
  (
     state_province_code             varchar2(3)                 not null,
     country_code                    varchar2(3)                 not null,
     name                            varchar2(50 char)           null,
     sc_tax_2_nbr                    number(5)                   null,
     sc_tax_2_code                   varchar2(10)                null,
     pc_tax_2_nbr                    number(5)                   null,
     pc_tax_2_code                   varchar2(10)                null,
     sc_tax_3_nbr                    number(5)                   null,
     sc_tax_3_code                   varchar2(10)                null,
     pc_tax_3_nbr                    number(5)                   null,
     pc_tax_3_code                   varchar2(10)                null,
     tax_1_exempt_flag               char(1)       DEFAULT 'N'   not null
         CONSTRAINT ckc_sp_tax_1_exempt_flag CHECK (tax_1_exempt_flag IN ('Y','N')),
     tax_2_exempt_flag               char(1)       DEFAULT 'N'   not null
         CONSTRAINT ckc_sp_tax_2_exempt_flag CHECK (tax_2_exempt_flag IN ('Y','N')),
     tax_3_exempt_flag               char(1)       DEFAULT 'N'   not null
         CONSTRAINT ckc_sp_tax_3_exempt_flag CHECK (tax_3_exempt_flag IN ('Y','N')),
     add_by                          varchar2(40)                null,
     add_date                        date                        null,
     change_by                       varchar2(40)                null,
     change_date                     date                        null,
     CONSTRAINT pk_state_province PRIMARY KEY (state_province_code, country_code)
         using index
         tablespace smartsoft_index
  )
  ORGANIZATION HEAP
  tablespace smartsoft_data
  /

1 个答案:

答案 0 :(得分:1)

在没有涉及的所有表的完整定义(即s1_citys1_state_province以及s1_name_and_address的情况下),我只能猜测实际的问题..但是,我猜测是可能您在多个表中都有 add_by 列和 change_by 列,并且对此有所抱怨。

检查您的定义..并通过以下方法弄清您要从组中的哪些列:

    GROUP BY tblPI.serial_nbr,
             :new.port_name,
             :new.city_name,
             :new.state_province_code,
             sc.zip_postal_code,
             :new.port_name,
             :new.country_code,
             ssp.name,
             add_by,     -- need to clarify this column
             change_by;   -- need to clarify this column

关于错误的事情,如果您找到它们所指的行,它们将足够准确。

LINE/COL  ERROR
--------- -------------------------------------------------------------
35/8      PL/SQL: SQL Statement ignored
84/18     PL/SQL: ORA-00918: column ambiguously defined
Errors: check compiler log

在这种情况下,它从DECLARE开始计数,而不是CREATE ..所以在第35行:

   INSERT INTO s1_name_and_address (name_and_address_id,

第84行是:

             change_by; 

因此肯定是在抱怨change_by列...一点推断表明add_by可能会有类似的问题;)