-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13. Database Programmability.sql
133 lines (108 loc) · 2.85 KB
/
13. Database Programmability.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
--Scalar function
CREATE FUNCTION udf_ProjectDurationWeeks(@StartDate DATETIME, @EndDate DATETIME)
RETURNS INT
AS
BEGIN
DECLARE @projectDuration INT
IF(@EndDate IS NULL)
BEGIN
SET @EndDate = GETDATE()
END
SET @projectDuration = DATEDIFF(WEEK, @StartDate, @EndDate)
RETURN @projectDuration
END
SELECT
[Name],
dbo.udf_ProjectDurationWeeks(StartDate, EndDate) AS Duration
FROM Projects
--inline table-valued function (TVF)
GO
CREATE FUNCTION udf_AverageSalaryByDepartment()
RETURNS TABLE
AS
RETURN
(
SELECT d.[Name], AVG(e.Salary) AS AverageSalary
FROM Departments AS d
JOIN Employees AS e ON d.DepartmentID = e.DepartmentID
GROUP BY d.DepartmentID, d.[Name]
)
SELECT
*
FROM dbo.udf_AverageSalaryByDepartment()
--multi-statement table-valued function (MSTVF)
CREATE FUNCTION udf_EmployeeListByDepartment(@depName nvarchar(20))
RETURNS @result TABLE(
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
DepartmentName nvarchar(20) NOT NULL) AS
BEGIN
WITH Employees_CTE (FirstName, LastName, DepartmentName)
AS(
SELECT e.FirstName, e.LastName, d.[Name]
FROM Employees AS e
LEFT JOIN Departments AS d ON d.DepartmentID = e.DepartmentID)
INSERT INTO @result SELECT FirstName, LastName, DepartmentName
FROM Employees_CTE WHERE DepartmentName = @depName
RETURN
END
SELECT
[ProjectID],
[StartDate],
[EndDate],
dbo.udf_ProjectDurationWeeks([StartDate],[EndDate]) AS ProjectWeeks
FROM [SoftUni].[dbo].[Projects]
SELECT * FROM dbo.udf_EmployeeListByDepartment('Production')
--
CREATE FUNCTION udf_GetSalaryLevel(@Salary MONEY)
RETURNS varchar(10)
AS
BEGIN
DECLARE @result varchar(10)
IF(@Salary < 30000)
SET @result = 'Low'
ELSE IF(@Salary <= 50000)
SET @result = 'Average'
ELSE
SET @result = 'High'
RETURN @result
END
SELECT
FirstName,
LastName,
Salary,
dbo.udf_GetSalaryLevel(Salary) AS SalaryLevel
FROM Employees
--Procedure
USE SoftUni
CREATE PROCEDURE usp_GetSeniorEmployees
AS
SELECT
*
FROM Employees
WHERE DATEDIFF(YEAR, HireDate, GETDATE()) > 18
EXEC dbo.usp_GetSeniorEmployees
--check if any object depend on procedure
EXEC sp_depends 'usp_GetSeniorEmployees'
--store procedure with parameters
CREATE OR ALTER PROCEDURE usp_GetSeniorEmployees @YearsOfService INT = 5
AS
SELECT
FirstName,
LastName,
DATEDIFF(YEAR, HireDate, GETDATE()) AS YearsOfService
FROM Employees
WHERE DATEDIFF(YEAR, HireDate, GETDATE()) > @YearsOfService
ORDER BY HireDate
EXECUTE usp_GetSeniorEmployees
EXECUTE usp_GetSeniorEmployees 19
--
CREATE PROCEDURE usp_AddNumbers
@FirstNumber INT,
@SecondNumber INT,
@Result INT OUT
AS
SET @Result = @FirstNumber + @SecondNumber
DECLARE @res INT
EXECUTE usp_AddNumbers 8, 12, @res OUT
SELECT @res AS Result