1
+ USE [Music];
2
+ GO
3
+
4
+
5
+ -- ----------------------------------------------------------------------------
6
+ -- checks if the trigger already exists using the sys.objects and drops it. --
7
+ -- ----------------------------------------------------------------------------
8
+ IF EXISTS (SELECT * FROM sys .objects WHERE [name] = N' TR_UniqueTitleNumberInAlbum' AND [type] = ' TR' )
9
+ DROP TRIGGER [TR_UniqueTitleNumberInAlbum];
10
+ GO
11
+
12
+
13
+ -- ----------------------------------------------------------------------------
14
+ --
15
+ -- ----------------------------------------------------------------------------
16
+ CREATE TRIGGER [TR_UniqueTitleNumberInAlbum] ON [title]
17
+ INSTEAD OF INSERT -- SQL Server does not support BEFORE triggers
18
+ AS BEGIN
19
+ DECLARE @TitleNumber SMALLINT ,
20
+ @TitleAlbum INT ;
21
+
22
+ DECLARE CUR_InsertedTitle CURSOR FORWARD_ONLY FOR
23
+ SELECT ins.[titlenumber], ins.[fk_album_id]
24
+ FROM [inserted] AS ins;
25
+
26
+ BEGIN TRANSACTION;
27
+ OPEN CUR_InsertedTitle;
28
+ FETCH NEXT FROM CUR_InsertedTitle INTO @TitleNumber, @TitleAlbum;
29
+
30
+ WHILE @@FETCH_STATUS = 0 BEGIN ;
31
+ IF ((SELECT Count (* ) FROM [title] AS t
32
+ WHERE t.[titlenumber] = @TitleNumber
33
+ AND t.[fk_album_id] = @TitleAlbum) > 0 )
34
+ BEGIN ;
35
+ ROLLBACK ;
36
+
37
+ -- close cursor and free allocated memory to prevent memory leaks
38
+ CLOSE CUR_InsertedTitle;
39
+ DEALLOCATE CUR_InsertedTitle;
40
+
41
+ THROW 47000 , ' Titlenumber already exists in this album' , 2 ;
42
+ END;
43
+
44
+ BEGIN TRY;
45
+ INSERT INTO
46
+ [title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id])
47
+ VALUES ();
48
+ END TRY BEGIN CATCH;
49
+ ROLLBACK ;
50
+
51
+ -- close cursor and free allocated memory to prevent memory leaks
52
+ CLOSE CUR_InsertedTitle;
53
+ DEALLOCATE CUR_InsertedTitle;
54
+
55
+ RETURN 1 ;
56
+ END CATCH;
57
+
58
+ FETCH NEXT FROM CUR_InsertedTitle INTO @TitleNumber, @TitleAlbum;
59
+ END;
60
+ ROLLBACK ;
61
+
62
+ -- close cursor and free allocated memory to prevent memory leaks
63
+ CLOSE CUR_InsertedTitle;
64
+ DEALLOCATE CUR_InsertedTitle;
65
+
66
+ /*
67
+ DECLARE CUR_AlbumTitle CURSOR FORWARD_ONLY FOR
68
+ SELECT t.[titlenumber] FROM [title] AS t
69
+ INNER JOIN [inserted] AS ins ON ins.fk_album_id = t.fk_album_id;
70
+
71
+ BEGIN TRANSACTION;
72
+ OPEN CUR_AlbumTitle;
73
+ FETCH NEXT FROM CUR_AlbumTitle INTO @TitleNumber;
74
+
75
+ WHILE @@FETCH_STATUS = 0 BEGIN;
76
+ IF (@TitleNumber = ANY(SELECT [titlenumber] FROM [inserted])) BEGIN;
77
+ ROLLBACK;
78
+ THROW 47000, 'Titlenumber already exists in this album', 2;
79
+ END;
80
+
81
+ FETCH NEXT FROM CUR_AlbumTitle INTO @TitleNumber;
82
+ END;
83
+ ROLLBACK;*/
84
+
85
+ -- close cursor and free allocated memory to prevent memory leaks
86
+ CLOSE CUR_AlbumTitle;
87
+ DEALLOCATE CUR_AlbumTitle;
88
+ END
89
+ GO
90
+
91
+
92
+ -- ----------------------------------------------------------------------------
93
+ -- test code for checking trigger behavior. --
94
+ -- ----------------------------------------------------------------------------
95
+ INSERT INTO [album] ([name], [releaseyear]) VALUES (' Album #1' , 2017 );
96
+ INSERT INTO [title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id]) VALUES (' Title #1' , 1 , 300 , 20000 , 1 , 1 );
97
+ INSERT INTO [title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id]) VALUES (' Title #2' , 1 , 400 , 30000 , 1 , 1 );
98
+
99
+ SELECT * FROM [title];
0 commit comments