Using the same function twice in a query (SQL Server)

In SQL Server 2005, when I write a query like

SELECT m.*, a.price p1, b.price p2
FROM mytable m
LEFT JOIN products_table_1 a
ON my_hash_function(m.name) = a.hash
LEFT JOIN products_table_2 b
ON my_hash_function(m.name) = b.hash

is my_hash_function(m.name)calculated twice or only once? If twice, how can I use a variable to avoid this?

+5
source share
2 answers

, . , . , WITH SCHEMABINDING, , , . - , . !

, , momobo .

+2
select  mm.*, a.price p1, b.price p2 from   
    (SELECT m.*, my_hash_function(m.name) as name
    FROM mytable m) mm
    LEFT JOIN products_table_1 a
    ON mm.name = a.hash
    LEFT JOIN products_table_2 b
    ON mm.name = b.hash
+7

All Articles