------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelSummenSpeicherRefs')
BEGIN
CREATE TABLE [dbo].[ut_BelSummenSpeicherRefs]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[SuSpNr] char(20) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[PosRefKey] char(6) DEFAULT ' '
  ,[Status] integer DEFAULT 0
  CONSTRAINT [Pk_BelSummenSpeicherRefs]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelSummenSpeicherRefs_KeyDel')
  DROP TRIGGER [dbo].[tr_BelSummenSpeicherRefs_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelSummenSpeicherRefs_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelSummenSpeicherRefs_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelSummenSpeicherRefs_Keys')
  DROP TABLE [dbo].[ut_BelSummenSpeicherRefs_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelSummenSpeicherRefs' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'SuSpNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] ADD [LfdNr] 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_BelSummenSpeicherRefs' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] ADD [PosRefKey] char(6) 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_BelSummenSpeicherRefs' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] ADD [Status] integer 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_BelSummenSpeicherRefs' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs' and sc.[name] = 'SuSpNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] ALTER COLUMN [SuSpNr] char(20)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelSummenSpeicherRefs' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelSummenSpeicherRefs] ALTER COLUMN [PosRefKey] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelSummenSpeicherRefs')
  DROP VIEW [dbo].[BelSummenSpeicherRefs]
GO
CREATE VIEW [dbo].[BelSummenSpeicherRefs] AS
  SELECT * FROM [dbo].[ut_BelSummenSpeicherRefs]
GO
CREATE TABLE [dbo].[ut_BelSummenSpeicherRefs_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(54) NOT NULL
  ,[Key01_Value] CHAR(54) NOT NULL
  ,[Key02_Filter] TINYINT NOT NULL
  ,[Key02_Value] CHAR(54) NOT NULL
  ,[Key03_Filter] TINYINT NOT NULL
  ,[Key03_Value] CHAR(54) NOT NULL
  ,[Key04_Filter] TINYINT NOT NULL
  ,[Key04_Value] CHAR(54) NOT NULL
  CONSTRAINT [Pk_ut_BelSummenSpeicherRefs_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelSummenSpeicherRefs_KeyDel] ON [dbo].[ut_BelSummenSpeicherRefs]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelSummenSpeicherRefs_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Filter],[Key02_Value],[Key03_Filter],[Key03_Value],[Key04_Filter],[Key04_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[SuSpNr])+STR([LfdNr],6,0)+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[PosRefKey])+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([LfdNr],6,0)+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(6),[PosRefKey])+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([LfdNr],6,0)+STR(RecId,10))

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Add_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Add_BelSummenSpeicherRefs] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelSummenSpeicherRefs])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelSummenSpeicherRefs] (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_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Upd_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelSummenSpeicherRefs]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_SuSpNr char(20)
 ,@Parm_LfdNr integer
 ,@Parm_PosRefKey char(6)
 ,@Parm_Status integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelSummenSpeicherRefs] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[SuSpNr] = RTRIM(@Parm_SuSpNr)
    ,[LfdNr] = @Parm_LfdNr
    ,[PosRefKey] = RTRIM(@Parm_PosRefKey)
    ,[Status] = @Parm_Status
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Sel_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelSummenSpeicherRefs]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[SuSpNr]
    ,[LfdNr]
    ,[PosRefKey]
    ,[Status]
  FROM [dbo].[ut_BelSummenSpeicherRefs]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Del_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Del_BelSummenSpeicherRefs]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelSummenSpeicherRefs] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Reorg_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelSummenSpeicherRefs]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs] 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_BelSummenSpeicherRefs] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_Append_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_Append_BelSummenSpeicherRefs]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_SuSpNr char(20)
 ,@Parm_LfdNr integer
 ,@Parm_PosRefKey char(6)
 ,@Parm_Status integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelSummenSpeicherRefs])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelSummenSpeicherRefs] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[SuSpNr]
   ,[LfdNr]
   ,[PosRefKey]
   ,[Status]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_SuSpNr)
   ,@Parm_LfdNr
   ,RTRIM(@Parm_PosRefKey)
   ,@Parm_Status
       )
  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_BelSummenSpeicherRefs')
  DROP PROCEDURE [dbo].[up_TrSel_BelSummenSpeicherRefs]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelSummenSpeicherRefs]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([SuSpNr]))
      +LTRIM(RTRIM([PosRefKey]))
    AS Data
  FROM     [dbo].[ut_BelSummenSpeicherRefs]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelSummenSpeicherRefs_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicherRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicherRefs_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelSummenSpeicherRefs_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicherRefs_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicherRefs_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key01]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelSummenSpeicherRefs_Keys]
( [Key02_Filter],[Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicherRefs_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicherRefs_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key02]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key02] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key02_Value] AS Key_Value FROM [dbo].[ut_BelSummenSpeicherRefs_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_BelSummenSpeicherRefs_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

CREATE INDEX [Ik_Key03] ON [dbo].[ut_BelSummenSpeicherRefs_Keys]
( [Key03_Filter],[Key03_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicherRefs_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicherRefs_Key03')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

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

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

CREATE INDEX [Ik_Key04] ON [dbo].[ut_BelSummenSpeicherRefs_Keys]
( [Key04_Filter],[Key04_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_FindL_BelSummenSpeicherRefs_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelSummenSpeicherRefs_Key04')
  DROP PROCEDURE [dbo].[up_FindF_BelSummenSpeicherRefs_Key04]
GO

CREATE PROCEDURE [dbo].[up_First_BelSummenSpeicherRefs_Key04]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 ORDER BY [Key04_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelSummenSpeicherRefs_Key04]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 ORDER BY [Key04_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelSummenSpeicherRefs_Key04] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 AND [Key04_Value] > (SELECT [Key04_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key04_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_BelSummenSpeicherRefs_Key04] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 AND [Key04_Value] < (SELECT [Key04_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key04_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_BelSummenSpeicherRefs_Key04] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 AND [Key04_Value] > (SELECT [Key04_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key04_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_BelSummenSpeicherRefs_Key04] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys]
  WHERE [Key04_Filter] = 1 AND [Key04_Value] < (SELECT [Key04_Value] FROM [dbo].[ut_BelSummenSpeicherRefs_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key04_Value] DESC
GO

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

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

