We have a scalar function that returns a DateTime. It performs a couple of quick table selects to get its return value. This function is already in use throughout the database – in default constraints, stored procs, etc. I would like to change the implementation of the function (to remove the table hits and make it more efficient) but apparently I can’t do that while it is referenced by other objects in the database.
Will I actually need to update or drop every object in the database that references it, update the function and then update or recreate all those objects to restore the reference to the function?
The function is being referenced by a handful of views, triggers, a couple of functions and a large number of default constraints and stored procs.
Thanks for any insight you can give.
The error I’m getting when I try to either Alter or Drop the function is:
Cannot [ALTER|DROP FUNCTION] ‘dbo.GetClientCurrentTime’ because it is being referenced by object ‘DF_tbl_PatientOrder_Note_RecordCreated’.
It depends. If the objects referencing the function have the
WITH SCHEMABINDINGoption then you’ll be explicitly prevented from messing with he function. Otherwise, the only restriction is ordinary DDL access locking restriction, meaning the plans in execution that use the function will place schema stability locks on the function and this will block your ALTER FUNCTION statements, as they require schema modification lock. But this would resolve itself when the plans finish execution.