Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7176297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:28:41+00:00 2026-05-28T16:28:41+00:00

In the following t-sql statement, how many times will the dbo.FUNC function get called?

  • 0

In the following t-sql statement, how many times will the dbo.FUNC function get called?

SELECT
    column1,
    column2,
    dbo.FUNC(column3) AS column3
FROM table1
WHERE dbo.FUNC(column3) >= 5
ORDER BY dbo.FUNC(column3) DESC

Will it called multiple separate times per row, or does the optimizer recognize that it is being used multiple times in a single statement, and only call it once?

How can I test this? I can’t insert into a table inside of a function, so incrementing a counter wont work…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:28:42+00:00Added an answer on May 28, 2026 at 4:28 pm

    This isn’t guaranteed.

    You would need to check the execution plan to find out. Some examples.

    CREATE FUNCTION dbo.FUNC1(@p1 int)
    RETURNS int
    AS
    BEGIN
        RETURN @p1 + 1
    END
    
    GO
    
    CREATE FUNCTION dbo.FUNC2(@p1 int)
    RETURNS int
    WITH SCHEMABINDING
    AS
    BEGIN
        RETURN @p1 + 1
    END
    
    GO
    SELECT 
           OBJECTPROPERTYEX(OBJECT_ID('dbo.FUNC1'), 'IsDeterministic'),
           OBJECTPROPERTYEX(OBJECT_ID('dbo.FUNC2'), 'IsDeterministic') 
    GO
    

    FUNC2 is created WITH SCHEMABINDING and is treated as deterministic. FUNC1 isn’t.

    SELECT
        dbo.FUNC1(number) AS FUNC1,
        dbo.FUNC2(number) AS FUNC2
    FROM master..spt_values
    WHERE dbo.FUNC1(number) >= 5 AND dbo.FUNC2(number) >= 5
    ORDER BY dbo.FUNC1(number), dbo.FUNC2(number)
    

    Gives Plan

    PLAN1

      |--Sort(ORDER BY:([Expr1003] ASC, [Expr1004] ASC))
           |--Compute Scalar(DEFINE:([Expr1003]=[test].[dbo].[FUNC1]([master].[dbo].[spt_values].[number])))
                |--Filter(WHERE:([test].[dbo].[FUNC1]([master].[dbo].[spt_values].[number])>=(5) AND [Expr1004]>=(5)))
                     |--Compute Scalar(DEFINE:([Expr1004]=[test].[dbo].[FUNC2]([master].[dbo].[spt_values].[number])))
                          |--Index Scan(OBJECT:([master].[dbo].[spt_values].[ix2_spt_values_nu_nc]))
    

    FUNC1 is evaluated twice (once in the filter and once in a compute scalar outputting a calculated column used for both the projection and the ordering), FUNC2 is only evaluated once.

    Rewriting as

    SELECT
        FUNC1,
        FUNC2
    FROM master..spt_values
    CROSS APPLY (SELECT dbo.FUNC1(number), dbo.FUNC2(number)) C(FUNC1, FUNC2)
    WHERE FUNC1 >= 5 AND FUNC2 >= 5
    ORDER BY FUNC1, FUNC2
    

    Changes the plan slightly and both are only evaluated once

    Plan 2

      |--Sort(ORDER BY:([Expr1003] ASC, [Expr1004] ASC))
           |--Filter(WHERE:([Expr1003]>=(5)))
                |--Compute Scalar(DEFINE:([Expr1003]=[test].[dbo].[FUNC1]([master].[dbo].[spt_values].[number])))
                     |--Filter(WHERE:([Expr1004]>=(5)))
                          |--Compute Scalar(DEFINE:([Expr1004]=[test].[dbo].[FUNC2]([master].[dbo].[spt_values].[number])))
                               |--Index Scan(OBJECT:([master].[dbo].[spt_values].[ix2_spt_values_nu_nc]))
    

    Now making a slight alteration to the query

    SELECT
        FUNC1 + 10,
        FUNC2 + 10
    FROM master..spt_values
    CROSS APPLY (SELECT dbo.FUNC1(number), dbo.FUNC2(number)) C(FUNC1, FUNC2)
    WHERE FUNC1 >= 5 AND FUNC2 >= 5
    ORDER BY FUNC1, FUNC2
    

    Gives the opposite of the original result in that FUNC2 is evaluated twice but FUNC1 only once.

    Plan 3

      |--Compute Scalar(DEFINE:([Expr1005]=[Expr1003]+(10)))
           |--Sort(ORDER BY:([Expr1003] ASC, [Expr1004] ASC))
                |--Filter(WHERE:([Expr1003]>=(5)))
                     |--Compute Scalar(DEFINE:([Expr1003]=[test].[dbo].[FUNC1]([master].[dbo].[spt_values].[number])))
                          |--Filter(WHERE:([Expr1004]>=(5)))
                               |--Compute Scalar(DEFINE:([Expr1004]=[test].[dbo].[FUNC2]([master].[dbo].[spt_values].[number]), [Expr1006]=[test].[dbo].[FUNC2]([master].[dbo].[spt_values].[number])+(10)))
                                    |--Index Scan(OBJECT:([master].[dbo].[spt_values].[ix2_spt_values_nu_nc]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following SQL Statement. I need to select the latest record for
How would the following sql statement translate to a linq query? select ID, Price,
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as
I have to following SQL Statement that I want to conver to LINQ Select
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET
I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l
I have the following SQL statement which returns a single record as expected: select
See the following SQL statement: SELECT datediff(d, MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS
I have the following SQL statement: SELECT article_id FROM publications WHERE category_id IN (SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.