简体   繁体   English

将变量用作列%TYPE(ORACLE)

[英]Using variable as column%TYPE (ORACLE)

I need to make a procedure for editing a table 'exemplare' edit_exemplare(p_id_exemplare IN NUMBER, p_column VARCHAR2, p_value ???) which updates the value in column of row with this ID . 我需要制定一个程序来编辑表“示例”表edit_exemplare(p_id_exemplare IN NUMBER, p_column VARCHAR2, p_value ???) ,该表将使用此ID更新行列中的

Problem is that columns of this table have diffrent data types. 问题在于该表的列具有不同的数据类型。 Can I do something like p_value exemplare.p_column%TYPE ? 我可以做类似p_value exemplare.p_column%TYPE事情吗? Or do I have to set it to VARCHAR2 and then (somehow) use converting to correct data type? 还是我必须将其设置为VARCHAR2,然后(以某种方式)使用转换来纠正数据类型?

Can I do something like p_value exemplare.p_column%TYPE? 我可以做类似p_value exemplare.p_column%TYPE的事情吗?

No. The signature of a procedure must be static. 否。过程的签名必须是静态的。 What you could do is overload the procedure in a package: 您可以做的是在程序包中重载该过程:

procedure edit_exemplare(p_id_exemplare IN NUMBER, p_column VARCHAR2, p_value VARCHAR2);
procedure edit_exemplare(p_id_exemplare IN NUMBER, p_column VARCHAR2, p_value DATE);
procedure edit_exemplare(p_id_exemplare IN NUMBER, p_column VARCHAR2, p_value NUMBER);

However, you're still need dynamic SQL to interpret the metadata of p_column so your code will remain clunky. 但是,您仍然需要动态SQL来解释p_column的元数据,因此您的代码将很笨拙。


This approach reminds me of the getter and setter paradigm which is still prevalent in object-oriented programming. 这种方法使我想起了getter和setter范例,该范例在面向对象的编程中仍然很普遍。 This is not an approach which fits SQL. 这不是适合SQL的方法。 To edit three table columns you will make three procedural calls which will generate and execute three dynamic UPDATE statements. 要编辑三个表列,您将进行三个过程调用,这些调用将生成并执行三个动态UPDATE语句。 This does not scale well, and it is the sort of thing which causes OO developers to assert that databases are slow, when it fact the problem is at the calling end. 这不能很好地扩展,这是一种导致OO开发人员断言数据库运行缓慢的事实,而实际上问题出在调用端。

There are various ways to solve this, and which is the correct one will depend on the precise details of what you're trying to do. 有多种解决方法,正确的方法取决于您要执行的操作的确切细节。 The key point is: a single transaction should execute *no more than one** update statement per record. 关键点是:单个事务应每条记录执行*不超过一个**更新语句。 A properly set-based operation which updates multiple records in one statement is even better. 基于正确设置的操作(在一条语句中更新多个记录)甚至更好。

you can use {table}%ROWTYPE as an input. 您可以使用{table}%ROWTYPE作为输入。 this is a good example 是一个很好的例子

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

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