3

EDIT: Based on answers below I added "dbo." and comments for clarification.

Question: How do I pass a SQL user-defined function (UDF) as an argument for another UDF? Note that the function returns a table.

select dbo.fn_Scalar()                      -- Returns 'sa'.
select * from dbo.fn_Tabler('sa')           -- Returns a table (not scalar).
select dbo.fn_ScalarArg(dbo.fn_Scalar())    -- Returns 'sa'
select * from dbo.fn_Tabler(dbo.fn_Scalar())    -- ERROR: Incorrect syntax near '.'.

(Currently using SQL Server 2008 R2)


EDIT: To ease debugging, here is the sample code to create the above functions:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Scalar]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_Scalar
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Tabler]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_Tabler
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_ScalarArg]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.fn_ScalarArg
GO

create function fn_Scalar()
returns char(200) as
begin
    declare @user char(200)
    select  @user = 'sa'
    return  @user
end
GO

create function fn_Tabler(@User char(200))
returns table as
    return select @User [User]
GO

create function fn_ScalarArg(@User char(200))
returns char(200) as
begin
    declare @u2 char(200)
    select  @u2 = @User
    return  @u2
end
go
3
  • Does select * from fn_Tabler(select fn_Scalar()) work? Commented Aug 19, 2011 at 7:48
  • @cdhowie: select * from dbo.fn_Tabler(select dbo.fn_Scalar()) yields "Incorrect syntax near the keyword 'select'" and "Incorrect syntax near ')'" Commented Aug 19, 2011 at 8:06
  • Badly worded question. You're not trying to pass a function to a function, you're trying to pass the return value of a function to a function. Commented Apr 10, 2019 at 16:54

2 Answers 2

1

Its working for me

    select [dbo].[GetOverAllWinLossDetailPointByDay] 
               (1,0,1,dbo.GetOverAllWinLossDetailPointByDay(1,0,1,1))

I think you forgot to add dbo before function name.

Sign up to request clarification or add additional context in comments.

1 Comment

select * from dbo.fn_Tabler(dbo.fn_Scalar()) yields: Incorrect syntax near '.'.
0

BOL says: "Scalar-valued functions must be invoked by using at least the two-part name of the function."

select * from dbo.fn_Tabler(dbo.fn_Scalar())    

EDIT: I was wrong, there is the table-valued function... Documentation says that only constants and @local_variables can be passed to table-valued functions (see here)

3 Comments

See my comments to Pranay Rana: fn_Tabler is not a scalar-valued function. It returns a table.
yes, I have tested on my server and retrieved the same error. See the edit for my answer.
That documentation comment is pretty well hidden in the huge document :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.