------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelMassPosRef')
BEGIN
CREATE TABLE [dbo].[ut_BelMassPosRef]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[LfdNr] char(6) DEFAULT ' '
  ,[AmRefKey] char(6) DEFAULT ' '
  ,[PosRefKey] char(6) DEFAULT ' '
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[BaRef] char(20) DEFAULT ' '
  ,[REBSumme] char(6) DEFAULT ' '
  CONSTRAINT [Pk_BelMassPosRef]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelMassPosRef_KeyDel')
  DROP TRIGGER [dbo].[tr_BelMassPosRef_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelMassPosRef_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelMassPosRef_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelMassPosRef_Keys')
  DROP TABLE [dbo].[ut_BelMassPosRef_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ADD [LfdNr] 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_BelMassPosRef' and sc.[name] = 'AmRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ADD [AmRefKey] 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_BelMassPosRef' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'BaRef')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ADD [BaRef] 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_BelMassPosRef' and sc.[name] = 'REBSumme')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ADD [REBSumme] char(6) NOT NULL DEFAULT ' '
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] 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_BelMassPosRef' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [BelNr] char(8)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [LfdNr] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'AmRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [AmRefKey] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [PosRefKey] char(6)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'BaRef')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [BaRef] char(20)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassPosRef' and sc.[name] = 'REBSumme')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassPosRef] ALTER COLUMN [REBSumme] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelMassPosRef')
  DROP VIEW [dbo].[BelMassPosRef]
GO
CREATE VIEW [dbo].[BelMassPosRef] AS
  SELECT * FROM [dbo].[ut_BelMassPosRef]
GO
CREATE TABLE [dbo].[ut_BelMassPosRef_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(34) NOT NULL
  ,[Key1_Value] CHAR(40) NOT NULL
  ,[Key2_Value] CHAR(40) NOT NULL
  ,[Key3_Value] CHAR(54) NOT NULL
  ,[Key4_Value] CHAR(34) NOT NULL
  CONSTRAINT [Pk_ut_BelMassPosRef_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelMassPosRef_KeyDel] ON [dbo].[ut_BelMassPosRef]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelMassPosRef_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelMassPosRef_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value],[Key1_Value],[Key2_Value],[Key3_Value],[Key4_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[LfdNr])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[AmRefKey])+convert(char(6),[PosRefKey])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[PosRefKey])+convert(char(6),[AmRefKey])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[BaRef])+convert(char(6),[PosRefKey])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[REBSumme])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelMassPosRef_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[LfdNr])+STR([RecId],10))
   ,[Key1_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[AmRefKey])+convert(char(6),[PosRefKey])+STR([RecId],10))
   ,[Key2_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[PosRefKey])+convert(char(6),[AmRefKey])+STR([RecId],10))
   ,[Key3_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[BaRef])+convert(char(6),[PosRefKey])+STR([RecId],10))
   ,[Key4_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[REBSumme])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Add_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Add_BelMassPosRef] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelMassPosRef])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelMassPosRef] (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_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Upd_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelMassPosRef]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_LfdNr char(6)
 ,@Parm_AmRefKey char(6)
 ,@Parm_PosRefKey char(6)
 ,@Parm_Datum datetime
 ,@Parm_BaRef char(20)
 ,@Parm_REBSumme char(6)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelMassPosRef] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[AmRefKey] = RTRIM(@Parm_AmRefKey)
    ,[PosRefKey] = RTRIM(@Parm_PosRefKey)
    ,[Datum] = @Parm_Datum
    ,[BaRef] = RTRIM(@Parm_BaRef)
    ,[REBSumme] = RTRIM(@Parm_REBSumme)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Sel_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelMassPosRef]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[LfdNr]
    ,[AmRefKey]
    ,[PosRefKey]
    ,[Datum]
    ,[BaRef]
    ,[REBSumme]
  FROM [dbo].[ut_BelMassPosRef]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Del_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Del_BelMassPosRef]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelMassPosRef] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Reorg_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelMassPosRef]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelMassPosRef] 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_BelMassPosRef] 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_BelMassPosRef] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_Append_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_Append_BelMassPosRef]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_LfdNr char(6)
 ,@Parm_AmRefKey char(6)
 ,@Parm_PosRefKey char(6)
 ,@Parm_Datum datetime
 ,@Parm_BaRef char(20)
 ,@Parm_REBSumme char(6)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelMassPosRef])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelMassPosRef] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
   ,[AmRefKey]
   ,[PosRefKey]
   ,[Datum]
   ,[BaRef]
   ,[REBSumme]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_LfdNr)
   ,RTRIM(@Parm_AmRefKey)
   ,RTRIM(@Parm_PosRefKey)
   ,@Parm_Datum
   ,RTRIM(@Parm_BaRef)
   ,RTRIM(@Parm_REBSumme)
       )
  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_BelMassPosRef')
  DROP PROCEDURE [dbo].[up_TrSel_BelMassPosRef]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelMassPosRef]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([AmRefKey]))
      +LTRIM(RTRIM([PosRefKey]))
      +LTRIM(RTRIM([BaRef]))
      +LTRIM(RTRIM([REBSumme]))
    AS Data
  FROM     [dbo].[ut_BelMassPosRef]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelMassPosRef_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_First_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_Prior_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_Next_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_FindL_BelMassPosRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassPosRef_Key0')
  DROP PROCEDURE [dbo].[up_FindF_BelMassPosRef_Key0]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassPosRef_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassPosRef_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassPosRef_Key0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

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

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

CREATE INDEX [Ik_Key1] ON [dbo].[ut_BelMassPosRef_Keys]
( [Key1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_First_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_Last_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_Prior_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_Next_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_FindL_BelMassPosRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassPosRef_Key1')
  DROP PROCEDURE [dbo].[up_FindF_BelMassPosRef_Key1]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassPosRef_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassPosRef_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassPosRef_Key1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

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

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

CREATE INDEX [Ik_Key2] ON [dbo].[ut_BelMassPosRef_Keys]
( [Key2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_First_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_Last_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_Prior_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_Next_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_FindL_BelMassPosRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassPosRef_Key2')
  DROP PROCEDURE [dbo].[up_FindF_BelMassPosRef_Key2]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassPosRef_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key2_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassPosRef_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key2_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassPosRef_Key2] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key2] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

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

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

CREATE INDEX [Ik_Key3] ON [dbo].[ut_BelMassPosRef_Keys]
( [Key3_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_First_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_Last_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_Prior_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_Next_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_FindL_BelMassPosRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassPosRef_Key3')
  DROP PROCEDURE [dbo].[up_FindF_BelMassPosRef_Key3]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassPosRef_Key3]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key3_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassPosRef_Key3]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key3_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassPosRef_Key3] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key3_Value] > (SELECT [Key3_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key3_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key3] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key3_Value] > (SELECT [Key3_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key3_Value]
GO

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

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

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

CREATE INDEX [Ik_Key4] ON [dbo].[ut_BelMassPosRef_Keys]
( [Key4_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_First_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_Last_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_Prior_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_Next_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_FindL_BelMassPosRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassPosRef_Key4')
  DROP PROCEDURE [dbo].[up_FindF_BelMassPosRef_Key4]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassPosRef_Key4]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key4_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassPosRef_Key4]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys] ORDER BY [Key4_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassPosRef_Key4] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key4_Value] > (SELECT [Key4_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key4_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassPosRef_Key4] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassPosRef_Keys]
  WHERE [Key4_Value] > (SELECT [Key4_Value] FROM [dbo].[ut_BelMassPosRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key4_Value]
GO

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

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

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

