------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelSummenSpeicher')
BEGIN
CREATE TABLE [dbo].[ut_BelSummenSpeicher]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[SuSpNr] char(20) DEFAULT ' '
  ,[SuSpName] char(50) DEFAULT ' '
  ,[Status] integer DEFAULT 0
  ,[bEkNetto] tinyint DEFAULT 0
  ,[bEkNe] tinyint DEFAULT 0
  ,[bVkMat] tinyint DEFAULT 0
  ,[bMinuten] tinyint DEFAULT 0
  ,[bEkLohn] tinyint DEFAULT 0
  ,[bVkLohn] tinyint DEFAULT 0
  ,[bBedarf] tinyint DEFAULT 0
  ,[bCuGewicht] tinyint DEFAULT 0
  CONSTRAINT [Pk_BelSummenSpeicher]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelSummenSpeicher_KeyDel')
  DROP TRIGGER [dbo].[tr_BelSummenSpeicher_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelSummenSpeicher_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelSummenSpeicher_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelSummenSpeicher_Keys')
  DROP TABLE [dbo].[ut_BelSummenSpeicher_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelSummenSpeicher' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'SuSpNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [SuSpNr] 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_BelSummenSpeicher' and sc.[name] = 'SuSpName')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [SuSpName] char(50) 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_BelSummenSpeicher' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [Status] 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_BelSummenSpeicher' and sc.[name] = 'bEkNetto')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [bEkNetto] 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_BelSummenSpeicher' and sc.[name] = 'bEkNe')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [bEkNe] 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_BelSummenSpeicher' and sc.[name] = 'bVkMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'bMinuten')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [bMinuten] 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_BelSummenSpeicher' and sc.[name] = 'bEkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'bVkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'bBedarf')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [bBedarf] 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_BelSummenSpeicher' and sc.[name] = 'bCuGewicht')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ADD [bCuGewicht] tinyint 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_BelSummenSpeicher' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ALTER COLUMN [BelNr] char(8)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelSummenSpeicher' and sc.[name] = 'SuSpNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ALTER COLUMN [SuSpNr] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelSummenSpeicher' and sc.[name] = 'SuSpName')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicher] ALTER COLUMN [SuSpName] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelSummenSpeicher')
  DROP VIEW [dbo].[BelSummenSpeicher]
GO
CREATE VIEW [dbo].[BelSummenSpeicher] AS
  SELECT * FROM [dbo].[ut_BelSummenSpeicher]
GO
CREATE TABLE [dbo].[ut_BelSummenSpeicher_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(48) NOT NULL
  ,[Key01_Filter] TINYINT NOT NULL
  ,[Key01_Value] CHAR(48) NOT NULL
  ,[Key02_Filter] TINYINT NOT NULL
  ,[Key02_Value] CHAR(48) NOT NULL
  CONSTRAINT [Pk_ut_BelSummenSpeicher_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelSummenSpeicher_KeyDel] ON [dbo].[ut_BelSummenSpeicher]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelSummenSpeicher_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelSummenSpeicher_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Filter],[Key01_Value],[Key02_Filter],[Key02_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR(RecId,10))
     ,CASE WHEN Status < 2 THEN 1 ELSE 0 END
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR(RecId,10))
     ,CASE WHEN Status > 0 THEN 1 ELSE 0 END
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelSummenSpeicher_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR([RecId],10))
   ,[Key01_Filter] = CASE WHEN Status < 2 THEN 1 ELSE 0 END
   ,[Key02_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR([RecId],10))
   ,[Key02_Filter] = CASE WHEN Status > 0 THEN 1 ELSE 0 END
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Add_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Add_BelSummenSpeicher] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelSummenSpeicher])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelSummenSpeicher] (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_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Upd_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelSummenSpeicher]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_SuSpNr char(20)
 ,@Parm_SuSpName char(50)
 ,@Parm_Status integer
 ,@Parm_bEkNetto tinyint
 ,@Parm_bEkNe tinyint
 ,@Parm_bVkMat tinyint
 ,@Parm_bMinuten tinyint
 ,@Parm_bEkLohn tinyint
 ,@Parm_bVkLohn tinyint
 ,@Parm_bBedarf tinyint
 ,@Parm_bCuGewicht tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelSummenSpeicher] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[SuSpNr] = RTRIM(@Parm_SuSpNr)
    ,[SuSpName] = RTRIM(@Parm_SuSpName)
    ,[Status] = @Parm_Status
    ,[bEkNetto] = @Parm_bEkNetto
    ,[bEkNe] = @Parm_bEkNe
    ,[bVkMat] = @Parm_bVkMat
    ,[bMinuten] = @Parm_bMinuten
    ,[bEkLohn] = @Parm_bEkLohn
    ,[bVkLohn] = @Parm_bVkLohn
    ,[bBedarf] = @Parm_bBedarf
    ,[bCuGewicht] = @Parm_bCuGewicht
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Sel_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelSummenSpeicher]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[SuSpNr]
    ,[SuSpName]
    ,[Status]
    ,[bEkNetto]
    ,[bEkNe]
    ,[bVkMat]
    ,[bMinuten]
    ,[bEkLohn]
    ,[bVkLohn]
    ,[bBedarf]
    ,[bCuGewicht]
  FROM [dbo].[ut_BelSummenSpeicher]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Del_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Del_BelSummenSpeicher]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelSummenSpeicher] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Reorg_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelSummenSpeicher]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelSummenSpeicher] 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_BelSummenSpeicher] 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_BelSummenSpeicher] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_Append_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_Append_BelSummenSpeicher]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_SuSpNr char(20)
 ,@Parm_SuSpName char(50)
 ,@Parm_Status integer
 ,@Parm_bEkNetto tinyint
 ,@Parm_bEkNe tinyint
 ,@Parm_bVkMat tinyint
 ,@Parm_bMinuten tinyint
 ,@Parm_bEkLohn tinyint
 ,@Parm_bVkLohn tinyint
 ,@Parm_bBedarf tinyint
 ,@Parm_bCuGewicht tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelSummenSpeicher])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelSummenSpeicher] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[SuSpNr]
   ,[SuSpName]
   ,[Status]
   ,[bEkNetto]
   ,[bEkNe]
   ,[bVkMat]
   ,[bMinuten]
   ,[bEkLohn]
   ,[bVkLohn]
   ,[bBedarf]
   ,[bCuGewicht]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_SuSpNr)
   ,RTRIM(@Parm_SuSpName)
   ,@Parm_Status
   ,@Parm_bEkNetto
   ,@Parm_bEkNe
   ,@Parm_bVkMat
   ,@Parm_bMinuten
   ,@Parm_bEkLohn
   ,@Parm_bVkLohn
   ,@Parm_bBedarf
   ,@Parm_bCuGewicht
       )
  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_BelSummenSpeicher')
  DROP PROCEDURE [dbo].[up_TrSel_BelSummenSpeicher]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelSummenSpeicher]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([SuSpNr]))
      +LTRIM(RTRIM([SuSpName]))
    AS Data
  FROM     [dbo].[ut_BelSummenSpeicher]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelSummenSpeicher_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicher_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicher_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicher_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelSummenSpeicher_Keys]
( [Key01_Filter],[Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicher_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicher_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicher_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_BelSummenSpeicher_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelSummenSpeicher_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_BelSummenSpeicher_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_BelSummenSpeicher_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_BelSummenSpeicher_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_BelSummenSpeicher_Keys]
      WHERE [Key01_Filter] = 1 AND [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_BelSummenSpeicher_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelSummenSpeicher_Keys]
( [Key02_Filter],[Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicher_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicher_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicher_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_BelSummenSpeicher_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelSummenSpeicher_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelSummenSpeicher_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_BelSummenSpeicher_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_BelSummenSpeicher_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_BelSummenSpeicher_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_BelSummenSpeicher_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

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

CREATE PROCEDURE [dbo].[up_FindL_BelSummenSpeicher_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicher_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

