Upload New File
This commit is contained in:
parent
db0e564dfa
commit
bad745ab17
79
Shared/IdentNrGuppirung_Trigger.sql
Normal file
79
Shared/IdentNrGuppirung_Trigger.sql
Normal file
@ -0,0 +1,79 @@
|
||||
SET ANSI_NULLS ON
|
||||
GO
|
||||
|
||||
SET QUOTED_IDENTIFIER ON
|
||||
GO
|
||||
|
||||
-- =============================================
|
||||
-- Author: Stoyan Zlatev
|
||||
-- Create date: 13-04-2023
|
||||
-- Description: That trigger should update IdentNrGruppierung, IdentNr_Gruppendetails
|
||||
-- when a group whit name (WPD [Nennweite] / [Temperatur]°)
|
||||
-- dose not exists in the IdentNr_Gruppendetails table.
|
||||
-- Update: 26-04-2023 - @GroupTyp added.
|
||||
-- The GroupTyp will be determinated in CASE WHEN ... query
|
||||
-- and later used for updates on [IdentNr_Gruppendetail] and [IdentNrGruppierung]
|
||||
-- =============================================
|
||||
ALTER TRIGGER [TRG_AfterInsert_UpdateIdentNrGrouping]
|
||||
ON [dbo].[Identnr]
|
||||
AFTER INSERT
|
||||
AS
|
||||
BEGIN
|
||||
-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
|
||||
SET NOCOUNT ON;
|
||||
|
||||
DECLARE @IdentNr INT;
|
||||
DECLARE @Nennweite INT;
|
||||
DECLARE @Temperatur INT;
|
||||
DECLARE @Typ VARCHAR(20);
|
||||
DECLARE @GroupId INT;
|
||||
DECLARE @GroupName VARCHAR(MAX);
|
||||
DECLARE @GroupTyp VARCHAR(20);
|
||||
|
||||
-- setting declared field from inserted table
|
||||
SELECT @IdentNr = [IdentNr]
|
||||
, @Nennweite = [Nennweite]
|
||||
, @Temperatur = [Temperatur]
|
||||
, @Typ = [Typ]
|
||||
FROM [inserted];
|
||||
|
||||
SET @GroupTyp = (CASE WHEN @Typ LIKE '%WPD%' THEN 'WPD'
|
||||
WHEN @Typ LIKE '%WPF%' THEN 'WPF'
|
||||
WHEN @Typ LIKE '%WSD%' THEN 'WSD'
|
||||
ELSE NULL END);
|
||||
|
||||
IF @GroupTyp IS NOT NULL
|
||||
BEGIN
|
||||
-- creating a group name from declared values "@GroupTyp [Nennweite] / [Temperatur]°"
|
||||
SET @GroupName = @GroupTyp
|
||||
+ ' '
|
||||
+ CASE WHEN @Nennweite IS NOT NULL THEN CAST(@Nennweite AS VARCHAR(5)) ELSE '0' END
|
||||
+ ' / '
|
||||
+ CASE WHEN @Temperatur IS NOT NULL THEN CAST(@Temperatur AS VARCHAR(5)) ELSE '0' END
|
||||
+ '°';
|
||||
|
||||
-- getting next group id
|
||||
SET @GroupId = (SELECT TOP 1 [ID]
|
||||
FROM [IdentNr_Gruppendetail]
|
||||
WHERE [Name] = @GroupName);
|
||||
|
||||
-- case the group name dose not exist into [IdentNr_Gruppendetail] will be inserted.
|
||||
IF @GroupId IS NULL
|
||||
BEGIN
|
||||
SET @GroupId = (SELECT MAX([ID]) FROM [IdentNr_Gruppendetail]) + 1;
|
||||
|
||||
INSERT INTO [IdentNr_Gruppendetail] ([ID], [Name])
|
||||
VALUES (@GroupId, @GroupName)
|
||||
END;
|
||||
|
||||
-- case the @IdentNr dose not exist [IdentNrGruppierung] will be mapped.
|
||||
IF (SELECT COUNT(1) FROM [IdentNrGruppierung] WHERE [IdentNr] = @IdentNr) = 0
|
||||
BEGIN
|
||||
SET IDENTITY_INSERT [IdentNrGruppierung] ON;
|
||||
INSERT INTO [IdentNrGruppierung] ([IdentNr], [GruppenID], [ID])
|
||||
VALUES (@IdentNr, @GroupId, (SELECT MAX([ID]) FROM [IdentNrGruppierung]) + 1)
|
||||
SET IDENTITY_INSERT [IdentNrGruppierung] OFF;
|
||||
END;
|
||||
END;
|
||||
END
|
||||
GO
|
||||
Loading…
Reference in New Issue
Block a user