简体   繁体   English

将Oracle SQL查询迁移到PostgreSql

[英]Migrate oracle sql query to PostgreSql

I have a simple Oracle query: 我有一个简单的Oracle查询:

SELECT SEQ_01.NEXTVAL FROM DUAL

I want write this query in SQL standard for run it in both DB 我想以SQL标准编写此查询以在两个数据库中运行它

The equivalent of Oracle's select seq_01.nextval from dual is: Oracle select seq_01.nextval from dual的等效项是:

select nextval('seq_01');

More details in the manual: http://www.postgresql.org/docs/current/static/functions-sequence.html 手册中的更多详细信息: http : //www.postgresql.org/docs/current/static/functions-sequence.html

There is no way you can write a SQL statement that works in both DBMS equally. 您无法编写在两个DBMS中均能正常工作的SQL语句。 Neither of them implements the syntax defined by SQL standard for sequences 它们都不实现SQL标准为序列定义的语法


Using your own functions you can actually make the same SQL work in both DBMS. 使用您自己的函数,您实际上可以在两个DBMS中使用相同的SQL。

For Oracle 对于Oracle

create or replace function seq_nextval(p_sequence_name varchar)
  return integer
as
  l_statement varchar(4000);
  l_value     integer;
begin 
  l_statement := 'select '||upper(p_sequence_name)||'.nextval from dual';
  execute immediate l_statement
    into l_value;
  return l_value;
end;
/  

select seq_nextval('seq_01')
from dual;

For Postgres 对于Postgres

create table dual (dummy varchar(1));
insert into dual values ('X');

create function seq_nextval(p_sequence_name text)
  returns bigint
as
$$
   select nextval(p_sequence_name);
$$
language sql;

select seq_nextval('seq_01')
from dual;

Note that you pay the usual price for DBMS independent SQL: it runs equally slow on all platforms. 请注意,您需要为DBMS独立SQL支付通常的价格:它在所有平台上的运行速度均相同。 Especially the Oracle workaround with PL/SQL and dynamic SQL is going to be massively slower than a plain seq_01.nextval call. 特别是在Oracle解决办法用PL / SQL和动态SQL将是大量慢比一个普通seq_01.nextval呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM