Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create Oracle Type which refers to table columns for data type?

I am trying to define a type using the following code.

CREATE OR REPLACE TYPE MY_TYPE AS OBJECT (
    app_id        some_table_name.app_id%type
);

If I run this, I get the error.

Error(4,32): PLS-00201: identifier 'some_table_name.app_id' must be declared

What is wrong with this?

like image 489
AppleGrew Avatar asked Mar 09 '12 13:03

AppleGrew


People also ask

How do you create a table type in Oracle?

Defining TABLE Types. TYPE table_type_name IS TABLE OF datatype [NOT NULL] INDEX BY BINARY_INTEGER; where table_type_name is a type specifier used in subsequent declarations of PL/SQL tables. The INDEX BY clause must specify datatype BINARY_INTEGER, which has a magnitude range of -2147483647 ..

What provides the data type of variable or table column?

The %TYPE attribute provides the datatype of a variable or table column. This is particularly useful when declaring variables that will hold values of a table column. For example, suppose you want to declare variables as the same datatype as the employee_id and last_name columns in employees table.

Which of the collection types are used as column datatype in a table?

The phone_varray_typ VARRAY type is used as a data type for a column in the dept_phone_list table.

How do I find the data type of a table 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.


1 Answers

What's wrong with it is that %type is PL/SQL syntax. It isn't supported in SQL. Now we use PL/SQL to define Types (especially member functions, constructors, etc) but the Types themselves are SQL objects, and so follow SQL rules. That means we must declare Type attributes with explicit datatypes.

I agree that's a shame, and it would be really neat if we could reference table columns in type declarations like this. Unfortunately Oracle have really slowed down the changes to their TYPE implementation over the last couple of versions, so I think it is unlikely this will change in the near future.

What I would really like to see is Oracle support this syntax:

CREATE OR REPLACE TYPE MY_TYPE AS OBJECT 
      (     one_row        some_table_name.%rowtype ); 

Dynamic objects for interfaces: how cool would that be?

like image 167
APC Avatar answered Oct 03 '22 15:10

APC