7 files changed

+272
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
USE Contacts;
2+
3+
DROP TYPE IF EXISTS dbo.ContactNote;
4+
5+
GO
6+
7+
CREATE TYPE dbo.ContactNote
8+
AS TABLE
9+
(
10+
Note VARCHAR(MAX) NOT NULL
11+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
USE Contacts;
2+
3+
GO
4+
5+
CREATE TYPE dbo.DrivingLicense
6+
FROM CHAR(16) NOT NULL;
7+
8+
DROP TYPE dbo.DrivingLicense;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
USE Contacts;
2+
3+
IF EXISTS(SELECT 1 FROM sys.procedures WHERE [name] = 'InsertContactNotes')
4+
BEGIN;
5+
DROP PROCEDURE dbo.InsertContactNotes;
6+
END;
7+
8+
GO
9+
10+
CREATE PROCEDURE dbo.InsertContactNotes
11+
(
12+
@ContactId INT,
13+
@Notes VARCHAR(MAX)
14+
)
15+
AS
16+
BEGIN;
17+
18+
DECLARE @NoteTable TABLE (Note VARCHAR(MAX));
19+
DECLARE @NoteValue VARCHAR(MAX);
20+
21+
INSERT INTO @NoteTable (Note)
22+
SELECT value
23+
FROM STRING_SPLIT(@Notes, ',');
24+
25+
WHILE ((SELECT COUNT(*) FROM @NoteTable) > 0)
26+
BEGIN;
27+
28+
SELECT TOP 1 @NoteValue = Note FROM @NoteTable;
29+
30+
INSERT INTO dbo.ContactNotes (ContactId, Notes)
31+
VALUES (@ContactId, @NoteValue);
32+
33+
DELETE FROM @NoteTable WHERE Note = @NoteValue;
34+
35+
END;
36+
37+
SELECT * FROM dbo.ContactNotes
38+
WHERE ContactId = @ContactId
39+
ORDER BY NoteId DESC;
40+
41+
END;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
USE Contacts;
2+
3+
DROP PROCEDURE IF EXISTS dbo.InsertContactNotes;
4+
5+
GO
6+
7+
CREATE PROCEDURE dbo.InsertContactNotes
8+
(
9+
@ContactId INT,
10+
@Notes ContactNote READONLY
11+
)
12+
AS
13+
BEGIN;
14+
15+
DECLARE @TempNotes ContactNote;
16+
17+
INSERT INTO @TempNotes (Note)
18+
SELECT Note FROM @Notes;
19+
20+
UPDATE @TempNotes SET Note = 'Pre: ' + Note;
21+
22+
INSERT INTO dbo.ContactNotes (ContactId, Notes)
23+
SELECT @ContactId, Note
24+
FROM @Notes;
25+
26+
SELECT * FROM dbo.ContactNotes
27+
WHERE ContactId = @ContactId
28+
ORDER BY NoteId DESC;
29+
30+
END;
31+
32+
--test script
33+
34+
DECLARE @TempNotes ContactNote;
35+
36+
INSERT INTO @TempNotes (Note)
37+
VALUES
38+
('Hi, Peter called.'),
39+
('Quick note to let you know Jo wants you to ring her. She rang at 14:30.'),
40+
('Terri asked about the quote, I have asked her to ring back tomorrow.');
41+
42+
EXEC dbo.InsertContactNotes
43+
@ContactId = 23,
44+
@Notes = @TempNotes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
USE Contacts;
2+
3+
DROP PROCEDURE IF EXISTS dbo.InsertContactAddress;
4+
5+
GO
6+
7+
CREATE PROCEDURE dbo.InsertContactAddress
8+
(
9+
@ContactId INT,
10+
@HouseNumber VARCHAR(200),
11+
@Street VARCHAR(200),
12+
@City VARCHAR(200),
13+
@Postcode VARCHAR(20)
14+
)
15+
AS
16+
BEGIN;
17+
18+
SET NOCOUNT ON;
19+
20+
DECLARE @AddressId INT;
21+
22+
SELECT @Street = UPPER(LEFT(@Street, 1)) + LOWER(RIGHT(@Street, LEN(@Street) -1));
23+
SELECT @City = UPPER(LEFT(@City, 1)) + LOWER(RIGHT(@City, LEN(@City) - 1));
24+
25+
INSERT INTO dbo.ContactAddresses (ContactId, HouseNumber, Street, City, Postcode)
26+
VALUES (@ContactId, @HouseNumber, @Street, @City, @Postcode);
27+
28+
SELECT @AddressId = SCOPE_IDENTITY();
29+
30+
SELECT ContactId, AddressId, HouseNumber, Street, City, Postcode
31+
FROM dbo.ContactAddresses
32+
WHERE ContactId = @ContactId;
33+
34+
SET NOCOUNT OFF;
35+
36+
END;
37+
38+
--test script
39+
40+
EXEC dbo.InsertContactAddress
41+
@ContactId = 24,
42+
@HouseNumber = '10',
43+
@Street = 'Downing Street',
44+
@City = 'London',
45+
@Postcode = 'SW1 2AA';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
USE Contacts;
2+
3+
DROP PROCEDURE IF EXISTS dbo.InsertContactNotes;
4+
5+
GO
6+
7+
CREATE PROCEDURE dbo.InsertContactNotes
8+
(
9+
@ContactId INT,
10+
@Notes VARCHAR(MAX)
11+
)
12+
AS
13+
BEGIN;
14+
15+
DECLARE @NoteTable TABLE (Note VARCHAR(MAX));
16+
DECLARE @NoteValue VARCHAR(MAX);
17+
18+
INSERT INTO @NoteTable (Note)
19+
SELECT value
20+
FROM STRING_SPLIT(@Notes, ',');
21+
22+
DECLARE NoteCursor CURSOR FOR
23+
SELECT Note FROM @NoteTable;
24+
25+
OPEN NoteCursor
26+
FETCH NEXT FROM NoteCursor INTO @NoteValue;
27+
28+
WHILE @@FETCH_STATUS = 0
29+
BEGIN;
30+
INSERT INTO dbo.ContactNotes (ContactId, Notes)
31+
VALUES (@ContactId, @NoteValue);
32+
33+
FETCH NEXT FROM NoteCursor INTO @NoteValue;
34+
35+
END;
36+
37+
CLOSE NoteCursor;
38+
DEALLOCATE NoteCursor;
39+
40+
SELECT * FROM dbo.ContactNotes
41+
WHERE ContactId = @ContactId
42+
ORDER BY NoteId DESC;
43+
44+
END;
45+
46+
--test script
47+
DECLARE @TempNotes ContactNote;
48+
49+
INSERT INTO @TempNotes (Note)
50+
VALUES
51+
('Hi, Peter called.'),
52+
('Quick note to let you know Jo wants you to ring her. She rang at 14:30.'),
53+
('Terri asked about the quote, I have asked her to ring back tomorrow.');
54+
55+
EXEC dbo.InsertContactNotes
56+
@ContactId = 23,
57+
@Notes = @TempNotes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
USE Contacts;
2+
3+
DROP PROCEDURE IF EXISTS dbo.InsertContactRole;
4+
5+
GO
6+
7+
CREATE PROCEDURE dbo.InsertContactRole
8+
(
9+
@ContactId INT,
10+
@RoleTitle VARCHAR(200)
11+
)
12+
AS
13+
BEGIN;
14+
15+
DECLARE @RoleId INT;
16+
17+
BEGIN TRY;
18+
19+
BEGIN TRANSACTION;
20+
21+
IF NOT EXISTS(SELECT 1 FROM dbo.Roles WHERE RoleTitle = @RoleTitle)
22+
BEGIN;
23+
INSERT INTO dbo.Roles (RoleTitle)
24+
VALUES (@RoleTitle);
25+
END;
26+
27+
SELECT @RoleId = RoleId FROM dbo.Roles WHERE RoleTitle = @RoleTitle;
28+
29+
IF NOT EXISTS(SELECT 1 FROM dbo.ContactRoles WHERE ContactId = @ContactId AND RoleId = @RoleId)
30+
BEGIN;
31+
INSERT INTO dbo.ContactRoles (ContactId, RoleId)
32+
VALUES (@ContactId, @RoleId);
33+
END;
34+
35+
COMMIT TRANSACTION;
36+
37+
SELECT C.ContactId, C.FirstName, C.LastName, R.RoleTitle
38+
FROM dbo.Contacts C
39+
INNER JOIN dbo.ContactRoles CR
40+
ON C.ContactId = CR.ContactId
41+
INNER JOIN dbo.Roles R
42+
ON CR.RoleId = R.RoleId
43+
WHERE C.ContactId = @ContactId;
44+
45+
END TRY
46+
BEGIN CATCH;
47+
IF (@@TRANCOUNT > 0)
48+
BEGIN;
49+
ROLLBACK TRANSACTION;
50+
END;
51+
PRINT 'Error occurred in ' + ERROR_PROCEDURE() + ' ' + ERROR_MESSAGE();
52+
RETURN -1;
53+
END CATCH;
54+
55+
RETURN 0;
56+
57+
END;
58+
59+
--test script
60+
DECLARE @RetVal INT;
61+
62+
EXEC @RetVal = dbo.InsertContactRole
63+
@ContactId = 22,
64+
@RoleTitle = 'Actor';
65+
66+
PRINT 'RetVal = ' + CONVERT(VARCHAR(10), @RetVal);

0 commit comments

Comments
 (0)