Reputation: 1
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.
Problem is that columns of this table have diffrent data types. Can I do something like p_value exemplare.p_column%TYPE
? Or do I have to set it to VARCHAR2 and then (somehow) use converting to correct data type?
Upvotes: 0
Views: 293
Reputation: 146209
Can I do something like 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.
This approach reminds me of the getter and setter paradigm which is still prevalent in object-oriented programming. This is not an approach which fits SQL. To edit three table columns you will make three procedural calls which will generate and execute three dynamic UPDATE statements. 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.
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.
Upvotes: 1
Reputation: 1536
you can use {table}%ROWTYPE
as an input.this is a good example
Upvotes: 0