------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_KGrpSummenDetails')
BEGIN
CREATE TABLE [dbo].[ut_KGrpSummenDetails]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NrSumme] integer DEFAULT 0
  ,[NrKGrp] integer DEFAULT 0
  ,[bEkMat] tinyint DEFAULT 0
  ,[bVkMat] tinyint DEFAULT 0
  ,[bEkLohn] tinyint DEFAULT 0
  ,[bVkLohn] tinyint DEFAULT 0
  ,[bEkFremd] tinyint DEFAULT 0
  ,[bVkFremd] tinyint DEFAULT 0
  ,[bZeit] tinyint DEFAULT 0
  ,[Kommentar] char(30) DEFAULT ' '
  CONSTRAINT [Pk_KGrpSummenDetails]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenDetails_KeyDel')
  DROP TRIGGER [dbo].[tr_KGrpSummenDetails_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenDetails_KeyUpd')
  DROP TRIGGER [dbo].[tr_KGrpSummenDetails_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_KGrpSummenDetails_Keys')
  DROP TABLE [dbo].[ut_KGrpSummenDetails_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenDetails' and sc.[name] = 'NrSumme')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [NrSumme] integer 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_KGrpSummenDetails' and sc.[name] = 'NrKGrp')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [NrKGrp] integer 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_KGrpSummenDetails' and sc.[name] = 'bEkMat')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bEkMat] 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_KGrpSummenDetails' and sc.[name] = 'bVkMat')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bVkMat] 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_KGrpSummenDetails' and sc.[name] = 'bEkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bEkLohn] 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_KGrpSummenDetails' and sc.[name] = 'bVkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bVkLohn] 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_KGrpSummenDetails' and sc.[name] = 'bEkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bEkFremd] 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_KGrpSummenDetails' and sc.[name] = 'bVkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bVkFremd] 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_KGrpSummenDetails' and sc.[name] = 'bZeit')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [bZeit] 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_KGrpSummenDetails' and sc.[name] = 'Kommentar')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ADD [Kommentar] char(30) NOT NULL DEFAULT ' '
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenDetails' and sc.[name] = 'Kommentar')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenDetails] ALTER COLUMN [Kommentar] char(30)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'KGrpSummenDetails')
  DROP VIEW [dbo].[KGrpSummenDetails]
GO
CREATE VIEW [dbo].[KGrpSummenDetails] AS
  SELECT * FROM [dbo].[ut_KGrpSummenDetails]
GO
CREATE TABLE [dbo].[ut_KGrpSummenDetails_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(16) NOT NULL
  CONSTRAINT [Pk_ut_KGrpSummenDetails_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_KGrpSummenDetails_KeyDel] ON [dbo].[ut_KGrpSummenDetails]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_KGrpSummenDetails_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_KGrpSummenDetails_KeyUpd] ON [dbo].[ut_KGrpSummenDetails]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_KGrpSummenDetails_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_KGrpSummenDetails_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([NrSumme],4,0)+STR([NrKGrp],2,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_KGrpSummenDetails_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([NrSumme],4,0)+STR([NrKGrp],2,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_KGrpSummenDetails] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Add_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Add_KGrpSummenDetails] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_KGrpSummenDetails])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenDetails] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Upd_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Upd_KGrpSummenDetails]
  @RI integer
 ,@Parm_NrSumme integer
 ,@Parm_NrKGrp integer
 ,@Parm_bEkMat tinyint
 ,@Parm_bVkMat tinyint
 ,@Parm_bEkLohn tinyint
 ,@Parm_bVkLohn tinyint
 ,@Parm_bEkFremd tinyint
 ,@Parm_bVkFremd tinyint
 ,@Parm_bZeit tinyint
 ,@Parm_Kommentar char(30)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_KGrpSummenDetails] SET
     [NrSumme] = @Parm_NrSumme
    ,[NrKGrp] = @Parm_NrKGrp
    ,[bEkMat] = @Parm_bEkMat
    ,[bVkMat] = @Parm_bVkMat
    ,[bEkLohn] = @Parm_bEkLohn
    ,[bVkLohn] = @Parm_bVkLohn
    ,[bEkFremd] = @Parm_bEkFremd
    ,[bVkFremd] = @Parm_bVkFremd
    ,[bZeit] = @Parm_bZeit
    ,[Kommentar] = RTRIM(@Parm_Kommentar)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Sel_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Sel_KGrpSummenDetails]
  @RI integer
AS
SELECT
     [NrSumme]
    ,[NrKGrp]
    ,[bEkMat]
    ,[bVkMat]
    ,[bEkLohn]
    ,[bVkLohn]
    ,[bEkFremd]
    ,[bVkFremd]
    ,[bZeit]
    ,[Kommentar]
  FROM [dbo].[ut_KGrpSummenDetails]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Del_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Del_KGrpSummenDetails]
  @RI integer
AS
  DELETE FROM [dbo].[ut_KGrpSummenDetails] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Reorg_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Reorg_KGrpSummenDetails]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_KGrpSummenDetails] 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_KGrpSummenDetails] 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_KGrpSummenDetails] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_Append_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_Append_KGrpSummenDetails]
  @Parm_NrSumme integer
 ,@Parm_NrKGrp integer
 ,@Parm_bEkMat tinyint
 ,@Parm_bVkMat tinyint
 ,@Parm_bEkLohn tinyint
 ,@Parm_bVkLohn tinyint
 ,@Parm_bEkFremd tinyint
 ,@Parm_bVkFremd tinyint
 ,@Parm_bZeit tinyint
 ,@Parm_Kommentar char(30)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_KGrpSummenDetails])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenDetails] (
    RecId
   ,[NrSumme]
   ,[NrKGrp]
   ,[bEkMat]
   ,[bVkMat]
   ,[bEkLohn]
   ,[bVkLohn]
   ,[bEkFremd]
   ,[bVkFremd]
   ,[bZeit]
   ,[Kommentar]
     ) VALUES (
    @NeueId
   ,@Parm_NrSumme
   ,@Parm_NrKGrp
   ,@Parm_bEkMat
   ,@Parm_bVkMat
   ,@Parm_bEkLohn
   ,@Parm_bVkLohn
   ,@Parm_bEkFremd
   ,@Parm_bVkFremd
   ,@Parm_bZeit
   ,RTRIM(@Parm_Kommentar)
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_KGrpSummenDetails')
  DROP PROCEDURE [dbo].[up_TrSel_KGrpSummenDetails]
GO
CREATE PROCEDURE [dbo].[up_TrSel_KGrpSummenDetails]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Kommentar]))
    AS Data
  FROM     [dbo].[ut_KGrpSummenDetails]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_KGrpSummenDetails_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_First_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_Last_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_Prior_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_Next_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_Next50_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_FindL_KGrpSummenDetails_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KGrpSummenDetails_Key00')
  DROP PROCEDURE [dbo].[up_FindF_KGrpSummenDetails_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_KGrpSummenDetails_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_KGrpSummenDetails_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_KGrpSummenDetails_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_KGrpSummenDetails_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_KGrpSummenDetails_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_KGrpSummenDetails_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_KGrpSummenDetails_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_KGrpSummenDetails_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_KGrpSummenDetails_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_KGrpSummenDetails_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_KGrpSummenDetails_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_KGrpSummenDetails_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_KGrpSummenDetails_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenDetails_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

