How When Writes QL in Pls QL
How When Writes QL in Pls QL
Steven Feuerstein
Oracle Architect, Applied PL/SQL
[email protected]
twitter: @sfonplsql
blog: stevenfeuersteinonplsql.blogspot.com
Copyright
2014
Oracle
and/or
its
aliates.
All
rights
reserved.
|
What
we
say
about
SQL
Page 3
The
Backend
Order
Table
Item
Table
Customer
Table
The result? Slow, buggy code that is dicult to opKmize and maintain.
Flashback
query
No
more
need
for
journal
tables,
history
tables,
etc.
select d.deptno
, (select count(*)
from emp e where e.deptno =
d.deptno) number_staff from dept
Copyright
2014
Oracle
and/or
its
aliates.
All
rights
reserved.
|
BEGIN
IF salary_in > 1505000 THEN ...
When
a
literal
changes,
you
must
nd
all
occurrences
and
change
them.
With
values
like
"45098",
a
global
search/replace
gets
the
job
done
(if
you
are
very
careful).
When
a
table
changes,
you
must
nd
all
the
SQL
statements
that
are
aected.
When
your
hard-coding
is
a
DML
statement,
you've
got
a
serious
problem.
How
can
you
be
sure
you
found
all
the
same
logical
occurrences?
Logically,
a
SQL
statement
and
a
literal
value
are
the
same,
when
it
comes
to
hard-
coding.
So
if
you
don't
like
hard-coded
literals,
you
should
hate
hard-coded
SQL.
10
DO
Hide
complex
queries
behind
views,
and
use
those
views
in
reports.
Put
all
your
other
SQL
statements
in
PL/SQL
packages
(cursors,
subprograms),
hiding
the
SQL
behind
an
API
of
procedures
and
funcKons.
Application
Code
Intermediate Layer
Order
Table
Item
Table
11g_frc_demo.sql
11g_frc_encapsulaKon.sql
11g_emplu.*
l_name employee_rp.fullname_t;
BEGIN
l_name :=
employee_rp.fullname (
employee_id_in);
...
END;
14
fullname.pkg
explimpl.pkg
Do
not
call
a
"cascade"
of
funcKons,
when
you
can
execute
a
single
query
(in
a
possibly
new
funcKon).
It's
kind
of
like
of
nested
cursor
FOR
loops
instead
of
a
join!
Both
bad
news....
Bad
Cascade
IS
l_employee := employees_pkg.one_row (employee_id_in);
l_department := departments_pkg.one_row (l_employee.department_id);
BEGIN
Copyright
2014
Oracle
and/or
its
aliates.
All
rights
reserved.
|
15
When
fetching
and
changing
mulKple
rows,
use
bulk
processing
features:
BULK
COLLECT
and
FORALL.
Covered
in
separate
webinar
in
this
series.
16
BEGIN
SELECT
INTO
FROM
WHERE
BEGIN
l_name := employees_pkg.full_name (employee_id_in);
17
usebinding.sp
useconcat*.sp
18
dropwhatever.sp
19
You
can
"limit"
the
eect
of
the
implicit
commit
to
just
that
DDL
statement
by
adding
the
AUTONOMOUS_TRANSACTION
pragma.
Oh,
and
you
should
also
probably
use
AUTHID
CURRENT_USER.
That
way,
you
ensure
that
the
eect
of
the
dynamic
DDL
(and
SQL)
is
on
the
invoking,
not
dening,
schema.
dropwhatever.sp
20
21
23