Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Alter User Defined Functions in SQL

by suresh

In our previous article, we explained to you about creating User Defined Functions. Let us see how to alter User Defined Functions in SQL Server, such as Renaming, Modifying, and deleting the existing UDFs.

How to see User Defined Functions in SQL?

To view the existing functions in SQL Server, Please select the Database that has UDFs. From the below screenshot, you can observe that our [SQL Tutorial] database has all the functions that we created in our User Defined Functions article.

Alter User Defined Functions in SQL 1

To execute the existing functions, Please select the function that you want to execute (AverageSale). Next, right-click on it and select the Script Function as -> SELECT To -> New Query Window Editor

Alter User Defined Functions in SQL 2

Once you select the New Query Window Editor option, Select from function query will return automatically by the SSMS

Alter User Defined Functions in SQL 3

Properties of SQL User Defined Functions using SSMS

First, Right-click on the required function name and select the SQL Server properties option.

Alter User Defined Functions in SQL 4

Once you click on the Properties option, a new window called Function Properties opened. The following are the list of SQL Server Function Properties, and they are:

  • Database: It shows the name of the database that contains the specified function. Here, it is AverageSale
  • Server: The name of the current SQL Server Instance.
  • User: It shows the name of the current user
  • Created Date: It displays the function created date.
  • Name: It shows the name of the current Function. i.e., AverageSale
  • Schema: It displays the schema that we used for this function.
  • System Object: It shows Boolean value True or False, indicating whether the current function is a System Object or Not.
  • ANSI NULLs: Displays Boolean value True or False, indicating whether the object was created with ANSI NULLs or Not.
  • Function type: It show’s whether the function is SCALAR or Table-Valued Functions
Alter User Defined Functions in SQL 5

Properties of SQL User Defined Functions using Query

How to find the definition of a SQL User Defined Functions using the OBJECT_DEFINITION.

-- It Returns the AverageSale function Definition  
SELECT OBJECT_DEFINITION (OBJECT_ID('dbo.AverageSale')) AS ObjectDefinition;  
GO
Alter User Defined Functions in SQL 6

The following query returns the remaining properties of SQL UDFs.

-- It Returns the AverageSale function Name & it's Properties  
SELECT smo.OBJECT_ID,   
   OBJECT_NAME(smo.OBJECT_ID) AS OBJECT_NAME,   
   Obj.TYPE,   
   Obj.TYPE_DESC,  
   obj.create_date,
   smo.uses_ansi_nulls,  
   smo.uses_quoted_identifier,  
   smo.is_schema_bound,  
   smo.execute_as_principal_id  
FROM sys.sql_modules AS smo  
JOIN sys.objects AS Obj ON smo.OBJECT_ID = Obj.OBJECT_ID  
WHERE smo.OBJECT_ID = OBJECT_ID('dbo.AverageSale')  
ORDER BY Obj.TYPE  
GO
Alter User Defined Functions in SQL 7

TIP: You can also return the Function definition by adding: smo.DEFINITION inside the Select Statement.

Rename User Defined Functions in SQL Server

To rename the SQL User Defined Functions using the Microsoft SQL Server Management Studio, right-click on the function that you want to modify (AverageSale) and select the Rename option

Alter User Defined Functions in SQL 8

Once you click on the Rename option, SSMS allows us to rename as per our requirements.

Alter User Defined Functions in SQL 9

Modify User Defined Functions in SQL Server

The following examples will help you understand modifying or altering the SQL User Defined Functions using the Microsoft SQL Server Management Studio (SSMS) and query.

Alter UDFs using SSMS

To modify the UDFs using the SQL Server Management Studio, right-click on the function (CustomerbyDepartmnet) that you want to change, and select the Modify option

Alter User Defined Functions in SQL 10

Once you choose the modify option, a new query window will open with the following query. You can edit as per your requirement.

Alter User Defined Functions in SQL 11

Alter SQL User Defined Functions using Query

Let me use the ALTER FUNCTION to modify the existing function. For demonstration purpose, we are concatenating the First Name and Last Name as NAME

ALTER FUNCTION [dbo].[CustomerbyDepartment] (@profession VARCHAR(50))
  RETURNS TABLE
  AS
     RETURN (
 SELECT  [FirstName] + ' ' + [LastName] AS Name
 ,[Occupation]
 ,[Education]
 ,dept.DepartmentName AS Department
 ,[YearlyIncome] AS Income
 ,[Sales]
   FROM [MyEmployees Table]
 INNER JOIN 
 Department AS dept ON
 Dept.[id] = [MyEmployees Table].DeptID
 WHERE [Occupation] = @profession
 )

Let us see the Output

SELECT * FROM [dbo].[CustomerbyDepartment] ('Management')
GO

From the below screenshot, you can see the function is returning the NAME instead of First Name and Last Name.

Alter User Defined Functions in SQL 12

Delete User Defined Functions in SQL Server

How to delete the SQL User Defined Functions using the Microsoft SQL Server Management Studio (SSMS), and T-SQL Query. Before we get into the examples, remember the limitations:

  • SQL Server will not allow you to delete the function if there are any references from Functions, or Views to this function.
  • We cannot delete, if there are any references from Computed Columns, CHECK, or Default Contrarians to this function.

To demonstrate the delete operation, we added two more scalar functions. Our task is to delete the EmployeeSale and SaleEmployees functions.

Alter User Defined Functions in SQL 13

Delete UDFs using SQL Server Management Studio

To delete the UDFs using the SQL Server Management Studio, right-click on the function name and click the Delete option. Here, we want to delete SaleEmployees (scalar) function

Alter User Defined Functions in SQL 14

Choosing the delete option opens a Delete Object window. Click on the Show Dependencies button to check the dependencies, and then click OK to delete that function.

Alter User Defined Functions in SQL 15

Delete Functions using SQL Query

How to use SQL Server DROP FUNCTION to delete the User Defined Functions

DROP FUNCTION [dbo].[EmployeesSale]
GO

TIP: It is good practice to check, whether the function exists in the database or not using IF OBJECT_ID (N’EmployeeSale’, N’IF’) IS NOT NULL

Let us see what happens when we call the deleted function.

Alter User Defined Functions in SQL 16

From the above screenshot, see we don’t have EmployeeSale function in our Object explorer, and querying on the function is throwing an error.

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021ยท All Rights Reserved.
About | Contact | Privacy Policy