Search code examples
oracle-databasevariablesplsqldeveloper

Declare Variables in Oracle (PL/SQL Developer)


I'm new to Oracle / PL/SQL Developer and was wondering the best practise to declare variables.

In TSQL, I'm use to doing the following below and was wondering what the equivalent in oracle is.

DECLARE @WeekNumber Date
SET @WeekNumber '2020-10-01'
SELECT @Date ..... 

Cheers, I appreciate this is quite basic.

enter image description here


Solution

  • In Plsql you have the option to declare a variable based on existing datatype of table column.

    Suppose you have a table table1 column Current_week as date datatype then you can declare it like this.

    declare
    weeknumber table1.current_week%type;
    begin
    select week into weeknumber from table1;
    end;
    

    This is best practice to declare variables in Oracle PL/SQL to avoid any datatype issues.