Download presentation
Presentation is loading. Please wait.
Published byDamon Hill Modified over 9 years ago
1
Script Writing
2
Objectives: Definition of script writing Types of script Creating/Modification and running script Execution of script Fundamental of Script Sequential and conditional control Iteration process
3
Facts: Procedural language is called PL/SQL Highly structured Embedded language Powerful exception handling Other programs may call PL/SQL 3
4
Script writing: 1. Unnamed Blocks 2. Procedures 3. Functions 4
5
PL/SQL Basic: Comments --(single line) /* */ (Multi line) Rem 5
6
PL/SQL Basic: Identifiers Constant Variables Exceptions Cursors Functions/Procedures Packages 6
7
Unnamed Blocks: [DECLARE] [Declarations] BEGIN [Execution Statements] [NULL;] END; / 7
8
Example: BEGIN NULL; END; / 8
9
Declarations: Variable Type [NOT NULL] [:= | Default] Value; OR Variable Type%TYPE; 9
10
Data Type: BOOLEAN CHAR CHARACTER STRING VARCHAR2 NUMBER DEC DECIMAL FLOAT INT 10
11
Data Type (More…) INTEGER SMALLINT REAL BINARY_INTEGER POSITIVE DATE 11
12
EXAMPLE: DECLARE Is_TrueBOOLEAN; FlagBOOLEAN := FALSE; B_DateDATE; TodayDATE := SYSDATE; TotalINTEGER; Sum1INTEGER := 20; Sum2INTEGER DEFAULT 20; CreditINTEGER RANGE 10..200; GradeNUMBER(3, 2) NOT NULL :=1.00; ANUMBER :=Sum1 * 3; BB_Date%TYPE; S_IDStudent.ID%TYPE; S_RecStudent%TYPE; 12
13
CONSTANTS: DECLARE No_Days_Per_YearCONSTANTSINTEGER:=366; Max_No_StudentCONSTANTSINTEGER:=22; RegisteredCONSTANTSBOOLEAN:=FALSE; I_GradeCONSTANTSCHAR := ‘I’; GradeCONSTANTSCHARDEFAULT ‘o’; 13
14
NULL Value in Declaration: Max_No_StudentINTEGER (4) NOT NULL := 1000; ZipcodeINTEGER NOT NULL := 21532; 14
15
Example: DECLARE TOTALINTEGER;--? TotalINTEGER;--? BEGIN Total :=24; --Errors END; / 15
16
Expression: ** +, - (unary) *, / +,- >, >=, =, <>, <=, IN, LIKE, IS NULL NOT AND OR SYSDATE – B_Date/365;--? 16
17
Example: DECLARE AINTEGER; BBOOLEAN; CINTEGER := 5; BEGIN A :=10; B := C+2 = A; END; / 17
18
Load Data into Variables: DECLARE Field1Faculty.Name%TYPE; Field2Faculty.ID%TYPE; Field3Faculty.Salary%TYPE; BEGIN SELECT Name, ID, Salary INTO Field1, Field2, Field3 FROMFaculty WHERE ID=1111; DBMS_OUTPUT.PUT_LINE (‘Name: ’ || Field1 || ‘ ID: ’ || Field2); END; / 18
19
Load Data into Variables: DECLARE MyNameFaculty.Name%TYPE; MySalaryFaculty.Salary%TYPE; BEGIN SELECTName, Salary, SSN INTOMyName, MySalary FROMID=11111; DELETE FROM Faculty WHEREName = MyName; END; / 19
20
Conditional Statement: One way Selection: IF condition THEN action ; END IF; Two way Selection: IF condition THEN action_1; ELSE action_2; END IF; 20
21
Conditional Statement: Multiple way Selection: IF condition THEN action_1; ELSIF condition THEN action_2; …… ELSE action_3 END IF; 21
22
Example: DECLARE aNUMBER(2) := 22; bNUMBER(2) :=44; cVARCHAR2 (20); BEGIN IF a IS NULL OR b IS NULL THEN c:=‘Empty’; END IF; IF a>=b THEN c:=‘a is the largest’; ELSE c:=‘a is the smallest’; END IF; DBMS_OUTPUT.PUT_LINE(c); END; / 22
23
Example DECLARE ScoreNUMBER; GradeCHAR(1); BEGIN …….. ……… IF Score>=90 THEN Grade :=‘A’; ELSIF Score>=80 THEN Grade: =‘B’; ELSIF Score>=70 THEN Grade:=‘C’; ELSE Grade :=‘F’; END IF; DBMS_OUTPUT.PUT_LINE(Grade); END; / 23
24
Simple Iteration: LOOP statements; END LOOP; 24
25
Example: DECLARE iBINARY_INTEGER :=0; NumNUMBER:=4; BEGIN LOOP i:=i+1; Num := Num+2; IF i=5 THEN EXIT; END IF; DBMS_OUTPUT.PUT_LINE(Num); END LOOP; END; / 25
26
WHILE LOOP: WHILEconditionLOOP statement(s); END LOOP; 26
27
Example DECLARE iBINARY_INTEGER :=0; NumNUMBER:=4; BEGIN WHILE i<>5 LOOP Num := Num+2; i:=i+1; END LOOP; DBMS_OUTPUT.PUT_LINE(Num); END; / 27
28
FOR LOOP: FORindex IN[REVERSE] exp..exp LOOP statement(s); END LOOP; 28
29
Example DECLARE iBINARY_INTEGER ; BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END; / 29
30
Example – REVERSE: DECLARE iBINARY_INTEGER ; BEGIN FOR i IN REVERSE 1..5 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END; / 30
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.