Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I drop a function if it already exists?

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.

like image 629
DavidStein Avatar asked Feb 18 '10 17:02

DavidStein


People also ask

How do you drop a function?

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.

What is DROP TABLE if exists?

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.

Can we drop a function in SQL?

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.

How do you drop a function in a database?

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.


1 Answers

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):

  • FN = Scalar Function
  • IF = Inlined Table Function
  • TF = Table Function
like image 195
adrianbanks Avatar answered Oct 19 '22 16:10

adrianbanks