Обсуждение: Problem with trigger function

Поиск
Список
Период
Сортировка

Problem with trigger function

От
"James B. Byrne"
Дата:
I am trying to implement a trigger in a PostgreSQL-9.6.17 database:

CREATE OR REPLACE FUNCTION customer_num_informix()
  RETURNS trigger AS $$
BEGIN
  -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
  -- DBMS for columns that have the SERIAL data type.  Informix will then
  -- use the incremented serial number in place of 0. PostgreSQL instead
  -- will simply take the value 0 and replace the incremented serial number.
  -- This trigger function emulates the Informix DBMS behaviour.
  --
  -- The NEW variable contains the data for the row to be INSERTed or
  -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
  -- are automatically created and populated by PostgreSQL whenever
  -- a data-change function is called.
  --
  IF NEW.customer_num = 0 THEN
    SELECT nextval('customer_customer_num_seq') INTO NEW.customer_customer_num;
  ELSE
    IF NEW.customer_customer_num > 0 THEN
      PERFORM setval('customer_customer_num_seq', NEW.customer_customer_num);
    END IF;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;

CREATE TRIGGER customer_num_serial
  BEFORE INSERT ON customer
    FOR EACH ROW EXECUTE customer_num_informix();

The problem is that I am getting a syntax error on the CREATE TRIGGER statement:

ERROR:  syntax error at or near "customer_num_informix"
LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();

I do not see what the error is. What is wrong with the syntax I used?

-- 
***          e-Mail is NOT a SECURE channel          ***
        Do NOT transmit sensitive data via e-Mail
   Unencrypted messages have no legal claim to privacy
 Do NOT open attachments nor follow links sent by e-Mail

James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
Harte & Lyne Limited          http://www.harte-lyne.ca
9 Brockley Drive              vox: +1 905 561 1241
Hamilton, Ontario             fax: +1 905 561 0757
Canada  L8E 3C3




Re: Problem with trigger function

От
Steve Baldwin
Дата:
Try ... EXECUTE PROCEDURE customer_num_informix()

Steve

On Fri, Feb 12, 2021 at 6:47 AM James B. Byrne <byrnejb@harte-lyne.ca> wrote:
I am trying to implement a trigger in a PostgreSQL-9.6.17 database:

CREATE OR REPLACE FUNCTION customer_num_informix()
  RETURNS trigger AS $$
BEGIN
  -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
  -- DBMS for columns that have the SERIAL data type.  Informix will then
  -- use the incremented serial number in place of 0. PostgreSQL instead
  -- will simply take the value 0 and replace the incremented serial number.
  -- This trigger function emulates the Informix DBMS behaviour.
  --
  -- The NEW variable contains the data for the row to be INSERTed or
  -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
  -- are automatically created and populated by PostgreSQL whenever
  -- a data-change function is called.
  --
  IF NEW.customer_num = 0 THEN
    SELECT nextval('customer_customer_num_seq') INTO NEW.customer_customer_num;
  ELSE
    IF NEW.customer_customer_num > 0 THEN
          PERFORM setval('customer_customer_num_seq', NEW.customer_customer_num);
        END IF;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;

CREATE TRIGGER customer_num_serial
  BEFORE INSERT ON customer
    FOR EACH ROW EXECUTE customer_num_informix();

The problem is that I am getting a syntax error on the CREATE TRIGGER statement:

ERROR:  syntax error at or near "customer_num_informix"
LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();

I do not see what the error is. What is wrong with the syntax I used?

--
***          e-Mail is NOT a SECURE channel          ***
        Do NOT transmit sensitive data via e-Mail
   Unencrypted messages have no legal claim to privacy
 Do NOT open attachments nor follow links sent by e-Mail

James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
Harte & Lyne Limited          http://www.harte-lyne.ca
9 Brockley Drive              vox: +1 905 561 1241
Hamilton, Ontario             fax: +1 905 561 0757
Canada  L8E 3C3



Re: Problem with trigger function

От
Dave Cramer
Дата:
FWIW, messing with serial numbers like this is pretty risky.Sequences have transactional semantics for a reason.

Dave Cramer
www.postgres.rocks


On Thu, 11 Feb 2021 at 14:57, Steve Baldwin <steve.baldwin@gmail.com> wrote:
Try ... EXECUTE PROCEDURE customer_num_informix()

Steve

On Fri, Feb 12, 2021 at 6:47 AM James B. Byrne <byrnejb@harte-lyne.ca> wrote:
I am trying to implement a trigger in a PostgreSQL-9.6.17 database:

CREATE OR REPLACE FUNCTION customer_num_informix()
  RETURNS trigger AS $$
BEGIN
  -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
  -- DBMS for columns that have the SERIAL data type.  Informix will then
  -- use the incremented serial number in place of 0. PostgreSQL instead
  -- will simply take the value 0 and replace the incremented serial number.
  -- This trigger function emulates the Informix DBMS behaviour.
  --
  -- The NEW variable contains the data for the row to be INSERTed or
  -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
  -- are automatically created and populated by PostgreSQL whenever
  -- a data-change function is called.
  --
  IF NEW.customer_num = 0 THEN
    SELECT nextval('customer_customer_num_seq') INTO NEW.customer_customer_num;
  ELSE
    IF NEW.customer_customer_num > 0 THEN
          PERFORM setval('customer_customer_num_seq', NEW.customer_customer_num);
        END IF;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;

CREATE TRIGGER customer_num_serial
  BEFORE INSERT ON customer
    FOR EACH ROW EXECUTE customer_num_informix();

The problem is that I am getting a syntax error on the CREATE TRIGGER statement:

ERROR:  syntax error at or near "customer_num_informix"
LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();

I do not see what the error is. What is wrong with the syntax I used?

--
***          e-Mail is NOT a SECURE channel          ***
        Do NOT transmit sensitive data via e-Mail
   Unencrypted messages have no legal claim to privacy
 Do NOT open attachments nor follow links sent by e-Mail

James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
Harte & Lyne Limited          http://www.harte-lyne.ca
9 Brockley Drive              vox: +1 905 561 1241
Hamilton, Ontario             fax: +1 905 561 0757
Canada  L8E 3C3



Re: Problem with trigger function

От
"David G. Johnston"
Дата:

On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com> wrote:
Try ... EXECUTE PROCEDURE customer_num_informix()


FUNCTION, not PROCEDURE

David J. 

Re: Problem with trigger function

От
Steve Baldwin
Дата:
David, from what I can see of the docs, for 9.6 it is PROCEDURE.  It seems FUNCTION didn't appear until 11.

Steve

On Fri, Feb 12, 2021 at 7:05 AM David G. Johnston <david.g.johnston@gmail.com> wrote:

On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com> wrote:
Try ... EXECUTE PROCEDURE customer_num_informix()


FUNCTION, not PROCEDURE

David J. 

Re: Problem with trigger function

От
"David G. Johnston"
Дата:

On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com> wrote:
David, from what I can see of the docs, for 9.6 it is PROCEDURE.  It seems FUNCTION didn't appear until 11.
 
Indeed.  I didn’t pay attention to the version.

David J.