------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Berichte')
BEGIN
CREATE TABLE [dbo].[ut_Berichte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Tabelle] char(40) DEFAULT ' '
  ,[Titel] char(40) DEFAULT ' '
  ,[Datei] char(20) DEFAULT ' '
  ,[Output] char(80) DEFAULT ' '
  ,[Nr] char(4) DEFAULT ' '
  ,[Design] tinyint DEFAULT 0
  ,[Engine] integer DEFAULT 0
  CONSTRAINT [Pk_Berichte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Berichte_KeyDel')
  DROP TRIGGER [dbo].[tr_Berichte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Berichte_KeyUpd')
  DROP TRIGGER [dbo].[tr_Berichte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Berichte_Keys')
  DROP TABLE [dbo].[ut_Berichte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Tabelle')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Tabelle] char(40) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Titel')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Titel] char(40) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Datei')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Datei] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Output')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Output] char(80) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Nr] char(4) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Design')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Design] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Engine')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ADD [Engine] integer NOT NULL DEFAULT 0
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Tabelle')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ALTER COLUMN [Tabelle] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Titel')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ALTER COLUMN [Titel] char(40)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Datei')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ALTER COLUMN [Datei] char(20)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Output')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ALTER COLUMN [Output] char(80)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Berichte' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Berichte] ALTER COLUMN [Nr] char(4)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Berichte')
  DROP VIEW [dbo].[Berichte]
GO
CREATE VIEW [dbo].[Berichte] AS
  SELECT RecId
    ,[Tabelle]
    ,[Titel]
    ,[Datei]
    ,[Output]
    ,[Nr]
    ,[Design]
    ,[Engine]
  FROM [dbo].[ut_Berichte]
GO

------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_Berichte', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_Berichte
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Berichte() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Berichte] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Berichte')
  DROP PROCEDURE [dbo].[up_Sel_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Sel_Berichte]
  @RI integer
AS
SELECT
     [Tabelle]
    ,[Titel]
    ,[Datei]
    ,[Output]
    ,[Nr]
    ,[Design]
    ,[Engine]
  FROM [dbo].[ut_Berichte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_Berichte')
  DROP PROCEDURE [dbo].[up_Sel2_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Sel2_Berichte]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[Berichte]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_Berichte')
  DROP PROCEDURE [dbo].[up_Append_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Append_Berichte]
  @Parm_Tabelle char(40)
 ,@Parm_Titel char(40)
 ,@Parm_Datei char(20)
 ,@Parm_Output char(80)
 ,@Parm_Nr char(4)
 ,@Parm_Design tinyint
 ,@Parm_Engine integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Berichte()
      INSERT INTO [dbo].[ut_Berichte] (RecId
       ,[Tabelle]
       ,[Titel]
       ,[Datei]
       ,[Output]
       ,[Nr]
       ,[Design]
       ,[Engine]
         ) VALUES (@NeueId
       ,RTRIM(@Parm_Tabelle)
       ,RTRIM(@Parm_Titel)
       ,RTRIM(@Parm_Datei)
       ,RTRIM(@Parm_Output)
       ,RTRIM(@Parm_Nr)
       ,@Parm_Design
       ,@Parm_Engine
           )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END
  SELECT @NeueId AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Berichte')
  DROP PROCEDURE [dbo].[up_Upd_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Upd_Berichte]
  @RI integer
 ,@Parm_Tabelle char(40)
 ,@Parm_Titel char(40)
 ,@Parm_Datei char(20)
 ,@Parm_Output char(80)
 ,@Parm_Nr char(4)
 ,@Parm_Design tinyint
 ,@Parm_Engine integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Berichte] SET
     [Tabelle] = RTRIM(@Parm_Tabelle)
    ,[Titel] = RTRIM(@Parm_Titel)
    ,[Datei] = RTRIM(@Parm_Datei)
    ,[Output] = RTRIM(@Parm_Output)
    ,[Nr] = RTRIM(@Parm_Nr)
    ,[Design] = @Parm_Design
    ,[Engine] = @Parm_Engine
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Berichte')
  DROP PROCEDURE [dbo].[up_Upd2_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Berichte]
  @Param_RecId varchar(20)
 ,@Param_Tabelle varchar(40)
 ,@Param_Titel varchar(40)
 ,@Param_Datei varchar(20)
 ,@Param_Output varchar(80)
 ,@Param_Nr varchar(4)
 ,@Param_Design varchar(10)
 ,@Param_Engine varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_Berichte] SET
     [Tabelle] = RTRIM(@Param_Tabelle)
    ,[Titel] = RTRIM(@Param_Titel)
    ,[Datei] = RTRIM(@Param_Datei)
    ,[Output] = RTRIM(@Param_Output)
    ,[Nr] = UPPER(RTRIM(@Param_Nr))
    ,[Design] = dbo.itcfunc_integer(@Param_Design)
    ,[Engine] = dbo.itcfunc_float(@Param_Engine)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Berichte] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Berichte')
  DROP PROCEDURE [dbo].[up_TrSel_Berichte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Berichte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Tabelle]))
      +LTRIM(RTRIM([Titel]))
      +LTRIM(RTRIM([Datei]))
      +LTRIM(RTRIM([Output]))
      +LTRIM(RTRIM([Nr]))
    AS Data
  FROM     [dbo].[ut_Berichte]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Berichte')
  DROP PROCEDURE [dbo].[up_Add_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Add_Berichte] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Berichte()
  INSERT INTO [dbo].[ut_Berichte] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Berichte')
  DROP PROCEDURE [dbo].[up_Del_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Del_Berichte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Berichte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Berichte')
  DROP PROCEDURE [dbo].[up_Reorg_Berichte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Berichte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Berichte] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_Berichte] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_Berichte] )
  END
GO


IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Berichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_KEY00')
CREATE INDEX [Ik_KEY00] ON [dbo].[ut_Berichte]
(Nr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_First_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_Last_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_Prior_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_Next_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_Prior50_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_Next50_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_FindL_Berichte_KEY00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Berichte_KEY00')
  DROP PROCEDURE [dbo].[up_FindF_Berichte_KEY00]
GO

------------------------------------------------------------
/* ersten Datensatz aus Berichte in Folge KEY00 ermittln */
CREATE PROCEDURE [dbo].[up_First_Berichte_KEY00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Berichte] ORDER BY Nr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Berichte in Folge KEY00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Berichte_KEY00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Berichte] ORDER BY Nr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Nr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Berichte_KEY00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nr FROM [dbo].[ut_Berichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
                   WHERE Nr = @oldNr AND RecId > @oldRecId
                   ORDER BY Nr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
                   WHERE Nr > @oldNr
                   ORDER BY Nr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Nr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Berichte_KEY00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nr FROM [dbo].[ut_Berichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
                   WHERE Nr = @oldNr AND RecId < @oldRecId
                   ORDER BY Nr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
                   WHERE Nr < @oldNr
                   ORDER BY Nr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Nr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Berichte_KEY00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
      WHERE SUBSTRING(Nr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Nr,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
      WHERE Nr = SUBSTRING(@xKey,1,4)
      ORDER BY Nr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Nr DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Berichte_KEY00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
      WHERE SUBSTRING(Nr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Nr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Berichte]
      WHERE Nr = SUBSTRING(@xKey,1,4)
      ORDER BY Nr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

