-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

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
 ,[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] varchar(50) DEFAULT ''
 ,[CalcSumm] tinyint DEFAULT 0
 ,[AttmStamp] char(32) DEFAULT ''
  CONSTRAINT [Pk_BelPosDimension]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
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

-- add new fields
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] varchar(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

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'PrjNr'))
  AND (175 = (select sc.xtype 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 (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'PrjNr'))
  AND (167 = (select sc.xtype 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] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'BelArt'))
  AND (175 = (select sc.xtype 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 (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'BelArt'))
  AND (167 = (select sc.xtype 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] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'BelNr'))
  AND (175 = (select sc.xtype 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 (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'BelNr'))
  AND (167 = (select sc.xtype 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] varchar(8)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'RefKey'))
  AND (175 = (select sc.xtype 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 (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'RefKey'))
  AND (167 = (select sc.xtype 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] varchar(6)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'DimCode'))
  AND (175 = (select sc.xtype 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] = 'DimCode'))
  AND (167 = (select sc.xtype 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] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'DimValue'))
  AND (175 = (select sc.xtype 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 (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'DimValue'))
  AND (167 = (select sc.xtype 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] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'Comment'))
  AND (175 = (select sc.xtype 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 (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'Comment'))
  AND (167 = (select sc.xtype 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] varchar(50)
END
GO
IF (32 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'AttmStamp'))
  AND (175 = (select sc.xtype 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 (32 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPosDimension' and sc.[name] = 'AttmStamp'))
  AND (167 = (select sc.xtype 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] varchar(32)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelPosDimension')
  DROP VIEW [dbo].[BelPosDimension]
GO
CREATE VIEW [dbo].[BelPosDimension] AS
  SELECT [RecId]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[RefKey]
 ,[DimCode]
 ,[DimValue]
 ,[Quantity]
 ,[Comment]
 ,[CalcSumm]
 ,[AttmStamp]
  FROM [dbo].[ut_BelPosDimension]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_BelPosDimension', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_BelPosDimension
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_BelPosDimension() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_BelPosDimension] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
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]  @mRI integer
AS
  SELECT
    [PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RefKey]
   ,[DimCode]
   ,[DimValue]
   ,[Quantity]
   ,[Comment]
   ,[CalcSumm]
   ,[AttmStamp]
  FROM [dbo].[ut_BelPosDimension] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Sel2_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Sel2_BelPosDimension]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[BelPosDimension] WHERE RecId = @mRI
GO

-- insert with classic client
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]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_RefKey char(6)
  ,@Param_DimCode char(20)
  ,@Param_DimValue char(20)
  ,@Param_Quantity float
  ,@Param_Comment varchar(50)
  ,@Param_CalcSumm tinyint
  ,@Param_AttmStamp char(32)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelPosDimension()
      INSERT INTO [dbo].[ut_BelPosDimension] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[RefKey]
        ,[DimCode]
        ,[DimValue]
        ,[Quantity]
        ,[Comment]
        ,[CalcSumm]
        ,[AttmStamp]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_RefKey))
        ,UPPER(RTRIM(@Param_DimCode))
        ,UPPER(RTRIM(@Param_DimValue))
        ,@Param_Quantity
        ,RTRIM(@Param_Comment)
        ,@Param_CalcSumm
        ,UPPER(RTRIM(@Param_AttmStamp))
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
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 = dbo.itcfunc_NextRecId_BelPosDimension()
  INSERT INTO [dbo].[ut_BelPosDimension] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
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] @mRI integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_RefKey char(6)
  ,@Param_DimCode char(20)
  ,@Param_DimValue char(20)
  ,@Param_Quantity float
  ,@Param_Comment varchar(50)
  ,@Param_CalcSumm tinyint
  ,@Param_AttmStamp char(32)
AS
  UPDATE [dbo].[ut_BelPosDimension] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[DimCode] = UPPER(RTRIM(@Param_DimCode))
   ,[DimValue] = UPPER(RTRIM(@Param_DimValue))
   ,[Quantity] = @Param_Quantity
   ,[Comment] = RTRIM(@Param_Comment)
   ,[CalcSumm] = @Param_CalcSumm
   ,[AttmStamp] = UPPER(RTRIM(@Param_AttmStamp))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelPosDimension')
  DROP PROCEDURE [dbo].[up_Upd2_BelPosDimension]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelPosDimension] @Param_RecId varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_RefKey varchar(6)
  ,@Param_DimCode varchar(20)
  ,@Param_DimValue varchar(20)
  ,@Param_Quantity varchar(12)
  ,@Param_Comment varchar(50)
  ,@Param_CalcSumm varchar(1)
  ,@Param_AttmStamp varchar(32)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelPosDimension] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[DimCode] = UPPER(RTRIM(@Param_DimCode))
   ,[DimValue] = UPPER(RTRIM(@Param_DimValue))
   ,[Quantity] = dbo.itcfunc_float(@Param_Quantity)
   ,[Comment] = RTRIM(@Param_Comment)
   ,[CalcSumm] = dbo.itcfunc_integer(@Param_CalcSumm)
   ,[AttmStamp] = UPPER(RTRIM(@Param_AttmStamp))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelPosDimension] @mRI
GO

-- delete
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] @mRI integer
AS
  DELETE FROM [dbo].[ut_BelPosDimension] WHERE RecId = @mRI
GO

-- text index
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

-- reindex
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

-- logical key  ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key00] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key00] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key00] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue > @oldDimValue)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode > @oldDimCode)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey > @oldRefKey)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key00] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue < @oldDimValue)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode < @oldDimCode)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey < @oldRefKey)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key00] @oldRecId')
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] = 'qp_Next50_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_BelPosDimension_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue >= @mDimValue AND SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key00] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 1 = CalcSumm ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key01] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 1 = CalcSumm ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key01] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key01] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId > @oldRecId)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue > @oldDimValue)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode > @oldDimCode)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey > @oldRefKey)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr > @oldPrjNr)) AND (1 = CalcSumm)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key01] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId < @oldRecId)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (DimValue < @oldDimValue)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode < @oldDimCode)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey < @oldRefKey)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr < @oldPrjNr)) AND (1 = CalcSumm)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key01] @oldRecId')
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] = 'qp_Next50_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_BelPosDimension_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue >= @mDimValue AND SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      AND (1 = CalcSumm)
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,DimValue,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      AND (1 = CalcSumm)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key01] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  DROP INDEX [Ik_Key02] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,DimCode,DimValue,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key02] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key02] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key02] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (DimValue > @oldDimValue)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode > @oldDimCode)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key02] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDimCode char(20)
  DECLARE @oldDimValue char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldDimCode = DimCode, @oldDimValue = DimValue FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (DimValue = @oldDimValue) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (DimValue < @oldDimValue)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode < @oldDimCode)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key02] @oldRecId')
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] = 'qp_Next50_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_BelPosDimension_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 58 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  IF @xLen < 58 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      DimValue >= @mDimValue AND SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,DimCode,DimValue,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key02] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 58 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      DimValue = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  IF @xLen < 58 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,DimValue desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key02] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  DROP INDEX [Ik_Key03] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  CREATE INDEX [Ik_Key03] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,DimCode,Comment,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key03] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key03] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key03] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDimCode char(20)
  DECLARE @oldComment varchar(50)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldDimCode = DimCode, @oldComment = Comment FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (Comment = @oldComment) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (Comment > @oldComment)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode > @oldDimCode)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key03] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDimCode char(20)
  DECLARE @oldComment varchar(50)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldDimCode = DimCode, @oldComment = Comment FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (Comment = @oldComment) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode = @oldDimCode) AND (Comment < @oldComment)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (DimCode < @oldDimCode)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key03] @oldRecId')
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] = 'qp_Next50_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_BelPosDimension_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)
  DECLARE @mComment varchar(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,50)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      Comment = @mComment
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      Comment >= @mComment AND SUBSTRING(Comment,1,LEN(@mComment)) = @mComment
      ORDER BY PrjNr,BelArt,BelNr,DimCode,Comment,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key03] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key03] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)
  DECLARE @mComment varchar(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,50)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      Comment = @mComment
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      DimCode = @mDimCode AND 
      SUBSTRING(Comment,1,LEN(@mComment)) = @mComment
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key03] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key03] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  DROP INDEX [Ik_Key04] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  CREATE INDEX [Ik_Key04] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key04] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key04] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key04] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldComment varchar(50)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldComment = Comment FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (Comment = @oldComment) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (Comment > @oldComment)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode > @oldDimCode)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey > @oldRefKey)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key04] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimCode char(20)
  DECLARE @oldComment varchar(50)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimCode = DimCode, @oldComment = Comment FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (Comment = @oldComment) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode = @oldDimCode) AND (Comment < @oldComment)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimCode < @oldDimCode)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey < @oldRefKey)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key04] @oldRecId')
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] = 'qp_Next50_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_BelPosDimension_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mComment varchar(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,50)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 94 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      Comment = @mComment
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  IF @xLen < 94 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      Comment >= @mComment AND SUBSTRING(Comment,1,LEN(@mComment)) = @mComment
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimCode,Comment,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key04] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key04] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key04')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mComment varchar(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,50)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 94 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      Comment = @mComment
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  IF @xLen < 94 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimCode = @mDimCode AND 
      SUBSTRING(Comment,1,LEN(@mComment)) = @mComment
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimCode desc,Comment desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key04] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key04] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  DROP INDEX [Ik_Key05] ON [dbo].[ut_BelPosDimension]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPosDimension' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  CREATE INDEX [Ik_Key05] ON [dbo].[ut_BelPosDimension] (PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_First_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_BelPosDimension_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPosDimension_Key05] AS EXECUTE [dbo].[up_First_BelPosDimension_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_Last_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPosDimension_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPosDimension_Key05] AS EXECUTE [dbo].[up_Last_BelPosDimension_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_Next_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPosDimension_Key05] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimValue char(20)
  DECLARE @oldDimCode char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimValue = DimValue, @oldDimCode = DimCode FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue = @oldDimValue) AND (DimCode = @oldDimCode) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue = @oldDimValue) AND (DimCode > @oldDimCode)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue > @oldDimValue)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey > @oldRefKey)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPosDimension_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPosDimension_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_Prior_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPosDimension_Key05] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldRefKey char(6)
  DECLARE @oldDimValue char(20)
  DECLARE @oldDimCode char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldRefKey = RefKey, @oldDimValue = DimValue, @oldDimCode = DimCode FROM [dbo].[ut_BelPosDimension] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue = @oldDimValue) AND (DimCode = @oldDimCode) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue = @oldDimValue) AND (DimCode < @oldDimCode)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey = @oldRefKey) AND (DimValue < @oldDimValue)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (RefKey < @oldRefKey)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPosDimension_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPosDimension_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_Next50_BelPosDimension_Key05]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[qp_Next50_BelPosDimension_Key05]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_Prior50_BelPosDimension_Key05]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[qp_Prior50_BelPosDimension_Key05]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_FindF_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPosDimension_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue AND 
      DimCode = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue >= @mDimValue AND SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue AND 
      DimCode >= @mDimCode AND SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr,BelArt,BelNr,RefKey,DimValue,DimCode,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPosDimension_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPosDimension_Key05] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPosDimension_Key05')
  DROP PROCEDURE [dbo].[up_FindL_BelPosDimension_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPosDimension_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mDimValue char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)
  DECLARE @mDimCode char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,45,20)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue AND 
      DimCode = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      SUBSTRING(DimValue,1,LEN(@mDimValue)) = @mDimValue
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  IF @xLen < 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPosDimension] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey AND 
      DimValue = @mDimValue AND 
      SUBSTRING(DimCode,1,LEN(@mDimCode)) = @mDimCode
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,DimValue desc,DimCode desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPosDimension_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPosDimension_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPosDimension_Key05] @xKey, @xLen')
GO


