简体   繁体   English

有没有一种方法可以创建与表的%rowtype相匹配的Object类型?

[英]Is there a way to create an Object type matching %rowtype of a table?

I need a schema level type (to reference it from c++ code) that will always be the same(same fields) as %rowtype record of a table. 我需要一个架构级别的类型(从c ++代码引用它),该类型始终与表的%rowtype记录相同(相同的字段)。 Sadly, I cannot use %rowtype directly from code and can only work with named types declared as 可悲的是,我不能直接从代码中使用%rowtype,只能与声明为

create type blabla is object...

Is there a way to create this in an automatic manner or will I have to manually create and support object type for each table I need this for? 有没有一种自动创建此方法的方法,还是我必须为需要此操作的每个表手动创建和支持对象类型?

Object types are schema-level database objects, which means that they must conform to SQL datatypes and syntax (except for those parts which are implemented in PL/SQL: the methods). 对象类型是架构级别的数据库对象,这意味着它们必须符合SQL数据类型和语法(在PL / SQL中实现的那些部分除外:方法)。

%ROWTYPE and %TYPE are both PL/SQL attributes and thus cannot be used when defining attributes of an object type. %ROWTYPE和%TYPE都是PL / SQL属性,因此在定义对象类型的属性时不能使用。

I know - it bums me out, too. 我知道-这也让我感到震惊。

If you need to create an object type that mirrors a table, for lots of tables, it will not be too difficult to write a generator with a query against user_tab_columns. 如果您需要创建一个镜像表的对象类型,对于许多表而言,编写带有针对user_tab_columns的查询的生成器将不会太困难。

You can not use the %ROWTYPE in building an object type, but you can get the code to build types based on tables without manually scanning the table structure and writing the type. 您不能在构建对象类型时使用%ROWTYPE ,但无需手动扫描表结构和编写类型就可以获取基于表构建类型的代码。

Say you have tables like these: 假设您有这样的表格:

create table someTypesTable( n   number(10),
                             n2  number(7, 3),
                             v   varchar2(16),
                             d   date, 
                             t   timestamp)

create table someOtherTypesTable( n number,
                                  c clob,
                                  t timestamp(3))

You can use USER_TAB_COLS ( or ALL_TAB_COLUMNS, DBA_TAB_COLUMNS, depending on your need) to get informations about the columns of these tables and build a piece of code that creates the types you need. 您可以使用USER_TAB_COLS (或ALL_TAB_COLUMNS,DBA_TAB_COLUMNS,具体取决于您的需求)来获取有关这些表的列的信息,并构建一段代码来创建所需的类型。

This could be a starting point, to be refined to handle the different types you may encounter in the tables: 这可能是一个起点,需要完善以处理表中可能遇到的不同类型:

select 'create or replace type tTab' || table_name ||
        ' as object ( ' ||
       listagg(
               column_name || ' ' ||
               data_type ||
               case when data_type = 'NUMBER' and data_precision is not null then '(' || data_precision || ',' || data_scale || ')'
                    when data_type = 'VARCHAR2' then '(' || data_length ||')'
               end
              , ', ' ) within group ( order by column_id) ||
       ')'
from user_tab_cols
where table_name in ('SOMETYPESTABLE', 'SOMEOTHERTYPESTABLE')
group by table_name

This gives, for the tables above, the following types: 对于上面的表,这提供了以下类型:

CREATE OR REPLACE TYPE tTabSOMEOTHERTYPESTABLE AS OBJECT
(
    N NUMBER,
    C CLOB,
    T TIMESTAMP(3)
)

CREATE OR REPLACE TYPE tTabSOMETYPESTABLE AS OBJECT
(
    N NUMBER(10, 0),
    N2 NUMBER(7, 3),
    V VARCHAR2(16),
    D DATE,
    T TIMESTAMP(6)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM