-- 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_WartungsDimensionen')
BEGIN
CREATE TABLE [dbo].[ut_WartungsDimensionen]
(
  [RecId]  integer NOT NULL
 ,[AnlagenId] integer DEFAULT 0
 ,[PosNr] integer DEFAULT 0
 ,[LfdNr] integer DEFAULT 0
 ,[Bez] char(40) DEFAULT ''
 ,[Wert] char(40) DEFAULT ''
  CONSTRAINT [Pk_WartungsDimensionen]  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_WartungsDimensionen_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsDimensionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsDimensionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsDimensionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsDimensionen_Keys')
  DROP TABLE [dbo].[ut_WartungsDimensionen_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_WartungsDimensionen' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsDimensionen] ADD [AnlagenId] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsDimensionen] ADD [PosNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsDimensionen] ADD [LfdNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsDimensionen] ADD [Bez] char(40) 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_WartungsDimensionen' and sc.[name] = 'Wert')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsDimensionen] ADD [Wert] char(40) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Bez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsDimensionen] ALTER COLUMN [Bez] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Bez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsDimensionen] ALTER COLUMN [Bez] varchar(40)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Wert'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Wert'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsDimensionen] ALTER COLUMN [Wert] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Wert'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsDimensionen' and sc.[name] = 'Wert'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsDimensionen] ALTER COLUMN [Wert] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsDimensionen')
  DROP VIEW [dbo].[WartungsDimensionen]
GO
CREATE VIEW [dbo].[WartungsDimensionen] AS
  SELECT [RecId]
 ,[AnlagenId]
 ,[PosNr]
 ,[LfdNr]
 ,[Bez]
 ,[Wert]
  FROM [dbo].[ut_WartungsDimensionen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Sel_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsDimensionen]  @mRI integer
AS
  SELECT
    [AnlagenId]
   ,[PosNr]
   ,[LfdNr]
   ,[Bez]
   ,[Wert]
  FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Append_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsDimensionen]
   @Param_AnlagenId integer
  ,@Param_PosNr integer
  ,@Param_LfdNr integer
  ,@Param_Bez char(40)
  ,@Param_Wert char(40)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsDimensionen()
      INSERT INTO [dbo].[ut_WartungsDimensionen] (RecId
        ,[AnlagenId]
        ,[PosNr]
        ,[LfdNr]
        ,[Bez]
        ,[Wert]
          ) VALUES (@NeueId
        ,@Param_AnlagenId
        ,@Param_PosNr
        ,@Param_LfdNr
        ,RTRIM(@Param_Bez)
        ,RTRIM(@Param_Wert)
          )
      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_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Add_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsDimensionen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsDimensionen()
  INSERT INTO [dbo].[ut_WartungsDimensionen] (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_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Upd_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsDimensionen] @mRI integer
  ,@Param_AnlagenId integer
  ,@Param_PosNr integer
  ,@Param_LfdNr integer
  ,@Param_Bez char(40)
  ,@Param_Wert char(40)
AS
  UPDATE [dbo].[ut_WartungsDimensionen] SET
    [AnlagenId] = @Param_AnlagenId
   ,[PosNr] = @Param_PosNr
   ,[LfdNr] = @Param_LfdNr
   ,[Bez] = RTRIM(@Param_Bez)
   ,[Wert] = RTRIM(@Param_Wert)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsDimensionen] @Param_RecId varchar(20)
  ,@Param_AnlagenId varchar(8)
  ,@Param_PosNr varchar(4)
  ,@Param_LfdNr varchar(3)
  ,@Param_Bez varchar(40)
  ,@Param_Wert varchar(40)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsDimensionen] SET
    [AnlagenId] = dbo.itcfunc_integer(@Param_AnlagenId)
   ,[PosNr] = dbo.itcfunc_integer(@Param_PosNr)
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[Bez] = RTRIM(@Param_Bez)
   ,[Wert] = RTRIM(@Param_Wert)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsDimensionen] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Del_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsDimensionen] @mRI integer
AS
  DELETE FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsDimensionen]
AS
  SELECT RecId,
    LTRIM(RTRIM([Bez]))
   +LTRIM(RTRIM([Wert]))
  AS Data FROM [dbo].[ut_WartungsDimensionen] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsDimensionen')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsDimensionen] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsDimensionen] 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_WartungsDimensionen] 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_WartungsDimensionen] )
  END
GO

-- logical key  ORDER BY AnlagenId,PosNr,LfdNr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_WartungsDimensionen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsDimensionen] (AnlagenId,PosNr,LfdNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsDimensionen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) ORDER BY AnlagenId,PosNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsDimensionen_Key00] AS EXECUTE [dbo].[up_First_WartungsDimensionen_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsDimensionen_Key00] AS EXECUTE [dbo].[up_Last_WartungsDimensionen_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key00] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId = @oldAnlagenId) AND (PosNr = @oldPosNr) AND (LfdNr > @oldLfdNr)
    ORDER BY AnlagenId,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId = @oldAnlagenId) AND (PosNr > @oldPosNr)
    ORDER BY AnlagenId,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,PosNr,LfdNr,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_WartungsDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsDimensionen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsDimensionen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key00] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId = @oldAnlagenId) AND (PosNr = @oldPosNr) AND (LfdNr < @oldLfdNr)
    ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId = @oldAnlagenId) AND (PosNr < @oldPosNr)
    ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,PosNr desc,LfdNr 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_WartungsDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsDimensionen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsDimensionen_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,13,3)

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId,PosNr,LfdNr,RecId
    RETURN
  END

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




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,13,3)

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

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




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


-- logical key  ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_WartungsDimensionen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_WartungsDimensionen] (AnlagenId,Bez,PosNr,LfdNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_First_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsDimensionen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsDimensionen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsDimensionen_Key01] AS EXECUTE [dbo].[up_First_WartungsDimensionen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsDimensionen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsDimensionen_Key01] AS EXECUTE [dbo].[up_Last_WartungsDimensionen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key01] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldBez char(40)
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldBez = Bez, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr = @oldPosNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr = @oldPosNr) AND (LfdNr > @oldLfdNr)
    ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr > @oldPosNr)
    ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez > @oldBez)
    ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,Bez,PosNr,LfdNr,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_WartungsDimensionen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsDimensionen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsDimensionen_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key01] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldBez char(40)
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldBez = Bez, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr = @oldPosNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr = @oldPosNr) AND (LfdNr < @oldLfdNr)
    ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez = @oldBez) AND (PosNr < @oldPosNr)
    ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Bez < @oldBez)
    ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr 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_WartungsDimensionen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsDimensionen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsDimensionen_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mBez char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,49,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,53,3)

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 52 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez
      ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez >= @mBez AND SUBSTRING(Bez,1,LEN(@mBez)) = @mBez
      ORDER BY AnlagenId,Bez,PosNr,LfdNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsDimensionen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mBez char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,49,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,53,3)

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 52 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Bez = @mBez
      ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      SUBSTRING(Bez,1,LEN(@mBez)) = @mBez
      ORDER BY AnlagenId desc,Bez desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END



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


-- logical key  ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  DROP INDEX [Ik_Key02] ON [dbo].[ut_WartungsDimensionen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsDimensionen' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  CREATE INDEX [Ik_Key02] ON [dbo].[ut_WartungsDimensionen] (AnlagenId,Wert,PosNr,LfdNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_First_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsDimensionen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsDimensionen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsDimensionen_Key02] AS EXECUTE [dbo].[up_First_WartungsDimensionen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsDimensionen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsDimensionen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsDimensionen_Key02] AS EXECUTE [dbo].[up_Last_WartungsDimensionen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsDimensionen_Key02] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldWert char(40)
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldWert = Wert, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr = @oldPosNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr = @oldPosNr) AND (LfdNr > @oldLfdNr)
    ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr > @oldPosNr)
    ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert > @oldWert)
    ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,Wert,PosNr,LfdNr,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_WartungsDimensionen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsDimensionen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsDimensionen_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsDimensionen_Key02] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldWert char(40)
  DECLARE @oldPosNr integer
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldWert = Wert, @oldPosNr = PosNr, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr = @oldPosNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr = @oldPosNr) AND (LfdNr < @oldLfdNr)
    ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert = @oldWert) AND (PosNr < @oldPosNr)
    ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId = @oldAnlagenId) AND (Wert < @oldWert)
    ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr 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_WartungsDimensionen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsDimensionen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsDimensionen_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsDimensionen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mWert char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,49,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,53,3)

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 52 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert
      ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert >= @mWert AND SUBSTRING(Wert,1,LEN(@mWert)) = @mWert
      ORDER BY AnlagenId,Wert,PosNr,LfdNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsDimensionen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsDimensionen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mWert char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,40)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,49,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,53,3)

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 52 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert AND 
      PosNr = @mPosNr
      ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      Wert = @mWert
      ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 48 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsDimensionen] WITH(INDEX = Ik_Key02) WHERE 
      AnlagenId = @mAnlagenId AND 
      SUBSTRING(Wert,1,LEN(@mWert)) = @mWert
      ORDER BY AnlagenId desc,Wert desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END



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


