初始化 pl/sql 记录类型

发布于 2024-12-14 09:46:07 字数 505 浏览 0 评论 0原文

在 PL/SQL 中,varray 可以在创建时初始化为:

TYPE colour_tab IS VARRAY(3) OF VARCHAR2(20);
    french_colours colour_tab := colour_tab('RED','WHITE','BLUE');

是否有 PL/SQL 记录类型的等效初始化方法?

type location_record_type is record (
      street_address       varchar2(40),
     postal_code          varchar2(12),
      city                 varchar2(30),
     state_province       varchar2(25),
     country_id           char(2) not null := 'US'
    );

In PL/SQL, a varray can be initialised at creation time as:

TYPE colour_tab IS VARRAY(3) OF VARCHAR2(20);
    french_colours colour_tab := colour_tab('RED','WHITE','BLUE');

Is there an equivalent method of initialisation for PL/SQL record types?

type location_record_type is record (
      street_address       varchar2(40),
     postal_code          varchar2(12),
      city                 varchar2(30),
     state_province       varchar2(25),
     country_id           char(2) not null := 'US'
    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何时共饮酒 2024-12-21 09:46:07

使用函数充当一种“构造函数”(查看函数 f()):

DECLARE
  TYPE ty_emp IS RECORD(
    id INTEGER,
    name VARCHAR(30),
    deptcode VARCHAR(10)
    );
  TYPE ty_tbl_emp IS TABLE OF ty_emp;
  tbl_emp ty_tbl_emp;
  FUNCTION f (             -- <==============
    id INTEGER,
    name VARCHAR,
    deptcode VARCHAR) RETURN ty_emp IS
  e ty_emp;
  BEGIN
    e.id := id;
    e.name := name;
    e.deptcode := deptcode;
    RETURN e;
  END f;
BEGIN

  tbl_emp := ty_tbl_emp(
    f(1, 'Johnson', 'SALES'), 
    f(2, 'Peterson', 'ADMIN'));
  Dbms_Output.put_line(tbl_emp(2).name);
END;  

Use a function to act as a kind of "constructor" function (look at function f()):

DECLARE
  TYPE ty_emp IS RECORD(
    id INTEGER,
    name VARCHAR(30),
    deptcode VARCHAR(10)
    );
  TYPE ty_tbl_emp IS TABLE OF ty_emp;
  tbl_emp ty_tbl_emp;
  FUNCTION f (             -- <==============
    id INTEGER,
    name VARCHAR,
    deptcode VARCHAR) RETURN ty_emp IS
  e ty_emp;
  BEGIN
    e.id := id;
    e.name := name;
    e.deptcode := deptcode;
    RETURN e;
  END f;
BEGIN

  tbl_emp := ty_tbl_emp(
    f(1, 'Johnson', 'SALES'), 
    f(2, 'Peterson', 'ADMIN'));
  Dbms_Output.put_line(tbl_emp(2).name);
END;  
攒眉千度 2024-12-21 09:46:07

Oracle 18c 允许使用 限定表达式

declare 
type location_record_type is record (
      street_address       varchar2(40),
     postal_code          varchar2(12),
      city                 varchar2(30),
     state_province       varchar2(25),
     country_id           char(2) not null := 'US'
    );

myvar location_record_type;
myvar2 location_record_type := location_record_type(street_address => 'my street'
                                                   ,postal_code    => 'my code'
                                                   ,city           => 'my city'
                                                   ,state_province => 'my state'
                                                   ,country_id     => 'GB'
                                                   );

begin
  dbms_output.put_line(myvar.country_id);
  dbms_output.put_line(myvar2.city);
end;
/

上面的输出是...

US
my city

您可以此处在 Oracle Live SQL 中运行上述示例代码。 (不幸的是,该网站需要登录。)

Oracle 18c allows record initialization with qualified expressions:

declare 
type location_record_type is record (
      street_address       varchar2(40),
     postal_code          varchar2(12),
      city                 varchar2(30),
     state_province       varchar2(25),
     country_id           char(2) not null := 'US'
    );

myvar location_record_type;
myvar2 location_record_type := location_record_type(street_address => 'my street'
                                                   ,postal_code    => 'my code'
                                                   ,city           => 'my city'
                                                   ,state_province => 'my state'
                                                   ,country_id     => 'GB'
                                                   );

begin
  dbms_output.put_line(myvar.country_id);
  dbms_output.put_line(myvar2.city);
end;
/

Output of the above is ...

US
my city

You can run the above sample code in Oracle Live SQL here. (Unfortunately that site requires a logon.)

陌生 2024-12-21 09:46:07

不,没有。您必须显式分配每个值。 此处参考文档

No, there is not. You have to assign each value explicitly. Documentation reference here.

溇涏 2024-12-21 09:46:07

记录类型实际上是为保存 SELECT 语句中的行而设计的。

    ....
    type location_record_type is record (
          street_address       varchar2(40),
         postal_code          varchar2(12),
          city                 varchar2(30),
         state_province       varchar2(25),
         country_id           char(2) not null := 'US'
        );
    type location_record_nt is table of location_record_type;
    loc_recs location_record_nt;
begin
    select street_name
           , pcode
           , city
           , region
           , country_code
    bulk collect into loc_recs
    from t69
    where ....

显然,对于查询不是 SELECT * FROM 单个表的情况(因为在这种情况下,我们可以使用 %ROWTYPE 代替)。

Record types are really designed for holding rows from SELECT statements.

    ....
    type location_record_type is record (
          street_address       varchar2(40),
         postal_code          varchar2(12),
          city                 varchar2(30),
         state_province       varchar2(25),
         country_id           char(2) not null := 'US'
        );
    type location_record_nt is table of location_record_type;
    loc_recs location_record_nt;
begin
    select street_name
           , pcode
           , city
           , region
           , country_code
    bulk collect into loc_recs
    from t69
    where ....

Obviously for cases where the query isn't a SELECT * FROM a single table (because in that scenario we can use %ROWTYPE instead.

简单爱 2024-12-21 09:46:07

记录初始化在其声明中执行
并通过从 DUAL 中选择来记录分配:

    declare
        type location_record_type is record
        (
            street_address       varchar2(40) := '1234 Fake Street',
            postal_code          varchar2(12) := '90210',
            city                 varchar2(30) := 'Springfield',
            state_province       varchar2(25) := 'KY',
            country_id           char(2) not null := 'US'
        );
        v_location location_record_type;

   begin 
      select 
        '4321 Another St.', '48288', 'Detroit', 'MI', v_location.country_id
      into v_location from dual;
    end;
    /

Record initialization is performed in its declaration
and record assignment by selecting into from DUAL:

    declare
        type location_record_type is record
        (
            street_address       varchar2(40) := '1234 Fake Street',
            postal_code          varchar2(12) := '90210',
            city                 varchar2(30) := 'Springfield',
            state_province       varchar2(25) := 'KY',
            country_id           char(2) not null := 'US'
        );
        v_location location_record_type;

   begin 
      select 
        '4321 Another St.', '48288', 'Detroit', 'MI', v_location.country_id
      into v_location from dual;
    end;
    /
心碎无痕… 2024-12-21 09:46:07

您可以创建一个返回该记录类型的函数。

请参阅下面的示例代码:

DECLARE
   type location_record_type is record (
      street_address       varchar2(40),
      postal_code          varchar2(12),
      city                 varchar2(30),
      state_province       varchar2(25),
      country_id           char(2) not null := 'US');
   v_loc_rec location_record_type;
   FUNCTION new_loc_rec RETURN location_record_type
   IS
      v_new_loc_rec location_record_type;
   BEGIN
      return v_new_loc_rec;
   END;
BEGIN
    v_loc_rec := new_loc_rec;
    v_loc_rec.state_province := 'SomeState';
    v_loc_rec.country_id := 'SU';
    dbms_output.put_line('State: '||v_loc_rec.state_province||'; Country_ID: '||v_loc_rec.country_id);
    v_loc_rec := new_loc_rec;
    dbms_output.put_line('State: '||v_loc_rec.state_province||'; Country_ID: '||v_loc_rec.country_id);
END;

You can create a function that return that record type.

See below sample code:

DECLARE
   type location_record_type is record (
      street_address       varchar2(40),
      postal_code          varchar2(12),
      city                 varchar2(30),
      state_province       varchar2(25),
      country_id           char(2) not null := 'US');
   v_loc_rec location_record_type;
   FUNCTION new_loc_rec RETURN location_record_type
   IS
      v_new_loc_rec location_record_type;
   BEGIN
      return v_new_loc_rec;
   END;
BEGIN
    v_loc_rec := new_loc_rec;
    v_loc_rec.state_province := 'SomeState';
    v_loc_rec.country_id := 'SU';
    dbms_output.put_line('State: '||v_loc_rec.state_province||'; Country_ID: '||v_loc_rec.country_id);
    v_loc_rec := new_loc_rec;
    dbms_output.put_line('State: '||v_loc_rec.state_province||'; Country_ID: '||v_loc_rec.country_id);
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文