0% found this document useful (0 votes)
43 views2 pages

Formulario PDF

Blocos define anonymous PL/SQL blocks that are declared and executed at runtime. They contain a DECLARE section to define objects, a BEGIN section for executable statements, and an EXCEPTION section to handle exceptions. Procedures and functions are subprograms that can be called. Triggers define code that runs automatically in response to events like INSERT, UPDATE, DELETE. Control structures include IF-THEN-ELSE, IF-THEN-ELSIF, LOOP, EXIT, EXIT-WHEN, WHILE, and FOR loops. Cursors allow processing of multiple rows from a query result set. Exceptions can be handled using exception blocks that catch specific errors or catch all errors.

Uploaded by

José Batista
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Formulario PDF

Blocos define anonymous PL/SQL blocks that are declared and executed at runtime. They contain a DECLARE section to define objects, a BEGIN section for executable statements, and an EXCEPTION section to handle exceptions. Procedures and functions are subprograms that can be called. Triggers define code that runs automatically in response to events like INSERT, UPDATE, DELETE. Control structures include IF-THEN-ELSE, IF-THEN-ELSIF, LOOP, EXIT, EXIT-WHEN, WHILE, and FOR loops. Cursors allow processing of multiple rows from a query result set. Exceptions can be handled using exception blocks that catch specific errors or catch all errors.

Uploaded by

José Batista
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

BLOCOS

So blocos annimos que so declarados numa aplicao no local onde devem ser executados, sendo passados em run-time ao interpretador PL/SQL para execuo. DECLARE --Definio de objectos PL/SQL a utilizar dentro do bloco. BEGIN --Aces executveis EXCEPTION --Processamento de excepes. END; Exemplo DECLARE CURSOR my_cursor IS SELECT sal + NVL(comm, 0) wages, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.wages > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.wages, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END;

SUBPROGRAMAS
PROCEDURE FUNCTION

TRIGGER

ESTRUTURAS DE CONTROLO
IF-THEN-ELSE
IF condition1 THEN statement1; ELSE IF condition2 THEN statement2; ELSE IF condition3 THEN statement3; END IF; END IF; END IF;

IF-THEN-ELSIF
IF condition1 THEN statement1; ELSIF condition2 THEN statement2; ELSIF condition3 THEN statement3; END IF;

LOOP
LOOP sequence_of_statements; END LOOP;

LOOP LABELS
<<outer>> LOOP ... LOOP ... EXIT outer WHEN ... END LOOP; ... END LOOP outer;

-- exit both loops

EXIT
LOOP IF count > 100 THEN EXIT; END IF; END LOOP;

EXIT-WHEN
LOOP EXIT WHEN count > 100; END LOOP;

WHILE-LOOP
WHILE condition LOOP sequence_of_statements; END LOOP;

FOR-LOOP
FOR counter IN [REVERSE] lower_bound..higher_bound LOOP sequence_of_statements; END LOOP;

CURSORES
CURSOR cursor_name [(parameter[, parameter]...)] IS select_statement; cursor_parameter_name [IN] datatype [{:= | DEFAULT} expr] DECLARE CURSOR c1 IS SELECT ename, job FROM emp WHERE sal < 3000; my_record c1%ROWTYPE; ... BEGIN OPEN c1; LOOP FETCH c1 INTO my_record; EXIT WHEN c1%NOTFOUND; -- process data record END LOOP; CLOSE c1; ... END; DECLARE CURSOR c1 (name VARCHAR2, salary NUMBER) IS SELECT ... BEGIN OPEN c1('ATTLEY', 1500); ... END;

EXCEPES
DECLARE pe_ratio NUMBER(3,1); BEGIN SELECT price / earnings INTO pe_ratio FROM stocks WHERE symbol = 'XYZ'; -- pode causar division-by-zero error INSERT INTO stats (symbol, ratio) VALUES ('XYZ', pe_ratio); EXCEPTION WHEN ZERO_DIVIDE THEN -- trata 'division by zero' error INSERT INTO stats (symbol, ratio) VALUES ('XYZ', NULL); ... WHEN OTHERS THEN -- handles all other errors ; END;

You might also like