在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'
);
答案 0 :(得分:7)
使用函数作为一种“构造函数”函数(查看函数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;
答案 1 :(得分:4)
不,没有。您必须明确指定每个值。 Documentation reference here
答案 2 :(得分:3)
记录类型实际上是为从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
代替。
答案 3 :(得分:2)
记录初始化在其声明中执行 并通过从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;
/
答案 4 :(得分:0)
Oracle 18c允许使用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'
);
--Oracle 18c Qualified Expression:
v_location location_record_type :=
location_record_type('1234 Fake Street', '90210', 'Springfield', 'KY', 'US');
begin
dbms_output.put_line('It worked!');
end;
/
您可以在Oracle Live SQL here中运行上面的示例代码。 (不幸的是,该网站需要登录。由于18c无法下载,因此测试新功能的方法并不多。)