Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view the type of a variable in PL/SQL?

Is there a function in PL/SQL to show a variable's exact type, like the DUMP function in SQL?

I've tried the following

DECLARE
   l_variable   INTEGER := 1;
BEGIN
   DBMS_OUTPUT.PUT_LINE (DUMP (l_variable));
END;

But it gives the following error:

PLS-00204: function or pseudo-column 'DUMP' may be used inside a SQL statement only

like image 690
csadam Avatar asked Jun 16 '13 14:06

csadam


People also ask

How do I find the data type of a variable in PL SQL?

If the variable doesn't exist you'll get a NO_DATA_FOUND , and if there are two variables with the same name in the object you'll get ORA-01422: exact fetch returns more than requested number of rows .

How do you determine the datatype of a variable in Oracle?

Also, if you have Toad for Oracle, you can highlight the statement and press CTRL + F9 and you'll get a nice view of column and their datatypes.

How do you display a variable value in PL SQL?

In PL/SQL, a variable is named storage location that stores a value of a particular data type. The value of the variable changes through the program. Before using a variable, you must declare it in the declaration section of a block.

What is type attribute in PL SQL?

The %TYPE attribute lets use the datatype of a field, record, nested table, database column, or variable in your own declarations, rather than hardcoding the type names. You can use the %TYPE attribute as a datatype specifier when declaring constants, variables, fields, and parameters.


2 Answers

declare
  a number(10,3);
  type_info varchar2(400);
begin
  a := 55.5;
  select dump(a) into type_info from dual;
  DBMS_OUTPUT.PUT_LINE(type_info);
end;
like image 169
Egor Skriptunoff Avatar answered Sep 23 '22 15:09

Egor Skriptunoff


as you should notice, DUMP is an overloaded function. it has 3 overloads.

So you can simulate the same thing within your code.

function myDump (x Varchar2) return varchar2 is begin return('string') ; end ;
function myDump (x number) return varchar2 is begin return('integer') ; end ;
function myDump (x date) return varchar2 is begin return('date') ; end ;

the above code may not work properly but should give you the idea how to deal the problem.

I hope that this will fulfil your requirements.

Note; you can put these functions in a Package and use them accordingly.

like image 34
Ali Avcı Avatar answered Sep 25 '22 15:09

Ali Avcı