I've got an Oracle procedure, that I want to be somehow generic. I would like to:
- pass a table name as a
varchar
parameter - use
EXECUTE IMMEDIATE
to dynamically select data - store the result in the
%ROWTYPE
variable of passed type
The third point seems to be a problem. I'm not sure if i can create a type dynamically inside the procedure body. It would be great to have something like this:
procedure CHANGE_GENERIC(tableName in VARCHAR2, someOldVal in integer,
someNewVal in integer) is
v_sql varchar2(200);
begin
v_sql := 'select * from ' || tableName || 'where ID = ' || someOldVal;
EXECUTE IMMEDIATE v_sql1 into **myDynamicRowThatIDontHave**;
-- some other code
end;
You probably can't do this (at least not usefully).
You could construct an entire anonymous PL/SQL block
In general, though, long before you start resorting to dynamic PL/SQL at runtime, you really want to take a step back and assess whether there isn't an easier solution to whatever problem you have. There are any number of frameworks, for example, that dynamically generate CRUD packages for each of your tables. That's using dynamic PL/SQL but it's only doing it once as part of a build rather than doing it every time you want to update data.
Have a look at DBMS_SQL Here is an example:
But as the other replies already mentioned, check carefully if you really need this "full" dynamic. You did not tell us what you like to do with retunrned variable values. Perhaps you don't need them explicitly. You can also create complex statements dynamically and run them.
See here a simple example (well, not that simple) I made some time ago. It updates several tables with nested tables of different object type without any variable usage.
I can recommend to use object types and object tables/views based on that types. Use can define root type and inherit all subtypes from it. Use can pass variable of root type into/from your generic proc and cast it to you specific type using treat inside dynamic SQL. You can pass SQL object types in/out to execute immediate as well.