Question

During migration of postgresql 9 functions faced the following issue: in Oracle you can use WITH just in complex select statements. Meanwhile, in postgres (and MS SQL 2008 too) you can use WITH and INSERT (update, delete) together.

Example:

     WITH prerows AS ( SELECT ObjectID, LoginID, Param FROM devices D
                         WHERE D.DevNum = '0003' AND ObjectID IS NULL )

     INSERT INTO dev_sub( ObjectID, LoginID, Param )
     SELECT ObjectID, LoginID, Param FROM prerows;

The query works fine in PostgreSQL 9.2 but writes error in oracle 11 XE: 'ORA-00928: missing SELECT keyword'. Maybe I just miss something? I'm an Oracle fun and it would be pity for me to know that it can't combine WITH statement and INSERT command.

Best regards, Anthony

Was it helpful?

Solution

Try:

 INSERT INTO dev_sub( ObjectID, LoginID, Param )

 WITH prerows AS ( SELECT ObjectID, LoginID, Param 
                   FROM devices D
                   WHERE D.DevNum = '0003' 
                     AND ObjectID IS NULL )
 SELECT ObjectID, LoginID, Param FROM prerows;

OTHER TIPS

Try:

INSERT INTO dev_sub( ObjectID, LoginID, Param )
  SELECT ObjectID, LoginID, Param 
    FROM devices D
    WHERE D.DevNum = '0003' 
      AND ObjectID IS NULL

It doesn't use a WITH clause on the SELECT (also called a subquery factoring clause) - but on the other hand one really isn't needed here. Oracle, SQL Server, and PostgreSQL all have their own idiosyncracies in their implementation of SQL, and there's no such thing as transparent migration.

Share and enjoy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top