I am using Oracle 11g, and I have a lot of stored procedure code that use the same SELECT statement (but a complex one), just a different input in a where clause:
select ... where ancestor = X
That SELECT statement right now is copied / pasted on hundreds of these, and I need to refactor so they use the same SELECT statement construction. Because all these stored procs already exist, the refactoring must work nicely with the current code, which looks like this:
create or replace procedure Foo
begin
select quantity, amount from TBRawData, (select ... where ancestor = X) temp, where TBRAWData.StoreID = temp.StoreID;
end;
In a nutshell, I need a PL/SQL means of standardizing a SELECT, but a ref cursor, array types, collections, and such will not work because they are not treated like a table (thus cannot be inner joined to TBRAWData). Would a global temporary table work here, or something else?
Please help!
View is good answer(thanks to Gary), but there is another possibility.
You can create object type in database schema and table type referenced it:
then, declare function returning required results:
then, you can use function in select statements and joins:
Of course, you can place function into the package.
But not object and table type declarations.
This solution applicable if number of rows returned by function is not too big (depends on server hardware, but generally not more than 1000-2000 records).
It better than use of view because Oracle would maintain single compiled and cached plan for parametrized query and don’t rebuild it for each query as in case of solution with view.