溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Oracle與PostgreSQL內(nèi)置的編程語(yǔ)言有什么不同

發(fā)布時(shí)間:2021-11-08 11:55:51 來(lái)源:億速云 閱讀:167 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫(kù)

本篇內(nèi)容主要講解“Oracle與PostgreSQL內(nèi)置的編程語(yǔ)言有什么不同”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Oracle與PostgreSQL內(nèi)置的編程語(yǔ)言有什么不同”吧!

Oracle和PostgreSQL都提供了內(nèi)置的編程語(yǔ)言(PL/SQL vs PL/pgSQL),在輸入輸出參數(shù)的聲明上有較大的不同,如輸入?yún)?shù)中存在inout/out參數(shù),Oracle的函數(shù)可以有返回值,但PG不允許有返回值,只能通過(guò)參數(shù)返回。

Oracle
創(chuàng)建函數(shù)

TEST-orcl@DESKTOP-V430TU3>CREATE or replace FUNCTION sf_testparameter(p1 varchar2,p2 int,p3 varchar2,p4  in out int)
  2  RETURN varchar2
  3  is
  4  begin
  5    p4 := 1;
  6    return 'test';
  7  end;
  8  /
Function created.

執(zhí)行

TEST-orcl@DESKTOP-V430TU3>select sf_testparameter(null,null,null,:p4) from dual;
select sf_testparameter(null,null,null,:p4) from dual
       *
ERROR at line 1:
ORA-06572: Function SF_TESTPARAMETER has out arguments

不能在SQL中執(zhí)行,但可以在PL/SQL中執(zhí)行

TEST-orcl@DESKTOP-V430TU3>set serveroutput on
TEST-orcl@DESKTOP-V430TU3>declare
  2    p4 number;
  3    ret varchar2(100);
  4  begin
  5    ret := sf_testparameter(null,null,null,p4);
  6    dbms_output.put_line('p4 = '||p4||',ret='||ret);
  7  end;
  8  /
p4 = 1,ret=test

PostgreSQL
創(chuàng)建函數(shù),參數(shù)中存在inout類(lèi)型的參數(shù),返回值必須與該參數(shù)類(lèi)型一樣,而且return不能返回實(shí)際值。

[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int)
pg12@testdb-#   RETURNS text 
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return 'test';
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
ERROR:  function result type must be integer because of OUT parameters
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=# drop function sf_testparameter;
ERROR:  could not find a function named "sf_testparameter"
[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int)
pg12@testdb-#   RETURNS int 
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return 1;
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
ERROR:  RETURN cannot have a parameter in function with OUT parameters
LINE 5:   return 1;
                 ^
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int)
pg12@testdb-#   RETURNS int 
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return;
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
CREATE FUNCTION
[local:/data/run/pg12]:5120 pg12@testdb=# select sf_testparameter(null,null,null,null);
 sf_testparameter 
------------------
(1 row)
[local:/data/run/pg12]:5120 pg12@testdb=#

如參數(shù)中有多個(gè)out參數(shù),則要求返回record類(lèi)型

[local:/data/run/pg12]:5120 pg12@testdb=# drop function sf_testparameter;
ION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int,out p5 text)
  RETURNS record
  AS $$
begin
  return;
end;
$$ LANGUAGE 'plpgsql';
\df sf_testparameter
select sf_testparameter(null,null,null,null,null);
select sf_testparameter(null,null,null,null);
DROP FUNCTION
[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int,out p5 text)
pg12@testdb-#   RETURNS text 
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return 'test';
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
ERROR:  function result type must be record because of OUT parameters
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=# drop function sf_testparameter;
ERROR:  could not find a function named "sf_testparameter"
[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int,out p5 text)
pg12@testdb-#   RETURNS record
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return;
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
CREATE FUNCTION
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=# \df sf_testparameter
List of functions
-[ RECORD 1 ]-------+------------------------------------------------------------
Schema              | public
Name                | sf_testparameter
Result data type    | record
Argument data types | p1 text, p2 integer, p3 text, INOUT p4 integer, OUT p5 text
Type                | func
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=# select sf_testparameter(null,null,null,null,null);
ERROR:  function sf_testparameter(unknown, unknown, unknown, unknown, unknown) does not exist
LINE 1: select sf_testparameter(null,null,null,null,null);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
[local:/data/run/pg12]:5120 pg12@testdb=# select sf_testparameter(null,null,null,null);
 sf_testparameter 
------------------
 (,)
(1 row)
[local:/data/run/pg12]:5120 pg12@testdb=#

不管有多少個(gè)out參數(shù),都?xì)w為return參數(shù),在調(diào)用時(shí)不需要作為參數(shù)輸入,難怪PG要求返回record類(lèi)型。

[local:/data/run/pg12]:5120 pg12@testdb=# drop function sf_testparameter;
DROP FUNCTION
[local:/data/run/pg12]:5120 pg12@testdb=# CREATE or replace FUNCTION sf_testparameter(p1 text,p2 int,p3 text,inout p4 int,out p5 text,out p6 text)
pg12@testdb-#   RETURNS record
pg12@testdb-#   AS $$
pg12@testdb$# begin
pg12@testdb$#   return;
pg12@testdb$# end;
pg12@testdb$# $$ LANGUAGE 'plpgsql';
CREATE FUNCTION
[local:/data/run/pg12]:5120 pg12@testdb=# select sf_testparameter(null,null,null,null);
 sf_testparameter 
------------------
 (,,)
(1 row)
[local:/data/run/pg12]:5120 pg12@testdb=# 
[local:/data/run/pg12]:5120 pg12@testdb=#

到此,相信大家對(duì)“Oracle與PostgreSQL內(nèi)置的編程語(yǔ)言有什么不同”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI