I have let's say 3 procedures that uses basically the same base query. With this if I have an update to the query, I have to copy it the same time to the 2 other procedures to ensure it is synced:
ie:
PROCEDURE get_something(name IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from multisource)
WHERE col1 = name;
...
}
PROCEDURE get_something2(address IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from multisource)
WHERE col2 = address;
...
}
PROCEDURE get_something3(var IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from multisource)
WHERE col3 = var;
...
}
I am thinking of creating a function that returns a table of the base query, and just call it from the 3 procedures. So if I have and update, I will just update the query under the function.
ie:
FUNCTION get_base()
RETURN table_t
PIPELINED IS
rec record_r;
BEGIN
SELECT * from multisource...
(maybe a pipeleined funcion or cursor here)
RETURN;
END get_base;
Then
PROCEDURE get_something(name IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from table(get_base))
WHERE col1 = name;
...
}
PROCEDURE get_something2(address IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from table(get_base))
WHERE col2 = address;
...
}
PROCEDURE get_something3(var IN VARCHAR2, result OUT VARCHAR2){
SELECT *
FROM (SELECT * from table(get_base))
WHERE col3 = var;
...
}
My question is, is this a good practice? Will I have performance issues using this? Are there alternative solutions?
Why not just tweak your logic and procedure to use just one select like this:
PROCEDURE get_something3(name IN VARCHAR2,
address IN VARCHAR2,
var IN VARCHAR2,
result OUT VARCHAR2){
....
SELECT *
FROM multisource
WHERE col1 = nvl(name,col1)
and col2 = nvl(address,col2)
and col3 = nvl(var,col3);
....
Then you call your procedure just passing the value you want to process and NULL in the other parameters.
Table functions are generally not a good way to promote the DRY (Don't Repeat Yourself) principle. A view would probably be a better approach.
Table functions are neat, and solve some interesting challenges. But they are a bit over-used and can cause a few problems:
Views are the standard way to prevent repeating SQL.
Unfortunately, most view implementations suck. But there are a few simple reasons why views get so ugly. If you plan ahead, you can avoid these common problems:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With