Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

How to Recompile UDF

Hi,

I sort of had this question in another thread but as it evolved from another question I thought I would repost this new one.

I have a number of connected inline and scaler UDF's which need to be recoompiled from time-to-time due to changes in underlying tables during development.  

Does anyone know how to "manually" recompile these as I don't believe the sp_recompile/WITH RECOMPILE switches work for UDF's.  My understanding, and according to the documention, is they only apply to Stored Procecedures.

I currently delete every UDF from the Db and then recreate them - a bit of a chore!

Thanks for any help.

James.
0
JAMES
Asked:
JAMES
  • 3
1 Solution
 
ram2098Commented:
User-defined functions in SQL 2000 itself are not pre-compiled ones.

So, there is no point of re-compilation.
0
 
JAMESAuthor Commented:
OK but...

When we change the structure of the underlying tables associated with cascaded UDF's (ie. one UDF calls another and adds additional functionality / values to it and uses * to obtain the parent columns) the headings get messed up when we use the UDF's.

Dropping the tables and recreating them fixes it.  We have also tried clearing the proc cache but this does not help.
0
 
ngolovinCommented:
Hello, here is a procedure, which recompiles a UDF.
It was written pretty quickly, some time ago, when I got sick of "touching" the UDFs manually every time something changes.
It does exactly what I did manually, which is probably rather brute-force than clever.
But works anyway, though limited to functions, whose code fits in varchar(8000).


create procedure recompile_udf
(@func sysname)
as
declare @text varchar(8000)
declare @i int

if exists (select * from dbo.syscomments where id=object_id(@func)) select @text=convert(varchar(8000), c.text) from dbo.syscomments c, dbo.sysobjects o     where o.id = c.id and c.id = object_id(@func) order by c.number, c.colid

set @i=patindex('CREATE%',@text)
set @text=substring(@text,@i+6,len(@text))
if substring(@text,2,1)=' '
      set @text=ltrim(@text)
else
set @text=' '+@text
set @text='ALTER ' + @text

exec (@text)
print @text

go
0
 
JAMESAuthor Commented:
I'll give it a try and report back!

Best backup my DB first ;-)

Thanks.
0
 
JAMESAuthor Commented:
My functions are still in one piece so thanks :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now