-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, 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_WartungsAnhang')
BEGIN
CREATE TABLE [dbo].[ut_WartungsAnhang]
(
  [RecId]  integer NOT NULL
 ,[AuftragId] integer DEFAULT 0
 ,[PosNr] integer DEFAULT 0
 ,[LfdNr] integer DEFAULT 0
 ,[ObjektId] integer DEFAULT 0
 ,[AnlagenId] integer DEFAULT 0
 ,[Stamp] char(32) DEFAULT ''
 ,[OrgName] char(250) DEFAULT ''
 ,[Extension] char(10) DEFAULT ''
  CONSTRAINT [Pk_WartungsAnhang]  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_WartungsAnhang_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsAnhang_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsAnhang_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsAnhang_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsAnhang_Keys')
  DROP TABLE [dbo].[ut_WartungsAnhang_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_WartungsAnhang' and sc.[name] = 'AuftragId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] ADD [AuftragId] 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_WartungsAnhang' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] 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_WartungsAnhang' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] 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_WartungsAnhang' and sc.[name] = 'ObjektId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] ADD [ObjektId] 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_WartungsAnhang' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] 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_WartungsAnhang' and sc.[name] = 'Stamp')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] ADD [Stamp] char(32) 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_WartungsAnhang' and sc.[name] = 'OrgName')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] ADD [OrgName] char(250) 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_WartungsAnhang' and sc.[name] = 'Extension')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnhang] ADD [Extension] char(10) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 32 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnhang' and sc.[name] = 'Stamp')
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnhang] ALTER COLUMN [Stamp] char(32)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnhang' and sc.[name] = 'OrgName')
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnhang] ALTER COLUMN [OrgName] char(250)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnhang' and sc.[name] = 'Extension')
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnhang] ALTER COLUMN [Extension] char(10)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsAnhang')
  DROP VIEW [dbo].[WartungsAnhang]
GO
CREATE VIEW [dbo].[WartungsAnhang] AS
  SELECT [RecId]
 ,[AuftragId]
 ,[PosNr]
 ,[LfdNr]
 ,[ObjektId]
 ,[AnlagenId]
 ,[Stamp]
 ,[OrgName]
 ,[Extension]
  FROM [dbo].[ut_WartungsAnhang]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsAnhang')
  DROP PROCEDURE [dbo].[up_Sel_WartungsAnhang]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsAnhang]  @mRI integer
AS
  SELECT
    [AuftragId]
   ,[PosNr]
   ,[LfdNr]
   ,[ObjektId]
   ,[AnlagenId]
   ,[Stamp]
   ,[OrgName]
   ,[Extension]
  FROM [dbo].[ut_WartungsAnhang] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsAnhang')
  DROP PROCEDURE [dbo].[up_Append_WartungsAnhang]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsAnhang]
   @Param_AuftragId integer
  ,@Param_PosNr integer
  ,@Param_LfdNr integer
  ,@Param_ObjektId integer
  ,@Param_AnlagenId integer
  ,@Param_Stamp char(32)
  ,@Param_OrgName char(250)
  ,@Param_Extension char(10)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsAnhang()
      INSERT INTO [dbo].[ut_WartungsAnhang] (RecId
        ,[AuftragId]
        ,[PosNr]
        ,[LfdNr]
        ,[ObjektId]
        ,[AnlagenId]
        ,[Stamp]
        ,[OrgName]
        ,[Extension]
          ) VALUES (@NeueId
        ,@Param_AuftragId
        ,@Param_PosNr
        ,@Param_LfdNr
        ,@Param_ObjektId
        ,@Param_AnlagenId
        ,UPPER(RTRIM(@Param_Stamp))
        ,RTRIM(@Param_OrgName)
        ,RTRIM(@Param_Extension)
          )
      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_WartungsAnhang')
  DROP PROCEDURE [dbo].[up_Add_WartungsAnhang]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsAnhang]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsAnhang()
  INSERT INTO [dbo].[ut_WartungsAnhang] (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_WartungsAnhang')
  DROP PROCEDURE [dbo].[up_Upd_WartungsAnhang]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsAnhang] @mRI integer
  ,@Param_AuftragId integer
  ,@Param_PosNr integer
  ,@Param_LfdNr integer
  ,@Param_ObjektId integer
  ,@Param_AnlagenId integer
  ,@Param_Stamp char(32)
  ,@Param_OrgName char(250)
  ,@Param_Extension char(10)
AS
  UPDATE [dbo].[ut_WartungsAnhang] SET
    [AuftragId] = @Param_AuftragId
   ,[PosNr] = @Param_PosNr
   ,[LfdNr] = @Param_LfdNr
   ,[ObjektId] = @Param_ObjektId
   ,[AnlagenId] = @Param_AnlagenId
   ,[Stamp] = UPPER(RTRIM(@Param_Stamp))
   ,[OrgName] = RTRIM(@Param_OrgName)
   ,[Extension] = RTRIM(@Param_Extension)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsAnhang')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsAnhang]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsAnhang] @Param_RecId varchar(20)
  ,@Param_AuftragId varchar(8)
  ,@Param_PosNr varchar(4)
  ,@Param_LfdNr varchar(4)
  ,@Param_ObjektId varchar(8)
  ,@Param_AnlagenId varchar(8)
  ,@Param_Stamp varchar(32)
  ,@Param_OrgName varchar(250)
  ,@Param_Extension varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsAnhang] SET
    [AuftragId] = dbo.itcfunc_integer(@Param_AuftragId)
   ,[PosNr] = dbo.itcfunc_integer(@Param_PosNr)
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[ObjektId] = dbo.itcfunc_integer(@Param_ObjektId)
   ,[AnlagenId] = dbo.itcfunc_integer(@Param_AnlagenId)
   ,[Stamp] = UPPER(RTRIM(@Param_Stamp))
   ,[OrgName] = RTRIM(@Param_OrgName)
   ,[Extension] = RTRIM(@Param_Extension)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsAnhang] @mRI
GO

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

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId > @oldAuftragId)
    ORDER BY AuftragId,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_WartungsAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAnhang_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAnhang_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId = @oldAuftragId) AND (PosNr = @oldPosNr) AND (LfdNr < @oldLfdNr)
    ORDER BY AuftragId 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_WartungsAnhang] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId = @oldAuftragId) AND (PosNr < @oldPosNr)
    ORDER BY AuftragId 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_WartungsAnhang] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId < @oldAuftragId)
    ORDER BY AuftragId 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_WartungsAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAnhang_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAnhang_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAnhang_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAnhang_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId 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,4)

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

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId,PosNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId,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_WartungsAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsAnhang_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsAnhang_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAnhang_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAnhang_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId 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,4)

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

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId AND 
      PosNr = @mPosNr AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId desc,PosNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnhang] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId 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_WartungsAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsAnhang_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsAnhang_Key00] @xKey, @xLen')
GO


