I have a sql which returns 3 records
select emp_no from employees where dept_no='S3407'
results
1089
2092
1999
I need to use the above SQL in a function and function should return all three values.
I would like to use this function in another sql statement like the following
select * from emp_history where emp_no in ('1089','2092','1999');
In the above sql I would like to use the function instead of hard coded employee numbers.
What is the best way to accomplish this?
As the query is quite complex as what I have mentioned above is only a sample of the query as the full query cannot be pasted here. So I need to use a function to achieve this.
Any help is highly appreciable.
Regards
I would recommend you to consider Andomar suggestion by the following reasons:
1. views permit to provide enough level of encapsulation
2. CBO works nearly perfect with views so your query will work more or less predictably depending of number of records.
Once you replace the views by a function you will face the case when CBO can’t estimate the cardinality of function so you have either to submit full scan or implement kind of package in order to calculate the cardinality of the function.
So +`1 to Andomar
Surely you could use pipelined functions and rewrite the query as
but as I explained before it’s not the best way
You could find the perfect article (by my opinion) about pipelined functions in the link http://www.oracle-base.com/articles/misc/pipelined-table-functions.php