0

I just started learning PL/SQL, I know I can use nested blocks to declare variable inside the execution section but can I do it without a nested block?

For example:

Begin
    Var x number;
End;

Tried using var keyword, it didn't work.

MT0
  • 143,790
  • 11
  • 59
  • 117

2 Answers2

2

Use the DECLARE section of the PL/SQL block to declare variables:

DECLARE
  x NUMBER;
BEGIN
  NULL; -- use x for something
END;
/
MT0
  • 143,790
  • 11
  • 59
  • 117
1

var you tried is related to SQL*Plus, Oracle's command-line tool.

Can you use it? Sure:

SQL> var x number            --> declare it at SQL level
SQL> exec :x := 5            --> set its value

PL/SQL procedure successfully completed.

SQL> set serveroutput on
SQL> begin
  2    dbms_output.put_line(:x * :x);    --> use it in PL/SQL
  3  end;
  4  /
25                                       --> result

PL/SQL procedure successfully completed.

SQL>

[EDIT], based on your comment: if you want to declare variable in executable section of a PL/SQL block, you can't do that the way you wanted:

SQL> begin
  2    l_var number;
  3  end;
  4  /
  l_var number;
        *
ERROR at line 2:
ORA-06550: line 2, column 9:
PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:
:= . ( @ % ;
The symbol ":=" was substituted for "NUMBER" to continue.

Variables are to be declared in DECLARE section (that's what syntax says):

SQL> declare
  2    l_var number;
  3  begin
  4    l_var := 5;
  5  end;
  6  /

PL/SQL procedure successfully completed.

However, you can embed a new PL/SQL block into existing executable section - just follow the rules (DECLARE!):

SQL> begin
  2    declare               --> new PL/SQL block begins here ...
  3      l_var number;
  4    begin
  5      l_var := 5;
  6    end;                  --> ... and ends here
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL>

Or, you can create a package that holds variables, constants, etc.

SQL> create or replace package pkg_var as
  2    l_var                 number;
  3    c_name     constant   varchar2(20) := 'Littlefoot';
  4  end;
  5  /

Package created.

SQL>
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
  • If the OP is asking about bind variables (which is what the SQL\*Plus command `VARIABLE` defines, and is not a PL/SQL variable) then the question is a duplicate of [How to declare and set variables in PL/SQL](https://stackoverflow.com/q/61185522/1509264). – MT0 Jun 07 '23 at 11:42
  • I wouldn't know, @MT0. "I just started learning PL/SQL" might mean *anything* when users are new in Oracle and they sometimes (often?) use wrong terminology. Maybe it is actually "started learning Oracle" or "started learning SQL*Plus" or even just "started learning SQL"; can't tell. Trying to suggest something useful - sometimes it is a success, sometimes (often?) it is a failure. – Littlefoot Jun 07 '23 at 11:54
  • I am just asking if I can declare and initialise a new variable inside execution section. – user8109723 Jun 07 '23 at 21:37
  • Yes; kind of. Have a look at edited answer. – Littlefoot Jun 08 '23 at 05:52