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

后端 未结 2 1234
野的像风
野的像风 2020-12-11 03:16

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
         


        
相关标签:
2条回答
  • 2020-12-11 03:27

    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?

    0 讨论(0)
  • 2020-12-11 03:42

    You cannot use some_table_name.app_id%type when declaring a type in the database, any more than you can do this:

    create table emp (empno number,
                      deptno dept.deptnp%type, -- NOT ALLOWED
                     );
    

    You must use either a built-in type such as NUMBER, VARCHAR2(10) or a user-defined type such as mytype

    0 讨论(0)
提交回复
热议问题