-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAllPersonsWithPartner.sql
92 lines (59 loc) · 2.67 KB
/
GetAllPersonsWithPartner.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
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `GetAllPersonsWithPartner`(IN `YearFrom` INT(4), IN `FamilyName` VARCHAR(50))
SQL SECURITY INVOKER
COMMENT 'To get all persons with a specific familyname and their partner'
BEGIN
-- CompletedOk defines the result of a database transaction, like this:
-- 0 = Transaction finished without problems.
-- 1 =
-- 2 = Transaction aborted due to problems during update and rollback performed
-- ...
DECLARE CompletedOk int;
-- NewTransNo is autonumber counter fetched from a seperate table and used for logging in a seperate log table
DECLARE NewTransNo int;
-- TransResult is used to count the number of seperate database operations and rissen with each step
DECLARE TransResult int;
-- RecCount is used to count the number of related records in depended tables.
DECLARE RecCount int;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SET CompletedOk = 2;
INSERT INTO humans.testlog
SET TestLog = CONCAT("Transaction-", IFNULL(NewTransNo, "null"), ". ", "Error occured in SPROC: GetAllPersonsWithPartner(). Rollback executed. CompletedOk= ", CompletedOk),
TestLogDateTime = NOW();
SELECT CompletedOk;
END;
main_proc:
BEGIN
SET CompletedOk = 0;
SET TransResult = 0;
SET NewTransNo = GetTranNo("GetAllPersonsWithPartner");
-- Schrijf start van deze SQL transactie naar log
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', TransResult, '. Start SPROC: GetAllPersonsWithPartner() voor personen geboren ná: ', YearFrom, ' en met familienaam: ', IFNULL(FamilyName, 'alle')),
TestLogDateTime = NOW();
SELECT DISTINCT
CONCAT(P.PersonGivvenName, ' ', P.PersonFamilyName) AS 'Person',
P.PersonID as 'PersonID',
T.Partner as 'Partner',
T.PartnerID as 'PartnerID'
FROM persons P
LEFT JOIN (
SELECT
CONCAT(PA.PersonGivvenName, " ", PA.PersonFamilyName) as Partner,
PA.PersonID as PartnerID,
R.RelationPerson,
RN.RelationnameName from relations R
JOIN relationnames RN ON R.RelationName = RN.RelationnameID
JOIN persons PA on R.RelationWithPerson = PA.PersonID
WHERE RN.RelationnameName = "Partner") AS T on P.PersonID = T.RelationPerson
WHERE P.PersonFamilyName LIKE CONCAT(FamilyName, '%')
AND YEAR(P.PersonDateOfBirth) >= YearFrom
ORDER BY P.PersonFamilyName, P.PersonDateOfBirth;
INSERT INTO humans.testlog
SET TestLog = CONCAT('TransAction-', IFNULL(NewTransNo, 'null'), '. TransResult= ', TransResult, '. Einde SPROC: GetAllPersonsWithPartner()'),
TestLogDateTime = NOW();
END;
END$$
DELIMITER ;