Difference between scalar, table-valued, and aggregate functions in SQL server?

前端 未结 4 1230
长发绾君心
长发绾君心 2020-12-23 13:09

What is the difference between scalar-valued, table-valued, and aggregate functions in SQL server? And does calling them from a query need a different method, or do we call

相关标签:
4条回答
  • 2020-12-23 14:05

    Aggregate and Scalar functions both return a single value but Scalar functions operate based on a single input value argument while Aggregate functions operate on a single input set of values (a collection or column name). Examples of Scalar functions are string functions, ISNULL, ISNUMERIC, for Aggregate functions examples are AVG, MAX and others you can find in Aggregate Functions section of Microsoft website.

    Table-Valued functions return a table regardless existence of any input argument. Execution of this functions is done by using them as a regular physical table e.g: SELECT * FROM fnGetMulEmployee()

    This following link is very useful to understand the difference: https://www.dotnettricks.com/learn/sqlserver/different-types-of-sql-server-functions

    0 讨论(0)
  • 2020-12-23 14:06

    Scalar function

    Returns a single value. It is just like writing functions in other programming languages using T-SQL syntax.

    Table Valued function

    Is a little different compared to the above. Returns a table value. Inside the body of this function you write a query that will return the exact table. For example:

    CREATE FUNCTION <function name>(parameter datatype)
    
    RETURN table
    
    AS
    
    RETURN
    
    (
    
    -- *write your query here* ---
    
    )
    

    Note that there is no BEGIN & END statements here.

    Aggregate Functions

    Includes built in functions that is used alongside GROUP clause. For example: SUM(),MAX(),MIN(),AVG(),COUNT() are aggregate functions.

    0 讨论(0)
  • 2020-12-23 14:09

    Scalar Functions

    Scalar functions (sometimes referred to as User-Defined Functions / UDFs) return a single value as a return value, not as a result set, and can be used in most places within a query or SET statement, except for the FROM clause (and maybe other places?). Also, scalar functions can be called via EXEC, just like Stored Procedures, though there are not many occasions to make use of this ability (for more details on this ability, please see my answer to the following question on DBA.StackExchange: Why scalar valued functions need execute permission rather than select?). These can be created in both T-SQL and SQLCLR.

    • T-SQL (UDF):

      • Prior to SQL Server 2019: these scalar functions are typically a performance issue because they generally run for every row returned (or scanned) and always prohibit parallel execution plans.
      • Starting in SQL Server 2019: certain T-SQL scalar UDFs can be inlined, that is, have their definitions placed directly into the query such that the query does not call the UDF (similar to how iTVFs work (see below)). There are restrictions that can prevent a UDF from being inlineable (if that wasn't a word before, it is now), and UDFs that can be inlined will not always be inlined due to several factors. This feature can be disabled at the database, query, and individual UDF levels. For more information on this really cool new feature, please see: Scalar UDF Inlining (be sure to review the "requirements" section).
    • SQLCLR (UDF): these scalar functions also typically run per each row returned or scanned, but there are two important benefits over T-SQL UDFs:

      • Starting in SQL Server 2012, return values can be constant-folded into the execution plan IF the UDF does not do any data access, and if it is marked IsDeterministic = true. In this case the function wouldn't run per each row.
      • SQLCLR scalar functions can work in parallel plans (
    0 讨论(0)
  • 2020-12-23 14:13

    A scalar function returns a single value. It might not even be related to tables in your database.

    A tabled-valued function returns your specified columns for rows in your table meeting your selection criteria.

    An aggregate-valued function returns a calculation across the rows of a table -- for example summing values.

    0 讨论(0)
提交回复
热议问题