Aliases in SQL Server

Are these two statements equivalent?

UPDATE Table1 SET Field1=( 
 SELECT Field2 FROM Table2 
 WHERE Table1.ID=Table2.ID
) 
FROM Table1 
WHERE Field1 is null



UPDATE t SET Field1=( 
 SELECT Field2 FROM Table2 
 WHERE t.ID=Table2.ID
) 
FROM Table1 t
WHERE Field1 is null

I am trying to reduce the number of aliases. I feel that adding an alias to the statement only adds another table name to track mentally.

My concern is that in Example 1, since I am not using an alias, it will update the entire table1 instead of filtering on WHERE. Field1 is null.

What is the thumb rule when smoothing is required?

+3
source share
3 answers

Yes, they are equivalent because aliasing will never change the effect of a statement, only its readability or the resolution of ambiguity.

But I would do this:

UPDATE Table1 
SET Field1 = Table2.Field2 
FROM Table1 
INNER JOIN Table2 ON Table1.ID=Table2.ID
WHERE Table1.Field1 is null

or

UPDATE t1
SET Field1 = t2.Field2 
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ID=t2.ID
WHERE t1.Field1 is null

. (1-3 ) , , , .

+7

, .

aliasing . , , , , .

, , ( ).

+5

.

, , .

+3

Source: https://habr.com/ru/post/1718052/


All Articles