Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advancement in SQL Programing in SQL environment Procedures Functions Cursors Triggers.

Similar presentations


Presentation on theme: "Advancement in SQL Programing in SQL environment Procedures Functions Cursors Triggers."— Presentation transcript:

1 Advancement in SQL Programing in SQL environment Procedures Functions Cursors Triggers

2 PL/SQL basics DECLARE NTEMP NUMBER(10,2); -- declare a numeric variable CTEM CHAR(20);-- fixed size character variable VCTEMP VARCHAR(20);-- variable length character variable PIDLIKE PARTS.PID%TYPE; -- variable according to some row BEGIN SELECT (pid,pname) INTO ( NTEMP,VCTEMP ) INTO FROM PARTS WHERE pid = 1; IF NTEMP > 0 THEN UPDATE PARTS SET PNAME=‘egyes’ WHERE pid = 1; ELSE VCTEMP:=‘nemegyes’; -- variable assignment UPDATE PARTS SET PNAME = VCTEMP WHERE pid = NTEMP; END IF; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO PARTS (PID,PNAME,COLOR) VALUES(1,’egyes’,‘sarga'); WHEN TO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE(‘Primary key violation with pid: ' || NTEMP); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(‘Unknown Error with pid: ' || NTEMP); END;

3 Saving our programs - procedures Create procedure: CREATE OR REPLACE PROCEDUREPROCNAME(PARAMNAME [IN|OUT] PARAMTYPE,…) IS // declaration is automatic // variables used in your program BEGIN // place your program here END PROCNAME; Drop procedure: drop procedure PROCNAME ; Example: check syntax Compile into Run: exec procname(parameters);

4 Saving our programs - functions Create function: CREATE OR REPLACE FUNCTIONFUNCNAME(PARAMNAME [IN|OUT] PARAMTYPE,…) RETURN rettype IS // declaration is automatic // variables used in your program BEGIN // place your program here END FUNCNAME; Drop function: drop function FUNCNAME ; Example: check syntax Compile into Run: select holvegyem('Traktor belso') from dual; v. valtozo:=holvegyem(‘Traktor belso’);

5 Cursors create or replace function beosztott(aFonok IN number,aBeosztott IN number) return number is Result number; cursor beoszt is select * from hierarchia where felettes=aFonok; begin for i in beoszt loop – a cursorba agyazaott select minden egyes sorat megvizsgaljuk if ( i.beosztott=aBeosztott ) then -- ha a beosztottkent megtalaljuk a keresett embert visszaadunk 1-et return 1; else -- ha ez a beosztott nem az akit keresunk nezzuk meg --az o beosztottjai kozt megtalaljuk-e.. result:= beosztott(i.beosztott,aBeosztott); if ( result=1 ) then -- ha megtalaltunk visszaterunk 1-el return 1; end if; end loop; -- ha itt vagyunk nem talaltuk return 0; end beosztott;

6 Triggers I Statement trigger: CREATE OR REPLACE TRIGGER MYTRIG1 BEFORE DELETE OR INSERT OR UPDATE ON PARTS BEGIN IF (TO_CHAR(SYSDATE,’dy’) IN (‘sat’,’sun’)) OR (TO_CHAR(SYSDATE,’hh24:mi’) NOT BETWEEN ’08:30’ AND ’18:30’) THEN RAISE_APPLICATION_ERROR(-20500,’Table is secured’); END IF; END;

7 Triggers II Row trigger: CREATE OR REPLACE TRIGGER CATALOG_ARCHIVER AFTER DELETE OR INSERT OR UPDATE ON CATALOG FOR EACH ROW BEGIN IF DELETING THEN INSERT INTO CATLOG (ACTION, SID,PID,COST,ACTIONDATE) VALUES (‘del’,:OLD.SID, :OLD.PID,:OLD.COST, SYSDATE); ELSIF INSERTING THEN INSERT INTO CATLOG (ACTION, SID,PID,COST,ACTIONDATE) VALUES (‘insert’,:NEW.SID, :NEW.PID,:NEW.COST, SYSDATE); ELSIF UPDATING (‘COST’) THEN INSERT INTO CATLOG (ACTION, SID,PID,COST,ACTIONDATE) VALUES (,’updatecost’,:OLD.SID, :OLD.PID, :NEW.COST, SYSDATE); ELSE -- UPDATE TO ANYTHING ELSE THAN COST INSERT INTO CATLOG (ACTION, SID,PID,COST,ACTIONDATE) VALUES (’update_other’,:OLD.SID, :OLD.PID, :OLD.COST, SYSDATE); END IF END;

8 Links Basic PL/SQL Block Structure Scope of Block Objects Declaring Variables And Constants PL/SQL Records PL/SQL Assignments Cursors Exception (error) Handling Processing Control Structures Procedures and Functions Database Triggers O’Reilly Oracle PL/SQL Programming:


Download ppt "Advancement in SQL Programing in SQL environment Procedures Functions Cursors Triggers."

Similar presentations


Ads by Google