Encryption and Schema Binding Option in User Defined Function
Last Updated :
24 Feb, 2023
Encryption and schema binding are two separate options that can be applied to user-defined functions in SQL Server.
Encryption:
When you create a user-defined function in SQL Server, you can choose to encrypt the function's definition. This means that the source code of the function will be stored in an encrypted format, making it more difficult for someone to view or modify the code. You can apply encryption to a function by specifying the "WITH ENCRYPTION" option when creating the function.
Syntax:
CREATE FUNCTION dbo.Function_NAME(
@Param1 DATATYPE, @Param2 DATATYPE)
RETURNS DATATYPE
WITH ENCRYPTION
AS
BEGIN
-- Function body
END
Example:
Employee TableHere we create the User-defined function GetNameByGender() which gives the Name of Employees by their Gender with an encryption option that encrypts the function definition.
CREATE FUNCTION dbo.
GetNameByGender()(@Gender varchar(20))
RETURNS varchar(50)
WITH ENCRYPTION
AS
BEGIN
RETURN (Select Name ,
Gender from Employee
where Gender=@Gender)
END

If we try to see the definition of the function using 'sp_helptext' it gives the message that 'The text for object 'GetNameByGender' is encrypted'.

Schema Binding:
 Schema binding is an option that you can use when creating a user-defined function to bind the function to the schema of the objects it references. This means that if you create a function that references tables or views in a specific schema, you can use the schema binding option to ensure that the schema of those objects cannot be changed. This can be useful in situations where you want to prevent accidental changes to the schema that could break the function. You can apply Schemabinding to a function by specifying the "WITH SCHEMABINDING" option when creating the function.
Syntax:
CREATE FUNCTION dbo.MyFunction
(@Param1 INT, @Param2 VARCHAR(50))
RETURNS INT
WITH SCHEMABINDING
AS
BEGIN
-- Function body
END
Example:


Let's consider the above two tables Employee  and EmployeeCITY, and create the function GetNameByCITY  with a schema binding option :
CREATE FUNCTION [dbo].
GetNameByCITY(@CITY varchar(20))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (Select E.Name,EC.CITY
from [dbo].[Employee] E
JOIN [dbo].EmployeeCITY EC
ON E.ID=EC.E_ID where ec.CITY=@CITY)

Now if we try to change or update the records then it gives the error:

Note that once you apply schema binding to a function, you cannot modify the schema of the objects it references without first dropping the function.
Hence, Encryption and schema-binding are highly helpful from a security and integrity point of view and also improve the performance of the code.
Similar Reads
MySQL | AES_ENCRYPT ( ) Function The MySQL AES_ENCRYPT function is used for encrypting a string using Advanced Encryption Standard (AES) algorithm. The MySQL AES_ENCRYPT function encodes the data with 128 bits key length but it can be extended up to 256 bits key length. It encrypts a string and returns a binary string. The value re
1 min read
MySQL | DES_ENCRYPT ( ) Function In todayâs world, keeping data safe is important for any database. MySQL, one of the most popular database systems, offers various tools to secure your information. Among these is the DES_ENCRYPT() function, which uses the Data Encryption Standard (DES) algorithm to encrypt your data. This function
3 min read
DROP FUNCTION and its Parameters DROP FUNCTION : This statement could be used to remove an existing user-defined function. Syntax : DROP FUNCTION [ IF EXISTS ] schema_name.function_name; Example - 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
2 min read
MySQL | AES_DECRYPT ( ) Function The MySQL AES_DECRYPT function returns the original string after decrypting an encrypted string. It uses AES(Advanced Encryption Standard) algorithm to perform the decryption. The AES_DECRYPT function returns the decrypted string or NULL if it detects invalid data. The value returned by the AES_DECR
1 min read
Create, Alter and Drop schema in MS SQL Server Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to cr
4 min read