You are on page 1of 3

PLSQL Cheat Sheet

by juliosueiras via cheatography.com/23055/cs/6461/

Function Packages (cont) Condit​ional and Loops (cont) Function vs Procedures

CREATE OR REPLACE FUNCTION END [packa​ge_​name]; ELSIF v_numb​er>=0 THEN Function must return a value.
function_name dbms_o​utp​ut.p​ut​_li​ne('it Procedure can not return a value

(param​eter_1 data_type, Bind variable is greater than 0'); Function and procedure can both
Parame​ter_2 data_type) ELSE return data in OUT and IN OUT
Need to specify type
parameters
RETURN data_type dbms_o​utp​ut.p​ut​_li​ne('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
[decla​rat​ion​_se​ction] END IF;
No need quote when reference the
BEGIN END; Can not perform a DML DDL within
variable
execut​abl​e_s​ection 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
except​ion​_se​ction] {state​ments};
Use PRINT to list out bind variable
END [funct​ion​_name]; END LOOP; CREATE [OR REPLACE]

WHILE condition TRIGGER trigger_name


Condit​ional and Loops
Procedures LOOP BEFORE | AFTER
Declare and use of [INSERT, UPDATE, DELETE
{state​ments};
Create [ or REPLACE ]
variable
END LOOP; [COLUMN NAME..]
PROCEDURE procedure_name
%TYPE %ROWTYPE ON table_name
LOOP
(
VARCHAR2 NUMBER DATE Refere​ncing [ OLD AS OLD |
{state​mens};
parame​ter​_name_1
Assignment operator :=
EXIT WHEN condition; NEW AS NEW ]
data_type,
Nested block variable FOR EACH ROW | FOR EACH
CONTINUE WHEN condition;
parame​ter​_name_2 data_type
scope
END LOOP; STATEMENT [ WHEN Condition
)
DECLARE ]
Loops
{ IS | AS }
myvar number;
DECLARE DECLARE
pl_sql​_block
BEGIN [decla​rat​ion​_se​ction]
i NUMBER :=10;
Parameter
myvar:=1;
BEGIN BEGIN
By position
dbms_o​utp​ut.p​ut​_li​ne(​myvar [execu​tab​le_​sec​tion]
FOR i IN 1..5 LOOP
By name
); EXCEPTION
dbms_o​utp​ut.p​ut​_li​ne(i);
DECLARE
END LOOP; [excep​tio​n_s​ection]
Packages
myvar number;
dbms_o​utp​ut.p​ut​_li​ne(i); END;
CREATE PACKAGE BEGIN
END;
package_name { IS | AS } myvar:=2;
CASE – Simple Case Substi​tution variable
proced​ure​_or​_fu​nct​ion​_sp​ec dbms_o​utp​ut.p​ut​_li​ne(​myvar
CASE expression
No need to specify type, as it is
i​fic​ati​on_1; );
WHEN value_1 THEN always character type
proced​ure​_or​_fu​nct​ion​_sp​ec END;
..
No need to wrap around quote
i​fic​ati​on_2; dbms_o​utp​ut.p​ut​_li​ne(​myvar
WHEN value_2 THEN when assign value
END [packa​ge_​name]; );
ELSE
Need quote when reference the
Package body END;
END CASE; variable
CREATE PACKAGE BODY IF THEN ELSE END IF
CASE – Searched Case
ACCEPT implicitly defined a
packag​e_name { IS | AS } DECLARE
WHEN boolea​n_e​xpr​ession substi​tution type variable
proced​ure​_or​_fu​nct​ion​_bo​dy v_number NUMBER;
THEN
Use DEFINE to list out substi​tution
_1; BEGIN
ELSE variable
proced​ure​_or​_fu​nct​ion​_bo​dy IF v_numb​er<=0 THEN
END CASE;
_2; dbms_o​utp​ut.p​ut​_li​ne('it
is less than 0');

By juliosueiras Published 17th December, 2015. Sponsored by ApolloPad.com


cheatography.com/juliosueiras/ Last updated 17th December, 2015. Set Your Pen Free and Finish Your Novel!
Page 1 of 3. https://apollopad.com
PLSQL Cheat Sheet
by juliosueiras via cheatography.com/23055/cs/6461/

Procedures Parts Parameter Modes in PL/SQL Parameter Modes in PL/SQL Packages Code Example (cont)
Subpro​grams (cont) Subpro​grams (cont)
S.N. Parts & Descri​ption ​SELECT count(1) INTO

1 Decl​arative 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
subpro​gram. It is a read-only subprogram and returns an
declar​ative part for a ​WHERE
parameter. Inside the updated value to the caller. It
subprogram does not start ​a.m​all​_na​me=​pi_mall
with the DECLARE subpro​gram, 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
declar​ations of types,
pass a constant, literal, corres​ponding to an IN OUT THEN
cursors, constants,
initia​lized variable, or formal parameter must be a ​ ​ ​SELECT cid into l_cid
variables, except​ions, and
expression as an IN variable, not a constant or an
nested subpro​grams. These ​ ​ FROM rop
parameter. You can also expres​sion. Formal parameter
items are local to the ​ ​ ​WHERE CITY=p​i_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 Exec​utable 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_​cit​y_c​ode​:=l​_cid;
perform the designated roppkg AS
action. returns a value to the calling END;
​PRO​CEDURE ropmall
program. Inside the
3 Exce​pti​on-​han​dling This FUNCTION roppop
subpro​gram, an OUT (pi_city varchar2 default
is again an optional part. It (pi_city varchar2
parameter acts like a variable. 'Missi​ssa​uga',
contains the code that You can change its value and
defaul​t'M​iss​iss​auga')
​pi_mall varchar2,
handles run-time errors. reference the value after RETURN NUMBER AS
​pi_​cit​y_code out
assigning it. The actual ​l_pop NUMBER;
varchar2) ;
Parameter Modes in PL/SQL parameter must be variable BEGIN
Subpro​grams FUNCTION roppop
and it is passed by value. ​SELECT population INTO
(pi_city varchar2
S.N. Parts & Descri​ption l_pop from
defaul​t'M​iss​iss​auga')
rop WHERE city=p​i_city;
RETURN NUMBER ;
​RETURN l_pop;
END;
END;
CREATE OR REPLACE PACKAGE
END;
BODY roppkg AS
​PRO​CEDURE ropmall
Function Example
(pi_city varchar2 default
'Missi​ssa​uga', CREATE or REPLACE FUNCTION
​pi_mall varchar2, roppop

​pi_​cit​y_code out (pi_city varchar2 )


varchar2) RETURN NUMBER AS

AS ​l_pop NUMBER;

​l_cnt NUMBER; BEGIN


​l_cid number; ​SELECT population INTO

BEGIN l_pop from


rop WHERE city=p​i_city;
​RETURN l_pop;
END;

By juliosueiras Published 17th December, 2015. Sponsored by ApolloPad.com


cheatography.com/juliosueiras/ Last updated 17th December, 2015. Set Your Pen Free and Finish Your Novel!
Page 2 of 3. https://apollopad.com
PLSQL Cheat Sheet
by juliosueiras via cheatography.com/23055/cs/6461/

Procedures Example

CREATE or REPLACE PROCEDURE ropmall


(pi_city varchar2 default 'Missi​ssa​uga',
​pi_mall varchar2,
​pi_​cit​y_code out varchar2)
AS
​l_cnt NUMBER;
​l_cid number;
BEGIN
​dbm​s_o​utp​ut.p​ut​_li​ne(​nvl​(pi​_ci​ty_​cod​e,'​NUL​L'));

​SELECT count(1) INTO l_cnt from


mall a
​WHERE
​a.m​all​_na​me=​pi_mall
​ ;
IF l_cnt = 0
THEN
​ ​ ​SELECT cid into l_cid
​ ​ FROM rop
​ ​ ​WHERE CITY=p​i_city;

​ ​ ​INSERT INTO mall VALUES (l_cid, pi_mall);


END IF;
​COMMIT;
​pi_​cit​y_c​ode​:=l​_cid;
END;

By juliosueiras Published 17th December, 2015. Sponsored by ApolloPad.com


cheatography.com/juliosueiras/ Last updated 17th December, 2015. Set Your Pen Free and Finish Your Novel!
Page 3 of 3. https://apollopad.com

You might also like