Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:30:57+00:00 2026-05-13T12:30:57+00:00

I have this big code where I want 3 things in my search: 1-

  • 0

I have this big code where I want 3 things in my search:
1- look for all the orders (delivered and not) that match the search:
2- look for all the pendent orders that match the search;
3- look for all the delivered orders that match the search;

create or replace
function search_order(search IN VARCHAR2, a_option NUMBER) RETURN types.ref_cursor
AS
    orders_cursor types.ref_cursor;

BEGIN
    if search is not null then
      if a_option = 0 then /*case 1*/
         OPEN orders_cursor FOR
          select value(f), value(p),i.qtd_if, i.prec_total_if , forn.nome_fornecedor
              from item_fornecimento i, produto p ,fornecimento f, fornecedor forn
              where f.id_fornecimento in (select f.id_fornecimento from fornecimento f where f.id_fornecedor in 
             (select f1.id_fornecedor from fornecedor  f1 where f1.nome_fornecedor LIKE '%'||search||'%')) 
              and f.id_fornecimento= i.id_fornecimento and i.id_prod= p.id_prod and
              f.id_fornecedor = forn.id_fornecedor
              order by forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod asc;  
        RETURN orders_cursor;


      ELSIF a_option = 1 then /*case 2*/
        OPEN orders_cursor FOR
        (...)
            where f.id_fornecimento in (select f.id_fornecimento from fornecimento f where f.id_fornecedor in 
            (select f1.id_fornecedor from fornecedor  f1 where f1.nome_fornecedor LIKE '%'||search||'%')and f.data_entrega is null) 
            (...) 
        RETURN orders_cursor;

        ELSE /* case 3*/
        OPEN orders_cursor FOR
          (...)
              where f.id_fornecimento in (select f.id_fornecimento from fornecimento f where f.id_fornecedor in 
              (select f1.id_fornecedor from fornecedor  f1 where f1.nome_fornecedor LIKE '%'||search||'%')and f.data_entrega is not null) 
              (...) 
          RETURN orders_cursor;    
      end if;
    end if;

END;

This works if my search is not null, but if it is I would like just to modify a little bit the inner select and turn it into something like this:
(select f1.id_fornecedor from fornecedor f1 where f1.nome_fornecedor LIKE '%'||search||'%')and f.data_entrega is not null) to –> (select f1.id_fornecedor from fornecedor f1)and f.data_entrega is not null)

So I have 3 conditions for search, and I would like to know if it’s possible to use something like case, decode or even another cursor with a parameter, to do this inner select with:
– LIKE if the search string is not null;
– without LIKE, if the string is null;

But I haven’t seen any examples of this and things can really go quite messy.
Could someone help a newbie with same code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:30:57+00:00Added an answer on May 13, 2026 at 12:30 pm

    I too would use dynamic SQL for this, but you can use a PLSQL case to control execution flow:

    BEGIN
    
      CASE
        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;
    
        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
                                 AND f.data_entrega IS NULL
              JOIN FORNECEDOR forn ON forn.id_fornecedor = f.id_fornecedor
          ORDER BY forn.nome_fornecedor,f.data_encomenda desc,p.nome_prod;
    
        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
                                 AND f.data_entrega IS NULL
              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;
      END CASE;
    
    END;
    

    It’s not complete, but you get the idea. I also converting your ANSI-89 JOIN syntax to ANSI-92, and got rid of the IN clauses in the process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this big function (1300+ lines of code) that takes data from the
I have a table that looks something like this: word big expensive smart fast
I have a script that loads big images into the body in this fashion
I have a var that contains a big list of words (millions) in this
I have this segment of code , a lot of things skipped for brevity
I have this big data-entry sort of page, a table kind of layout using
I have this big doubt. When ever i use base64Binary in an .xsd schema
so I have this HTML table with a bunch of big numbers in it
Here's the deal. I have a big class hierarchy and I have this one
Outline OK, I have Google'd this and already expecting a big fat NO!! But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.