alter table dbo.personal add EMail varchar(255)
go
update dbo.personal set email='';
go
alter table dbo.Temporaer_Berechtigung add Kontaktid INT
update Temporaer_Berechtigungset Kontaktid=0
go
USE [bea_prod]
GO

/****** Object:  Table [dbo].[MailLog]    Script Date: 24.05.2021 13:41:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[MailLog](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Sendedatum] [datetime] NULL,
	[Empfaenger] [varchar](255) NULL,
	[MailTyp] [int] NULL,
	[Betreff] [varchar](255) NULL,
	[Inhalt] [varchar](2048) NULL,
 CONSTRAINT [PK_MailLog] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [Default]
) ON [Default]
GO

/****** Object:  Table [dbo].[MailTexte]    Script Date: 24.05.2021 13:41:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[MailTexte](
	[id] [int] NOT NULL,
	[Beschreibung] [varchar](255) NULL,
	[Betreff] [varchar](255) NULL,
	[Inhalt] [varchar](2048) NULL,
	[erstellt_am] [datetime] NULL,
	[mutiert_am] [datetime] NULL,
	[mutierer] [int] NULL,
	[aktiv] [bit] NULL,
 CONSTRAINT [PK_MailTexte] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [Default]
) ON [Default]
GO

/****** Object:  Table [dbo].[ADLog]    Script Date: 24.05.2021 13:41:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ADLog](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Eintrag] [varchar](max) NULL,
	[Zeit] [datetime] NULL,
 CONSTRAINT [PK_ADLog] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [Default]
) ON [Default] TEXTIMAGE_ON [Default]
GO



/****** Object:  StoredProcedure [dbo].[pr_Personal_Insert]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_Personal_Insert]
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_Update]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_Personal_Update]
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_Delete]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_Personal_Delete]
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_SelectOne]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_Personal_SelectOne]
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_SelectAll]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_Personal_SelectAll]
GO

/****** Object:  StoredProcedure [dbo].[sp_set_Temporaerberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[sp_set_Temporaerberechtigung]
GO

/****** Object:  StoredProcedure [dbo].[sp_get_temporaerberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[sp_get_temporaerberechtigung]
GO

/****** Object:  StoredProcedure [dbo].[sp_get_tempberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[sp_get_tempberechtigung]
GO

/****** Object:  StoredProcedure [dbo].[sp_admin]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[sp_admin]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Insert]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_MailTexte_Insert]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Update]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_MailTexte_Update]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Delete]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_MailTexte_Delete]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_SelectOne]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_MailTexte_SelectOne]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_SelectAll]    Script Date: 24.05.2021 13:39:23 ******/
DROP PROCEDURE [dbo].[pr_MailTexte_SelectAll]
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_SelectAll]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will select all rows from the table 'MailTexte'
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_MailTexte_SelectAll]
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- SELECT all rows from the table.
SELECT
	[id],
	[Beschreibung],
	[Betreff],
	[Inhalt],
	[erstellt_am],
	[mutiert_am],
	[mutierer],
	[aktiv]
FROM [dbo].[MailTexte]
ORDER BY 
	[id] ASC
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_SelectOne]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will select an existing row from the table 'MailTexte'
-- based on the Primary Key.
-- Gets: @iid int
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_MailTexte_SelectOne]
	@iid int,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- SELECT an existing row from the table.
SELECT
	[id],
	[Beschreibung],
	[Betreff],
	[Inhalt],
	[erstellt_am],
	[mutiert_am],
	[mutierer],
	[aktiv]
FROM [dbo].[MailTexte]
WHERE
	[id] = @iid
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Delete]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will delete an existing row from the table 'MailTexte'
-- using the Primary Key. 
-- Gets: @iid int
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_MailTexte_Delete]
	@iid int,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- DELETE an existing row from the table.
DELETE FROM [dbo].[MailTexte]
WHERE
	[id] = @iid
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Update]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will update an existing row in the table 'MailTexte'
-- Gets: @iid int
-- Gets: @sBeschreibung varchar(255)
-- Gets: @sBetreff varchar(255)
-- Gets: @sInhalt varchar(2048)
-- Gets: @daerstellt_am datetime
-- Gets: @damutiert_am datetime
-- Gets: @imutierer int
-- Gets: @baktiv bit
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_MailTexte_Update]
	@iid int,
	@sBeschreibung varchar(255),
	@sBetreff varchar(255),
	@sInhalt varchar(2048),
	@daerstellt_am datetime,
	@damutiert_am datetime,
	@imutierer int,
	@baktiv bit,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- UPDATE an existing row in the table.
UPDATE [dbo].[MailTexte]
SET 
	[Beschreibung] = @sBeschreibung,
	[Betreff] = @sBetreff,
	[Inhalt] = @sInhalt,
	[erstellt_am] = @daerstellt_am,
	[mutiert_am] = @damutiert_am,
	[mutierer] = @imutierer,
	[aktiv] = @baktiv
WHERE
	[id] = @iid
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_MailTexte_Insert]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will insert 1 row in the table 'MailTexte'
-- Gets: @iid int
-- Gets: @sBeschreibung varchar(255)
-- Gets: @sBetreff varchar(255)
-- Gets: @sInhalt varchar(2048)
-- Gets: @daerstellt_am datetime
-- Gets: @damutiert_am datetime
-- Gets: @imutierer int
-- Gets: @baktiv bit
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_MailTexte_Insert]
	@iid int,
	@sBeschreibung varchar(255),
	@sBetreff varchar(255),
	@sInhalt varchar(2048),
	@daerstellt_am datetime,
	@damutiert_am datetime,
	@imutierer int,
	@baktiv bit,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- INSERT a new row in the table.
INSERT [dbo].[MailTexte]
(
	[id],
	[Beschreibung],
	[Betreff],
	[Inhalt],
	[erstellt_am],
	[mutiert_am],
	[mutierer],
	[aktiv]
)
VALUES
(
	@iid,
	@sBeschreibung,
	@sBetreff,
	@sInhalt,
	@daerstellt_am,
	@damutiert_am,
	@imutierer,
	@baktiv
)
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[sp_admin]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_admin]
AS
BEGIN
	SET NOCOUNT ON;

	create table #tmp1(
		 [Datatype] [varchar](14) NOT NULL
		,[Personalnr] [int] NOT NULL
		,[Bezeichnung] [varchar](255) NULL
		,[Name] [varchar](255) NULL
		,[FunktionstelleNr] [int] NOT NULL
		,[Expr1] [varchar](255) NULL
		,[Beschreibung] [varchar](1024) NULL
		,[Unterlagen] [varchar](1024) NULL
		,[Bearbeiter_ISI] [varchar](50) NULL
		,[Kontaktperson_FB] [varchar](255) NULL
		,[Termin] [datetime] NULL
		,[aktiv] [bit] NULL
		,[erstellt_am] [datetime] NULL
		,[mutiert_am] [datetime] NULL
		,[mutierer] [int] NULL
		,[Funktionsstelle] [varchar](255) NULL
		,[NrTempBerechtigung] [int] NOT NULL
		)
	DECLARE @tmp2 TABLE (
		Beschreibung VARCHAR(max)
		,TGNummer VARCHAR(max)
		,Name VARCHAR(max)
		,Termin DATETIME
		,Empfaenger_TG VARCHAR(max)
		,Empfaenger_Name VARCHAR(max)
		,Empfaenger_Mail VARCHAR(max)
		,Tage INT
		,fnktstelle VARCHAR(max)
		)

	INSERT #tmp1
	EXEC sp_get_tempberechtigung

	INSERT @tmp2
	SELECT dbo.Temporaer_Berechtigung.Beschreibung
		,dbo.Personal.TGNummer
		,dbo.Personal.Name
		,dbo.Temporaer_Berechtigung.Termin
		,Personal_1.TGNummer AS Empfaenger_TG
		,Personal_1.Name AS Empfaenger_Name
		,Personal_1.EMail AS Empfaenger_Mail
		,DATEDIFF(dd, GETDATE(), 
		 dbo.Temporaer_Berechtigung.Termin) AS Tage
	    ,#tmp1.Expr1
	FROM dbo.Personal
	INNER JOIN dbo.Personal_FunktionStelle ON dbo.Personal.Personalnr = dbo.Personal_FunktionStelle.PersonalNr
	INNER JOIN dbo.Temporaer_Berechtigung ON dbo.Personal_FunktionStelle.Personal_FunktionStelleNr = dbo.Temporaer_Berechtigung.Personal_FunktionStelleNr
	INNER JOIN #tmp1 ON dbo.Temporaer_Berechtigung.NrTempBerechtigung = #tmp1.NrTempBerechtigung
	LEFT OUTER JOIN dbo.Personal AS Personal_1 ON dbo.Temporaer_Berechtigung.KontaktID = Personal_1.Personalnr
	WHERE (dbo.Temporaer_Berechtigung.aktiv = 1)
		AND (NOT (Personal_1.EMail IS NULL))
		AND (dbo.Personal_FunktionStelle.Aktiv = 1)

	DECLARE @beschreibung VARCHAR(255)
	DECLARE @tgnummer VARCHAR(255)
	DECLARE @name VARCHAR(255)
	DECLARE @termin DATETIME
	DECLARE @empfaenger_tg VARCHAR(255)
	DECLARE @empfaenger_name VARCHAR(255)
	DECLARE @empfaenger_mail VARCHAR(255)
	DECLARE @mailinhalt VARCHAR(2048)
	DECLARE @betreff VARCHAR(255)
	DECLARE @body VARCHAR(2048)
	DECLARE @subject VARCHAR(255)
	DECLARE @termintext VARCHAR(10)
	DECLARE @fnktStelle VARCHAR(255)

	SELECT @mailinhalt = inhalt
		,@betreff = betreff
	FROM mailtexte
	WHERE ID = 2

	DECLARE xc CURSOR
	FOR
	SELECT Beschreibung
		,TGNummer
		,Name
		,Termin
		,Empfaenger_TG
		,Empfaenger_Name
		,Empfaenger_Mail
		,fnktstelle
	FROM @tmp2
	WHERE tage = 14

	OPEN xc

	FETCH NEXT
	FROM xc
	INTO @beschreibung
		,@tgnummer
		,@name
		,@termin
		,@empfaenger_tg
		,@empfaenger_name
		,@empfaenger_mail
		,@fnktstelle

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @body = @mailinhalt
		SET @subject = @betreff
		SET @termintext = convert(VARCHAR, @termin, 104)
		SET @body = REPLACE(@body, '@EmpfaengerName', @empfaenger_name)
		SET @body = REPLACE(@body, '@TGNummer', @tgnummer)
		SET @body = REPLACE(@body, '@Name', @Name)
		SET @body = REPLACE(@body, '@Termin', @termintext)
		SET @body = REPLACE(@body, '@Fnktstelle', @fnktstelle)
		SET @subject = REPLACE(@subject, '@EmpfaengerName', @empfaenger_name)
		SET @subject = REPLACE(@subject, '@TGNummer', @tgnummer)
		SET @subject = REPLACE(@subject, '@Name', @Name)
		SET @subject = REPLACE(@subject, '@Termin', @termintext)
		SET @subject = REPLACE(@subject, '@Fnktstelle', @fnktstelle)

		INSERT INTO [dbo].[MailLog] (
			[Sendedatum]
			,[Empfaenger]
			,[MailTyp]
			,[Betreff]
			,[Inhalt]
			)
		VALUES (
			getdate()
			,@empfaenger_mail
			,1
			,@subject
			,@body
			)

		FETCH NEXT
		FROM xc
		INTO @beschreibung
			,@tgnummer
			,@name
			,@termin
			,@empfaenger_tg
			,@empfaenger_name
			,@empfaenger_mail
			,@fnktstelle
	END

	CLOSE xc

	DEALLOCATE xc

	SELECT @mailinhalt = inhalt
		,@betreff = betreff
	FROM mailtexte
	WHERE ID = 3

	DECLARE xc CURSOR
	FOR
	SELECT Beschreibung
		,TGNummer
		,Name
		,Termin
		,Empfaenger_TG
		,Empfaenger_Name
		,Empfaenger_Mail
		,fnktstelle
	FROM @tmp2
	WHERE tage = 5

	OPEN xc

	FETCH NEXT
	FROM xc
	INTO @beschreibung
		,@tgnummer
		,@name
		,@termin
		,@empfaenger_tg
		,@empfaenger_name
		,@empfaenger_mail
		,@fnktstelle

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @body = @mailinhalt
		SET @subject = @betreff
		SET @termintext = convert(VARCHAR, @termin, 104)
		SET @body = REPLACE(@body, '@EmpfaengerName', @empfaenger_name)
		SET @body = REPLACE(@body, '@TGNummer', @tgnummer)
		SET @body = REPLACE(@body, '@Name', @Name)
		SET @body = REPLACE(@body, '@Termin', @termintext)
		SET @body = REPLACE(@body, '@Fnktstelle', @fnktstelle)
		SET @subject = REPLACE(@subject, '@EmpfaengerName', @empfaenger_name)
		SET @subject = REPLACE(@subject, '@TGNummer', @tgnummer)
		SET @subject = REPLACE(@subject, '@Name', @Name)
		SET @subject = REPLACE(@subject, '@Termin', @termintext)
		SET @subject = REPLACE(@subject, '@Fnktstelle', @fnktstelle)

		INSERT INTO [dbo].[MailLog] (
			[Sendedatum]
			,[Empfaenger]
			,[MailTyp]
			,[Betreff]
			,[Inhalt]
			)
		VALUES (
			getdate()
			,@empfaenger_mail
			,1
			,@subject
			,@body
			)

		FETCH NEXT
		FROM xc
		INTO @beschreibung
			,@tgnummer
			,@name
			,@termin
			,@empfaenger_tg
			,@empfaenger_name
			,@empfaenger_mail
			,@fnktstelle
	END

	CLOSE xc

	DEALLOCATE xc

	drop table #tmp1
END
GO

/****** Object:  StoredProcedure [dbo].[sp_get_tempberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:          <Author,,Name>
-- Create date: <Create Date,,>
-- Description:     <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_get_tempberechtigung]
AS
BEGIN
	SET NOCOUNT ON;

	SELECT 'Funktionstelle' AS Datatype
		,dbo.Personal.Personalnr
		,dbo.Personal.Bezeichnung
		,dbo.Personal.Name
		,dbo.Funktionstelle.FunktionstelleNr
		,dbo.Funktionstelle.Bezeichnung AS Expr1
		,dbo.Temporaer_Berechtigung.Beschreibung
		,dbo.Temporaer_Berechtigung.Unterlagen
		,dbo.Temporaer_Berechtigung.Bearbeiter_ISI
		,
		--dbo.Temporaer_Berechtigung.Kontaktperson_FB ,
		Personal_1.Name AS Kontaktperson_FB
		,dbo.Temporaer_Berechtigung.Termin
		,dbo.Temporaer_Berechtigung.aktiv
		,dbo.Temporaer_Berechtigung.erstellt_am
		,dbo.Temporaer_Berechtigung.mutiert_am
		,dbo.Temporaer_Berechtigung.mutierer
		,dbo.Funktionstelle.Funktionsstelle
		,dbo.Temporaer_Berechtigung.NrTempBerechtigung
	INTO #tmp1
	FROM dbo.Temporaer_Berechtigung
	INNER JOIN dbo.Personal_FunktionStelle ON dbo.Temporaer_Berechtigung.Personal_FunktionStelleNr = dbo.Personal_FunktionStelle.Personal_FunktionStelleNr
	INNER JOIN dbo.Personal ON dbo.Personal_FunktionStelle.PersonalNr = dbo.Personal.Personalnr
	INNER JOIN dbo.Funktionstelle ON dbo.Personal_FunktionStelle.FunktionStelleNr = dbo.Funktionstelle.FunktionstelleNr
	LEFT OUTER JOIN dbo.Personal AS Personal_1 ON dbo.Temporaer_Berechtigung.KontaktID = Personal_1.Personalnr
	WHERE datatype = 0
	ORDER BY dbo.Temporaer_Berechtigung.Termin

	INSERT #tmp1
	SELECT 'Berechtigung' AS Datatype
		,dbo.Personal.Personalnr
		,dbo.Personal.Bezeichnung
		,dbo.Personal.Name
		,dbo.Berechtigung.BerechtigungNr
		,dbo.Berechtigung.Bezeichnung AS Expr1
		,dbo.Temporaer_Berechtigung.Beschreibung
		,dbo.Temporaer_Berechtigung.Unterlagen
		,dbo.Temporaer_Berechtigung.Bearbeiter_ISI
		,Personal_1.Name AS Kontaktperson_FB
		,dbo.Temporaer_Berechtigung.Termin
		,dbo.Temporaer_Berechtigung.aktiv
		,dbo.Temporaer_Berechtigung.erstellt_am
		,dbo.Temporaer_Berechtigung.mutiert_am
		,dbo.Temporaer_Berechtigung.mutierer
		,'' AS Expr3
		,dbo.Temporaer_Berechtigung.NrTempBerechtigung
		
	FROM dbo.Temporaer_Berechtigung
	INNER JOIN dbo.Personal_Berechtigung ON dbo.Temporaer_Berechtigung.Personal_FunktionStelleNr = dbo.Personal_Berechtigung.Personal_BerechtigungNr
	INNER JOIN dbo.Personal ON dbo.Personal_Berechtigung.PersonalNr = dbo.Personal.Personalnr
	INNER JOIN dbo.Berechtigung ON dbo.Personal_Berechtigung.BerechtigungNr = dbo.Berechtigung.BerechtigungNr
	LEFT OUTER JOIN dbo.Personal AS Personal_1 ON dbo.Temporaer_Berechtigung.KontaktID = Personal_1.Personalnr
	WHERE (dbo.Temporaer_Berechtigung.datatype = 1)
	ORDER BY dbo.Temporaer_Berechtigung.Termin

	INSERT #tmp1
	SELECT 'Plattform' AS Datatype
		,dbo.Personal.Personalnr
		,dbo.Personal.Bezeichnung
		,dbo.Personal.Name
		,dbo.Plattform.PlattformNr
		,dbo.Plattform.Bezeichnung AS Expr1
		,dbo.Temporaer_Berechtigung.Beschreibung
		,dbo.Temporaer_Berechtigung.Unterlagen
		,dbo.Temporaer_Berechtigung.Bearbeiter_ISI
		,
		--dbo.Temporaer_Berechtigung.Kontaktperson_FB ,
		Personal_1.Name AS Kontaktperson_FB
		,dbo.Temporaer_Berechtigung.Termin
		,dbo.Temporaer_Berechtigung.aktiv
		,dbo.Temporaer_Berechtigung.erstellt_am
		,dbo.Temporaer_Berechtigung.mutiert_am
		,dbo.Temporaer_Berechtigung.mutierer
		,''
		,dbo.Temporaer_Berechtigung.NrTempBerechtigung
	FROM dbo.Temporaer_Berechtigung
	INNER JOIN dbo.Personal_Plattform ON dbo.Temporaer_Berechtigung.Personal_FunktionStelleNr = dbo.Personal_Plattform.Personal_PlattformNr
	INNER JOIN dbo.Personal ON dbo.Personal_Plattform.PersonalNr = dbo.Personal.Personalnr
	INNER JOIN dbo.Plattform ON dbo.Personal_Plattform.PlattformNr = dbo.Plattform.PlattformNr
	LEFT OUTER JOIN dbo.Personal AS Personal_1 ON dbo.Temporaer_Berechtigung.KontaktID = Personal_1.Personalnr
	WHERE datatype = 2
	ORDER BY dbo.Temporaer_Berechtigung.Termin

	SELECT * 
	FROM #tmp1
	ORDER BY Termin
END
GO

/****** Object:  StoredProcedure [dbo].[sp_get_temporaerberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:          <Author,,Name>
-- Create date: <Create Date,,>
-- Description:     <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_get_temporaerberechtigung]
@keyvalue int
AS
BEGIN
SELECT        dbo.Temporaer_Berechtigung.NrTempBerechtigung, dbo.Temporaer_Berechtigung.Personal_FunktionStelleNr, dbo.Temporaer_Berechtigung.Beschreibung, dbo.Temporaer_Berechtigung.Unterlagen, 
                         dbo.Temporaer_Berechtigung.Bearbeiter_ISI, 
						 dbo.Temporaer_Berechtigung.Termin, dbo.Temporaer_Berechtigung.aktiv, dbo.Temporaer_Berechtigung.erstellt_am, 
                         dbo.Temporaer_Berechtigung.mutiert_am, dbo.Temporaer_Berechtigung.mutierer, dbo.Temporaer_Berechtigung.datatype, dbo.Temporaer_Berechtigung.KontaktID, dbo.Personal.Bezeichnung as KontaktTG, dbo.Personal.Name as  Kontaktperson_FB
FROM            dbo.Temporaer_Berechtigung LEFT OUTER JOIN
                         dbo.Personal ON dbo.Temporaer_Berechtigung.KontaktID = dbo.Personal.Personalnr
						 WHERE Personal_FunktionStelleNr=@keyvalue AND dbo.Temporaer_Berechtigung.aktiv=1
   --SELECT * FROM dbo.Temporaer_Berechtigung WHERE Personal_FunktionStelleNr=@keyvalue AND aktiv=1
END


GO

/****** Object:  StoredProcedure [dbo].[sp_set_Temporaerberechtigung]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:          <Author,,Name>
-- Create date: <Create Date,,>
-- Description:     <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_set_Temporaerberechtigung] 
@beschreibung VARCHAR(1024),
@unterlagen VARCHAR(1024),
@bearbeiter varchar(50),
@kontkat varchar(255),
@termin DATETIME,
@keyvalue INT,
@mitarbeiternr INT,
@datatype int,
@kontaktid int

AS
BEGIN
       SET NOCOUNT ON;
    declare @rc int
       SELECT @rc=COUNT(*) FROM dbo.Temporaer_Berechtigung WHERE aktiv=1 AND Personal_FunktionStelleNr=@keyvalue
       IF @rc=0 BEGIN
  INSERT dbo.Temporaer_Berechtigung
          ( Personal_FunktionStelleNr ,
            Beschreibung ,
            Unterlagen ,
            Bearbeiter_ISI ,
            Kontaktperson_FB ,
            Termin ,
            aktiv ,
            erstellt_am ,
            mutiert_am ,
            mutierer,
                    datatype, kontaktid
          )
  VALUES  ( @keyvalue , -- Personal_FunktionStelleNr - int
            @beschreibung , -- Beschreibung - varchar(1024)
            @unterlagen , -- Unterlagen - varchar(1024)
            @bearbeiter , -- Bearbeite_ISI - varchar(50)
            @kontkat , -- Kontaktperson_FB - varchar(255)
            @termin , -- Termin - varchar(255)
            1, -- aktv - bit
            GETDATE() , -- erstellt_am - datetime
            GETDATE() , -- mutiert_am - datetime
            @mitarbeiternr,  -- mutierer - int
                    @datatype,
					@kontaktid
          )  
  END ELSE BEGIN
  UPDATE dbo.Temporaer_Berechtigung SET Beschreibung=@beschreibung, Unterlagen=@unterlagen,Bearbeiter_ISI=@bearbeiter,Kontaktperson_FB=@kontkat,
  mutierer=@mitarbeiternr,Termin=@termin, mutiert_am=GETDATE(),kontaktid=@kontaktid
  WHERE Personal_FunktionStelleNr=@keyvalue
  

  end  
END


GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_SelectAll]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will select all rows from the table 'Personal'
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_Personal_SelectAll]
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- SELECT all rows from the table.
SELECT
	[Personalnr],
	[ParentID],
	[Bezeichnung],
	[Beschreibung],
	[TGNummer],
	[Name],
	[Sequenz],
	[Aktiv],
	[Erstellt_am],
	[Mutiert_am],
	[Mutierer],
	[VerantwTKBMA],
	[NextControlDate],
	[EMail]
FROM [dbo].[Personal]
ORDER BY 
	[Personalnr] ASC
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_SelectOne]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will select an existing row from the table 'Personal'
-- based on the Primary Key.
-- Gets: @iPersonalnr int
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_Personal_SelectOne]
	@iPersonalnr int,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- SELECT an existing row from the table.
SELECT
	[Personalnr],
	[ParentID],
	[Bezeichnung],
	[Beschreibung],
	[TGNummer],
	[Name],
	[Sequenz],
	[Aktiv],
	[Erstellt_am],
	[Mutiert_am],
	[Mutierer],
	[VerantwTKBMA],
	[NextControlDate],
	[EMail]
FROM [dbo].[Personal]
WHERE
	[Personalnr] = @iPersonalnr
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_Delete]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will delete an existing row from the table 'Personal'
-- using the Primary Key. 
-- Gets: @iPersonalnr int
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_Personal_Delete]
	@iPersonalnr int,
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- DELETE an existing row from the table.
DELETE FROM [dbo].[Personal]
WHERE
	[Personalnr] = @iPersonalnr
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_Update]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will update an existing row in the table 'Personal'
-- Gets: @iPersonalnr int
-- Gets: @iParentID int
-- Gets: @sBezeichnung varchar(255)
-- Gets: @sBeschreibung varchar(255)
-- Gets: @sTGNummer varchar(255)
-- Gets: @sName varchar(255)
-- Gets: @iSequenz int
-- Gets: @bAktiv bit
-- Gets: @daErstellt_am datetime
-- Gets: @daMutiert_am datetime
-- Gets: @iMutierer int
-- Gets: @sVerantwTKBMA varchar(255)
-- Gets: @sNextControlDate varchar(255)
-- Gets: @sEMail varchar(-1)
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_Personal_Update]
	@iPersonalnr int,
	@iParentID int,
	@sBezeichnung varchar(255),
	@sBeschreibung varchar(255),
	@sTGNummer varchar(255),
	@sName varchar(255),
	@iSequenz int,
	@bAktiv bit,
	@daErstellt_am datetime,
	@daMutiert_am datetime,
	@iMutierer int,
	@sVerantwTKBMA varchar(255),
	@sNextControlDate varchar(255),
	@sEMail varchar(255),
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- UPDATE an existing row in the table.
UPDATE [dbo].[Personal]
SET 
	[ParentID] = @iParentID,
	[Bezeichnung] = @sBezeichnung,
	[Beschreibung] = @sBeschreibung,
	[TGNummer] = @sTGNummer,
	[Name] = @sName,
	[Sequenz] = @iSequenz,
	[Aktiv] = @bAktiv,
	[Erstellt_am] = @daErstellt_am,
	[Mutiert_am] = @daMutiert_am,
	[Mutierer] = @iMutierer,
	[VerantwTKBMA] = @sVerantwTKBMA,
	[NextControlDate] = @sNextControlDate,
	[EMail] = @sEMail
WHERE
	[Personalnr] = @iPersonalnr
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO

/****** Object:  StoredProcedure [dbo].[pr_Personal_Insert]    Script Date: 24.05.2021 13:39:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


---------------------------------------------------------------------------------
-- Stored procedure that will insert 1 row in the table 'Personal'
-- Gets: @iPersonalnr int
-- Gets: @iParentID int
-- Gets: @sBezeichnung varchar(255)
-- Gets: @sBeschreibung varchar(255)
-- Gets: @sTGNummer varchar(255)
-- Gets: @sName varchar(255)
-- Gets: @iSequenz int
-- Gets: @bAktiv bit
-- Gets: @daErstellt_am datetime
-- Gets: @daMutiert_am datetime
-- Gets: @iMutierer int
-- Gets: @sVerantwTKBMA varchar(255)
-- Gets: @sNextControlDate varchar(255)
-- Gets: @sEMail varchar(-1)
-- Returns: @iErrorCode int
---------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[pr_Personal_Insert]
	@iPersonalnr int,
	@iParentID int,
	@sBezeichnung varchar(255),
	@sBeschreibung varchar(255),
	@sTGNummer varchar(255),
	@sName varchar(255),
	@iSequenz int,
	@bAktiv bit,
	@daErstellt_am datetime,
	@daMutiert_am datetime,
	@iMutierer int,
	@sVerantwTKBMA varchar(255),
	@sNextControlDate varchar(255),
	@sEMail varchar(255),
	@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- INSERT a new row in the table.
INSERT [dbo].[Personal]
(
	[Personalnr],
	[ParentID],
	[Bezeichnung],
	[Beschreibung],
	[TGNummer],
	[Name],
	[Sequenz],
	[Aktiv],
	[Erstellt_am],
	[Mutiert_am],
	[Mutierer],
	[VerantwTKBMA],
	[NextControlDate],
	[EMail]
)
VALUES
(
	@iPersonalnr,
	@iParentID,
	@sBezeichnung,
	@sBeschreibung,
	@sTGNummer,
	@sName,
	@iSequenz,
	@bAktiv,
	@daErstellt_am,
	@daMutiert_am,
	@iMutierer,
	@sVerantwTKBMA,
	@sNextControlDate,
	@sEMail
)
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
GO


