DECLARE
price NUMBER:=4000;
discount FLOAT;
BEGIN
CASE WHEN price>200
THEN
discount:=15;
WHEN price>300 and price<=500
THEN discount:=20;
else
discount:=0.5;
end CASE;
DBMS_OUTPUT.PUT_LINE(discount);
END;
DECLARE
CNT NUMBER:=20;
discount FLOAT;
BEGIN
WHILE CNT>10
LOOP
DBMS_OUTPUT.PUT_LINE(CNT);
CNT:=CNT-1;
END LOOP;
END;
DECLARE
CNT NUMBER:=20;
BEGIN
FOR CNT IN REVERSE 20..30
LOOP
DBMS_OUTPUT.PUT_LINE(CNT);
END LOOP;
END;
DECLARE
emp_id INTEGER:=100;
fname VARCHAR2(25);
lname VARCHAR2(25);
BEGIN
SELECT FIRST_NAME, LAST_NAME INTO fname, lname
FROM HR.EMPLOYEES WHERE EMPLOYEE_ID=emp_id;
DBMS_OUTPUT.PUT_LINE('FIRST NAME:'||fname);
DBMS_OUTPUT.PUT_LINE('LAST NAME:'||lname);
END;
DECLARE
emp_id INTEGER:=100;
fname HR.EMPLOYEES.FIRST_NAME%TYPE;
lname HR.EMPLOYEES.LAST_NAME%TYPE;
BEGIN
SELECT FIRST_NAME, LAST_NAME INTO fname, lname
FROM HR.EMPLOYEES WHERE EMPLOYEE_ID=emp_id;
DBMS_OUTPUT.PUT_LINE('FIRST NAME:'||fname);
DBMS_OUTPUT.PUT_LINE('LAST NAME:'||lname);
END;
CREATE PROCEDURE PROC1(emp_id IN INTEGER) AS
fname HR.EMPLOYEES.FIRST_NAME%TYPE;
lname HR.EMPLOYEES.LAST_NAME%TYPE;
BEGIN
SELECT FIRST_NAME, LAST_NAME INTO fname, lname
FROM HR.EMPLOYEES WHERE EMPLOYEE_ID=emp_id;
DBMS_OUTPUT.PUT_LINE('FIRST NAME:'||fname);
DBMS_OUTPUT.PUT_LINE('LAST NAME:'||lname);
END PROC1;
FUNCTION return a value but a PROCEDURE don’t.
FUNCTION can be used within a SQL. Procedure can be called inside a anonymous block.
Select function1(arg..) from dual;
BEGIN
Procedure19arg..)
END
DECLARE
emp_id INTEGER:=6000;
fname VARCHAR2(25);
lname VARCHAR2(25);
U_EXCEPTION_1 EXCEPTION;
BEGIN
IF emp_id>5000 THEN
RAISE U_EXCEPTION_1;
END IF;
SELECT FIRST_NAME, LAST_NAME INTO fname, lname
FROM HR.EMPLOYEES WHERE EMPLOYEE_ID=emp_id;
DBMS_OUTPUT.PUT_LINE('FIRST NAME:'||fname);
DBMS_OUTPUT.PUT_LINE('LAST NAME:'||lname);
EXCEPTION
WHEN U_EXCEPTION_1 THEN
DBMS_OUTPUT.PUT_LINE('USER CREATED EXCEPTION');
END;
CREATE TRIGGER customer_before_update
BEFORE UPDATE
ON customer
DECLARE
v_user varchar2(10);
BEGIN
select user into v_user from dual;
INSERT INTO AUDIT_TABLE VALUES ….
END;
CREATE OR REPLACE TRIGGER TRIGGER_NAME
AFTER INSERT OR UPDATE OR DELETE
OF <columnname> —put column level restriction
ON CUSTOMER
FOR EACH ROW —-- for row level trigger
WHEN <condition> —conditional trigger
DECLARE
….
IF INSERTING THEN
….
ELSEIF UPDATING THEN
ELSEIF DELETING THEN
..
END IF;
END;
OLD AND NEW PSEUDO RECORDS DURING TRIGGER INSERT UPDATE DELETE
OLD.COLUMNNAME
NEW.COLUMNNAME
ALTER TRIGGER TRIGGER_NAME DISABLE;
ALTER TRIGGER TRIGGER_NAME ENABLE;
ALTER TABLE TABLE_NAME DISABLE/ENABLE ALL TRIGGER;
DROP TRIGGER TRIGGER_NAME;