title: How many times is function called
body: <p>Lets have a following query:</p>
  
  <pre><code>SELECT * FROM {tablename} WHERE ColumnId = dbo.GetId()
  </code></pre>
  
  <p>where dbo.GetId() is non-deterministic user defined function. The question is whether dbo.GetId() is called only once for entire query and its result is then applied or is it called for each row? I think it is called for every row, but I don't know of any way how to prove it.</p>
  
  <p>Also would following query be more efficient?</p>
  
  <pre><code>DECLARE @Id int
  SET @Id = dbo.GetId()
  SELECT * FROM {tablename} WHERE ColumnId = @Id
  </code></pre>