简体   繁体   English

是否可以遍历 Oracle 中的 rowtype 字段?

[英]Is it possible to traverse rowtype fields in Oracle?

Say i have something like this:说我有这样的事情:

somerecord SOMETABLE%ROWTYPE;

Is it possible to access the fields of somerecord with out knowing the fields names?是否可以在不知道字段名称的情况下访问 somerecord 的字段? Something like somerecord[i] such that the order of fields would be the same as the column order in the table?像 somerecord[i] 这样的字段顺序与表中的列顺序相同?

I have seen a few examples using dynamic sql but i was wondering if there is a cleaner way of doing this.我已经看到了一些使用动态 sql 的示例,但我想知道是否有更清洁的方法来执行此操作。

What i am trying to do is generate/get the DML (insert query) for a specific row in my table but i havent been able to find anything on this.我想要做的是为我的表中的特定行生成/获取 DML(插入查询),但我无法找到任何关于此的内容。

If there is another way of doing this i'd be happy to use but would also be very curious in knowing how to do the former part of this question - it's more versatile.如果有另一种方法可以做到这一点,我很乐意使用,但也很想知道如何做这个问题的前一部分——它更通用。

Thanks谢谢

This doesn't exactly answer the question you asked, but might get you the result you want...这并不能完全回答您提出的问题,但可能会得到您想要的结果......

You can query the USER_TAB_COLUMNS view (or the other similar *_TAB_COLUMN views) to get information like the column name (COLUMN_NAME), position (COLUMN_ID), and data type (DATA_TYPE) on the columns in a table (or a view) that you might use to generate DML.您可以查询 USER_TAB_COLUMNS 视图(或其他类似的 *_TAB_COLUMN 视图)以获取您在表(或视图)中的列的列名 (COLUMN_NAME)、position (COLUMN_ID) 和数据类型 (DATA_TYPE) 等信息。可用于生成 DML。

You would still need to use dynamic SQL to execute the generated DML (or at least generate static SQL separately).您仍然需要使用动态 SQL 来执行生成的 DML(或至少单独生成 static SQL)。

However, this approach won't work for identifying the columns in an arbitrary query (unless you create a view of it).但是,这种方法不适用于识别任意查询中的列(除非您创建它的视图)。 If you need that, you might need to resort to DBMS_SQL (or other tools).如果需要,您可能需要使用 DBMS_SQL(或其他工具)。

Hope this helps.希望这可以帮助。

As far as I know there is no clean way of referencing record fields by their index.据我所知,没有通过索引引用记录字段的干净方法。

However, if you have a lot of different kinds of updates of the same table each with its own column set to update, you might want to avoid dynamic sql and look in the direction of statically populating your record with values, and then issuing update someTable set row = someTableRecord where someTable.id = someTableRecord.id;但是,如果您对同一个表有很多不同类型的更新,每个更新都有自己的列设置更新,您可能希望避免使用动态 sql 并查看用值静态填充记录的方向,然后发出update someTable set row = someTableRecord where someTable.id = someTableRecord.id; . .

This approach has it's own drawbacks, like, issuing an update to every, even unchanged column, and thus creating additional redo log data, but I believe it should be considered.这种方法有其自身的缺点,例如,对每个甚至未更改的列进行更新,从而创建额外的重做日志数据,但我认为应该考虑它。

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

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