------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelPosDimension')
BEGIN
CREATE TABLE [dbo].[ut_BelPosDimension]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[RefKey] char(6) DEFAULT ' '
  ,[DimCode] char(20) DEFAULT ' '
  ,[DimValue] char(20) DEFAULT ' '
  ,[Quantity] float DEFAULT 0
  ,[Comment] char(50) DEFAULT ' '
  ,[CalcSumm] tinyint DEFAULT 0
  ,[AttmStamp] char(32) DEFAULT ' '
  CONSTRAINT [Pk_BelPosDimension]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelPosDimension_KeyDel')
  DROP TRIGGER [dbo].[tr_BelPosDimension_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelPosDimension_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelPosDimension_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelPosDimension_Keys')
  DROP TABLE [dbo].[ut_BelPosDimension_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [RefKey] 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_BelPosDimension' and sc.[name] = 'DimCode')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [DimCode] 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_BelPosDimension' and sc.[name] = 'DimValue')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [DimValue] 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_BelPosDimension' and sc.[name] = 'Quantity')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [Quantity] 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_BelPosDimension' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [Comment] char(50) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'CalcSumm')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [CalcSumm] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'AttmStamp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ADD [AttmStamp] char(32) 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_BelPosDimension' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] 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_BelPosDimension' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ALTER COLUMN [RefKey] char(6)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'DimCode')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ALTER COLUMN [DimCode] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'DimValue')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ALTER COLUMN [DimValue] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ALTER COLUMN [Comment] char(50)
END
GO
IF 32 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'AttmStamp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPosDimension] ALTER COLUMN [AttmStamp] char(32)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelPosDimension')
  DROP VIEW [dbo].[BelPosDimension]
GO
CREATE VIEW [dbo].[BelPosDimension] AS
  SELECT * FROM [dbo].[ut_BelPosDimension]
GO
CREATE TABLE [dbo].[ut_BelPosDimension_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(74) NOT NULL
  ,[Key01_Filter] TINYINT NOT NULL
  ,[Key01_Value] CHAR(74) NOT NULL
  ,[Key02_Value] CHAR(68) NOT NULL
  ,[Key03_Value] CHAR(98) NOT NULL
  ,[Key04_Value] CHAR(104) NOT NULL
  CONSTRAINT [Pk_ut_BelPosDimension_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelPosDimension_KeyDel] ON [dbo].[ut_BelPosDimension]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelPosDimension_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelPosDimension_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Filter],[Key01_Value],[Key02_Value],[Key03_Value],[Key04_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR(RecId,10))
     ,CASE WHEN 1 =  CalcSumm THEN 1 ELSE 0 END
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[DimCode])+convert(char(50),[Comment])+STR(RecId,10))
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(50),[Comment])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelPosDimension_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR([RecId],10))
   ,[Key01_Filter] = CASE WHEN 1 =  CalcSumm THEN 1 ELSE 0 END
   ,[Key02_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR([RecId],10))
   ,[Key03_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(20),[DimCode])+convert(char(50),[Comment])+STR([RecId],10))
   ,[Key04_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(20),[DimCode])+convert(char(50),[Comment])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Add_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Add_BelPosDimension] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelPosDimension])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelPosDimension] (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_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Upd_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelPosDimension]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_DimCode char(20)
 ,@Parm_DimValue char(20)
 ,@Parm_Quantity float
 ,@Parm_Comment char(50)
 ,@Parm_CalcSumm tinyint
 ,@Parm_AttmStamp char(32)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelPosDimension] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[RefKey] = RTRIM(@Parm_RefKey)
    ,[DimCode] = RTRIM(@Parm_DimCode)
    ,[DimValue] = RTRIM(@Parm_DimValue)
    ,[Quantity] = @Parm_Quantity
    ,[Comment] = RTRIM(@Parm_Comment)
    ,[CalcSumm] = @Parm_CalcSumm
    ,[AttmStamp] = RTRIM(@Parm_AttmStamp)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Sel_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelPosDimension]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[RefKey]
    ,[DimCode]
    ,[DimValue]
    ,[Quantity]
    ,[Comment]
    ,[CalcSumm]
    ,[AttmStamp]
  FROM [dbo].[ut_BelPosDimension]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Del_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Del_BelPosDimension]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelPosDimension] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Reorg_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelPosDimension]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelPosDimension] 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_BelPosDimension] 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_BelPosDimension] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Append_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Append_BelPosDimension]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_DimCode char(20)
 ,@Parm_DimValue char(20)
 ,@Parm_Quantity float
 ,@Parm_Comment char(50)
 ,@Parm_CalcSumm tinyint
 ,@Parm_AttmStamp char(32)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelPosDimension])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelPosDimension] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RefKey]
   ,[DimCode]
   ,[DimValue]
   ,[Quantity]
   ,[Comment]
   ,[CalcSumm]
   ,[AttmStamp]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_RefKey)
   ,RTRIM(@Parm_DimCode)
   ,RTRIM(@Parm_DimValue)
   ,@Parm_Quantity
   ,RTRIM(@Parm_Comment)
   ,@Parm_CalcSumm
   ,RTRIM(@Parm_AttmStamp)
       )
  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_BelPosDimension')
  DROP PROCEDURE [dbo].[up_TrSel_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelPosDimension]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([RefKey]))
      +LTRIM(RTRIM([DimCode]))
      +LTRIM(RTRIM([DimValue]))
      +LTRIM(RTRIM([Comment]))
      +LTRIM(RTRIM([AttmStamp]))
    AS Data
  FROM     [dbo].[ut_BelPosDimension]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelPosDimension_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelPosDimension_Keys]
( [Key01_Filter],[Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key01]
GO

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

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelPosDimension_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key02_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_BelPosDimension_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_BelPosDimension_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

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

CREATE INDEX [Ik_Key03] ON [dbo].[ut_BelPosDimension_Keys]
( [Key03_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key03_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_BelPosDimension_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_BelPosDimension_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

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

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

CREATE INDEX [Ik_Key04] ON [dbo].[ut_BelPosDimension_Keys]
( [Key04_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key04]
GO

CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key04]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key04_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key04]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys] ORDER BY [Key04_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_BelPosDimension_Key04] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelPosDimension_Keys]
  WHERE [Key04_Value] > (SELECT [Key04_Value] FROM [dbo].[ut_BelPosDimension_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key04_Value]
GO

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

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

