0

What I'm mostly doing these days is converting an Oracle SQL database into a PostgreSQL (9.1.6) database.

The most common issues that I tend to come across are as follows:

1. decode --> case when
2. nvl --> coalesce
3. (+) --> outer join
4. connect by prior --> connectby function with tablefunc contrib module.
5. merge into --> ???
etc...

I am currently skimming through the PostgreSQL manual and funnily enough there seems to be no MERGE INTO statement (or any direct substitutions for it).

I am aware that PL/pgSQL could be used as an alternative but that would mean a lot of extra workload for me. Is there something that I'm missing about this?

5
  • There is no simple merge replacement. For reasons, and possible solutions check this blogpost. Commented Jun 19, 2013 at 12:50
  • 4) connect by --> recursive common table expression: postgresql.org/docs/current/static/queries-with.html Commented Oct 26, 2013 at 15:22
  • possible duplicate of Insert, on duplicate update (postgresql) Commented Oct 26, 2013 at 15:22
  • Old question, but I'm downvoting because there is no explanation of what MERGE INTO does, and how the OP wants to use it, making it hard to come up with a meaningful answer. Commented Oct 26, 2013 at 15:33
  • 1
    Upvoting, because MERGE is a very widely used command in Oracle SQL, and this is a valid question that crops up quite a bit. Commented Oct 26, 2013 at 17:30

1 Answer 1

1

There is no simple replacement, and certainly not for 9.1.

The best option is to upgrade to 9.2 and look at writeable CTE's. These allow you to multi-stage write operations in a similar manner. For example, you could:

WITH up (UPDATE foo set bar = 'baz' where id > 1000
         returning id)
INSERT INTO foo(id, bar) 
SELECT s, 'baz'
  FROM generate_sequence(1001, 10000) s
 WHERE s NOT IN (select id from up);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.