This question is more or less the same as this
In the package header :
Declared the following row type:
TYPE exch_row IS RECORD(
currency_cd VARCHAR2(9),
exch_rt_eur NUMBER,
exch_rt_usd NUMBER);
And this table type:
TYPE exch_tbl IS TABLE OF exch_row INDEX BY BINARY_INTEGER;
Added a variable:
exch_rt exch_tbl;
In the package body:
Fill this table variable with some data.
In a procedure in the package body:
I want to use the following statement:
CURSOR c0 IS
SELECT i.*, rt.exch_rt_eur, rt.exch_rt_usd
FROM item i, exch_rt rt
WHERE i.currency = rt.exchange_cd
How to do this in Oracle ?
Notes
Actually I'm looking for the 'Table Variable' solution in MSSQL:
DECLARE @exch_tbl TABLE
(
currency_cd VARCHAR(9),
exch_rt_eur NUMBER,
exch_rt_usd NUMBER)
)
And use this Table Variable inside my StoredProcedure.
You can't do it in a single query inside the package - you can't mix the SQL and PL/SQL types, and would need to define the types in the SQL layer as Tony, Marcin and Thio have said.
If you really want this done locally, and you can index the table type by VARCHAR instead of BINARY_INTEGER, you can do something like this:
So inside your loop, wherever you would have expected to use
r0.exch_rt_eur
you instead useexch_rt(r0.currency).exch_rt_eur
, and the same for USD. Testing from an anonymous block:Based on the answer Stef posted, this doesn't need to be in a package at all; the same results could be achieved with an
insert
statement. AssumingEXCH
holds exchange rates of other currencies against the Euro, including USD withcurrency_key=1
:With items valued at 19.99 GBP and 25.00 AUD, you get
detail_items
:If you want the currency stuff to be more re-usable you could create a view:
And then insert using values from that:
Prior to Oracle 12C you cannot select from PL/SQL-defined tables, only from tables based on SQL types like this:
In Oracle 12C it is now possible to select from PL/SQL tables that are defined in a package spec.
In SQL you may only use table type which is defined at schema level (not at package or procedure level), and index-by table (associative array) cannot be defined at schema level. So - you have to define nested table like this
And then you can use it in SQL with TABLE operator, for example:
Thanks for all help at this issue. I'll post here my solution:
Package Header
Package Body
Please review.