------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_KGrpSummenTypen')
BEGIN
CREATE TABLE [dbo].[ut_KGrpSummenTypen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NrSumme] integer DEFAULT 0
  ,[Bez1] char(80) DEFAULT ' '
  ,[LohnAnt1] float DEFAULT 0
  ,[LohnAnt2] float DEFAULT 0
  CONSTRAINT [Pk_KGrpSummenTypen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenTypen_KeyDel')
  DROP TRIGGER [dbo].[tr_KGrpSummenTypen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenTypen_KeyUpd')
  DROP TRIGGER [dbo].[tr_KGrpSummenTypen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_KGrpSummenTypen_Keys')
  DROP TABLE [dbo].[ut_KGrpSummenTypen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenTypen' and sc.[name] = 'NrSumme')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenTypen] 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_KGrpSummenTypen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenTypen] ADD [Bez1] 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_KGrpSummenTypen' and sc.[name] = 'LohnAnt1')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenTypen] ADD [LohnAnt1] float 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_KGrpSummenTypen' and sc.[name] = 'LohnAnt2')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenTypen] ADD [LohnAnt2] float NOT NULL DEFAULT 0
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenTypen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenTypen] ALTER COLUMN [Bez1] char(80)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'KGrpSummenTypen')
  DROP VIEW [dbo].[KGrpSummenTypen]
GO
CREATE VIEW [dbo].[KGrpSummenTypen] AS
  SELECT * FROM [dbo].[ut_KGrpSummenTypen]
GO
CREATE TABLE [dbo].[ut_KGrpSummenTypen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(14) NOT NULL
  CONSTRAINT [Pk_ut_KGrpSummenTypen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_KGrpSummenTypen_KeyDel] ON [dbo].[ut_KGrpSummenTypen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_KGrpSummenTypen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Add_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Add_KGrpSummenTypen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_KGrpSummenTypen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenTypen] (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_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Upd_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Upd_KGrpSummenTypen]
  @RI integer
 ,@Parm_NrSumme integer
 ,@Parm_Bez1 char(80)
 ,@Parm_LohnAnt1 float
 ,@Parm_LohnAnt2 float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_KGrpSummenTypen] SET
     [NrSumme] = @Parm_NrSumme
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[LohnAnt1] = @Parm_LohnAnt1
    ,[LohnAnt2] = @Parm_LohnAnt2
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Sel_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Sel_KGrpSummenTypen]
  @RI integer
AS
SELECT
     [NrSumme]
    ,[Bez1]
    ,[LohnAnt1]
    ,[LohnAnt2]
  FROM [dbo].[ut_KGrpSummenTypen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Del_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Del_KGrpSummenTypen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_KGrpSummenTypen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Reorg_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_KGrpSummenTypen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_KGrpSummenTypen] 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_KGrpSummenTypen] 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_KGrpSummenTypen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_Append_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_Append_KGrpSummenTypen]
  @Parm_NrSumme integer
 ,@Parm_Bez1 char(80)
 ,@Parm_LohnAnt1 float
 ,@Parm_LohnAnt2 float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_KGrpSummenTypen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenTypen] (
    RecId
   ,[NrSumme]
   ,[Bez1]
   ,[LohnAnt1]
   ,[LohnAnt2]
     ) VALUES (
    @NeueId
   ,@Parm_NrSumme
   ,RTRIM(@Parm_Bez1)
   ,@Parm_LohnAnt1
   ,@Parm_LohnAnt2
       )
  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_KGrpSummenTypen')
  DROP PROCEDURE [dbo].[up_TrSel_KGrpSummenTypen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_KGrpSummenTypen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Bez1]))
    AS Data
  FROM     [dbo].[ut_KGrpSummenTypen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_KGrpSummenTypen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_First_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_Last_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_Next_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_KGrpSummenTypen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KGrpSummenTypen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_KGrpSummenTypen_Key00]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_KGrpSummenTypen_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_KGrpSummenTypen_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_KGrpSummenTypen_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenTypen_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

