Home All Groups Group Topic Archive Search About
Author
4 Nov 2005 2:19 PM
fdudan
Hi,

I have several question about UDF and table variable:

- Is there a way to pass columns as parameters to table-valued UDF in join
clauses ?

ex: select * from mytable t inner join dbo.fn_myfunc(t.col1,t.col2)

- table variable declaration must always use a separate declare statement ?

ex: declare @myvar int, @mytable table (col1 int)

- when using table variable in UDF, should we declare early in the UDF the
table variable as with temporary table in storeproc ? Is there a performance
penalty underneath ?

ex:

create function myfunc (param1 int)
as
begin

if param1 is null
  return null

declare @mytable table (col1 int)

end

OR

create function myfunc (param1 int)
as
begin

declare @mytable table (col1 int)

if param1 is null
  return null

end

which one is better ?

These are tricky programming points which are addressed by the BOL.
Any comments are welcomed.

Thanks in advance.

Author
4 Nov 2005 2:45 PM
Anith Sen
>> Is there a way to pass columns as parameters to table-valued UDF in join
>> clauses ?

No, it is documented in SQL Server Books Online.

>> table variable declaration must always use a separate declare statement ?

Yes, this is by design.

>> when using table variable in UDF, should we declare early in the UDF the
>> table variable as with temporary table in storeproc ? Is there a
>> performance penalty underneath ?

If you have a specific repro which shows a performance difference, please
post it here.

Table variables are explicitly created in tempdb and thus intertwining its
declaration with regular DML, especially when the function body is complex,
might have some impact on performance.

--
Anith

AddThis Social Bookmark Button