I'm trying to write a script that can be run regardless of whether a user-defined function already exists. Per this link, I have the following:
IF OBJECT_ID('dbo.fn_myFunc') IS NOT NULL
DROP FUNCTION dbo.fn_myFunc;
go
CREATE FUNCTION fn_myFunc (
...
) RETURNS BIT AS
BEGIN
...
END
go
The issue is, later on, I have
ALTER TABLE my_table
ADD CONSTRAINT my_table_chk_myFunc CHECK (dbo.fn_myFunc(...) = 1)
go
So when the DROP FUNCTION
happens, I get
Cannot DROP FUNCTION 'dbo.fn_myFunc' because it is being referenced by object 'my_table_chk_myFunc '.
I tried, instead of using ...IS NOT NULL DROP... CREATE FUNCTION
using ...IS NULL CREATE... ALTER FUNCTION
, but either I got the syntax wrong, or it doesn't work.
How do I best get around this?