Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL difference between DEFAULT and assignment operator

Tags:

oracle

plsql

I'm newbie in PL/SQL and this question might seem to be 'childish' so I'm sorry in advance, but Google didn't help me at all...

Is there any difference between following procedures?

Procedure p1(a Number DEFAULT 0) Is
Begin
    DBMS_OUTPUT.put_line(a);
End;


Procedure p2(a Number := 0) Is
Begin
    DBMS_OUTPUT.put_line(a);
End;

I have checked them, so when I call both of them without args the output is 0, but I'm not sure if there are any side effects.

like image 842
LibertyPaul Avatar asked Sep 14 '16 15:09

LibertyPaul


People also ask

What is the assignment operator in PL SQL?

The assignment statement sets a previously-declared variable or formal OUT or IN OUT parameter to the value of an expression.

What is the difference between %type and %Rowtype?

Difference between %type and %rowtype in tabular format.If you dont know the datatype of specified column and you require to assign that datatype to the variable then you can use %ROWTYPE.

What is the difference between the and := operators in PL SQL?

= is the equality comparison operator, both in PL/SQL and SQL. := is the PL/SQL value assignment operator.

What is DEFAULT keyword in PL SQL?

The DEFAULT keyword provides a default value to a column when the Oracle INSERT INTO statement does not provide a specific value. The default value can be literal value, an expression, or a SQL Function, such as SYSDATE.


1 Answers

If you follow Oracle's documentation

You can use the keyword DEFAULT instead of the assignment operator to initialize variables. You can also use DEFAULT to initialize subprogram parameters, cursor parameters, and fields in a user-defined record.

Use DEFAULT for variables that have a typical value. Use the assignment operator for variables (such as counters and accumulators) that have no typical value.

DECLARE
    blood_type CHAR DEFAULT 'O';         -- Same as blood_type CHAR := 'O';
    hours_worked    INTEGER DEFAULT 40;  -- Typical value
    employee_count  INTEGER := 0;        -- No typical value

  BEGIN
    NULL;
  END;
  /

So I guess internally is the same.

like image 77
vercelli Avatar answered Sep 28 '22 03:09

vercelli