Solved

How to Recompile UDF

Posted on 2004-09-23
5
1,203 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

808 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