[英]What is the use of KEY keyword in oracle ? Do we have any alternative for KEY in postgresql
What is the use of KEY keyword in oracle.what is the equivalent for this keyword in postgresql?this is a sample code from a function mentioned in package.here.KEY is not known one because this is writter years ago, cursor variable is not supported in postgres,what are the equivalent for migrating this code to postgresql oracle 中 KEY 关键字的用途是什么。postgresql 中此关键字的等价物是什么?这是 package 中提到的 function 的示例代码。这里。KEY 是未知的,因为这是多年前的作者,cursor 变量不是在 postgres 中支持,将此代码迁移到 postgresql 的等效项是什么
Code:代码:
create or replace
function Rate ( p_base_curr CHAR ,
p_orig_curr CHAR ,
p_time_string CHAR ,
p_rate_type CHAR ) returns INT as $$ declare rate INT;
LoI INTEGER;
HiI INTEGER;
mI INTEGER;
mKey VARCHAR (30);
mLKey VARCHAR (30);
i INTEGER;
cursor get_rates_c is
select
( TRIM (e.frk_category) || TRIM (e.fk_prim_currency) || TRIM (e.fk_scndry_currency) || TRIM (e.time_string) ) key ,
( e.primary_units / e.secondary_units ) Rate ,
e.to_time_string ToTS
from
xxx e
where
e.to_time_string >= TRIM ( TO_CHAR ( TO_NUMBER ( TO_CHAR ( SYSDATE,
'YYYY' ) ) - Years_To_Cache ,
'9999' ) )
order by
( TRIM (e.frk_category) || TRIM (e.fk_prim_currency) || TRIM (e.fk_scndry_currency) || TRIM (e.time_string) ) desc;
type rates_t is VARRAY (120000) of get_rates_c%ROWTYPE;
rates rates_t;
begin if p_base_curr = p_orig_curr then rate := 1;
else mLKey := TRIM (p_base_curr) || TRIM (p_orig_curr) || TRIM (p_rate_type);
mKey := mLKey || TRIM (p_time_string);
LoI := 1;
HiI := rates.COUNT;
while LoI <= HiI loop mI := ( LoI + HiI ) / 2;
-- EXIT WHEN rates ( mI ).KEY = mKey;
case
when rates (mI).key < mKey then LoI := mI + 1;
else HiI := mi - 1;
end if;
end loop;
if ( rates (mi).key <= mKey )
and ( SUBSTR ( rates (mi).key ,
1 ,
LENGTH (mLKey) ) = mLKey )
and ( p_time_string <= rates (mi).ToTS ) then rate := rates (mi).rate;
else begin
-- SQLINES LICENSE FOR EVALUATION USE ONLY
select
coalesce ( ( e.primary_units / e.secondary_units ),
-1 )
into
RATE
from
xxx e
where
e.fk_prim_currency = p_base_curr
and e.fk_scndry_currency = p_orig_curr
and p_time_string between e.time_string and e.to_time_string
and e.frk_category = p_rate_type;
exception
when others then rate := -1;
end;
end if;
end if;
case
when rate = -1 then
case
when Return_Zero_On_Missing = 1 then rate := 0;
else
end;
$$ language plpgsql;
if;
end if;
return rate;
end Rate;
It isn't a keyword in this context.在这种情况下,它不是关键字。 You were referring to the use at:
您指的是在以下位置使用:
exit when rates(mi).KEY = mKey;
Where rates
is defined with: rates
定义为:
type rates_t is VARRAY (120000) of get_rates_c%ROWTYPE;
rates rates_t;
The use of ROWTYPE
means that each element of the array is a record with the same structure as a row returned by the get_rates_c
cursor, which is:使用
ROWTYPE
意味着数组的每个元素都是一个记录,其结构与get_rates_c
cursor 返回的行相同,即:
cursor get_rates_c is
select
( TRIM (e.frk_category) || TRIM (e.fk_prim_currency) || TRIM (e.fk_scndry_currency) || TRIM (e.time_string) ) key ,
( e.primary_units / e.secondary_units ) Rate ,
e.to_time_string ToTS
from
xxx e
...
So rates(mi).key
is the value constructed from ( TRIM (e.frk_category) || TRIM (e.fk_prim_currency) || TRIM (e.fk_scndry_currency) || TRIM (e.time_string) )
for the corresponding row from that cursor query.所以
rates(mi).key
是从( TRIM (e.frk_category) || TRIM (e.fk_prim_currency) || TRIM (e.fk_scndry_currency) || TRIM (e.time_string) )
构建的对应行的值cursor查询。
So, it isn't a keyword, it's the column alias from your cursor query.所以,它不是关键字,它是 cursor 查询中的列别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.