Sql 如何比较Oracle中的复杂集合?
假设我有两种表类型(对象类型的表),我想比较它们是否相等 对象类型有多个字段,例如,Sql 如何比较Oracle中的复杂集合?,sql,database,oracle,plsql,Sql,Database,Oracle,Plsql,假设我有两种表类型(对象类型的表),我想比较它们是否相等 对象类型有多个字段,例如,整数、varchar2和日期 我见过一些例子,人们使用MULTISET EXCEPT来有效地对两个INTEGER表执行减号 但这不适用于两个复杂对象类型的表 此外,我还提到了使用MAP MEMBER FUNCTION,以便在使用SET操作符时使复杂集合工作,但没有提到MULTISET功能 我目前比较相等的方法是取表类型1(TT1)和表类型2(TT2),如果TT1减去TT2=0和TT2减去TT1=0,则表示它们相等
整数
、varchar2
和日期
我见过一些例子,人们使用MULTISET EXCEPT
来有效地对两个INTEGER
表执行减号
但这不适用于两个复杂对象类型的表
此外,我还提到了使用MAP MEMBER FUNCTION
,以便在使用SET
操作符时使复杂集合工作,但没有提到MULTISET
功能
我目前比较相等的方法是取表类型1(TT1)和表类型2(TT2),如果TT1减去TT2=0和TT2减去TT1=0,则表示它们相等。但在这里,我只是从两个表中选择减号的PK,我还希望能够比较多个字段
我希望MULTISET
更快
谢谢。是的,您可以使用映射成员函数来支持比较类型的嵌套表
--#1: Create object
create or replace type type1 is object
(
a integer,
b varchar2(100),
c date,
map member function compare return raw
);
/
--#2: Create map function for comparisons.
--Returns the concatenated RAW of all attributes.
--Note that this will consider NULLs to be equal!
create or replace type body type1 as
map member function compare return raw is
begin
return
utl_raw.cast_to_raw(to_char(a))||
utl_raw.cast_to_raw(b)||
utl_raw.cast_to_raw(to_char(c, 'DD-MON-YYYY HH24:MI:SS'));
end;
end;
/
--#3: Create nested table of the types
create or replace type type1_nt is table of type1;
/
--#4: Compare.
--You could use MULTISET, but it's easier to use regular operators like "<>" "and =".
declare
tt1 type1_nt := type1_nt(type1(0, 'A', date '2000-01-01'),
type1(0, 'A', date '2000-01-01'));
tt2 type1_nt := type1_nt(type1(0, 'B', date '2000-01-01'),
type1(0, 'B', date '2000-01-01'));
tt3 type1_nt := type1_nt(type1(0, 'B', date '2000-01-01'),
type1(0, 'B', date '2000-01-01'));
begin
if tt1 <> tt2 and tt2 = tt3 then
dbms_output.put_line('Pass');
else
dbms_output.put_line('Fail');
end if;
end;
/
——#1:创建对象
创建或替换类型type1是对象
(
一个整数,
b varchar2(100),
c日期,
映射成员函数比较返回原始值
);
/
--#2:创建用于比较的映射函数。
--返回所有属性的串联原始值。
注意,这将考虑空值相等!
创建或替换类型主体type1作为
映射成员函数比较返回原始值为
开始
返回
utl_raw.将_转换为_raw(转换为_char(a))||
utl_未加工。铸造至未加工(b)||
utl_raw.cast_to_raw(to_char(c,'DD-MON-yyyyy HH24:MI:SS'));
结束;
结束;
/
--#3:创建类型的嵌套表
创建或替换类型1\n是类型1的表;
/
--#4:比较一下。
--您可以使用MULTISET,但使用诸如“”和“”之类的常规运算符更容易。
声明
tt1类型1\u nt:=类型1\u nt(类型1(0,'A',日期'2000-01-01'),
第1类(0,‘A’,日期‘2000-01-01’);
tt2类型1\u nt:=类型1\u nt(类型1(0,'B',日期'2000-01-01'),
第1类(0,‘B’,日期‘2000-01-01’);
tt3类型1\u nt:=类型1\u nt(类型1(0,'B',日期'2000-01-01'),
第1类(0,‘B’,日期‘2000-01-01’);
开始
如果tt1 tt2和tt2=tt3,则
dbms_output.put_行('Pass');
其他的
dbms_output.put_行('Fail');
如果结束;
结束;
/
我不知道这是否比手动比较每个属性快。但我想差别不会很大