-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocedures.sql
67 lines (53 loc) · 2.18 KB
/
procedures.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
----STORED PROCEDURE----------------------------------------------------------------------
--"As a person from HR responsible for creating employees emails list (task from my manager),
-- I wold like to have them quickly combined from employee Name, Surname and our shop name like:
--"As a person from HR responsible for adding information about employees to AssociatedPersons
--table I would like not to be able to add record with duplicate email value and see apropriate error message
--that is clear for non-technical person. I could then come back to this guy/girl to agree on his/her new
--email personally before adding this information to the database"
SELECT * FROM AssociatedPersons
WHERE PersonType = 'Employee'
--a simple procedure to create list of employees emials only
CREATE PROCEDURE spCreateEmailForEmployeeOnly
AS
BEGIN
SELECT LOWER(ap.Name) + '.'+ LOWER(ap.Surname) + '@coffevestore.com' AS Email
FROM AssociatedPersons ap
WHERE PersonType = 'Employee'
END
exec spCreateEmailForEmployeeOnly
--a procedure to add an employee record and show "user friendly" information about an error
--regarding violation of unique value in email column (to show try catch block)
CREATE PROC spInsertEmployees
@PersonType varchar(50),
@Name varchar(50),
@Surname varchar(50),
@DateOfBirth date,
@Gender char(1),
@Email varchar(50)
AS
BEGIN
BEGIN TRY
BEGIN TRAN
INSERT INTO AssociatedPersons(PersonType, Name, Surname, DateOfBirth, Gender, Email)
VALUES( @PersonType, @Name, @Surname, @DateOfBirth, @Gender, @Email)
COMMIT TRAN
END TRY
BEGIN CATCH
PRINT 'There was an error. You cannot input data with duplicate email address!'
PRINT Error_Number()
PRINT Error_Message()
ROLLBACK TRAN
END CATCH
END
--SELECT * FROM AssociatedPersons WHERE PersonType = 'Employee'
exec spInsertEmployees
@PersonType = 'Employee',
@Name = 'Nora',
@Surname = 'Biden',
@DateOfBirth = '1990-01-02',
@Gender = 'F',
@Email = '[email protected]'
--SELECT * FROM AssociatedPersons
--WHERE PersonType = 'Employee' AND Name ='Sophia' AND Surname = 'Warren'