How to declare and display a variable in Oracle

David picture David · Jan 4, 2012 · Viewed 164.9k times · Source

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.

Answer

Sathyajith Bhat picture Sathyajith Bhat · Jan 4, 2012

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;