diff --git a/db/4_sem/BuchAutoren/BuchAutoren/BuchAutoren.ssmssqlproj b/db/4_sem/BuchAutoren/BuchAutoren/BuchAutoren.ssmssqlproj index c21dd14..f978a23 100644 --- a/db/4_sem/BuchAutoren/BuchAutoren/BuchAutoren.ssmssqlproj +++ b/db/4_sem/BuchAutoren/BuchAutoren/BuchAutoren.ssmssqlproj @@ -37,6 +37,12 @@ insert_data.sql + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local)\SQLEXPRESS:True + (local)\SQLEXPRESS + + triggger_solutions.sql + diff --git a/db/4_sem/BuchAutoren/BuchAutoren/insert_data.sql b/db/4_sem/BuchAutoren/BuchAutoren/insert_data.sql index 85c5381..fd37ee7 100644 --- a/db/4_sem/BuchAutoren/BuchAutoren/insert_data.sql +++ b/db/4_sem/BuchAutoren/BuchAutoren/insert_data.sql @@ -1,4 +1,5 @@ -- Daten einfügen +use BuchAutorDB; INSERT INTO buch.tblBuch VALUES ('3-89842-141-1', 'SELECT * FROM SQL Server 2000', 49.90); INSERT INTO buch.tblBuch (ISBN, Titel) diff --git a/db/4_sem/BuchAutoren/BuchAutoren/triggger_solutions.sql b/db/4_sem/BuchAutoren/BuchAutoren/triggger_solutions.sql new file mode 100644 index 0000000..1982831 --- /dev/null +++ b/db/4_sem/BuchAutoren/BuchAutoren/triggger_solutions.sql @@ -0,0 +1,60 @@ +-- ================================================ +-- Template generated from Template Explorer using: +-- Create Trigger (New Menu).SQL +-- +-- Use the Specify Values for Template Parameters +-- command (Ctrl-Shift-M) to fill in the parameter +-- values below. +-- +-- See additional Create Trigger templates for more +-- examples of different Trigger statements. +-- +-- This block of comments will not be included in +-- the definition of the function. +-- ================================================ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +-- ============================================= +-- Author: Zweili Andreas +-- Create date: 2017-11-24 +-- Description: +-- ============================================= +ALTER TRIGGER buch.trigAutorBuch_Insert_Update + ON buch.tblAutorBuch + AFTER INSERT, UPDATE +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + -- Insert statements for trigger here + + declare @ID int; + declare @ISBN char(13); + declare @anteil decimal(5,2); + declare @anteil_summe decimal(5,2); + + select @ID = ID, @ISBN = ISBN from inserted; + + select @anteil_summe = sum(Anteil) from tblAutorBuch + where tblAutorBuch.ISBN = @ISBN; + + if @anteil_summe > 100.00 delete from tblAutorBuch + where tblAutorBuch.ID = @ID; + print 'Summe ist zu hoch' +END +GO + +-- Trigger Test +use BuchAutorDB; +insert into buch.tblAutor (Nachname, Vorname) + values ('Muster', 'Max'); + +insert into buch.tblAutorBuch (ID, ISBN, Anteil) + values (1, '3-89842-129-5', 90.00); + +insert into buch.tblAutorBuch (ID, ISBN, Anteil) + values (2, '3-89842-129-5', 15.00);