I know this must be simple, but how do I preface the creation of a function with a check to see if it already exists? If it exists, I want to drop and re-create it.
To drop a function, you must have the DELETE privilege for the mysql database. This is because DROP FUNCTION removes the row from the mysql. func system table that records the function's name, type and shared library name. For dropping a stored function, see DROP FUNCTION.
Description. The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist.
The syntax to a drop a function in SQL Server (Transact-SQL) is: DROP FUNCTION function_name; function_name. The name of the function that you wish to drop.
Description. The DROP FUNCTION statement is used to drop a stored function or a user-defined function (UDF). That is, the specified routine is removed from the server, along with all privileges specific to the function. You must have the ALTER ROUTINE privilege for the routine in order to drop it.
IF EXISTS ( SELECT * FROM sysobjects WHERE id = object_id(N'function_name') AND xtype IN (N'FN', N'IF', N'TF') ) DROP FUNCTION function_name GO
If you want to avoid the sys* tables, you could instead do (from here in example A):
IF object_id(N'function_name', N'FN') IS NOT NULL DROP FUNCTION function_name GO
The main thing to catch is what type of function you are trying to delete (denoted in the top sql by FN, IF and TF):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With