|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
UDF and table variableI 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. >> Is there a way to pass columns as parameters to table-valued UDF in join No, it is documented in SQL Server Books Online.>> clauses ? >> 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 If you have a specific repro which shows a performance difference, please >> table variable as with temporary table in storeproc ? Is there a >> performance penalty underneath ? 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 |
|||||||||||||||||||||||