sql-server add tag
igelr (imported from SE)
I have encountered a script like this:
    
    set @sum = sum = (case when somecol1 is null then 
        DATEDIFF(d,[somecol2],somecol3) else 0 end)

I can't understand the meaning of the equal sign (=) after the second keyword sum. When I run the query it is not showing any errors both with the equal sign and without. 

I want to know the purpose of putting an equal sign after the keyword `sum`. Is that a mistake or not?
Top Answer
Paul White (imported from SE)
This is documented in [UPDATE (Transact-SQL)][1]:

> SET @variable = column = expression sets the variable to the same value as the column. This differs from SET @variable = column, column = expression, which sets the variable to the pre-update value of the column.

In your code example, `sum` is the (unwise) name of a column, not an aggregate.

Demo:

<>https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=8aa0603ba344816390cb287b842385de


  [1]: https://docs.microsoft.com/en-us/sql/t-sql/queries/update-transact-sql
  [2]: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=8aa0603ba344816390cb287b842385de

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.