This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
/
tentamen2 - oktober 2013.sql
85 lines (71 loc) · 1.87 KB
/
tentamen2 - oktober 2013.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
create trigger opgave1
on ArticleIngredient
after delete
as
begin
declare @rowcount int
select @rowcount = @@ROWCOUNT
if @rowcount = 0
return
begin try
declare @articleid
if exists (SELECT a.articleID FROM Article a inner join ArticleIngredient ai on a.articleid = ai.articleid group by a.articleID HAVING COUNT(ai.ingredientID) < 2)
BEGIN
DELETE articleID from ArticleIngredient where articleID = @articleid
END
ELSE
BEGIN
rollback transaction
raiserror ('Er zijn meer dan twee ingredienten voor dit artikel', 16,1)
END
end try
begin catch
declare @ErrorMsg VARCHAR(400),
@ErrorSeverity INT,
@ErrorState INT
select @ErrorMsg = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
RAISERROR (@ErrorMessage , @ErrorSeverity, @ErrorState)
IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION
end
return
end catch
--opgave 2
create trigger opgave2
on ArticleIngredient
after insert
as
begin
declare @rowcount int
select @rowcount = @@ROWCOUNT
if @rowcount = 0
return
begin try
declare @ingredientid INT
declare @description varchar(50)
if exists (select a.articleID from Article a inner join Production p
ON a.articleID = p.articleID
where ProdDate <= GETDATE() )
begin
insert into ArticleIngredient VALUES (@ingredientid, @description)
end
else
begin
rollback transaction
raiserror ('Er mogen geen ingredienten meer toegevoegd worden', 16,1)
end
end try
begin catch
declare @ErrorMsg VARCHAR(400),
@ErrorSeverity INT,
@ErrorState INT
select @ErrorMsg = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
RAISERROR (@ErrorMsg , @ErrorSeverity, @ErrorState)
IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION
end
return
end catch
end