Anda di halaman 1dari 3

Exhibit 1 Associative Array Translation Example

8/13/15, 8:43 PM

Exhibit 1 Associative Array Translation Example


Category: PL/SQL Translation Tool
Published Date

Oracle
CREATE or REPLACE PROCEDURE myExampleAA(p_place IN pls_integer, p_team OUT varchar2)
IS
-- to execute oracle:
-exec myExampleAA(1, :myVar);
-- postgres:
-SELECT myExampleAA(1);
type
rt_soccer IS RECORD (
team
varchar2(2000)
);
type
t_aa_results is table of rt_soccer INDEX BY BINARY_INTEGER;
aa_results t_aa_results;
v_i
pls_integer := 1;
BEGIN
dbms_output.put_line('begin myExampleAA input = ' || p_place);
--- init the Associative Array
-aa_results(v_i).team := 'Germany';
v_i := v_i + 1;
aa_results(v_i).team := 'Argentina';
v_i := v_i + 1;
aa_results(v_i).team := 'Netherlands';
v_i := v_i + 1;
aa_results(v_i).team := 'Brazil';
--- display the contents
-for v_result_i IN aa_results.first .. aa_results.last loop
dbms_output.put_line('
team name ' || aa_results(v_result_i).team);
end loop;
-p_team := aa_results(p_place).team;
-EXCEPTION
WHEN others THEN
dbms_output.put_line('ERROR myExampleAA sqlerrm: ' || sqlerrm);
RAISE;
END myExampleAA;

Postgres as translated by magicHat


-- magicHat translation v2.1 July 30, 2014
SET search_path = david, public;
-- DROP TYPE IF EXISTS t_aa_results;
CREATE TYPE t_aa_results AS (team text, arrIx integer);
CREATE or REPLACE FUNCTION myExampleAA(
p_place IN integer,
p_team OUT varchar)
RETURNS varchar AS $body$
DECLARE
-- to execute oracle:
-exec myExampleAA(1, :myVar);
-- postgres:
-SELECT myExampleAA(1);
-- type
rt_soccer IS RECORD (
-- team
varchar2(2000)
-- );
-- type
t_aa_results is table of rt_soccer INDEX BY BINARY_INTEGER;
http://postgresmigrations.com/database-migration/stored-proceduressociative-array-translation-example?tmpl=component&print=1&page=

Page 1 of 3

Exhibit 1 Associative Array Translation Example

8/13/15, 8:43 PM

aa_results
t_aa_results;
v_i
integer := 1;
c_gt1_aa_results CURSOR FOR SELECT * FROM t_aa_results_l1 where arrIx >= 0 order by arrIx; -- traverse te
aa_results_1 t_aa_results; -- index by table emulation intermediate result
v_result_i integer; -- added for translation support
aa_results_2 t_aa_results; -- local instance from temp table
BEGIN
perform gen_t_aa_results_l1(0); -- create or init array emulation table for aa_results
RAISE NOTICE 'begin myExampleAA input = %', p_place;
--- init the Associative Array
-aa_results.team := 'Germany';
UPDATE t_aa_results_l1 SET team = aa_results.team WHERE arrIx = v_i; -- update collection aa_results;
if NOT FOUND then
INSERT INTO t_aa_results_l1 (arrIx, team) values (v_i, aa_results.team);
end if;
v_i := v_i + 1;
aa_results.team := 'Argentina';
UPDATE t_aa_results_l1 SET team = aa_results.team WHERE arrIx = v_i; -- update collection aa_results;
if NOT FOUND then
INSERT INTO t_aa_results_l1 (arrIx, team) values (v_i, aa_results.team);
end if;
v_i := v_i + 1;
aa_results.team := 'Netherlands';
UPDATE t_aa_results_l1 SET team = aa_results.team WHERE arrIx = v_i; -- update collection aa_results;
if NOT FOUND then
INSERT INTO t_aa_results_l1 (arrIx, team) values (v_i, aa_results.team);
end if;
v_i := v_i + 1;
aa_results.team := 'Brazil';
UPDATE t_aa_results_l1 SET team = aa_results.team WHERE arrIx = v_i; -- update collection aa_results;
if NOT FOUND then
INSERT INTO t_aa_results_l1 (arrIx, team) values (v_i, aa_results.team);
end if;
--- display the contents
-FOR aa_results_1 IN c_gt1_aa_results LOOP
v_result_i := aa_results_1.arrIx;
RAISE NOTICE '
team name %', aa_results_1.team;
end loop;
-SELECT * INTO STRICT aa_results_2 FROM t_aa_results_l1 WHERE arrIx = p_place;
- p_team := aa_results(p_place).te ...
p_team := aa_results_2.team;
-EXCEPTION
WHEN others THEN
RAISE NOTICE 'ERROR myExampleAA sqlerrm: %', sqlerrm;
RAISE;
END; -- myExampleAA
$body$ LANGUAGE plpgsql SECURITY DEFINER;
-- *********************************************************************
CREATE or REPLACE FUNCTION gen_t_aa_results_l1(p_initNum IN integer)
RETURNS void AS
$$
BEGIN
--- Create temp table for global storage of the result set (aa_results).
-BEGIN
TRUNCATE TABLE t_aa_results_l1;
EXCEPTION
when UNDEFINED_TABLE or WRONG_OBJECT_TYPE then
BEGIN
CREATE TEMP TABLE t_aa_results_l1(
team
text,
arrIx
int NOT NULL UNIQUE);
EXCEPTION
when others then
http://postgresmigrations.com/database-migration/stored-proceduressociative-array-translation-example?tmpl=component&print=1&page=

Page 2 of 3

Exhibit 1 Associative Array Translation Example

8/13/15, 8:43 PM

RAISE EXCEPTION 'gen_t_aa_results_l1 create table t_aa_results_l1 issue NUM:%, DETAILS:%',


END;
when others then
RAISE EXCEPTION 'gen_t_aa_results_l1 truncate table t_aa_results_l1 issue NUM:%, DETAILS:%', SQLSTAT
END;
END; -- gen_t_aa_results_l1
$$ LANGUAGE plpgsql SECURITY DEFINER;

http://postgresmigrations.com/database-migration/stored-proceduressociative-array-translation-example?tmpl=component&print=1&page=

Page 3 of 3

Anda mungkin juga menyukai