Académique Documents
Professionnel Documents
Culture Documents
CREATE OR REPLACE FUNCTION END [package_name]; ELSIF v_number>=0 THEN Function must return a value.
function_name dbms_output.put_line('it Procedure can not return a value
(parameter_1 data_type, Bind variable is greater than 0'); Function and procedure can both
Parameter_2 data_type) ELSE return data in OUT and IN OUT
Need to specify type
parameters
RETURN data_type dbms_output.put_line('not
Need to wrap around quote when
{ IS | AS } either of the case'); Function can be called from SQL,
assign string value
but not for procedure
[declaration_section] END IF;
No need quote when reference the
BEGIN END; Can not perform a DML DDL within
variable
executable_section Loops function, while allowed in procedure
Value can only be assigned in a
[EXCEPTION FOR IN .. LOOP
PL, via exec or Begin / End block Trigger
exception_section] {statements};
Use PRINT to list out bind variable
END [function_name]; END LOOP; CREATE [OR REPLACE]
Procedures Parts Parameter Modes in PL/SQL Parameter Modes in PL/SQL Packages Code Example (cont)
Subprograms (cont) Subprograms (cont)
S.N. Parts & Description SELECT count(1) INTO
1 Declarative Part It is an 1 IN An IN parameter lets you 3 IN OUT An IN OUT parameter l_cnt from
pass a value to the passes an initial value to a
optional part. However, the mall a
subprogram. It is a read-only subprogram and returns an
declarative part for a WHERE
parameter. Inside the updated value to the caller. It
subprogram does not start a.mall_name=pi_mall
with the DECLARE subprogram, an IN parameter can be assigned a value and
;
keyword. It contains acts like a constant. It cannot its value can be read. The
be assigned a value. You can actual parameter IF l_cnt = 0
declarations of types,
pass a constant, literal, corresponding to an IN OUT THEN
cursors, constants,
initialized variable, or formal parameter must be a SELECT cid into l_cid
variables, exceptions, and
expression as an IN variable, not a constant or an
nested subprograms. These FROM rop
parameter. You can also expression. Formal parameter
items are local to the WHERE CITY=pi_city;
subprogram and cease to initialize it to a default value; must be assigned a value.
exist when the subprogram however, in that case, it is Actual parameter is passed by
omitted from the subprogram value. INSERT INTO mall VALUES
completes execution.
call. It is the default mode of (l_cid, pi_mall);
2 Executable Part This is a
parameter passing. Parameters Packages Code Example END IF;
mandatory part and are passed by reference. COMMIT;
contains statements that CREATE OR REPLACE PACKAGE
2 OUT An OUT parameter pi_city_code:=l_cid;
perform the designated roppkg AS
action. returns a value to the calling END;
PROCEDURE ropmall
program. Inside the
3 Exception-handling This FUNCTION roppop
subprogram, an OUT (pi_city varchar2 default
is again an optional part. It (pi_city varchar2
parameter acts like a variable. 'Mississauga',
contains the code that You can change its value and
default'Mississauga')
pi_mall varchar2,
handles run-time errors. reference the value after RETURN NUMBER AS
pi_city_code out
assigning it. The actual l_pop NUMBER;
varchar2) ;
Parameter Modes in PL/SQL parameter must be variable BEGIN
Subprograms FUNCTION roppop
and it is passed by value. SELECT population INTO
(pi_city varchar2
S.N. Parts & Description l_pop from
default'Mississauga')
rop WHERE city=pi_city;
RETURN NUMBER ;
RETURN l_pop;
END;
END;
CREATE OR REPLACE PACKAGE
END;
BODY roppkg AS
PROCEDURE ropmall
Function Example
(pi_city varchar2 default
'Mississauga', CREATE or REPLACE FUNCTION
pi_mall varchar2, roppop
AS l_pop NUMBER;
Procedures Example