Werbung / Advertisements

Renvoie une valeur si aucune ligne n’est trouvée SQL

SQL
Werbung / Advertisements

Que se passe-t-il réellement lorsque vous exécutez une requête SQL qui ne renvoie aucune ligne ? Par exemple, pensez à une clause WHERE avec la condition 1=2, qui ne peut jamais être remplie et donc les lignes ne peuvent jamais être retournées.

D’abord, examinons quelques exemples qui devraient vous montrer ce à quoi vous pouvez vous attendre comme valeur de retour pour différents scénarios.

SELECT *
FROM DUAL
WHERE 1=1
-- Return: X

SELECT *
FROM DUAL
WHERE 1=2
-- Return: - (pas NULL!)

SELECT NVL(DUMMY,1)
FROM DUAL
WHERE 1=2
-- Return: - (pas NULL!)

SELECT COUNT(*)
FROM DUAL
WHERE 1=2
-- Return: 0

SELECT MAX(dummy)
FROM DUAL
WHERE 1=2
-- Return: NULL

NVL et NVL2

Werbung / Advertisements

Comme vous l’avez vu dans l’exemple ci-dessus, vous ne pouvez pas utiliser NVL et NVL2 car aucune ligne n’est renvoyée. L’utilisation de NVL et NVL2 n’a de sens que si les lignes sont retournées là où toutes les valeurs de colonne n’ont pas de contenu. Celle-ci peut ensuite être modifiée ou changée à l’aide de ces fonctions. Pour notre problème, cependant, NVL et NLV2 peuvent être exclus en toute sécurité ici. Vous trouverez plus d’informations sur l’utilisation correcte ainsi que de nombreux trucs et astuces dans le Article sur NVL et NVL2.

MAX/MIN in conjunction with NVL or COALESCE

One trick to get a row returned is to use the MAX or MIN function in conjunction with MAX or MIN. The MAX or MIN functions always return a row value, provided this is also NULL. With MAX or MIN, at least one line is always returned. Now you can change this value with the functions NVL, NVL2 or COALESCE. This is a quick process to respond to queries that would not return any rows.

Werbung / Advertisements

Below are two examples to help you understand how the concept works. As you can see, at least one value is now returned.

Werbung / Advertisements
SELECT NVL(MAX(dummy), 'Z') 
FROM DUAL
WHERE 1=2
-- Return: Z

SELECT COALESCE(MAX(dummy), 'Z') 
FROM DUAL
WHERE 1=2
-- Return: Z

CASE and EXISTS

Another possibility is the query with an IF clause. To do this, you use the EXISTS function and check whether there is even a line that would come back. The first step is to check whether a row exists in your query. If this is the case, you can output the lines. However, if there is no line, you can output an alternative via the ELSE branch and thus always get a line displayed.

Werbung / Advertisements
SELECT CASE
    WHEN EXISTS 
        (SELECT 1 
         FROM DUAL
         WHERE 1=2)
    THEN 
        (SELECT * 
         FROM DUAL
         WHERE 1=2)
    ELSE 'Z'
    END
FROM DUAL
-- Return: Z

CASE and COUNT

Another possibility is the CASE statement in conjunction with the COUNT function. As you have already seen above, COUNT gives you the number of rows returned and can therefore even give you the number 0 (if no row is returned). If COUNT is a positive number, the values of the query are returned. If the number is 0, on the other hand, the alternative path should be selected, which means that one line is returned as the return value.

SELECT CASE
    WHEN 
        (SELECT COUNT(*)
         FROM DUAL
         WHERE 1=2) <> 0
    THEN 
        (SELECT * 
         FROM DUAL
         WHERE 1=2)
    ELSE 'Z'
    END
FROM DUAL
-- Return: Z

You can find more tips and tricks for SQL Developer as well as for SQL and databases on the overview page.

Vous aimerez aussi...

Laisser un commentaire

Werbung / Advertisements