Sie sind auf Seite 1von 23

PL SQL

 SET SERVEROUTPUT ON;


 SQL> DECLARE
 2 a NUMBER := 3;
 3 BEGIN
 4 a := a + 1;
 5 dbms_output.put_line( a );
 6 END;
 7 .
 SQL> /
 4

 PL/SQL procedure successfully completed.


Assigning Values to a Variable
 tax := price * tax_rate;
 bonus := current_salary * 0.10;
 amount := TO_NUMBER(SUBSTR('750
dollars', 1, 3));
 valid := FALSE;
 credit_limit CONSTANT REAL :=
5000.00;
%type
 DECLARE
 Salary_total employee.salary%type;
 CURSOR c1 IS SELECT fname, salary,
dno FROM employee;
 BEGIN
 FOR emp_rec IN c1 LOOP
 salary_total := salary_total+emp_rec.salary;
 dbms_output.put_line( salary_total );
 END LOOP;
 End ;
PROCEDURE
 SET SERVEROUTPUT ON;
 Create or replace procedure fib( n number ) is
 F1 number;
 F2 number;
 I number ;
 C number;
 Begin
 F1 := 0;
 F2 := 1;
 Dbms_output.put_linr(f1);
 Dbms_output.put_linr(f2);
 For I in 1 .. N
 Loop
 C:=f1+f2;
 Dbms_output.put_linr(c);
 F1 :=f2;
 F2 := c;
 End loop;
 End;
 /
 Sql>exec fib(&n);
FUNCTION
 SET SERVEROUTPUT ON;
 Create or replace function fact( n number ) return
number is
 F number;
 I number ;
 Begin
 F := 1;
 For I in 1 .. N
 Loop
 F := f * i;
 End loop;
 Return f;
 End;
CALL function
 SET SERVEROUTPUT ON;
 Declare
 A number;
 Begin
 A:=fact(&n);
 Dbms_output.put_line(‘ The factorial of the
given number is ‘ || a);
 End;
 /
IF else
 IF condition THEN sequence_of_statements
 END IF;

 IF sales > quota THEN


 compute_bonus(empid);
 UPDATE payroll SET pay = pay + bonus
WHERE empno = emp_id;
 END IF;
IF else

 LOOP
 IF credit_rating < 3 THEN ...
 EXIT; -- exit loop immediately END IF;
END LOOP;
IF else

 IF x > y THEN high := x; END IF;

 IF-THEN-ELSE

 IF trans_type = 'CR' THEN


 UPDATE accounts SET balance = balance + credit WHERE

 ELSE
 UPDATE accounts SET balance = balance - debit
WHERE ...
 END IF;
IF else

 IF trans_type = 'CR' THEN


 UPDATE accounts SET balance = balance + credit
WHERE ...
 ELSE
 IF new_balance >= minimum_balance THEN
 UPDATE accounts SET balance = balance - debit
WHERE ...
 ELSE RAISE insufficient_funds;
 END IF;
 END IF;
IF else
 IF-THEN-ELSIF

 IF condition1 THEN
 sequence_of_statements1

 ELSIF condition2 THEN


 sequence_of_statements2

 ELSE
 sequence_of_statements3
 END IF;
WHILE
 WHILE condition LOOP sequence_of_statements
 END LOOP;

 WHILE total <= 25000 LOOP


 ...
 SELECT salary INTO sal FROM employee
WHERE ...
 total := total + sal;
 END LOOP;
 declare
  2      cursor employeeCursor
  3      is select e.first_name || ' ' || e.last_name name, e.salary
  4           from employee e;
  5
  6      name       varchar2(200);
  7      salary     number(9,2);
  8    begin
  9      for c1 in (select avg(e.salary) avg_salary
 10                   from employee e) loop
 11        dbms_output.put_line('average salary '||c1.avg_salary);
 12      end loop;
 13
 14      open employeeCursor;
 15      fetch employeeCursor into name, salary;
 16
 17      dbms_output.put_line(chr(13) || chr(13));
 18      dbms_output.put_line('EmployeeS');
 19      dbms_output.put_line('------------------------');
 20
 21      while employeeCursor%FOUND loop
 22        dbms_output.put_line(name || ' makes $' || salary);
 23        fetch employeeCursor into name, salary;
 24      end loop;
 25      close employeeCursor;
 26    end;
 27    /
 CURSOR OUTPUT

 average salary 4071.7525


EmployeeS
------------------------
Jason Martin makes $1234.56
Alison Mathews makes $6661.78
James Smith makes $6544.78
Celia Rice makes $2344.78
Robert Black makes $2334.78
Linda Green makes $4322.78
David Larry makes $7897.78
James Cat makes $1232.78
PL/SQL procedure successfully completed.
%rowtype
 declare
  2      l_dept employee%rowtype;
  3    begin
  4      l_dept.id := 100;
  5      l_dept.first_name := 'Tech';
  6      insert into employee (id, first_name )
  7           values( l_dept.id, l_dept.first_name );
  8    end;
  9    /

PL/SQL procedure successfully completed.
VIEW
 create view view_employee as
  2      select id view_id, first_name view_first_name
  3        from employee;
View created.
SQL>
SQL>
SQL>  select * from view_employee;
VIEW VIEW_FIRST
---- ----------
01   Jason
02   Alison
03   James
04   Celia
05   Robert
06   Linda
07   David
08   James
8 rows selected.
SQL>
SQL>
SQL> drop view view_employee;
TRIGGER
 select * from Employee


ID   FNAME LNAME   Start_DAT  END_DATE      SALARY 

01   Jason      Martin     25-JUL-96  25-JUL-06    1234.56 
 02   Alison     Mathews  21-MAR-76  21-FEB-86    6661.78 
03   James      Smith     12-DEC-78  15-MAR-90    6544.78 
 04   Celia      Rice        24-OCT-82  21-APR-99    2344.78 
 05   Robert     Black      15-JAN-84  08-AUG-98    2334.78 
 06   Linda      Green      30-JUL-87  04-JAN-96    4322.78 
 07   David      Larry       31-DEC-90  12-FEB-98    7897.78 
 08   James      Cat        17-SEP-96  15-APR-02    1232.78 
 SQL>
SQL>
SQL> create table employees_copy as select * from e
mployee;
SQL>
SQL> create table employees_log(
  2    who varchar2(30),
  3    when date );
SQL>
SQL> create trigger biud_employees_copy
  2    before insert or update or delete
  3       on employees_copy
  4  begin
  5    insert into employees_log( who, when )
  6                       values( user, sysdate );
  7  end;
  8  /
 SQL>
SQL> update employees_copy set salary = salary * 1.
1;
SQL>
SQL> select * from employees_log;
WHO                            WHEN
------------------------------ ---------
JAVA2S                         09-SEP-06
SQL>
SQL> delete from employees_copy;
SQL>
SQL> select * from employees_log;
WHO                            WHEN
------------------------------ ---------
JAVA2S                         09-SEP-06
JAVA2S                         09-SEP-06
 SQL>
SQL> delete from employee;
8 rows deleted.
SQL>
SQL> select * from employee_audit;
ID       SALARY DELETE_DA DELETED_BY
---- ---------- --------- ---------------
01      1234.56 09-SEP-06 JAVA2S
02      6661.78 09-SEP-06 JAVA2S
03      6544.78 09-SEP-06 JAVA2S
04      2344.78 09-SEP-06 JAVA2S
05      2334.78 09-SEP-06 JAVA2S
06      4322.78 09-SEP-06 JAVA2S
07      7897.78 09-SEP-06 JAVA2S
08      1232.78 09-SEP-06 JAVA2S
8 rows selected.
 CREATE OR REPLACE TRIGGER employee_after_delete
  2      AFTER DELETE
  3          ON employee
  4          FOR EACH ROW
  5
  6      DECLARE
  7          v_username varchar2(10);
  8
  9      BEGIN
 10
 11          -- Find username of person performing the DELETE on the table
 12          SELECT user INTO v_username
 13          FROM dual;
 14
 15          -- Insert record into audit table
 16          INSERT INTO employee_audit (id, salary, delete_date, deleted_
by)
 17                              VALUES ( :old.id, :old.salary, sysdate, v_username 
);
 18
 19      END;
 20      /
Trigger created.
 SQL>
SQL> delete from employee;
8 rows deleted.
SQL>
SQL> select * from employee_audit;
ID       SALARY DELETE_DA DELETED_BY
---- ---------- --------- ---------------
01      1234.56 09-SEP-06 JAVA2S
02      6661.78 09-SEP-06 JAVA2S
03      6544.78 09-SEP-06 JAVA2S
04      2344.78 09-SEP-06 JAVA2S
05      2334.78 09-SEP-06 JAVA2S
06      4322.78 09-SEP-06 JAVA2S
07      7897.78 09-SEP-06 JAVA2S
08      1232.78 09-SEP-06 JAVA2S
8 rows selected.

Das könnte Ihnen auch gefallen