How to return at least one record in an Oracle query

Hello,

In an Oracle Database 11gR2, I have the following table:

BLOQUEIO_MATERIAL
  ( ID_UNNEGOCIO              VARCHAR2 (  4 BYTE),
    ID_MATERIAL               VARCHAR2 ( 20 BYTE),
    ID_FAMILIA                VARCHAR2 (  2 BYTE),
    DESCRIPTION               VARCHAR2 (200 BYTE),
    BLOQUEADO_BUSCA           VARCHAR2 (  1 BYTE),
    BLOQUEADO_CALCULO_DEMANDA VARCHAR2 (  1 BYTE) )

If there is NOT a record with values¿¿:

   ID_UNNEGOCIO = '0067' AND ID_MATERIAL = '627275'

The query:

SELECT id_material,
       id_unnegocio,
       bloqueado_busca,
       bloqueado_calculo_demanda
  FROM bloqueio_material
 WHERE     id_unnegocio = '0067'
       AND id_material  = '627275';

Will return no records.

What is the correct way to write the query so that it will return a record as:

id_material   id_unnegocio   bloqueado_busca   bloqueado_calculo_demanda
-----------   ------------   ---------------   -------------------------
627275        0067           N                 N

Grateful.

Elective Junior Zanotto.
ElectivoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
create a dummy view to hold your values then do an OUTER JOIN to that view


SELECT val_material id_material,
       val_unnegocio id_unnegocio,
       NVL(bloqueado_busca, 'N') bloqueado_busca,
       NVL(bloqueado_calculo_demanda, 'N') bloqueado_calculo_demanda
  FROM (SELECT '627275' val_material, '0067' val_unnegocio FROM DUAL)
       LEFT JOIN bloqueio_material ON id_unnegocio = '0067' AND id_material = '627275';
0
ElectivoAuthor Commented:
Excuses to add the email to text ... was force of habit
0
Mark GeerlingsDatabase AdministratorCommented:
Or, use your query plus "union all" and a "not exists" sub-query like this:

SELECT id_material,
       id_unnegocio,
       bloqueado_busca,
       bloqueado_calculo_demanda
  FROM bloqueio_material
 WHERE     id_unnegocio = '0067'
       AND id_material  = '627275'
union all
SELECT '627275', '0067','N','N'
  FROM DUAL
WHERE not exists (select 1 from FROM bloqueio_material
 WHERE     id_unnegocio = '0067'
       AND id_material  = '627275');
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdstuberCommented:
note the union with subquery will require more io.  Even cached on the second hit, it's still likely to be less efficient.

Do try both though. There are likely conditions where the cost of an outer join would be more expensive.
0
ElectivoAuthor Commented:
Found a very good idea.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.