Solved

How to Recompile UDF

Posted on 2004-09-23
5
1,188 Views
Last Modified: 2012-06-21
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
Comment
Question by:JAMES
  • 3
5 Comments
 
LVL 11

Expert Comment

by:ram2098
ID: 12131145
User-defined functions in SQL 2000 itself are not pre-compiled ones.

So, there is no point of re-compilation.
0
 

Author Comment

by:JAMES
ID: 12131171
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
 
LVL 2

Accepted Solution

by:
ngolovin earned 500 total points
ID: 12131447
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
 

Author Comment

by:JAMES
ID: 12131459
I'll give it a try and report back!

Best backup my DB first ;-)

Thanks.
0
 

Author Comment

by:JAMES
ID: 12131500
My functions are still in one piece so thanks :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now