Variables that are used in a block must generally be defined in the declaration section of the block unless they are global variables or package-level variables. A variable declaration consists of a name that is assigned to the variable and the data type of the variable. Optionally, the variable can be initialized to a default value within the variable declaration.
>>-name--+----------+--type--+----------+-----------------------> '-CONSTANT-' '-NOT NULL-' >--+-----------------------------+----------------------------->< '-+-:=------+--+-expression-+-' '-DEFAULT-' '-NULL-------'
CREATE OR REPLACE PROCEDURE dept_salary_rpt (
p_deptno NUMBER
)
IS
todays_date DATE := SYSDATE;
rpt_title VARCHAR2(60) := 'Report For Department # ' || p_deptno
|| ' on ' || todays_date;
base_sal INTEGER := 35525;
base_comm_rate NUMBER := 1.33333;
base_annual NUMBER := ROUND(base_sal * base_comm_rate, 2);
BEGIN
DBMS_OUTPUT.PUT_LINE(rpt_title);
DBMS_OUTPUT.PUT_LINE('Base Annual Salary: ' || base_annual);
END;
The following sample output was obtained by calling
this procedure:CALL dept_salary_rpt(20);
Report For Department # 20 on 10-JUL-07 16:44:45
Base Annual Salary: 47366.55
create table T(col1 integer);
insert into T values null;
declare
N integer not null := 0;
null_variable exception;
begin
select col1 into N from T;
if N is null then
raise null_variable;
end if;
exception
when null_variable then
-- Handle error condition here.
dbms_output.put_line('Null variable detected');
end;