Call dynamic function name in SQL

sql, sql-function, sql-server, t-sql

Solution

You will need to build (either type it in, or build it dynamically based on your table) a SQL statement like:

SELECT
    functionid
        ,CASE  functionid
            WHEN 1 THEN dbo.Function_1()
            WHEN 2 THEN dbo.Function_2()
            WHEN 3 THEN dbo.Function_3()
         END AS results
    FROM List_of_Functions

Instead of building all those functions, wouldn't it be better to build one function, and pass in a value that the function can use to differentiate the processing? like:

SELECT
    functionid
        ,dbo.Function(functionid) AS results
    FROM List_of_Functions_Parameters

Problem

Is it possible to call a function with a dynamic name in SQL? For example: ``` SELECT functionid, (SELECT results FROM dbo.Function_*functionid*) AS results FROM List_of_Functions ``` This would call a different function for every row in the table List_of_Functions. Or am I going about this all wrong?

Original source