has inline function got any catch?

Rajesh Kumar Yadav 20 Reputation points
2024-03-11T05:57:35.8533333+00:00

hi,

I heard many people saying "functions are denounced in tsql" is there any catch?

q1) need to know what are the cases where we should not use?

q2) should we avoid scaler functions also?

q3) should we avoid scaler functions if we have to call it only once?

yours sincerley

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,004 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,940 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,592 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,290 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 28,041 Reputation points
    2024-03-11T07:40:20.2666667+00:00

    Hi @Rajesh Kumar Yadav

    Scalar functions have some drawbacks: they are executed for each row of the result set, they prevent parallelism, and they can cause hidden conversions or errors.

    For instance, avoid using scalar functions in WHERE clauses as they can cause scans and affect index efficiency. Inline table-valued functions are preferable over scalar functions, as they can be inlined and optimized by the query optimizer.

    Sometimes, you may not need to use functions to achieve the same result. Computed columns can be used to define a column in a table calculated from other columns using a scalar expression, eliminating the need for scalar functions in SELECT statements or indexes. Alternatively, CROSS APPLY can be used to join a table with a table-valued function that takes a column from the table as a parameter, avoiding the need for scalar functions in JOIN conditions or expressions. Additionally, common table expressions (CTEs) can be used to define temporary result sets that can be reused within the same query, eliminating the need for table-valued functions in subqueries or joins.

    What are the differences between scalar and table-valued functions in T-SQL?

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Olaf Helper 45,106 Reputation points
    2024-03-11T06:17:19.38+00:00

    I heard many people saying "functions are denounced in tsql" is there any catch?

    Then ask those people "why".

    Functions with data access can be trouble maker in meaning of performanse, because the UDF will be execute per row.


  3. Bruce (SqlWork.com) 66,706 Reputation points
    2024-03-26T17:11:52.99+00:00

    a function is a data transformation that removes any data cardinality statistics. in a result column it is not much of an issue, but in join or where clause this will effect query optimization. this is true of user defined and built in functions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.