文档中心MogDBMogDB StackUqbar
v5.0

文档:v5.0

支持的版本:

其他版本:

支持PLPGSQL subtype

可获得性

本特性自MogDB 5.0.0版本开始引入。

特性简介

subtype是Oracle PL/SQL中,用户根据已有的类型来自定义的子类型。本特性在PLPGSQL支持该语法和用法,在已有类型的基础上自定义的类型。

客户价值

增强MogDB与Oracle的兼容性,减少应用程序的迁移代价。

特性约束

当前不支持RANGE low_value .. high_value语法

语法描述

SUBTYPE subtype_name IS base_type
  { precision [, scale ] } [ NOT NULL ]

base_type可以为任何已有类型或者用户自定义的类型,例如CHAR、DATE 等。PG语法的数组和series类型当前暂不支持。

当前暂不支持subtype,只支持非subtype类型。

示例

-- 场景1:匿名块内部定义subtype并使用
DECLARE
subtype varchar2_10 IS varchar2(10);
xx varchar2_10;
BEGIN
xx:='10';
raise notice '%',xx;
END;
NOTICE:  10
ANONYMOUS BLOCK EXECUTE

DECLARE
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
xx tyt_varchar2_10;
BEGIN
xx(1):='10';
xx(2):='x';
raise notice '%',xx;
END;
NOTICE:  {10,x}
ANONYMOUS BLOCK EXECUTE

-- 场景2:存储过程内部定义subtype并使用
CREATE PROCEDURE test_subtype_proc IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
xx tyt_varchar2_10;
aa varchar2_10;
BEGIN
xx(1):='10';
xx(2):='x';
aa:='abc';
raise notice '%-%',xx,aa;
END;

BEGIN
test_subtype_proc;
END;
NOTICE:  {10,x}-abc
ANONYMOUS BLOCK EXECUTE

-- 场景3:函数内部定义subtype并使用
CREATE FUNCTION test_subtype_func RETURN varchar2 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
xx tyt_varchar2_10;
aa varchar2_10;
BEGIN
xx(1):='10';
xx(2):='x';
aa:='abc';
RETURN xx(1)||'-'||xx(2)||'-'||aa;
END;

SELECT test_subtype_func;
 test_subtype_func
-------------------
 10-x-abc
(1 row)

-- 场景4:package内部定义subtype
CREATE PACKAGE test_subtype_pkg IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
END test_subtype_pkg;

-- 场景5:package内部定义subtype,并在package内部的存储过程作为参数类型使用
CREATE PACKAGE test_subtype_pkg1 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
PROCEDURE test_proc(a varchar2_10);
END test_subtype_pkg1;

CREATE PACKAGE test_subtype_pkg2 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
PROCEDURE test_proc(a test_subtype_pkg2.varchar2_10);
END test_subtype_pkg2;

CREATE PACKAGE test_subtype_pkg3 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
PROCEDURE test_proc;
END test_subtype_pkg3;
CREATE PACKAGE body test_subtype_pkg3 IS
PROCEDURE test_proc IS
xx varchar2_10;
BEGIN
xx:=3;
END;
END test_subtype_pkg3;

-- 场景6:在package中定义subtype,在外部存储过程中调用
CREATE PACKAGE test_subtype_pkg4 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
END test_subtype_pkg4;

DECLARE
xx test_subtype_pkg4.varchar2_10;
BEGIN
xx:='10';
raise notice '%',xx;
END;

DECLARE
xx test_subtype_pkg4.tyt_varchar2_10;
BEGIN
xx(1):='10';
xx(2):='x';
raise notice '%',xx;
END;

-- 场景7:subtype默认值
DECLARE
SUBTYPE empno IS VARCHAR2(10);
verb empno := 'run';
BEGIN
null;
END;

-- 场景8:在package的proc参数中使用subtype
CREATE OR REPLACE package test_subtype_pkg5 IS
subtype varchar2_10 IS varchar2(10);
type tyt_varchar2_10 IS TABLE of varchar2_10;
END test_subtype_pkg5;

CREATE OR REPLACE package tst_pro IS
PROCEDURE tst_pro(a test_subtype_pkg5.varchar2_10);
END tst_pro;

CREATE OR REPLACE package body tst_pro IS
PROCEDURE tst_pro(a test_subtype_pkg5.varchar2_10) IS 
BEGIN 
null;
END;
END tst_pro;

-- 场景9:subtype作为record的元素
CREATE OR REPLACE package pki_type as
subtype tstr IS varchar(60);
subtype tstatus IS varchar(3);
type t_data IS record(
cust_no tstr,
cust_flag tstatus);
type t_data_array IS TABLE of t_data INDEX BY binary_integer;
END pki_type;
Copyright © 2011-2024 www.enmotech.com All rights reserved.