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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Table as Input Parameter to function

Author  Topic 

akashenk
Posting Yak Master

111 Posts

Posted - 2007-06-21 : 15:13:11
Is it possible to pass a table to a scalar-valued function? So, for instance, I have a table with two fields. I would like to pass the contents of this table to a function, which will extract the records from the table and create a particular string. This string will be returned by the function?

When defining the function, an input parameter of type TABLE is not allowed. Is there a different syntax, or is this not possible?

P.S.. the function I want to create is one that will create a delimited string out of a particular field of the table by passing the table, the fieldName and the delimiter. Perhaps there is already a built in function that does this?

Thanks,

Al

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-21 : 20:39:53
You can't. Why not hard code the table name in the function ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

akashenk
Posting Yak Master

111 Posts

Posted - 2007-06-21 : 21:17:39
...because I would like to have this utility for a large number of tables. I can, of course, have the same code throughout many stored procedures, however, I was hoping to encapsulate the functionality in a single function.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-21 : 21:20:14
use a CASE WHEN to do it.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

akashenk
Posting Yak Master

111 Posts

Posted - 2007-06-21 : 21:25:13
I'm not quite sure I follow.. but if you mean hard coding the hundreds of table names that might use this function and having to update it every time I add a table, that would defeat the purpose. Is there a way to pass the results of a Select statement, other than in a table?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-21 : 21:35:44
if you have hundreds of tables that need to do this, sounds to me like a design issue.

quote:
Is there a way to pass the results of a Select statement, other than in a table?

Wait for Katmai

Can you use stored procedure to form the particular string ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-22 : 08:46:00
1 Read about Cross apply
2 http://www.sommarskog.se/arrays-in-sql.html


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -