Document 19
Document 19
Guide
Dr M AOUDE
February 4, 2025
Contents
1 Introduction to SQL Server Functions 3
1.1 Key Characteristics . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Introduction to SQL Server Functions 3
1
6 CLR Functions 9
6.1 Enabling CLR Support . . . . . . . . . . . . . . . . . . . . . . 9
6.2 Example CLR Function . . . . . . . . . . . . . . . . . . . . . 9
7 Deterministic vs Nondeterministic Functions 10
7.1 Deterministic Functions . . . . . . . . . . . . . . . . . . . . . 10
7.2 Nondeterministic Functions . . . . . . . . . . . . . . . . . . . 10
8 Deterministic vs Nondeterministic Functions 10
8.1 Deterministic Functions . . . . . . . . . . . . . . . . . . . . . 10
8.2 Nondeterministic Functions . . . . . . . . . . . . . . . . . . . 11
9 Best Practices 11
9.1 Performance Considerations . . . . . . . . . . . . . . . . . . . 11
9.2 Security Considerations . . . . . . . . . . . . . . . . . . . . . 11
10 Best Practices 11
10.1 Schema Binding . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10.2 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . 12
10.3 Performance Considerations . . . . . . . . . . . . . . . . . . . 12
11 Conclusion 12
11.1 Example CLR Function . . . . . . . . . . . . . . . . . . . . . 13
12 Key Takeaways 13
2
1 Introduction to SQL Server Functions
A function in SQL Server is a reusable code module that accepts parameters
and returns a value or result set. Unlike stored procedures, functions can be
used within SELECT statements and WHERE clauses.
Note
Unlike stored procedures, functions must be called without EXE-
CUTE keyword and must return a value.
3
6 SUM(Salary) AS TotalPayroll
7 FROM Employees;
4
16 END
17 END;
5
3 DATEADD(YEAR, 1, GETDATE()) AS OneYearFromNow,
4 DATEDIFF(YEAR, '1990-01-01', GETDATE()) AS YearsSince1990,
5 DATEPART(MONTH, GETDATE()) AS CurrentMonth,
6 EOMONTH(GETDATE()) AS LastDayOfMonth;
6
1 -- Create a function to calculate age
2 CREATE FUNCTION CalculateAge
3 (
4 @BirthDate DATE
5 )
6 RETURNS INT
7 WITH SCHEMABINDING
8 AS
9 BEGIN
10 RETURN DATEDIFF(YEAR, @BirthDate, GETDATE()) -
11 CASE
12 WHEN (MONTH(@BirthDate) > MONTH(GETDATE())) OR
13 (MONTH(@BirthDate) = MONTH(GETDATE()) AND
14 DAY(@BirthDate) > DAY(GETDATE()))
15 THEN 1
16 ELSE 0
17 END
18 END;
19
20 -- Use the function
21 SELECT
22 FirstName,
23 LastName,
24 BirthDate,
25 dbo.CalculateAge(BirthDate) AS Age
26 FROM Employees;
7
18 DepartmentID = @DepartmentID
19 );
20
21 -- Use the function
22 SELECT * FROM dbo.GetEmployeesByDepartment(5);
8
5 CLR Functions
5.1 Enable CLR Support
Before using CLR functions, enable CLR support on the server:
1 sp_configure 'clr_enabled', 1;
2 GO
3 RECONFIGURE;
4 GO
6 CLR Functions
6.1 Enabling CLR Support
Before using CLR functions, enable CLR support on the server:
1 sp_configure 'clr_enabled', 1;
2 GO
3 RECONFIGURE;
4 GO
9
7 Deterministic vs Nondeterministic Functions
7.1 Deterministic Functions
Functions that always return the same result for the same input:
1 CREATE FUNCTION SquareNumber(@num INT)
2 RETURNS INT
3 WITH SCHEMABINDING
4 AS
5 BEGIN
6 RETURN @num * @num
7 END;
10
8.2 Nondeterministic Functions
Functions that may return dierent results for the same input:
1 -- Built-in nondeterministic function
2 SELECT GETDATE();
3
4 -- Custom nondeterministic function
5 CREATE FUNCTION GenerateRandomPrice
6 (
7 @BasePrice DECIMAL(18,2)
8 )
9 RETURNS DECIMAL(18,2)
10 AS
11 BEGIN
12 RETURN @BasePrice * (1 + (RAND() * 0.2));
13 END;
9 Best Practices
9.1 Performance Considerations
Use schema binding when possible
10 Best Practices
10.1 Schema Binding
Use schema binding when possible to improve performance and maintain-
ability:
1 CREATE FUNCTION dbo.GetFullName
2 (
3 @FirstName NVARCHAR(50),
4 @LastName NVARCHAR(50)
11
5 )
6 RETURNS NVARCHAR(101)
7 WITH SCHEMABINDING
8 AS
9 BEGIN
10 RETURN @FirstName + ' ' + @LastName;
11 END;
11 Conclusion
SQL Server functions are powerful tools for:
12
Code reusability
Data consistency
Performance optimization
Business logic implementation
When used correctly, they can signicantly improve database design and
application performance.
12 Key Takeaways
Functions must always return a value
They can be used in SELECT statements and WHERE clauses
Schema binding helps with performance and maintainability
Inline table-valued functions generally perform better than multi-statement
ones
Be cautious with nondeterministic functions in computed columns or
indexes
CLR functions are ideal for complex calculations or external operations
13