0

这是我的触发器,当我尝试执行此操作时出现错误

扳机:

CREATE OR REPLACE FUNCTION vqm_minoutline_cp_trg()
  RETURNS trigger AS
$BODY$ DECLARE 
   --TYPE RECORD IS REFCURSOR;
  Cur_Defaults                RECORD;
  v_M_Product_ID              VARCHAR(32); --OBTG:varchar2--
  v_VQM_Parameter_ID          VARCHAR(32); --OBTG:varchar2--
  vqm_minoutline_parameter_id    VARCHAR(32);
  v_count             NUMERIC;
BEGIN

    IF AD_isTriggerEnabled()='N' THEN IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; 
    END IF;

    --  Default Quality Parameter for Product
    IF (TG_OP = 'INSERT') THEN

    FOR Cur_Defaults IN
(
    SELECT VQM_Parameter_ID, Description, Criteria, MinValue, MaxValue, TextValue
        FROM  VQM_Product_Parameter VPP
    WHERE VPP.M_PRODUCT_ID=new.M_PRODUCT_ID

)
LOOP
    /*
        Creating quality lines for Purchase Order Line
    */

     SELECT * INTO  vqm_minoutline_parameter_id FROM Ad_Sequence_Next('vqm_minoutline_parameter', Cur_Defaults.VQM_Parameter_ID);

          INSERT INTO vqm_minoutline_parameter    //here line 65 and so on
          (vqm_minoutline_parameter_id, m_inoutline_id, VQM_PARAMETER_ID, AD_Client_ID, AD_Org_ID, 
          IsActive, Created, CreatedBy, Updated, UpdatedBy, Criteria, MaxValue, MinValue, 
          TextValue, Description)
          VALUES
          (vqm_minoutline_parameter_id, new.m_inoutline_id, Cur_Defaults.VQM_PARAMETER_ID, 
          new.AD_Client_ID, new.AD_Org_ID, 'Y', TO_DATE(NOW()), new.CreatedBy, TO_DATE(NOW()), new.UpdatedBy,
          Cur_Defaults.Criteria, Cur_Defaults.MaxValue, Cur_Defaults.MinValue, Cur_Defaults.TextValue, 
          Cur_Defaults.Description);

END LOOP;


ELSIF (TG_OP = 'UPDATE') THEN

    IF new.M_PRODUCT_ID != old.M_PRODUCT_ID THEN

        DELETE FROM vqm_minoutline_parameter WHERE m_inoutline_id = new.m_inoutline_id;

        FOR Cur_Defaults IN
        (
        SELECT VQM_Parameter_ID, Description, Criteria, MinValue, MaxValue, TextValue
        FROM  VQM_Product_Parameter VPP
        WHERE VPP.M_PRODUCT_ID=new.M_PRODUCT_ID
        )
        LOOP
        /*
        Creating quality lines for Purchase Order Line
        */

        SELECT * INTO  vqm_minoutline_parameter_id FROM Ad_Sequence_Next('vqm_minoutline_parameter', Cur_Defaults.VQM_Parameter_ID);

        INSERT INTO vqm_minoutline_parameter
        (
        vqm_minoutline_parameter_id, m_inoutline_id, VQM_PARAMETER_ID, AD_Client_ID, AD_Org_ID, 
        IsActive, Created, CreatedBy, Updated, UpdatedBy, Criteria, MaxValue, MinValue, 
        TextValue, Description
        )

        VALUES
        (
        vqm_minoutline_parameter_id, new.m_inoutline_id, Cur_Defaults.VQM_PARAMETER_ID, 
        new.AD_Client_ID, new.AD_Org_ID, 'Y', TO_DATE(NOW()), new.CreatedBy, TO_DATE(NOW()), new.UpdatedBy,
        Cur_Defaults.Criteria, Cur_Defaults.MaxValue, Cur_Defaults.MinValue, Cur_Defaults.TextValue, 
        Cur_Defaults.Description
        );

        END LOOP;

    END IF; 

END IF;

    IF TG_OP = 'DELETE' THEN RETURN OLD; ELSE RETURN NEW; END IF; 

END 

; $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION vqm_minoutline_cp_trg()
  OWNER TO saksham27;

这是错误:

ERROR:  syntax error at or near "$1"
LINE 1: INSERT INTO vqm_minoutline_parameter ( $1 , m_inoutline_id, ...
                                           ^
QUERY:  INSERT INTO vqm_minoutline_parameter ( $1 , m_inoutline_id, VQM_PARAMETER_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, Criteria, MaxValue, MinValue, TextValue, Description) VALUES ( $1 ,  $2 ,  $3 ,  $4 ,  $5 , 'Y', TO_DATE(NOW()),  $6 , TO_DATE(NOW()),  $7 ,  $8 ,  $9 ,  $10 ,  $11 ,  $12 )
CONTEXT:  SQL statement in PL/PgSQL function "vqm_minoutline_cp_trg" near line 66


********** Error **********

ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "vqm_minoutline_cp_trg" near line 66
4

1 回答 1

0

在此查询中:

  INSERT INTO vqm_minoutline_parameter    //here line 65 and so on
  (vqm_minoutline_parameter_id, m_inoutline_id, VQM_PARAMETER_ID, AD_Client_ID, AD_Org_ID, 
  IsActive, Created, CreatedBy, Updated, UpdatedBy, Criteria, MaxValue, MinValue, 
  TextValue, Description)
  VALUES
  (vqm_minoutline_parameter_id, new.m_inoutline_id, Cur_Defaults.VQM_PARAMETER_ID, 
  new.AD_Client_ID, new.AD_Org_ID, 'Y', TO_DATE(NOW()), new.CreatedBy, TO_DATE(NOW()), new.UpdatedBy,
  Cur_Defaults.Criteria, Cur_Defaults.MaxValue, Cur_Defaults.MinValue, Cur_Defaults.TextValue, 
  Cur_Defaults.Description);

vqm_minoutline_parameter_id同时使用列名和 plpgsql 变量名。结果,plpgsql 替换了$1导致错误查询的列名。

解决方法是将变量名称更改为不与任何列名称冲突的名称。

该文档在Variable Substitution中提到了该问题,并附有以下说明:

注意:9.0 之前的 PostgreSQL 版本会尝试在所有三种情况下替换变量,从而导致语法错误。

大概您使用的是 9.0 之前的版本,否则不会发生错误。理论上,列名不再替换为更新的版本。

于 2013-11-08T13:49:27.050 回答