Question

I have been told that i can omit declare keyword in a Declaration section of a stored procedure. Is declare keyword optional for a Declaration section? If it is, then why does it exist at all?

create or replace procedure myproc is

-- starting Declaration section
DECLARE  -- is DECLARE keyword optional? Can i omit it here?
cursor mycursor as
select ...;
var1 number;
var2 varchar2;
-- end of Declaration section

BEGIN
...
END myproc;
Was it helpful?

Solution

Following is the syntax for creating a pl/sql procedure. Declaration of variable and cursor is on need basis and is not mandatory. So if you want to declare then declare else ignore:

CREATE [OR REPLACE] PROCEDURE procedure_name
    [ (parameter [,parameter]) ]

IS
    [declaration_section]

BEGIN
    executable_section

[EXCEPTION
    exception_section]

END [procedure_name];

Also want to add that Declare keyword is not used in procedure as declaration block is implicitly there.

EDIT: Declare keyword exists for cases where declare block is not implicit. Lets say Anonymous block, now here you need to specify Declare block else there will be no place to declare variables.

Syntax of Anonymous block:

DECLARE
 <constant name> CONSTANT <data type> := <value>;
 <constant name> CONSTANT <data type> DEFAULT <value>;
BEGIN
  <valid statement>;
EXCEPTION
  <exception handler>;
END;

OTHER TIPS

create or replace procedure myproc is
DECLARE
    cursor mycursor as select 1 from dual;

    var1 TABLE1%ROWTYPE;
BEGIN
    NULL;
END myproc;
/*
PROCEDURE MYPROC compiled
Errors: check compiler log
*/

create or replace procedure myproc is
    /* DECLARE */
    cursor mycursor /*as*/ is select 1 from dual;

    var1 TABLE1%ROWTYPE;
BEGIN
    NULL;
END myproc;
/*
PROCEDURE MYPROC compiled
*/

Anonymous block:

DECLARE
/* declaration section */

BEGIN
/* code execution */

END;

Stored procedure:

CREATE OR REPLACE PROCEDURE example_proc
IS
/* declaration section */

BEGIN
/* code execution */

END;

Function:

CREATE OR REPLACE FUNCTION example_fun RETURN NUMBER
IS
/* declaration section */

BEGIN
/* code execution */

END;

In A anonymous block you use declaration section when you need it (when you need to declare something) i.e. a variable:

DECLARE
    l_var NUMBER;
BEGIN
    SELECT  1
    INTO    l_var
    FROM    DUAL;

    DBMS_OUTPUT.PUT_LINE(TO_CHAR(l_var));
END;
-- anonymous block completed
-- 1

Sometimes you do not need to declare anything, i.e:

CREATE OR REPLACE PACKAGE my_print IS
    PROCEDURE my_line;
END my_print;
-- PACKAGE MY_PRINT compiled

CREATE OR REPLACE PACKAGE BODY my_print IS
    PROCEDURE my_line
    IS
        /* nothing to declare */
    BEGIN
        DBMS_OUTPUT.PUT_LINE('a string');
    END my_line;
END my_print;
-- PACKAGE BODY MY_PRINT compiled

/* nothing to declare */
BEGIN
    my_print.my_line;
END;
-- anonymous block completed
-- a string

...or simply:

/* nothing to declare */
BEGIN
    DBMS_OUTPUT.PUT_LINE('a string');
END;

...or:

BEGIN
    INSERT INTO a_table
    SELECT * FROM b_table WHERE d_date > TO_DATE('2013-01-01','YYYY-MM-DD');

    COMMIT;
END;

Is it clear?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top