title: SQL Server: function as argument for another function
body: <p><strong>EDIT</strong>: Based on answers below I added "dbo." and comments for clarification.</p>
  
  <p><strong>Question</strong>: How do I pass a SQL user-defined function (UDF) as an argument for another UDF? Note that the function returns a table.</p>
  
  <pre><code>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 '.'.
  </code></pre>
  
  <p>(Currently using SQL Server 2008 R2)</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: To ease debugging, here is the sample code to create the above functions:</p>
  
  <pre><code>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
  </code></pre>