<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
(	O
also	O
termed	O
proc	O
,	O
storp	B-General_Concept
,	O
sproc	B-General_Concept
,	O
StoPro	O
,	O
StoredProc	B-General_Concept
,	O
StoreProc	O
,	O
sp	O
,	O
or	O
SP	O
)	O
is	O
a	O
subroutine	O
available	O
to	O
applications	O
that	O
access	O
a	O
relational	B-Application
database	I-Application
management	I-Application
system	I-Application
(	O
RDBMS	O
)	O
.	O
</s>
<s>
Such	O
procedures	O
are	O
stored	O
in	O
the	O
database	O
data	B-Application
dictionary	I-Application
.	O
</s>
<s>
Uses	O
for	O
stored	B-General_Concept
procedures	I-General_Concept
include	O
data-validation	O
(	O
integrated	O
into	O
the	O
database	O
)	O
or	O
access-control	O
mechanisms	O
.	O
</s>
<s>
Furthermore	O
,	O
stored	B-General_Concept
procedures	I-General_Concept
can	O
consolidate	O
and	O
centralize	O
logic	O
that	O
was	O
originally	O
implemented	O
in	O
applications	O
.	O
</s>
<s>
To	O
save	O
time	O
and	O
memory	O
,	O
extensive	O
or	O
complex	O
processing	O
that	O
requires	O
execution	O
of	O
several	O
SQL	B-Language
statements	O
can	O
be	O
saved	O
into	O
stored	B-General_Concept
procedures	I-General_Concept
,	O
and	O
all	O
applications	O
call	O
the	O
procedures	O
.	O
</s>
<s>
One	O
can	O
use	O
nested	O
stored	B-General_Concept
procedures	I-General_Concept
by	O
executing	O
one	O
stored	B-General_Concept
procedure	I-General_Concept
from	O
within	O
another	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
may	O
return	O
result	B-Application
sets	I-Application
,	O
i.e.	O
,	O
the	O
results	O
of	O
a	O
SELECT	O
statement	O
.	O
</s>
<s>
Such	O
result	B-Application
sets	I-Application
can	O
be	O
processed	O
using	O
cursors	B-Language
,	O
by	O
other	O
stored	B-General_Concept
procedures	I-General_Concept
,	O
by	O
associating	O
a	O
result-set	O
locator	O
,	O
or	O
by	O
applications	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
may	O
also	O
contain	O
declared	O
variables	O
for	O
processing	O
data	O
and	O
cursors	B-Language
that	O
allow	O
it	O
to	O
loop	O
through	O
multiple	O
rows	O
in	O
a	O
table	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
can	O
receive	O
variables	O
,	O
return	O
results	O
or	O
modify	O
variables	O
and	O
return	O
them	O
,	O
depending	O
on	O
how	O
and	O
where	O
the	O
variable	O
is	O
declared	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
are	O
similar	O
to	O
user-defined	B-Language
functions	I-Language
(	O
UDFs	O
)	O
.	O
</s>
<s>
The	O
major	O
difference	O
is	O
that	O
UDFs	O
can	O
be	O
used	O
like	O
any	O
other	O
expression	O
within	O
SQL	B-Language
statements	O
,	O
whereas	O
stored	B-General_Concept
procedures	I-General_Concept
must	O
be	O
invoked	O
using	O
the	O
CALL	O
statement	O
.	O
</s>
<s>
The	O
exact	O
and	O
correct	O
implementation	O
of	O
stored	B-General_Concept
procedures	I-General_Concept
varies	O
from	O
one	O
database	O
system	O
to	O
the	O
other	O
.	O
</s>
<s>
Depending	O
on	O
the	O
database	O
system	O
,	O
stored	B-General_Concept
procedures	I-General_Concept
can	O
be	O
implemented	O
in	O
a	O
variety	O
of	O
programming	O
languages	O
,	O
for	O
example	O
SQL	B-Language
,	O
Java	B-Language
,	O
C	B-Language
,	O
or	O
C++	B-Language
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
written	O
in	O
non-SQL	O
languages	O
may	O
or	O
may	O
not	O
execute	O
SQL	B-Language
statements	O
themselves	O
.	O
</s>
<s>
The	O
increasing	O
adoption	O
of	O
stored	B-General_Concept
procedures	I-General_Concept
led	O
to	O
the	O
introduction	O
of	O
procedural	O
elements	O
to	O
the	O
SQL	B-Language
language	I-Language
in	O
the	O
SQL:1999	B-Language
and	O
SQL:2003	B-Language
standards	O
in	O
the	O
part	O
SQL/PSM	B-Language
.	O
</s>
<s>
That	O
made	O
SQL	B-Language
an	O
imperative	B-Application
programming	I-Application
language	I-Application
.	O
</s>
<s>
Most	O
database	O
systems	O
offer	O
proprietary	O
and	O
vendor-specific	O
extensions	O
,	O
exceeding	O
SQL/PSM	B-Language
.	O
</s>
<s>
A	O
standard	O
specification	O
for	O
Java	B-Language
stored	I-Language
procedures	I-Language
exists	O
as	O
well	O
as	O
SQL/JRT	B-Language
.	O
</s>
<s>
Overhead	O
Because	O
stored	B-General_Concept
procedure	I-General_Concept
statements	O
are	O
stored	O
directly	O
in	O
the	O
database	O
,	O
they	O
may	O
remove	O
all	O
or	O
part	O
of	O
the	O
compiling	O
overhead	O
that	O
is	O
typically	O
needed	O
in	O
situations	O
where	O
software	O
applications	O
send	O
inline	O
(	O
dynamic	O
)	O
SQL	B-Language
queries	O
to	O
a	O
database	O
.	O
</s>
<s>
(	O
However	O
,	O
most	O
database	O
systems	O
implement	O
statement	O
caches	O
and	O
other	O
methods	O
to	O
avoid	O
repetitively	O
compiling	O
dynamic	O
SQL	B-Language
statements	O
.	O
)	O
</s>
<s>
Also	O
,	O
while	O
they	O
avoid	O
some	O
pre-compiled	O
SQL	B-Language
,	O
statements	O
add	O
to	O
the	O
complexity	O
of	O
creating	O
an	O
optimal	O
execution	O
plan	O
because	O
not	O
all	O
arguments	O
of	O
the	O
SQL	B-Language
statement	O
are	O
supplied	O
at	O
compile	O
time	O
.	O
</s>
<s>
Depending	O
on	O
the	O
specific	O
database	O
implementation	O
and	O
configuration	O
,	O
mixed	O
performance	O
results	O
will	O
be	O
seen	O
from	O
stored	B-General_Concept
procedures	I-General_Concept
versus	O
generic	O
queries	O
or	O
user	B-Language
defined	I-Language
functions	I-Language
.	O
</s>
<s>
Avoiding	O
network	O
traffic	O
A	O
major	O
advantage	O
of	O
stored	B-General_Concept
procedures	I-General_Concept
is	O
that	O
they	O
can	O
run	O
directly	O
within	O
the	O
database	B-General_Concept
engine	I-General_Concept
.	O
</s>
<s>
This	O
becomes	O
more	O
important	O
for	O
complex	O
series	O
of	O
SQL	B-Language
statements	O
.	O
</s>
<s>
Encapsulating	O
business	B-Architecture
logic	I-Architecture
Stored	B-General_Concept
procedures	I-General_Concept
allow	O
programmers	O
to	O
embed	O
business	B-Architecture
logic	I-Architecture
as	O
an	O
API	O
in	O
the	O
database	O
,	O
which	O
can	O
simplify	O
data	O
management	O
and	O
reduce	O
the	O
need	O
to	O
encode	O
the	O
logic	O
elsewhere	O
in	O
client	O
programs	O
.	O
</s>
<s>
The	O
database	O
system	O
can	O
ensure	O
data	O
integrity	O
and	O
consistency	B-General_Concept
with	O
the	O
help	O
of	O
stored	B-General_Concept
procedures	I-General_Concept
.	O
</s>
<s>
Delegating	O
access-rights	O
In	O
many	O
systems	O
,	O
stored	B-General_Concept
procedures	I-General_Concept
can	O
be	O
granted	O
access	O
rights	O
to	O
the	O
database	O
that	O
users	O
who	O
execute	O
those	O
procedures	O
do	O
not	O
directly	O
have	O
.	O
</s>
<s>
Some	O
protection	O
from	O
SQL	B-Language
injection	O
attacks	O
Stored	B-General_Concept
procedures	I-General_Concept
can	O
be	O
used	O
to	O
protect	O
against	O
injection	O
attacks	O
.	O
</s>
<s>
Stored	B-General_Concept
procedure	I-General_Concept
parameters	O
will	O
be	O
treated	O
as	O
data	O
even	O
if	O
an	O
attacker	O
inserts	O
SQL	B-Language
commands	O
.	O
</s>
<s>
However	O
,	O
a	O
stored	B-General_Concept
procedure	I-General_Concept
that	O
in	O
turn	O
generates	O
dynamic	O
SQL	B-Language
using	O
the	O
input	O
is	O
still	O
vulnerable	O
to	O
SQL	B-Language
injections	O
unless	O
proper	O
precautions	O
are	O
taken	O
.	O
</s>
<s>
In	O
some	O
systems	O
,	O
stored	B-General_Concept
procedures	I-General_Concept
can	O
be	O
used	O
to	O
control	O
transaction	O
management	O
;	O
in	O
others	O
,	O
stored	B-General_Concept
procedures	I-General_Concept
run	O
inside	O
a	O
transaction	O
such	O
that	O
transactions	O
are	O
effectively	O
transparent	O
to	O
them	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
can	O
also	O
be	O
invoked	O
from	O
a	O
database	B-Language
trigger	I-Language
or	O
a	O
condition	O
handler	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
stored	B-General_Concept
procedure	I-General_Concept
may	O
be	O
triggered	O
by	O
an	O
insert	O
on	O
a	O
specific	O
table	O
,	O
or	O
update	O
of	O
a	O
specific	O
field	O
in	O
a	O
table	O
,	O
and	O
the	O
code	O
inside	O
the	O
stored	B-General_Concept
procedure	I-General_Concept
would	O
be	O
executed	O
.	O
</s>
<s>
Writing	O
stored	B-General_Concept
procedures	I-General_Concept
as	O
condition	O
handlers	O
also	O
allows	O
database	O
administrators	O
to	O
track	O
errors	O
in	O
the	O
system	O
with	O
greater	O
detail	O
by	O
using	O
stored	B-General_Concept
procedures	I-General_Concept
to	O
catch	O
the	O
errors	O
and	O
record	O
some	O
audit	O
information	O
in	O
the	O
database	O
or	O
an	O
external	O
resource	O
like	O
a	O
file	O
.	O
</s>
<s>
Functions	O
must	O
return	O
a	O
value	O
(	O
using	O
the	O
RETURN	O
keyword	O
)	O
,	O
but	O
for	O
stored	B-General_Concept
procedures	I-General_Concept
this	O
is	O
not	O
mandatory	O
.	O
</s>
<s>
Stored	B-General_Concept
procedures	I-General_Concept
can	O
use	O
RETURN	O
keyword	O
but	O
with	O
no	O
value	O
being	O
passed	O
.	O
</s>
<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
can	O
return	O
multiple	O
values	O
using	O
the	O
OUT	O
parameter	O
,	O
or	O
return	O
no	O
value	O
.	O
</s>
<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
saves	O
the	O
query	O
compiling	O
time	O
.	O
</s>
<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
is	O
a	O
database	O
object	O
.	O
</s>
<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
is	O
a	O
material	O
object	O
.	O
</s>
<s>
Prepared	B-Language
statements	I-Language
take	O
an	O
ordinary	O
statement	O
or	O
query	O
and	O
parameterize	O
it	O
so	O
that	O
different	O
literal	O
values	O
can	O
be	O
used	O
at	O
a	O
later	O
time	O
.	O
</s>
<s>
Like	O
stored	B-General_Concept
procedures	I-General_Concept
,	O
they	O
are	O
stored	O
on	O
the	O
server	O
for	O
efficiency	O
and	O
provide	O
some	O
protection	O
from	O
SQL	B-Language
injection	O
attacks	O
.	O
</s>
<s>
Although	O
simpler	O
and	O
more	O
declarative	O
,	O
prepared	B-Language
statements	I-Language
are	O
not	O
ordinarily	O
written	O
to	O
use	O
procedural	O
logic	O
and	O
cannot	O
operate	O
on	O
variables	O
.	O
</s>
<s>
Because	O
of	O
their	O
simple	O
interface	O
and	O
client-side	O
implementations	O
,	O
prepared	B-Language
statements	I-Language
are	O
more	O
widely	O
reusable	O
between	O
DBMS	O
.	O
</s>
<s>
Smart	B-Application
contract	I-Application
is	O
a	O
term	O
applied	O
to	O
executable	O
code	O
stored	O
on	O
a	O
blockchain	B-Application
as	O
opposed	O
to	O
an	O
RDBMS	O
.	O
</s>
<s>
Despite	O
the	O
execution	O
result	O
consensus	O
mechanisms	O
of	O
public	O
blockchain	B-Application
networks	O
differing	O
in	O
principle	O
from	O
traditional	O
private	O
or	O
federated	O
databases	O
,	O
they	O
perform	O
ostensibly	O
the	O
same	O
function	O
as	O
stored	B-General_Concept
procedures	I-General_Concept
,	O
albeit	O
usually	O
with	O
a	O
sense	O
of	O
value	O
transaction	O
.	O
</s>
<s>
Stored	B-General_Concept
procedure	I-General_Concept
languages	O
are	O
often	O
vendor-specific	O
.	O
</s>
<s>
Changing	O
database	O
vendors	O
usually	O
requires	O
rewriting	O
existing	O
stored	B-General_Concept
procedures	I-General_Concept
.	O
</s>
<s>
Changes	O
to	O
stored	B-General_Concept
procedures	I-General_Concept
are	O
harder	O
to	O
keep	O
track	O
of	O
within	O
a	O
version	O
control	O
system	O
than	O
other	O
code	O
.	O
</s>
<s>
Errors	O
in	O
stored	B-General_Concept
procedures	I-General_Concept
cannot	O
be	O
caught	O
as	O
part	O
of	O
a	O
compilation	O
or	O
build	O
step	O
in	O
an	O
application	O
IDE	O
-	O
the	O
same	O
is	O
true	O
if	O
a	O
stored	B-General_Concept
procedure	I-General_Concept
went	O
missing	O
or	O
was	O
accidentally	O
deleted	O
.	O
</s>
<s>
Stored	B-General_Concept
procedure	I-General_Concept
languages	O
from	O
different	O
vendors	O
have	O
different	O
levels	O
of	O
sophistication	O
.	O
</s>
<s>
For	O
example	O
,	O
Postgres	B-Application
 '	O
pgpsql	O
has	O
more	O
language	O
features	O
(	O
especially	O
via	O
extensions	O
)	O
than	O
Microsoft	O
's	O
T-SQL	B-Language
.	O
</s>
<s>
Tool	O
support	O
for	O
writing	O
and	O
debugging	O
stored	B-General_Concept
procedures	I-General_Concept
is	O
often	O
not	O
as	O
good	O
as	O
for	O
other	O
programming	O
languages	O
,	O
but	O
this	O
differs	O
between	O
vendors	O
and	O
languages	O
.	O
</s>
<s>
For	O
example	O
,	O
both	O
PL/SQL	B-Language
and	O
T-SQL	B-Language
have	O
dedicated	O
IDEs	O
and	O
debuggers	O
.	O
</s>
<s>
PL/PgSQL	B-Language
can	O
be	O
debugged	O
from	O
various	O
IDEs	O
.	O
</s>
