From 6eb2eb2fa4790c6053b1912b4a2bfbab42d6a5b2 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 4 Nov 2017 11:55:31 +0100 Subject: [PATCH] add solutions for A2.6 --- ...QL-StoredProcedures-Aufgaben_Loesungen.sql | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/db/4_sem/arzt_db/ArztDB/TSQL-StoredProcedures-Aufgaben_Loesungen.sql b/db/4_sem/arzt_db/ArztDB/TSQL-StoredProcedures-Aufgaben_Loesungen.sql index 188f86a..bea8e3c 100644 --- a/db/4_sem/arzt_db/ArztDB/TSQL-StoredProcedures-Aufgaben_Loesungen.sql +++ b/db/4_sem/arzt_db/ArztDB/TSQL-StoredProcedures-Aufgaben_Loesungen.sql @@ -150,3 +150,106 @@ GO exec usp_ListOfDoctorsA2_5 go +--A2.6 +if OBJECT_ID('usp_InsertNewCityA2_6') is not null + drop procedure usp_InsertNewCityA2_6 +go + +CREATE PROCEDURE usp_InsertNewCityA2_6 + ( + @CityName varchar(30), + @PLZ int + ) +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + -- Insert statements for procedure here + insert into Ort (PLZ, Ort) + values (@PLZ, @CityName); + END +GO + +exec usp_InsertNewCityA2_6 'Herzogenbuchsee', 3360 +go + + +--A2.6.1 +if OBJECT_ID('usp_InsertNewCityA2_6_1') is not null + drop procedure usp_InsertNewCityA2_6_1 +go + +CREATE PROCEDURE usp_InsertNewCityA2_6_1 + ( + @CityName varchar(30), + @PLZ int + ) +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + -- Insert statements for procedure here + if (select count(*) from Ort where PLZ = @PLZ) = 0 + begin + print 'Ort wird hinzugefügt.'; + insert into Ort (PLZ, Ort) + values (@PLZ, @CityName) + end + else + begin + print 'Ortschaft existiert bereits.'; + update Ort + set Ort.Ort = @CityName + where Ort.PLZ = @PLZ; + end + END +GO + +exec usp_InsertNewCityA2_6_1 + @CityName = 'Thörigen', + @PLZ = 3361 +go + +--A2.6.2 +if OBJECT_ID('usp_InsertNewCityA2_6_2') is not null + drop procedure usp_InsertNewCityA2_6_2 +go + +CREATE PROCEDURE usp_InsertNewCityA2_6_2 + ( + @CityName varchar(30), + @PLZ int + ) +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + -- Insert statements for procedure here + if not exists (select OrtNr from Ort where PLZ = @PLZ) + begin + print 'Ort wird hinzugefügt.'; + insert into Ort (PLZ, Ort) + values (@PLZ, @CityName) + end + else + begin + print 'Ortschaft existiert bereits.'; + update Ort + set Ort.Ort = @CityName + where Ort.PLZ = @PLZ; + end + END +GO + +exec usp_InsertNewCityA2_6_2 + @CityName = 'Thörigen', + @PLZ = 3361 +go + +select * from Ort;