------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelMassRef')
BEGIN
CREATE TABLE [dbo].[ut_BelMassRef]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[AmRefKey] char(6) DEFAULT ' '
  ,[Wert] float DEFAULT 0
  ,[AmNr] integer DEFAULT 0
  ,[Blatt] char(4) DEFAULT ' '
  ,[Spalte] char(4) DEFAULT ' '
  ,[RaumNr] char(6) DEFAULT ' '
  ,[Bauteil] char(6) DEFAULT ' '
  ,[REBStart] char(6) DEFAULT ' '
  ,[REBSumme] char(6) DEFAULT ' '
  CONSTRAINT [Pk_BelMassRef]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelMassRef_KeyDel')
  DROP TRIGGER [dbo].[tr_BelMassRef_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelMassRef_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelMassRef_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelMassRef_Keys')
  DROP TABLE [dbo].[ut_BelMassRef_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'AmRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'Wert')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [Wert] 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_BelMassRef' and sc.[name] = 'AmNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [AmNr] 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_BelMassRef' and sc.[name] = 'Blatt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [Blatt] char(4) 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_BelMassRef' and sc.[name] = 'Spalte')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [Spalte] char(4) 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_BelMassRef' and sc.[name] = 'RaumNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [RaumNr] 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_BelMassRef' and sc.[name] = 'Bauteil')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [Bauteil] 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_BelMassRef' and sc.[name] = 'REBStart')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ADD [REBStart] 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_BelMassRef' and sc.[name] = 'REBSumme')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] 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_BelMassRef' and sc.[name] = 'AmRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [AmRefKey] char(6)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'Blatt')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [Blatt] char(4)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'Spalte')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [Spalte] char(4)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'RaumNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [RaumNr] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'Bauteil')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [Bauteil] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'REBStart')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [REBStart] char(6)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelMassRef' and sc.[name] = 'REBSumme')
BEGIN
  ALTER TABLE [dbo].[ut_BelMassRef] ALTER COLUMN [REBSumme] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelMassRef')
  DROP VIEW [dbo].[BelMassRef]
GO
CREATE VIEW [dbo].[BelMassRef] AS
  SELECT * FROM [dbo].[ut_BelMassRef]
GO
CREATE TABLE [dbo].[ut_BelMassRef_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(34) NOT NULL
  ,[Key1_Value] CHAR(38) NOT NULL
  ,[Key2_Value] CHAR(44) NOT NULL
  ,[Key3_Value] CHAR(44) NOT NULL
  ,[Key4_Value] CHAR(34) NOT NULL
  ,[Key5_Value] CHAR(34) NOT NULL
  CONSTRAINT [Pk_ut_BelMassRef_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelMassRef_KeyDel] ON [dbo].[ut_BelMassRef]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelMassRef_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelMassRef_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value],[Key1_Value],[Key2_Value],[Key3_Value],[Key4_Value],[Key5_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[AmRefKey])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([AmNr],4,0)+convert(char(6),[AmRefKey])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([AmNr],4,0)+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR([AmNr],4,0)+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[REBSumme])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[REBStart])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelMassRef_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[AmRefKey])+STR([RecId],10))
   ,[Key1_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([AmNr],4,0)+convert(char(6),[AmRefKey])+STR([RecId],10))
   ,[Key2_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+STR([AmNr],4,0)+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR([RecId],10))
   ,[Key3_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RaumNr])+convert(char(6),[Bauteil])+STR([AmNr],4,0)+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))
   ,[Key5_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[REBStart])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelMassRef')
  DROP PROCEDURE [dbo].[up_Add_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Add_BelMassRef] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelMassRef])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelMassRef] (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_BelMassRef')
  DROP PROCEDURE [dbo].[up_Upd_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelMassRef]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_AmRefKey char(6)
 ,@Parm_Wert float
 ,@Parm_AmNr integer
 ,@Parm_Blatt char(4)
 ,@Parm_Spalte char(4)
 ,@Parm_RaumNr char(6)
 ,@Parm_Bauteil char(6)
 ,@Parm_REBStart char(6)
 ,@Parm_REBSumme char(6)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelMassRef] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[AmRefKey] = RTRIM(@Parm_AmRefKey)
    ,[Wert] = @Parm_Wert
    ,[AmNr] = @Parm_AmNr
    ,[Blatt] = RTRIM(@Parm_Blatt)
    ,[Spalte] = RTRIM(@Parm_Spalte)
    ,[RaumNr] = RTRIM(@Parm_RaumNr)
    ,[Bauteil] = RTRIM(@Parm_Bauteil)
    ,[REBStart] = RTRIM(@Parm_REBStart)
    ,[REBSumme] = RTRIM(@Parm_REBSumme)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelMassRef')
  DROP PROCEDURE [dbo].[up_Sel_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelMassRef]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[AmRefKey]
    ,[Wert]
    ,[AmNr]
    ,[Blatt]
    ,[Spalte]
    ,[RaumNr]
    ,[Bauteil]
    ,[REBStart]
    ,[REBSumme]
  FROM [dbo].[ut_BelMassRef]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelMassRef')
  DROP PROCEDURE [dbo].[up_Del_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Del_BelMassRef]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelMassRef] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelMassRef')
  DROP PROCEDURE [dbo].[up_Reorg_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelMassRef]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelMassRef] 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_BelMassRef] 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_BelMassRef] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelMassRef')
  DROP PROCEDURE [dbo].[up_Append_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_Append_BelMassRef]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_AmRefKey char(6)
 ,@Parm_Wert float
 ,@Parm_AmNr integer
 ,@Parm_Blatt char(4)
 ,@Parm_Spalte char(4)
 ,@Parm_RaumNr char(6)
 ,@Parm_Bauteil char(6)
 ,@Parm_REBStart char(6)
 ,@Parm_REBSumme char(6)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelMassRef])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelMassRef] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[AmRefKey]
   ,[Wert]
   ,[AmNr]
   ,[Blatt]
   ,[Spalte]
   ,[RaumNr]
   ,[Bauteil]
   ,[REBStart]
   ,[REBSumme]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_AmRefKey)
   ,@Parm_Wert
   ,@Parm_AmNr
   ,RTRIM(@Parm_Blatt)
   ,RTRIM(@Parm_Spalte)
   ,RTRIM(@Parm_RaumNr)
   ,RTRIM(@Parm_Bauteil)
   ,RTRIM(@Parm_REBStart)
   ,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_BelMassRef')
  DROP PROCEDURE [dbo].[up_TrSel_BelMassRef]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelMassRef]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([AmRefKey]))
      +LTRIM(RTRIM([Blatt]))
      +LTRIM(RTRIM([Spalte]))
      +LTRIM(RTRIM([RaumNr]))
      +LTRIM(RTRIM([Bauteil]))
      +LTRIM(RTRIM([REBStart]))
      +LTRIM(RTRIM([REBSumme]))
    AS Data
  FROM     [dbo].[ut_BelMassRef]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelMassRef_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key0')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key0]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key1] ON [dbo].[ut_BelMassRef_Keys]
( [Key1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key1')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key1]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key2] ON [dbo].[ut_BelMassRef_Keys]
( [Key2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key2')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key2]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key3] ON [dbo].[ut_BelMassRef_Keys]
( [Key3_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key3')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key3]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key4] ON [dbo].[ut_BelMassRef_Keys]
( [Key4_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key4')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key4]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key5] ON [dbo].[ut_BelMassRef_Keys]
( [Key5_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_First_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_Last_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_Prior_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_Next_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_Prior50_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_Next50_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_FindL_BelMassRef_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelMassRef_Key5')
  DROP PROCEDURE [dbo].[up_FindF_BelMassRef_Key5]
GO

CREATE PROCEDURE [dbo].[up_First_BelMassRef_Key5]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassRef_Keys] ORDER BY [Key5_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelMassRef_Key5]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassRef_Keys] ORDER BY [Key5_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelMassRef_Key5] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelMassRef_Keys]
  WHERE [Key5_Value] > (SELECT [Key5_Value] FROM [dbo].[ut_BelMassRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key5_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelMassRef_Key5] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelMassRef_Keys]
  WHERE [Key5_Value] > (SELECT [Key5_Value] FROM [dbo].[ut_BelMassRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key5_Value]
GO

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

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

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

