CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
< declarations >
BEGIN
< procedure_body >
EXCEPTION -- Exception-handling part begins
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
END procedure_name;
The following simple procedure displays the text "Hello World" in a client that supports dbms_output
.
CREATE OR REPLACE PROCEDURE helloworld
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
You need to execute this at the SQL prompt to create the procedure in the database, or you can run the query below to get the same result:
SELECT 'Hello World!' from dual;
PL/SQL uses IN, OUT, IN OUT keywords to define what can happen to a passed parameter.
IN specifies that the parameter is read only and the value cannot be changed by the procedure.
OUT specifies the parameter is write only and a procedure can assign a value to it, but not reference the value.
IN OUT specifies the parameter is available for reference and modification.
PROCEDURE procedureName(x IN INT, strVar IN VARCHAR2, ans OUT VARCHAR2)
...
...
END procedureName;
procedureName(firstvar, secondvar, thirdvar);
The variables passed in the above example need to be typed as they are defined in the procedure parameter section.