Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:59:06+00:00 2026-06-03T04:59:06+00:00

I have a UDF that takes a table as a parameter (a 2 column

  • 0

I have a UDF that takes a table as a parameter (a 2 column table) and outputs a string, much like this article. I have a table that I want to apply the UDF over multiple columns like this:

Date   Unit   Line   Revenue   Orders
4/1/12 D      R      20.00     3
4/2/12 D      R      25.00     4
4/1/12 H      W      33.00     1
4/2/12 H      W      35.00     3

I want to call my UDF on every row of this table that has the most current date and pass the UDF a table with the columns Date and Revenue for each distinct Unit and Line. And I also want to call the UDF and pass Date and Orders for each distinct Unit and Line. This is a report and the UDF will always get passed the Date column and another column that I want to apply some calculation to and store and report. So I have looked at a lot of things including CROSS APPLY and I want to do something like this:

SELECT        T.unit
              , T.line
              , dbo.fn_myUDF((SELECT T2.Date, T2.Revenue FROM #Table T2)) as UDFRevenueResult
              , dbo.fn_myUDF((SELECT T2.Date, T2.Orders FROM #Table T2)) as UDFOrderResult
FROM          #Table T
WHERE         T.Date = @ReportDate

This gives me the error “Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.” I looked at CROSS APPLY but that seems only apply for when you are returning a table variable. So I could probably answer my own question by using a cursor and a loop maybe and constructing each table I want to pass to the UDF in each loop iteration but that just doesn’t seem right. Can anyone give me an idea how to do what I want in SQL Server 2008?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:59:07+00:00Added an answer on June 3, 2026 at 4:59 am

    Subqueries can’t be used like that to return table variables in the SELECT portion of the query. They can only return a single value. As in:

    set @myParam = (select myvalue from table where primarykey = 1)
    

    or

    set @myParam = (select top 1 myvalue from table)
    

    But, considering that your subqueries do not have any relation to the rows being selected, you can do something like this.

    declare @t1 table (DateTime c1, float c2)
    insert @t1
    SELECT Date, Revenue 
    FROM #Table 
    
    declare @t2 table (DateTime c1, int c2) -- not sure on data type of 'Orders'
    insert @t2
    SELECT Date, Orders FROM #Table
    
    
    SELECT T.unit               
         , T.line               
         , dbo.fn_myUDF(@t1) as UDFRevenueResult               
         , dbo.fn_myUDF(@t2) as UDFOrderResult 
    FROM #Table T 
    WHERE T.Date = @ReportDate 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table-valued, inline UDF. I want to filter the results of that
I have a UDF in sql server. I want to call this function at
Ok we have a simple udf that takes a XML integer list and returns
I have written a table-valued UDF that starts by a CTE to return a
I have a UDF that queries data out of a table. The table, however,
I have an UDF that selects top 6 objects from a table (with a
I have a SQL Table UDF That gets a Standard Deviation from a 20
I have two table functions that return a single column each. One function is
I have a UDF which returns table variable like -- -- RETURNS @ElementTable TABLE
I have a UDF that splits a character string delimited by a space. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.