PL / SQL过程参数中的类似通用行为

时间:2021-01-01 19:52:23

Suppose I have some data types defined in PL/SQL:

假设我在PL / SQL中定义了一些数据类型:

TYPE foo_t IS RECORD (...);
TYPE foo_table_t IS TABLE OF foo_t INDEX BY BINARY_INTEGER;

TYPE bar_t IS RECORD (...);
TYPE bar_table_t IS TABLE OF bar_t INDEX BY BINARY_INTEGER;

Is it possible for me to write a procedure capable of accepting any data type derived from TABLE (for example, either a foo_table_t or a bar_table_t) as a parameter? The procedure has no need for knowledge of the table's row types. A trivial example:

我是否可以编写一个能够接受从TABLE派生的任何数据类型(例如,foo_table_t或bar_table_t)作为参数的过程?该过程不需要知道表的行类型。一个简单的例子:

PROCEDURE remove_last(some_table ...) IS
BEGIN
    some_table.DELETE(some_table.LAST);
END;

1 个解决方案

#1


Not directly. From the PL/SQL programmer's guide:

不是直接的。从PL / SQL程序员指南:

"The actual parameter and its corresponding formal parameter must have compatible datatypes."

“实际参数及其相应的形式参数必须具有兼容的数据类型。”

PL/SQL does an implicit conversion of actual parameter datatypes to formal parameter datatypes. So, you could pass a number value to a procedure that wants a string, and it would work because you can do an implicit conversion.

PL / SQL将实际参数数据类型隐式转换为形式参数数据类型。因此,您可以将数字值传递给需要字符串的过程,并且它可以工作,因为您可以执行隐式转换。

The best you could do would be to write overloaded procedures:

您可以做的最好的事情是编写重载过程:

PROCEDURE generic(foo IN OUT foo_t);

PROCEDURE泛型(foo IN OUT foo_t);

PROCEDURE generic(bar IN OUT bar_t);

PROCEDURE generic(bar IN OUT bar_t);

Then you can call generic with either record type. This loses attractiveness in proportion to the number of record types to handle :-D

然后,您可以使用任一记录类型调用泛型。这与处理的记录类型数量成比例地失去吸引力:-D

#1


Not directly. From the PL/SQL programmer's guide:

不是直接的。从PL / SQL程序员指南:

"The actual parameter and its corresponding formal parameter must have compatible datatypes."

“实际参数及其相应的形式参数必须具有兼容的数据类型。”

PL/SQL does an implicit conversion of actual parameter datatypes to formal parameter datatypes. So, you could pass a number value to a procedure that wants a string, and it would work because you can do an implicit conversion.

PL / SQL将实际参数数据类型隐式转换为形式参数数据类型。因此,您可以将数字值传递给需要字符串的过程,并且它可以工作,因为您可以执行隐式转换。

The best you could do would be to write overloaded procedures:

您可以做的最好的事情是编写重载过程:

PROCEDURE generic(foo IN OUT foo_t);

PROCEDURE泛型(foo IN OUT foo_t);

PROCEDURE generic(bar IN OUT bar_t);

PROCEDURE generic(bar IN OUT bar_t);

Then you can call generic with either record type. This loses attractiveness in proportion to the number of record types to handle :-D

然后,您可以使用任一记录类型调用泛型。这与处理的记录类型数量成比例地失去吸引力:-D