Just want to extend and oppose Oded's answer:
Is it possible to call a function with a dynamic name in SQL?
Not in pure SQL.
You can achieve this using dynamic SQL, but not without some risk, in particular that of SQL Injection.
Yes, it is possible without using dynamic SQL.
Preparing:
CREATE TABLE List_of_Functions(functionid INT);
INSERT INTO List_of_functions(functionid) VALUES(1),(2),(3),(4);
CREATE FUNCTION Function_1()
RETURNS VARCHAR(100)
AS
BEGIN
RETURN 'Return from Function_1';
END;
CREATE FUNCTION Function_2()
RETURNS VARCHAR(100)
AS
BEGIN
RETURN 'Return from Function_2';
END;
CREATE FUNCTION Function_3()
RETURNS VARCHAR(100)
AS
BEGIN
RETURN 'Return from Function_3';
END;
Core function:
CREATE FUNCTION Function_dispatcher(@name SYSNAME)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @r VARCHAR(100);
IF OBJECT_ID(@name, N'FN') IS NULL --handling non-existing function
RETURN NULL;
EXEC @r = @name;
RETURN @r;
END;
And final call:
SELECT *, dbo.Function_dispatcher(s.n) AS result
FROM List_of_Functions lf
OUTER APPLY(SELECT CONCAT('dbo.Function_', lf.functionid)) s(n);
DBFiddle Demo
Output:
┌────────────┬────────────────┬────────────────────────┐
│ functionid │ n │ result │
├────────────┼────────────────┼────────────────────────┤
│ 1 │ dbo.Function_1 │ Return from Function_1 │
│ 2 │ dbo.Function_2 │ Return from Function_2 │
│ 3 │ dbo.Function_3 │ Return from Function_3 │
│ 4 │ dbo.Function_4 │ null │
└────────────┴────────────────┴────────────────────────┘
Using this approach you can avoid of changing main function every time new function will appear(CASE WHEN ...) like in KM.'s answer