title: Avoid Repeated Function call in WHERE Clause
body: <p>I know this is bad, I just don't know how to re-write it... any help appreciated!</p>
  
  <pre><code>DECLARE @i   INT, 
      @var VARCHAR(max) 
  
  SET @i = 0 
  
  WHILE 1 = 1 
  BEGIN 
    INSERT INTO mrs.dbo.nav_worldcheck_results 
                ("counter", 
                 "entity no", 
                 "entity first name", 
                 "entity last name", 
                 "entity full name", 
                 "worldcheck uid", 
                 "worldcheck first name", 
                 "worldcheck last name", 
                 "worldcheck full name", 
                 "percentage match") 
    SELECT TOP(1) [Counter] = @i, 
                  EN.[entity no_], 
                  EN.[name 2], 
                  EN.[name], 
                  EN.[name 2] + ' ' + EN.[name]          AS EntityName, 
                  SM.uid                                 AS WorldCheckID, 
                  SM.[first name], 
                  SM.[last name], 
                  SM.[first name] + ' ' + SM.[last name] AS WorldCheckName, 
                  dbo.Fn_calculatejarowinkler(EN.[name 2] + ' ' + EN.[name], 
                  SM.[first name] + ' ' + SM.[last name]) 
    FROM   [NAV_LIVE].dbo.[entitytable$entity] AS EN 
           CROSS JOIN [NAV_LIVE].dbo.[worldcheck master] AS SM 
    WHERE  ( EN.inactive = 0 ) 
           AND dbo.Fn_calculatejarowinkler(EN.[name 2] + ' ' + EN.[name], 
                   SM.[first name] + ' ' + SM.[last name]) &gt;= .75 
           AND NOT EXISTS(SELECT * 
                          FROM   mrs.dbo.nav_worldcheck_results AS WCR 
                          WHERE  EN.[entity no_] = WCR.[entity no] 
                                 AND WCR.[worldcheck uid] = SM.uid) 
  
    SET @i = @i + 1 
  END 
  </code></pre>
  
  <p>I want to avoid the repeated function call in the where statement and I've tried assigning all as variables and then processing but I fall into the trap of you can't mix sets with data retrieval and my poor little brain is in Friday mode! :o/</p>