HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Can we put an equal sign (=) after aggregate functions in Transact-SQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
aftercanequalsqlputtransactfunctionssignaggregate

Problem

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?

Thanks

Solution

This is documented in UPDATE (Transact-SQL):


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.

db<>fiddle demo

Context

StackExchange Database Administrators Q#240995, answer score: 19

Revisions (0)

No revisions yet.