Function in WHERE clause

  • Can someone tell me if function "IntListCount" get processed for each row or
    does MSSQL calculate it once before using it?  
    select 
    *
    from
    PersonCategoryAssign
    inner join
    dbo.IntListToTable( @categoryIDs ) 
    on number = PersonCategoryID 
    where
    ACID = Person.ACID 
    group by 
    Person.ACID
    having
    count(*) = dbo.IntListCount( @categoryIDs )
     
    Thanks.
  • I am pretty sure it is processed for each row.  You could do something like this:

    declare @CatCount int
    select @CatCount = dbo.IntListCount(@categoryIDs)
    select

    *

    from

    PersonCategoryAssign

    inner join

    dbo.IntListToTable( @categoryIDs )

    on number = PersonCategoryID

    where

    ACID = Person.ACID

    group by

    Person.ACID

    having

    count(*) = @CatCount 

    Aunt Kathi Data Platform MVP
    Author of Expert T-SQL Window Functions
    Simple-Talk Editor

  • Thanks Kathi. 

    That is actually what I did after posting the message.  Also, because that snippet actually lives in a WHERE clause (I do an EXISTS on it), I also process IntListToTable() into a variable @table before I join it. 

    --Lenard

  • Also do not forget the order of precedence.

    The having clause is done after the join and the where clause are "executed", the dataset is available and just after the filter in the having clause is executed.



    Bye
    Gabor

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply