P. 1
Program an Do Em Oracle 9i Pl-SQL [ Www.apostilaria.com ]

Program an Do Em Oracle 9i Pl-SQL [ Www.apostilaria.com ]

|Views: 1,230|Likes:
Published by Bruno Coelho

More info:

Published by: Bruno Coelho on Mar 30, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/01/2012

pdf

text

original

O Oracle8i e versões superiores permitem em uma operação passar todas as linhas
de uma coleção ao mecanismo SQL eliminando a necessidade de alternação de contexto
entre SQL e PL/SQL para cada linha da coleção.

Exemplo:

SQL> set serveroutput on format wrapped
SQL>
SQL> DECLARE

2 TYPE t_Numbers IS TABLE OF temp_table.num_col%TYPE;
3 TYPE t_Strings IS TABLE OF temp_table.char_col%TYPE;
4 v_Numbers t_Numbers := t_Numbers(1);
5 v_Strings t_Strings := t_Strings(1);
6
7 PROCEDURE PrintTotalRows(p_Message IN VARCHAR2) IS
8 v_Count NUMBER;
9 BEGIN
10 SELECT COUNT(*)
11 INTO v_Count
12 FROM temp_table;
13 DBMS_OUTPUT.PUT_LINE(p_Message || ': Count is ' ||

v_Count);
14 END PrintTotalRows;
15
16 BEGIN
17 DELETE FROM temp_table;
18
19 v_Numbers.EXTEND(1000);
20 v_Strings.EXTEND(1000);
21 FOR v_Count IN 1..1000 LOOP
22 v_Numbers(v_Count) := v_Count;
23 v_Strings(v_Count) := 'Element #' || v_Count;
24 END LOOP;
25

26 FORALL v_Count IN 1..1000
27 INSERT INTO temp_table VALUES
28 (v_Numbers(v_Count), v_Strings(v_Count));

29
30 PrintTotalRows('After first insert');
31

32 FORALL v_Count IN 501..1000
33 INSERT INTO temp_table VALUES
34 (v_Numbers(v_Count), v_Strings(v_Count));

164

35
36 PrintTotalRows('After second insert');
37

38 FORALL v_Count IN 1..1000
39 UPDATE temp_table
40 SET char_col = 'Changed!'
41 WHERE num_col = v_Numbers(v_Count);

42
43 DBMS_OUTPUT.PUT_LINE(
44 'Update processed ' || SQL%ROWCOUNT || ' rows.');
45

46 FORALL v_Count IN 401..600
47 DELETE FROM temp_table
48 WHERE num_col = v_Numbers(v_Count);

49
50 PrintTotalRows('After delete');
51 END;
52 /
After first insert: Count is 1000
After second insert: Count is 1500
Update processed 1500 rows.
After delete: Count is 1200

PL/SQL procedure successfully completed.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->