Oracle query to PostgreSQL conversion











up vote
0
down vote

favorite












Could you please help with some converted Oracle queries to postgreSQL queries, it is exists?



I have an Oracle query which I want to adopt for postgreSQL, could you please help me with this?



merge into TABLE_NAME using dual on 
(ID='CF9EB9FE6F6D4CC9B75EC0CD420421B91541944569' and ANOTHER_ID='E198D55909D94895AF747ED7E032AC58')
when matched then update set USER='USERNAME'
when not matched then insert values('EEA2620A4A0F31CE05E69B','CFB75EC0CD41B91541944569','E198D55909D94895AF747ED7E032AC58','USERNAME',null)









share|improve this question




























    up vote
    0
    down vote

    favorite












    Could you please help with some converted Oracle queries to postgreSQL queries, it is exists?



    I have an Oracle query which I want to adopt for postgreSQL, could you please help me with this?



    merge into TABLE_NAME using dual on 
    (ID='CF9EB9FE6F6D4CC9B75EC0CD420421B91541944569' and ANOTHER_ID='E198D55909D94895AF747ED7E032AC58')
    when matched then update set USER='USERNAME'
    when not matched then insert values('EEA2620A4A0F31CE05E69B','CFB75EC0CD41B91541944569','E198D55909D94895AF747ED7E032AC58','USERNAME',null)









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Could you please help with some converted Oracle queries to postgreSQL queries, it is exists?



      I have an Oracle query which I want to adopt for postgreSQL, could you please help me with this?



      merge into TABLE_NAME using dual on 
      (ID='CF9EB9FE6F6D4CC9B75EC0CD420421B91541944569' and ANOTHER_ID='E198D55909D94895AF747ED7E032AC58')
      when matched then update set USER='USERNAME'
      when not matched then insert values('EEA2620A4A0F31CE05E69B','CFB75EC0CD41B91541944569','E198D55909D94895AF747ED7E032AC58','USERNAME',null)









      share|improve this question















      Could you please help with some converted Oracle queries to postgreSQL queries, it is exists?



      I have an Oracle query which I want to adopt for postgreSQL, could you please help me with this?



      merge into TABLE_NAME using dual on 
      (ID='CF9EB9FE6F6D4CC9B75EC0CD420421B91541944569' and ANOTHER_ID='E198D55909D94895AF747ED7E032AC58')
      when matched then update set USER='USERNAME'
      when not matched then insert values('EEA2620A4A0F31CE05E69B','CFB75EC0CD41B91541944569','E198D55909D94895AF747ED7E032AC58','USERNAME',null)






      sql database oracle postgresql database-migration






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 19:26









      marc_s

      567k12810981249




      567k12810981249










      asked Nov 11 at 14:08









      liotur

      549




      549
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You do the upsert in Postgres using insert . . . on conflict:



          insert into table_name (?, id, anotherid, ?, ?)   -- put in the column names
          values('EEA2620A4A0F31CE05E69B',
          'CFB75EC0CD41B91541944569',
          'E198D55909D94895AF747ED7E032AC58',
          'USERNAME', null
          )
          on conflict (id, anotherid)
          do update set USER = 'USERNAME';


          You want a unique constraint on (id, anotherid) so the conflict is recognized:



          alter table table_name add constraint unq_tablename_id_anotherid unique (id, anotherid);


          The unique constraint needs to be defined on the columns that would cause the conflict.






          share|improve this answer























          • I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
            – liotur
            Nov 11 at 14:23










          • @liotur - so do you have a unique constraint defined on your target table?
            – APC
            Nov 11 at 14:33










          • Yes I have, lets call it OID.
            – liotur
            Nov 11 at 14:38










          • @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
            – liotur
            Nov 11 at 14:57










          • @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
            – Gordon Linoff
            Nov 11 at 15:09











          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',
          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%2f53249548%2foracle-query-to-postgresql-conversion%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








          up vote
          1
          down vote



          accepted










          You do the upsert in Postgres using insert . . . on conflict:



          insert into table_name (?, id, anotherid, ?, ?)   -- put in the column names
          values('EEA2620A4A0F31CE05E69B',
          'CFB75EC0CD41B91541944569',
          'E198D55909D94895AF747ED7E032AC58',
          'USERNAME', null
          )
          on conflict (id, anotherid)
          do update set USER = 'USERNAME';


          You want a unique constraint on (id, anotherid) so the conflict is recognized:



          alter table table_name add constraint unq_tablename_id_anotherid unique (id, anotherid);


          The unique constraint needs to be defined on the columns that would cause the conflict.






          share|improve this answer























          • I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
            – liotur
            Nov 11 at 14:23










          • @liotur - so do you have a unique constraint defined on your target table?
            – APC
            Nov 11 at 14:33










          • Yes I have, lets call it OID.
            – liotur
            Nov 11 at 14:38










          • @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
            – liotur
            Nov 11 at 14:57










          • @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
            – Gordon Linoff
            Nov 11 at 15:09















          up vote
          1
          down vote



          accepted










          You do the upsert in Postgres using insert . . . on conflict:



          insert into table_name (?, id, anotherid, ?, ?)   -- put in the column names
          values('EEA2620A4A0F31CE05E69B',
          'CFB75EC0CD41B91541944569',
          'E198D55909D94895AF747ED7E032AC58',
          'USERNAME', null
          )
          on conflict (id, anotherid)
          do update set USER = 'USERNAME';


          You want a unique constraint on (id, anotherid) so the conflict is recognized:



          alter table table_name add constraint unq_tablename_id_anotherid unique (id, anotherid);


          The unique constraint needs to be defined on the columns that would cause the conflict.






          share|improve this answer























          • I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
            – liotur
            Nov 11 at 14:23










          • @liotur - so do you have a unique constraint defined on your target table?
            – APC
            Nov 11 at 14:33










          • Yes I have, lets call it OID.
            – liotur
            Nov 11 at 14:38










          • @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
            – liotur
            Nov 11 at 14:57










          • @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
            – Gordon Linoff
            Nov 11 at 15:09













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You do the upsert in Postgres using insert . . . on conflict:



          insert into table_name (?, id, anotherid, ?, ?)   -- put in the column names
          values('EEA2620A4A0F31CE05E69B',
          'CFB75EC0CD41B91541944569',
          'E198D55909D94895AF747ED7E032AC58',
          'USERNAME', null
          )
          on conflict (id, anotherid)
          do update set USER = 'USERNAME';


          You want a unique constraint on (id, anotherid) so the conflict is recognized:



          alter table table_name add constraint unq_tablename_id_anotherid unique (id, anotherid);


          The unique constraint needs to be defined on the columns that would cause the conflict.






          share|improve this answer














          You do the upsert in Postgres using insert . . . on conflict:



          insert into table_name (?, id, anotherid, ?, ?)   -- put in the column names
          values('EEA2620A4A0F31CE05E69B',
          'CFB75EC0CD41B91541944569',
          'E198D55909D94895AF747ED7E032AC58',
          'USERNAME', null
          )
          on conflict (id, anotherid)
          do update set USER = 'USERNAME';


          You want a unique constraint on (id, anotherid) so the conflict is recognized:



          alter table table_name add constraint unq_tablename_id_anotherid unique (id, anotherid);


          The unique constraint needs to be defined on the columns that would cause the conflict.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 14:46

























          answered Nov 11 at 14:14









          Gordon Linoff

          749k34285391




          749k34285391












          • I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
            – liotur
            Nov 11 at 14:23










          • @liotur - so do you have a unique constraint defined on your target table?
            – APC
            Nov 11 at 14:33










          • Yes I have, lets call it OID.
            – liotur
            Nov 11 at 14:38










          • @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
            – liotur
            Nov 11 at 14:57










          • @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
            – Gordon Linoff
            Nov 11 at 15:09


















          • I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
            – liotur
            Nov 11 at 14:23










          • @liotur - so do you have a unique constraint defined on your target table?
            – APC
            Nov 11 at 14:33










          • Yes I have, lets call it OID.
            – liotur
            Nov 11 at 14:38










          • @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
            – liotur
            Nov 11 at 14:57










          • @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
            – Gordon Linoff
            Nov 11 at 15:09
















          I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
          – liotur
          Nov 11 at 14:23




          I am receiving now ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
          – liotur
          Nov 11 at 14:23












          @liotur - so do you have a unique constraint defined on your target table?
          – APC
          Nov 11 at 14:33




          @liotur - so do you have a unique constraint defined on your target table?
          – APC
          Nov 11 at 14:33












          Yes I have, lets call it OID.
          – liotur
          Nov 11 at 14:38




          Yes I have, lets call it OID.
          – liotur
          Nov 11 at 14:38












          @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
          – liotur
          Nov 11 at 14:57




          @Gordon Linoff, I don't want to change something in the tables and columns. I have something that works in Oracle, and I need to convert it to postgreSQL. Is there way to do it without changing the key?
          – liotur
          Nov 11 at 14:57












          @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
          – Gordon Linoff
          Nov 11 at 15:09




          @liotur . . . Adding a unique constraint on a pair of columns that should be unique does not seem like "changing" the tables. In any case, that is how upsert works in Postgres. Your alternative is using separate update and insert statements within a single transaction.
          – Gordon Linoff
          Nov 11 at 15:09


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53249548%2foracle-query-to-postgresql-conversion%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python