Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare and display a variable in Oracle

I would like to declare and display a variable in Oracle.

In T-SQL I would do something like this

DECLARE @A VARCHAR(10) --Declares @A
SELECT @A = '12' --Assigns @A
SELECT @A --Displays @A

How can I do this in Oracle.

like image 767
David Avatar asked Jan 04 '12 09:01

David


People also ask

How do you display variables in PL SQL?

Variable Declaration in PL/SQLPL/SQL variables must be declared in the declaration section or in a package as a global variable. When you declare a variable, PL/SQL allocates memory for the variable's value and the storage location is identified by the variable name.


3 Answers

If you're talking about PL/SQL, you should put it in an anonymous block.

DECLARE
    v_text VARCHAR2(10); -- declare
BEGIN
    v_text := 'Hello';  --assign
    dbms_output.Put_line(v_text); --display
END; 
like image 144
Sathyajith Bhat Avatar answered Sep 24 '22 13:09

Sathyajith Bhat


If using sqlplus you can define a variable thus:

define <varname>=<varvalue>

And you can display the value by:

define <varname>

And then use it in a query as, for example:

select *
from tab1
where col1 = '&varname';
like image 27
John Doyle Avatar answered Sep 25 '22 13:09

John Doyle


If you are using pl/sql then the following code should work :

set server output on -- to retrieve and display a buffer

DECLARE

    v_text VARCHAR2(10); -- declare
BEGIN

    v_text := 'Hello';  --assign
    dbms_output.Put_line(v_text); --display
END; 

/

-- this must be use to execute pl/sql script

like image 3
Jahanzaib Mazhar Avatar answered Sep 23 '22 13:09

Jahanzaib Mazhar