------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_KGrpSummenWerte')
BEGIN
CREATE TABLE [dbo].[ut_KGrpSummenWerte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[NrSumme] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Bez1] char(80) DEFAULT ' '
  ,[EkMat] float DEFAULT 0
  ,[VkMat] float DEFAULT 0
  ,[EkLohn] float DEFAULT 0
  ,[VkLohn] float DEFAULT 0
  ,[EkFremd] float DEFAULT 0
  ,[VkFremd] float DEFAULT 0
  ,[Zeit] float DEFAULT 0
  CONSTRAINT [Pk_KGrpSummenWerte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenWerte_KeyDel')
  DROP TRIGGER [dbo].[tr_KGrpSummenWerte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KGrpSummenWerte_KeyUpd')
  DROP TRIGGER [dbo].[tr_KGrpSummenWerte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_KGrpSummenWerte_Keys')
  DROP TABLE [dbo].[ut_KGrpSummenWerte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [PrjNr] char(8) 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_KGrpSummenWerte' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [BelArt] char(2) 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_KGrpSummenWerte' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [BelNr] char(8) 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_KGrpSummenWerte' and sc.[name] = 'NrSumme')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] 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_KGrpSummenWerte' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] 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_KGrpSummenWerte' and sc.[name] = 'EkMat')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [EkMat] 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_KGrpSummenWerte' and sc.[name] = 'VkMat')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [VkMat] 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_KGrpSummenWerte' and sc.[name] = 'EkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [EkLohn] 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_KGrpSummenWerte' and sc.[name] = 'VkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [VkLohn] 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_KGrpSummenWerte' and sc.[name] = 'EkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [EkFremd] 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_KGrpSummenWerte' and sc.[name] = 'VkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [VkFremd] 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_KGrpSummenWerte' and sc.[name] = 'Zeit')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ADD [Zeit] float NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ALTER COLUMN [BelNr] char(8)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_KGrpSummenWerte' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_KGrpSummenWerte] ALTER COLUMN [Bez1] char(80)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'KGrpSummenWerte')
  DROP VIEW [dbo].[KGrpSummenWerte]
GO
CREATE VIEW [dbo].[KGrpSummenWerte] AS
  SELECT * FROM [dbo].[ut_KGrpSummenWerte]
GO
CREATE TABLE [dbo].[ut_KGrpSummenWerte_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(32) NOT NULL
  ,[Key01_Value] CHAR(32) NOT NULL
  CONSTRAINT [Pk_ut_KGrpSummenWerte_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_KGrpSummenWerte_KeyDel] ON [dbo].[ut_KGrpSummenWerte]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_KGrpSummenWerte_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_KGrpSummenWerte_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([NrSumme],4,0)+STR(RecId,10))
     ,UPPER(STR([NrSumme],4,0)+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_KGrpSummenWerte_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([NrSumme],4,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(STR([NrSumme],4,0)+convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Add_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Add_KGrpSummenWerte] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_KGrpSummenWerte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenWerte] (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_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Upd_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Upd_KGrpSummenWerte]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_NrSumme integer
 ,@Parm_Datum datetime
 ,@Parm_Bez1 char(80)
 ,@Parm_EkMat float
 ,@Parm_VkMat float
 ,@Parm_EkLohn float
 ,@Parm_VkLohn float
 ,@Parm_EkFremd float
 ,@Parm_VkFremd float
 ,@Parm_Zeit float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_KGrpSummenWerte] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[NrSumme] = @Parm_NrSumme
    ,[Datum] = @Parm_Datum
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[EkMat] = @Parm_EkMat
    ,[VkMat] = @Parm_VkMat
    ,[EkLohn] = @Parm_EkLohn
    ,[VkLohn] = @Parm_VkLohn
    ,[EkFremd] = @Parm_EkFremd
    ,[VkFremd] = @Parm_VkFremd
    ,[Zeit] = @Parm_Zeit
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Sel_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Sel_KGrpSummenWerte]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[NrSumme]
    ,[Datum]
    ,[Bez1]
    ,[EkMat]
    ,[VkMat]
    ,[EkLohn]
    ,[VkLohn]
    ,[EkFremd]
    ,[VkFremd]
    ,[Zeit]
  FROM [dbo].[ut_KGrpSummenWerte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Del_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Del_KGrpSummenWerte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_KGrpSummenWerte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Reorg_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_KGrpSummenWerte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_KGrpSummenWerte] 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_KGrpSummenWerte] 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_KGrpSummenWerte] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_Append_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_Append_KGrpSummenWerte]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_NrSumme integer
 ,@Parm_Datum datetime
 ,@Parm_Bez1 char(80)
 ,@Parm_EkMat float
 ,@Parm_VkMat float
 ,@Parm_EkLohn float
 ,@Parm_VkLohn float
 ,@Parm_EkFremd float
 ,@Parm_VkFremd float
 ,@Parm_Zeit float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_KGrpSummenWerte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_KGrpSummenWerte] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[NrSumme]
   ,[Datum]
   ,[Bez1]
   ,[EkMat]
   ,[VkMat]
   ,[EkLohn]
   ,[VkLohn]
   ,[EkFremd]
   ,[VkFremd]
   ,[Zeit]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,@Parm_NrSumme
   ,@Parm_Datum
   ,RTRIM(@Parm_Bez1)
   ,@Parm_EkMat
   ,@Parm_VkMat
   ,@Parm_EkLohn
   ,@Parm_VkLohn
   ,@Parm_EkFremd
   ,@Parm_VkFremd
   ,@Parm_Zeit
       )
  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_KGrpSummenWerte')
  DROP PROCEDURE [dbo].[up_TrSel_KGrpSummenWerte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_KGrpSummenWerte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([Bez1]))
    AS Data
  FROM     [dbo].[ut_KGrpSummenWerte]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_KGrpSummenWerte_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_First_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_Last_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_Prior_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_Next_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_Next50_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_FindL_KGrpSummenWerte_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KGrpSummenWerte_Key00')
  DROP PROCEDURE [dbo].[up_FindF_KGrpSummenWerte_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_KGrpSummenWerte_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_First_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_Last_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_Prior_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_Next_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_Next50_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_FindL_KGrpSummenWerte_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KGrpSummenWerte_Key01')
  DROP PROCEDURE [dbo].[up_FindF_KGrpSummenWerte_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_KGrpSummenWerte_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenWerte_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_KGrpSummenWerte_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenWerte_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_KGrpSummenWerte_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_KGrpSummenWerte_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_KGrpSummenWerte_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_KGrpSummenWerte_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_KGrpSummenWerte_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_KGrpSummenWerte_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

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

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

