Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pass Parameters to Table Function

Author  Topic 

petersrj
Starting Member

20 Posts

Posted - 2011-06-06 : 11:39:12
I have written a custom function which takes in 5 parameters and returns a table after manipulating those input parameters. The function works correctly when run as follows:

Select * from dbo.MyNewFunc(1, 2, 3, 4, 5)

What I would like to do is to have the five parameters come from a select statement. Something like:

Select * from dbo.MyNewFunc(
(Select Data1, Data2, Data3, Data4, Data5 from AnotherTable)
)

When I try this I get than an insufficient number of arguements were supplied for the function. Is this possible?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-06-06 : 12:19:13
What version of SQL Server are you in?


Might work:

Select A.Data1, A.Data2, A.Data3, A.Data4, A.Data5, B.*
From AnotherTable A
Cross Apply dbo.MyNewFunc(Data1, Data2, Data3, Data4, Data5) B



Corey

I Has Returned!!
Go to Top of Page

petersrj
Starting Member

20 Posts

Posted - 2011-06-06 : 12:37:58
Seventhnight, thanks you for your help. I am using SQL 2008 Express. Your suggestion worked perfectly on the first try.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-06-06 : 12:42:17
If not using the Apply operator I believe you have to pass in the values as variables.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-06-06 : 13:33:10
Wow! A swing and a HIT!



Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -