Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "%Type" mean in Oracle sql?

Tags:

sql

oracle

plsql

I'm getting my first experience with Oracle and TOAD (I know SSMS). I came across this "%Type" next to an input parameter in an update procedure and I have no idea what it is or what it means. I found links on Google related to "%Rowtype". Is the same thing or something entirely different?

If this is vague, I apologize. As always, thanks for the help.

like image 419
Matt M Avatar asked Sep 24 '10 20:09

Matt M


People also ask

What is %type in SQL?

The %TYPE attribute, used in PL/SQL variable and parameter declarations, is supported by the data server. Use of this attribute ensures that type compatibility between table columns and PL/SQL variables is maintained.

What does %type do 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.

What is %type and %ROW type in Oracle?

The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Variables declared using %ROWTYPE are treated like those declared using a datatype name.

What is %type and Rowtype in PL SQL?

The %ROWTYPE attribute, used to declare PL/SQL variables of type record with fields that correspond to the columns of a table or view, is supported by the Db2® data server. Each field in a PL/SQL record assumes the data type of the corresponding column in the table.


2 Answers

Oracle (and PostgreSQL) have:

  • %TYPE
  • %ROWTYPE

%TYPE

%TYPE is used to declare variables with relation to the data type of a column in an existing table:

DECLARE v_id ORDERS.ORDER_ID%TYPE 

The benefit here is that if the data type changes, the variable data type stays in sync.

Reference: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/fundamentals.htm#i6080

%ROWTYPE

This is used in cursors to declare a single variable to contain a single record from the resultset of a cursor or table without needing to specify individual variables (and their data types). Ex:

DECLARE   CURSOR c1 IS      SELECT last_name, salary, hire_date, job_id         FROM employees        WHERE employee_id = 120;    -- declare record variable that represents a row fetched from the employees table   employee_rec c1%ROWTYPE;   BEGIN  -- open the explicit cursor and use it to fetch data into employee_rec  OPEN c1;  FETCH c1 INTO employee_rec;  DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_rec.last_name); END; / 
like image 115
OMG Ponies Avatar answered Oct 02 '22 10:10

OMG Ponies


Apart from the purpose pointed by OMG Ponies, %TYPE is also used for inheriting the same data type used by a previously declared variable.

The syntax is :

 DECLARE             L_num NUMBER(5,2) NOT NULL default 3.21;             L_num_Test L_num%TYPE := 1.123; 

So there is no need to declare the data type for the second variable i.e L_num_Test.

Comment if anyone needs further clarification regarding this topic.

Reference: https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/fundamentals.htm#BEIIGBBF

like image 24
Roshan jha Avatar answered Oct 02 '22 12:10

Roshan jha