Спасибо за ваши ответы, я нашел их очень интересными, и мой код стал более понятным и читабельным, я думаю. Тем не менее, OMG Ponies, кажется, подходит лучше, чем другие. Но я обнаружил, что мой код все еще большой, возможно, я упрямый!
Вот конечный результат, кому это может заинтересовать
create or replace
function search_order(search IN VARCHAR2, a_option NUMBER) RETURN types.ref_cursor
AS
orders_cursor types.ref_cursor;
BEGIN
CASE
/*all the orders that match, no matter if they're delivered or not*/
WHEN search IS NOT NULL AND a_option = 0 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
AND forn.nome_fornecedor LIKE '%'||search||'%'
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
/*all the orders, no matter if they're delivered or not*/
WHEN search IS NULL AND a_option = 0 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
/*########################## OPTION 1 #################################*/
/*all the matched and pendent orders*/
WHEN search IS NOT NULL AND a_option = 1 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
AND forn.nome_fornecedor LIKE '%'||search||'%'AND f.data_entrega IS NULL
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
/*all the pendent orders*/
WHEN search IS NULL AND a_option = 1 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
AND f.data_entrega IS NULL
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
/*########################## OPTION 2 #################################*/
/*all the matched and delivered orders*/
WHEN search IS NOT NULL AND a_option = 2 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
AND forn.nome_fornecedor LIKE '%'||search||'%'AND f.data_entrega IS NOT NULL
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
/*all the delivered orders*/
WHEN search IS NULL AND a_option = 2 THEN
OPEN orders_cursor FOR
SELECT VALUE(f),
VALUE(p),
i.qtd_if,
i.prec_total_if,
forn.nome_fornecedor
FROM ITEM_FORNECIMENTO i
JOIN PRODUTO p ON p.id_prod = i.id_prod
JOIN FORNECIMENTO f ON f.id_fornecimento = i.id_fornecimento
JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
AND f.data_entrega IS NOT NULL
ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
RETURN orders_cursor;
end case;
END;