Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update with sub select - How to handle NULL values?

I'm trying an update with a conditional sub-select which could return null...

UPDATE 
aTable SET 
aColumn = 
(   
    SELECT TOP 1    
        CASE 
            WHEN bTable.someColumn = 1 THEN someValue1 
            WHEN bTable.someColumn = 2 THEN someValue2 
            ELSE someValue3
        END  
    FROM         
        bTable
    WHERE
        bTable = @someCriteria
    ORDER BY
        someSortColumn
) WHERE 
aTable.id = @someId;

If the "bTable = @someCriteria" clause causes no results to be returned from the SELECT, it attempts to insert a NULL into "aColumn", which in this case is a NOT NULL column.

Question

How do I get it to simply leave "aColumn" alone in this circumstance?

Many thanks.

like image 735
ETFairfax Avatar asked Jun 17 '11 10:06

ETFairfax


People also ask

How do you UPDATE a table with null value?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

What happens when subquery returns NULL?

For a scalar subquery: If the subquery returns no rows, the result of the scalar subquery is NULL . If the subquery returns more than one row, it is an error. If the subquery returns one row, the result is the value of the query's (only) column for that row.


3 Answers

...
aColumn = 

    ISNULL(
        (   
            SELECT TOP 1    
                CASE 
                    WHEN bTable.someColumn = 1 THEN someValue1 
                    WHEN bTable.someColumn = 2 THEN someValue2 
                    ELSE someValue3
                END  
            FROM         
                bTable
            WHERE
                bTable = @someCriteria
            ORDER BY
                someSortColumn
        ), aColumn)
...
like image 146
gbn Avatar answered Oct 28 '22 18:10

gbn


UPDATE A SET 
aColumn = B.Value 
FROM aTable AS A
  CROSS JOIN  
            (   
                SELECT TOP 1    
                    CASE 
                        WHEN bTable.someColumn = 1 THEN someValue1 
                        WHEN bTable.someColumn = 2 THEN someValue2 
                        ELSE someValue3
                    END  
                FROM         
                    bTable
                WHERE
                    bTable = @someCriteria
                ORDER BY
                    someSortColumn
            ) AS B(Value)
WHERE            
  A.id = @someId;

Difference between my answer and answer by gbn is that here column aColumn is not modified. I think that the isnull(..., aColumn) actually updates the value so if you have an update trigger it will fire.

like image 28
Mikael Eriksson Avatar answered Oct 28 '22 18:10

Mikael Eriksson


UPDATE aTable
SET aColumn = s.Value
FROM (   
    SELECT TOP 1    
        CASE 
            WHEN bTable.someColumn = 1 THEN someValue1 
            WHEN bTable.someColumn = 2 THEN someValue2 
            ELSE someValue3
        END AS Value
    FROM         
        bTable
    WHERE
        bTable = @someCriteria
    ORDER BY
        someSortColumn
) s
WHERE aTable.id = @someId
  AND s.Value IS NOT NULL;
like image 33
Andriy M Avatar answered Oct 28 '22 18:10

Andriy M