DROP FUNCTION and its Parameters
DROP FUNCTION :
This statement could be used to remove an existing user-defined function.
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.
DROP FUNCTION [ IF EXISTS ] schema_name.function_name;
Let’s consider Geeks is the function you want to delete then use the following syntax as follows.
DROP FUNCTION Geeks;
To drop more than one user-defined functions, use below syntax :
DROP FUNCTION [IF EXISTS] schema_name.function_name1, schema_name.function_name2, ...;
- IF EXISTS –
IF EXISTS parameter is optional used to drop the function only if it exists. In case of removing a non-existing function without using the IF EXISTS option, SQL Server will throw an error.
- schema_name –
The schema_name is an optional parameter. The schema_name defines the name of the schema to which the user-defined function belongs.
- function_name –
The function_name is the name of the function that will be removed.
If there are constraints like CHECK or DEFAULT in the function, the DROP FUNCTION statement will return error.
Lets us create a function that calculates discount from amount, list, and percentage :
Creating “Geek.discount_amount” function –
CREATE FUNCTION Geek.discount_amount ( @amount INT, @list DEC(10,2), @percentage DEC(4,2) ) RETURNS DEC(10,2) AS BEGIN RETURN @quantity * @amount * @percentage END
To drop the function, you can use the following SQL query given below.
DROP FUNCTION IF EXISTS Geek.discount_amount;