postgres trigger pass input to function



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I am trying to make a trigger that when a value is put into table1 it is then copied into table2. I have the following function which works correctly:



Create function func1(a integer) returns void
Language SQL as $$
insert into table2 values (a);$$;


and here is the trigger (it doesn't work):



Create trigger trig1
Before update on table1
For each row
Execute function func1(old.attr1)


I am trying to follow this documentation. Here is the part in particular I am following:



CREATE TRIGGER log_update
AFTER UPDATE ON accounts
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE PROCEDURE log_account_update();


I figured that old.attribute gives a particular attribute of the old row before it is changed. If I'm wrong on this let me know. Thanks










share|improve this question




























    -1















    I am trying to make a trigger that when a value is put into table1 it is then copied into table2. I have the following function which works correctly:



    Create function func1(a integer) returns void
    Language SQL as $$
    insert into table2 values (a);$$;


    and here is the trigger (it doesn't work):



    Create trigger trig1
    Before update on table1
    For each row
    Execute function func1(old.attr1)


    I am trying to follow this documentation. Here is the part in particular I am following:



    CREATE TRIGGER log_update
    AFTER UPDATE ON accounts
    FOR EACH ROW
    WHEN (OLD.* IS DISTINCT FROM NEW.*)
    EXECUTE PROCEDURE log_account_update();


    I figured that old.attribute gives a particular attribute of the old row before it is changed. If I'm wrong on this let me know. Thanks










    share|improve this question
























      -1












      -1








      -1








      I am trying to make a trigger that when a value is put into table1 it is then copied into table2. I have the following function which works correctly:



      Create function func1(a integer) returns void
      Language SQL as $$
      insert into table2 values (a);$$;


      and here is the trigger (it doesn't work):



      Create trigger trig1
      Before update on table1
      For each row
      Execute function func1(old.attr1)


      I am trying to follow this documentation. Here is the part in particular I am following:



      CREATE TRIGGER log_update
      AFTER UPDATE ON accounts
      FOR EACH ROW
      WHEN (OLD.* IS DISTINCT FROM NEW.*)
      EXECUTE PROCEDURE log_account_update();


      I figured that old.attribute gives a particular attribute of the old row before it is changed. If I'm wrong on this let me know. Thanks










      share|improve this question














      I am trying to make a trigger that when a value is put into table1 it is then copied into table2. I have the following function which works correctly:



      Create function func1(a integer) returns void
      Language SQL as $$
      insert into table2 values (a);$$;


      and here is the trigger (it doesn't work):



      Create trigger trig1
      Before update on table1
      For each row
      Execute function func1(old.attr1)


      I am trying to follow this documentation. Here is the part in particular I am following:



      CREATE TRIGGER log_update
      AFTER UPDATE ON accounts
      FOR EACH ROW
      WHEN (OLD.* IS DISTINCT FROM NEW.*)
      EXECUTE PROCEDURE log_account_update();


      I figured that old.attribute gives a particular attribute of the old row before it is changed. If I'm wrong on this let me know. Thanks







      postgresql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 10:20









      fredfred

      459418




      459418






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The doc says this about the function that is called in the trigger:




          A user-supplied function that is declared as taking no arguments and
          returning type trigger, which is executed when the trigger fires.




          This function has access to the OLD and NEW objects. You can modify your existing function:



          Create function func1() RETURNS trigger
          as $$
          BEGIN
          insert into table2 values (OLD.attr1);
          RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;

          Create trigger trig1
          Before update on table1
          For each row
          Execute function func1();





          share|improve this answer

























          • when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

            – fred
            Nov 15 '18 at 22:00











          • @fred ah yes, see edit

            – JGH
            Nov 16 '18 at 0:37











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317197%2fpostgres-trigger-pass-input-to-function%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The doc says this about the function that is called in the trigger:




          A user-supplied function that is declared as taking no arguments and
          returning type trigger, which is executed when the trigger fires.




          This function has access to the OLD and NEW objects. You can modify your existing function:



          Create function func1() RETURNS trigger
          as $$
          BEGIN
          insert into table2 values (OLD.attr1);
          RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;

          Create trigger trig1
          Before update on table1
          For each row
          Execute function func1();





          share|improve this answer

























          • when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

            – fred
            Nov 15 '18 at 22:00











          • @fred ah yes, see edit

            – JGH
            Nov 16 '18 at 0:37















          0














          The doc says this about the function that is called in the trigger:




          A user-supplied function that is declared as taking no arguments and
          returning type trigger, which is executed when the trigger fires.




          This function has access to the OLD and NEW objects. You can modify your existing function:



          Create function func1() RETURNS trigger
          as $$
          BEGIN
          insert into table2 values (OLD.attr1);
          RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;

          Create trigger trig1
          Before update on table1
          For each row
          Execute function func1();





          share|improve this answer

























          • when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

            – fred
            Nov 15 '18 at 22:00











          • @fred ah yes, see edit

            – JGH
            Nov 16 '18 at 0:37













          0












          0








          0







          The doc says this about the function that is called in the trigger:




          A user-supplied function that is declared as taking no arguments and
          returning type trigger, which is executed when the trigger fires.




          This function has access to the OLD and NEW objects. You can modify your existing function:



          Create function func1() RETURNS trigger
          as $$
          BEGIN
          insert into table2 values (OLD.attr1);
          RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;

          Create trigger trig1
          Before update on table1
          For each row
          Execute function func1();





          share|improve this answer















          The doc says this about the function that is called in the trigger:




          A user-supplied function that is declared as taking no arguments and
          returning type trigger, which is executed when the trigger fires.




          This function has access to the OLD and NEW objects. You can modify your existing function:



          Create function func1() RETURNS trigger
          as $$
          BEGIN
          insert into table2 values (OLD.attr1);
          RETURN NEW;
          END;
          $$ LANGUAGE plpgsql;

          Create trigger trig1
          Before update on table1
          For each row
          Execute function func1();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 0:36

























          answered Nov 15 '18 at 13:11









          JGHJGH

          3,92941226




          3,92941226












          • when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

            – fred
            Nov 15 '18 at 22:00











          • @fred ah yes, see edit

            – JGH
            Nov 16 '18 at 0:37

















          • when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

            – fred
            Nov 15 '18 at 22:00











          • @fred ah yes, see edit

            – JGH
            Nov 16 '18 at 0:37
















          when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

          – fred
          Nov 15 '18 at 22:00





          when I try to make the fuction func1 as you describe i get this error: "SQL functions cannot return type trigger"

          – fred
          Nov 15 '18 at 22:00













          @fred ah yes, see edit

          – JGH
          Nov 16 '18 at 0:37





          @fred ah yes, see edit

          – JGH
          Nov 16 '18 at 0:37



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317197%2fpostgres-trigger-pass-input-to-function%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus