-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, 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_WartungsAuftraege')
BEGIN
CREATE TABLE [dbo].[ut_WartungsAuftraege]
(
  [RecId]  integer NOT NULL
 ,[ObjektId] integer DEFAULT 0
 ,[AnlagenId] integer DEFAULT 0
 ,[Nr] char(20) DEFAULT ''
 ,[PrjNr] char(8) DEFAULT ''
 ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[BelNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[Status] integer DEFAULT 0
 ,[Termin] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Uhrzeit] char(20) DEFAULT ''
 ,[Monteur] char(4) DEFAULT ''
 ,[Art] integer DEFAULT 0
 ,[Ident] integer DEFAULT 0
 ,[KdNr] char(15) DEFAULT ''
 ,[Betreff] char(80) DEFAULT ''
 ,[MfrStatus] integer DEFAULT 0
 ,[MfrUpload] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[MfrDownload] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[MfrDone] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[MfrClosed] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[MfrIdent] char(30) DEFAULT ''
 ,[Locked] tinyint DEFAULT 0
  CONSTRAINT [Pk_WartungsAuftraege]  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_WartungsAuftraege_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsAuftraege_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsAuftraege_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsAuftraege_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsAuftraege_Keys')
  DROP TABLE [dbo].[ut_WartungsAuftraege_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_WartungsAuftraege' and sc.[name] = 'ObjektId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Nr] 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_WartungsAuftraege' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Status] 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_WartungsAuftraege' and sc.[name] = 'Termin')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Termin] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Uhrzeit] 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_WartungsAuftraege' and sc.[name] = 'Monteur')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Monteur] char(4) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Art')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Art] 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_WartungsAuftraege' and sc.[name] = 'Ident')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Ident] 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_WartungsAuftraege' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [KdNr] char(15) 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_WartungsAuftraege' and sc.[name] = 'Betreff')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Betreff] char(80) 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_WartungsAuftraege' and sc.[name] = 'MfrStatus')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrStatus] 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_WartungsAuftraege' and sc.[name] = 'MfrUpload')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrUpload] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrDownload')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrDownload] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrDone')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrDone] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrClosed')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrClosed] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrIdent')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [MfrIdent] char(30) 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_WartungsAuftraege' and sc.[name] = 'Locked')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAuftraege] ADD [Locked] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Nr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Nr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Nr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Nr] varchar(20)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [BelNr] 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_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] 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_WartungsAuftraege' 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_WartungsAuftraege' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [BelArt] varchar(2)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Uhrzeit'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Uhrzeit] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Uhrzeit'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Uhrzeit] varchar(20)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Monteur'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Monteur] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Monteur'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Monteur] varchar(4)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'KdNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [KdNr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'KdNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [KdNr] varchar(15)
END
GO
IF (80 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Betreff'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Betreff'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Betreff] char(80)
END
GO
IF (80 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Betreff'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'Betreff'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [Betreff] varchar(80)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrIdent'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrIdent'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [MfrIdent] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrIdent'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAuftraege' and sc.[name] = 'MfrIdent'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAuftraege] ALTER COLUMN [MfrIdent] varchar(30)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsAuftraege')
  DROP VIEW [dbo].[WartungsAuftraege]
GO
CREATE VIEW [dbo].[WartungsAuftraege] AS
  SELECT [RecId]
 ,[ObjektId]
 ,[AnlagenId]
 ,[Nr]
 ,[PrjNr]
 ,[Datum]
 ,[BelNr]
 ,[BelArt]
 ,[Status]
 ,[Termin]
 ,[Uhrzeit]
 ,[Monteur]
 ,[Art]
 ,[Ident]
 ,[KdNr]
 ,[Betreff]
 ,[MfrStatus]
 ,[MfrUpload]
 ,[MfrDownload]
 ,[MfrDone]
 ,[MfrClosed]
 ,[MfrIdent]
 ,[Locked]
  FROM [dbo].[ut_WartungsAuftraege]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_Sel_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsAuftraege]  @mRI integer
AS
  SELECT
    [ObjektId]
   ,[AnlagenId]
   ,[Nr]
   ,[PrjNr]
   ,[Datum]
   ,[BelNr]
   ,[BelArt]
   ,[Status]
   ,[Termin]
   ,[Uhrzeit]
   ,[Monteur]
   ,[Art]
   ,[Ident]
   ,[KdNr]
   ,[Betreff]
   ,[MfrStatus]
   ,[MfrUpload]
   ,[MfrDownload]
   ,[MfrDone]
   ,[MfrClosed]
   ,[MfrIdent]
   ,[Locked]
  FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_Append_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsAuftraege]
   @Param_ObjektId integer
  ,@Param_AnlagenId integer
  ,@Param_Nr char(20)
  ,@Param_PrjNr char(8)
  ,@Param_Datum datetime
  ,@Param_BelNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_Status integer
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(20)
  ,@Param_Monteur char(4)
  ,@Param_Art integer
  ,@Param_Ident integer
  ,@Param_KdNr char(15)
  ,@Param_Betreff char(80)
  ,@Param_MfrStatus integer
  ,@Param_MfrUpload datetime
  ,@Param_MfrDownload datetime
  ,@Param_MfrDone datetime
  ,@Param_MfrClosed datetime
  ,@Param_MfrIdent char(30)
  ,@Param_Locked tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsAuftraege()
      INSERT INTO [dbo].[ut_WartungsAuftraege] (RecId
        ,[ObjektId]
        ,[AnlagenId]
        ,[Nr]
        ,[PrjNr]
        ,[Datum]
        ,[BelNr]
        ,[BelArt]
        ,[Status]
        ,[Termin]
        ,[Uhrzeit]
        ,[Monteur]
        ,[Art]
        ,[Ident]
        ,[KdNr]
        ,[Betreff]
        ,[MfrStatus]
        ,[MfrUpload]
        ,[MfrDownload]
        ,[MfrDone]
        ,[MfrClosed]
        ,[MfrIdent]
        ,[Locked]
          ) VALUES (@NeueId
        ,@Param_ObjektId
        ,@Param_AnlagenId
        ,UPPER(RTRIM(@Param_Nr))
        ,UPPER(RTRIM(@Param_PrjNr))
        ,@Param_Datum
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,@Param_Status
        ,@Param_Termin
        ,RTRIM(@Param_Uhrzeit)
        ,UPPER(RTRIM(@Param_Monteur))
        ,@Param_Art
        ,@Param_Ident
        ,UPPER(RTRIM(@Param_KdNr))
        ,RTRIM(@Param_Betreff)
        ,@Param_MfrStatus
        ,@Param_MfrUpload
        ,@Param_MfrDownload
        ,@Param_MfrDone
        ,@Param_MfrClosed
        ,RTRIM(@Param_MfrIdent)
        ,@Param_Locked
          )
      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_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_Add_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsAuftraege]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsAuftraege()
  INSERT INTO [dbo].[ut_WartungsAuftraege] (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_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_Upd_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsAuftraege] @mRI integer
  ,@Param_ObjektId integer
  ,@Param_AnlagenId integer
  ,@Param_Nr char(20)
  ,@Param_PrjNr char(8)
  ,@Param_Datum datetime
  ,@Param_BelNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_Status integer
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(20)
  ,@Param_Monteur char(4)
  ,@Param_Art integer
  ,@Param_Ident integer
  ,@Param_KdNr char(15)
  ,@Param_Betreff char(80)
  ,@Param_MfrStatus integer
  ,@Param_MfrUpload datetime
  ,@Param_MfrDownload datetime
  ,@Param_MfrDone datetime
  ,@Param_MfrClosed datetime
  ,@Param_MfrIdent char(30)
  ,@Param_Locked tinyint
AS
  UPDATE [dbo].[ut_WartungsAuftraege] SET
    [ObjektId] = @Param_ObjektId
   ,[AnlagenId] = @Param_AnlagenId
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[Datum] = @Param_Datum
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[Status] = @Param_Status
   ,[Termin] = @Param_Termin
   ,[Uhrzeit] = RTRIM(@Param_Uhrzeit)
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Art] = @Param_Art
   ,[Ident] = @Param_Ident
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[Betreff] = RTRIM(@Param_Betreff)
   ,[MfrStatus] = @Param_MfrStatus
   ,[MfrUpload] = @Param_MfrUpload
   ,[MfrDownload] = @Param_MfrDownload
   ,[MfrDone] = @Param_MfrDone
   ,[MfrClosed] = @Param_MfrClosed
   ,[MfrIdent] = RTRIM(@Param_MfrIdent)
   ,[Locked] = @Param_Locked
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsAuftraege] @Param_RecId varchar(20)
  ,@Param_ObjektId varchar(8)
  ,@Param_AnlagenId varchar(8)
  ,@Param_Nr varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_Datum varchar(10)
  ,@Param_BelNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_Status varchar(1)
  ,@Param_Termin varchar(10)
  ,@Param_Uhrzeit varchar(20)
  ,@Param_Monteur varchar(4)
  ,@Param_Art varchar(1)
  ,@Param_Ident varchar(8)
  ,@Param_KdNr varchar(15)
  ,@Param_Betreff varchar(80)
  ,@Param_MfrStatus varchar(1)
  ,@Param_MfrUpload varchar(10)
  ,@Param_MfrDownload varchar(10)
  ,@Param_MfrDone varchar(10)
  ,@Param_MfrClosed varchar(10)
  ,@Param_MfrIdent varchar(30)
  ,@Param_Locked varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsAuftraege] SET
    [ObjektId] = dbo.itcfunc_integer(@Param_ObjektId)
   ,[AnlagenId] = dbo.itcfunc_integer(@Param_AnlagenId)
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[Datum] = dbo.itcfunc_datetime(@Param_Datum)
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[Status] = dbo.itcfunc_integer(@Param_Status)
   ,[Termin] = dbo.itcfunc_datetime(@Param_Termin)
   ,[Uhrzeit] = RTRIM(@Param_Uhrzeit)
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Art] = dbo.itcfunc_integer(@Param_Art)
   ,[Ident] = dbo.itcfunc_integer(@Param_Ident)
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[Betreff] = RTRIM(@Param_Betreff)
   ,[MfrStatus] = dbo.itcfunc_integer(@Param_MfrStatus)
   ,[MfrUpload] = dbo.itcfunc_datetime(@Param_MfrUpload)
   ,[MfrDownload] = dbo.itcfunc_datetime(@Param_MfrDownload)
   ,[MfrDone] = dbo.itcfunc_datetime(@Param_MfrDone)
   ,[MfrClosed] = dbo.itcfunc_datetime(@Param_MfrClosed)
   ,[MfrIdent] = RTRIM(@Param_MfrIdent)
   ,[Locked] = dbo.itcfunc_integer(@Param_Locked)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsAuftraege] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsAuftraege')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsAuftraege]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsAuftraege]
AS
  SELECT RecId,
    LTRIM(RTRIM([Nr]))
   +LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([Uhrzeit]))
   +LTRIM(RTRIM([Monteur]))
   +LTRIM(RTRIM([KdNr]))
   +LTRIM(RTRIM([Betreff]))
   +LTRIM(RTRIM([MfrIdent]))
  AS Data FROM [dbo].[ut_WartungsAuftraege] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key00) ORDER BY Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key00] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key00] @oldRecId integer AS
  DECLARE @oldIdent integer
  SELECT @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key00)
    WHERE (Ident > @oldIdent)
    ORDER BY Ident,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_WartungsAuftraege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key00] @oldRecId integer AS
  DECLARE @oldIdent integer
  SELECT @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key00)
    WHERE (Ident < @oldIdent)
    ORDER BY Ident 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_WartungsAuftraege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key00) WHERE 
      Ident = @mIdent
      ORDER BY Ident,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_WartungsAuftraege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsAuftraege_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsAuftraege_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key00) WHERE 
      Ident = @mIdent
      ORDER BY Ident 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_WartungsAuftraege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsAuftraege_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsAuftraege_Key00] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) ORDER BY AnlagenId,Nr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key01] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key01] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key01] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident > @oldIdent)
    ORDER BY AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr > @oldNr)
    ORDER BY AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,Nr,Ident,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_WartungsAuftraege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key01] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident < @oldIdent)
    ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId = @oldAnlagenId) AND (Nr < @oldNr)
    ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,Nr desc,Ident 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_WartungsAuftraege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)

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

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY AnlagenId,Nr,Ident,RecId
    RETURN
  END

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

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


  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY AnlagenId,Nr,Ident,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)

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

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

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

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


  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key01) WHERE 
      AnlagenId = @mAnlagenId AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key02] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key02] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key02] @oldRecId integer AS
  DECLARE @oldObjektId integer
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldObjektId = ObjektId, @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident > @oldIdent)
    ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr > @oldNr)
    ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId > @oldAnlagenId)
    ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId > @oldObjektId)
    ORDER BY ObjektId,AnlagenId,Nr,Ident,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_WartungsAuftraege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key02] @oldRecId integer AS
  DECLARE @oldObjektId integer
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldObjektId = ObjektId, @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident < @oldIdent)
    ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr < @oldNr)
    ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId = @oldObjektId) AND (AnlagenId < @oldAnlagenId)
    ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02)
    WHERE (ObjektId < @oldObjektId)
    ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident 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_WartungsAuftraege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mObjektId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,37,8)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId
      ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId
      ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

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



  IF @xLen < 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mObjektId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,37,8)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId
      ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId
      ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

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



  IF @xLen < 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key02) WHERE 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key03] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key03] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key03] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldObjektId integer
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldStatus = Status, @oldObjektId = ObjektId, @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident > @oldIdent)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr > @oldNr)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId > @oldObjektId)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status > @oldStatus)
    ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,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_WartungsAuftraege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key03] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldObjektId integer
  DECLARE @oldAnlagenId integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldStatus = Status, @oldObjektId = ObjektId, @oldAnlagenId = AnlagenId, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr = @oldNr) AND (Ident < @oldIdent)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId = @oldAnlagenId) AND (Nr < @oldNr)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId = @oldObjektId) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status = @oldStatus) AND (ObjektId < @oldObjektId)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident 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_WartungsAuftraege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mObjektId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,10,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,38,8)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END

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




  IF @xLen < 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Status,ObjektId,AnlagenId,Nr,Ident,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key03')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mObjektId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,10,8)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,38,8)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      Nr = @mNr
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

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




  IF @xLen < 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key03) WHERE 
      Status = @mStatus AND 
      ObjektId = @mObjektId AND 
      AnlagenId = @mAnlagenId AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Status desc,ObjektId desc,AnlagenId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) ORDER BY AnlagenId,Status,Nr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key04] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key04] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key04] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldStatus integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldAnlagenId = AnlagenId, @oldStatus = Status, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY AnlagenId,Status,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr = @oldNr) AND (Ident > @oldIdent)
    ORDER BY AnlagenId,Status,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr > @oldNr)
    ORDER BY AnlagenId,Status,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status > @oldStatus)
    ORDER BY AnlagenId,Status,Nr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,Status,Nr,Ident,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_WartungsAuftraege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key04] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldStatus integer
  DECLARE @oldNr char(20)
  DECLARE @oldIdent integer
  SELECT @oldAnlagenId = AnlagenId, @oldStatus = Status, @oldNr = Nr, @oldIdent = Ident FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr = @oldNr) AND (Ident < @oldIdent)
    ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Nr < @oldNr)
    ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status < @oldStatus)
    ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,Status desc,Nr desc,Ident 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_WartungsAuftraege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key04] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,1)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,30,8)

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

  IF @xLen = 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY AnlagenId,Status,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Nr = @mNr
      ORDER BY AnlagenId,Status,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus
      ORDER BY AnlagenId,Status,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId,Status,Nr,Ident,RecId
    RETURN
  END

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



  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY AnlagenId,Status,Nr,Ident,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key04')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,1)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,20)
  DECLARE @mIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,30,8)

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

  IF @xLen = 37 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Nr = @mNr
      ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus
      ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId
      ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

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



  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key04) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY AnlagenId desc,Status desc,Nr desc,Ident desc,RecId desc
    RETURN
  END


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


-- logical key  ORDER BY Art,KdNr,Datum,Nr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  DROP INDEX [Ik_Key05] ON [dbo].[ut_WartungsAuftraege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  CREATE INDEX [Ik_Key05] ON [dbo].[ut_WartungsAuftraege] (Art,KdNr,Datum,Nr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) ORDER BY Art,KdNr,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key05] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key05] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key05] @oldRecId integer AS
  DECLARE @oldArt integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldArt = Art, @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY Art,KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr > @oldNr)
    ORDER BY Art,KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum > @oldDatum)
    ORDER BY Art,KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr > @oldKdNr)
    ORDER BY Art,KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art > @oldArt)
    ORDER BY Art,KdNr,Datum,Nr,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_WartungsAuftraege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key05] @oldRecId integer AS
  DECLARE @oldArt integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldArt = Art, @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr < @oldNr)
    ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr = @oldKdNr) AND (Datum < @oldDatum)
    ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art = @oldArt) AND (KdNr < @oldKdNr)
    ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05)
    WHERE (Art < @oldArt)
    ORDER BY Art desc,KdNr desc,Datum desc,Nr 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_WartungsAuftraege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key05] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mArt integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,17,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,27,20)

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

  IF @xLen = 46 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 46 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Art,KdNr,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key05')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mArt integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,17,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,27,20)

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

  IF @xLen = 46 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 46 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key05) WHERE 
      Art = @mArt AND 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Art desc,KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) ORDER BY PrjNr,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key06] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key06] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key06] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldPrjNr = PrjNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr > @oldNr)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum > @oldDatum)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,Datum,Nr,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_WartungsAuftraege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key06] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key06] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldPrjNr = PrjNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr < @oldNr)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr = @oldPrjNr) AND (Datum < @oldDatum)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,Datum desc,Nr 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_WartungsAuftraege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key06] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key06')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,Datum desc,Nr 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_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) ORDER BY Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key07] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) ORDER BY Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key07] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key07] @oldRecId integer AS
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum = @oldDatum) AND (Nr > @oldNr)
    ORDER BY Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum > @oldDatum)
    ORDER BY Datum,Nr,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_WartungsAuftraege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key07] @oldRecId integer AS
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum = @oldDatum) AND (Nr < @oldNr)
    ORDER BY Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07)
    WHERE (Datum < @oldDatum)
    ORDER BY Datum desc,Nr 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_WartungsAuftraege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key07] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum
      ORDER BY Datum,Nr,RecId
    RETURN
  END

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


  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key07')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key07) WHERE 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE Status < 3 ORDER BY Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key08] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE Status < 3 ORDER BY Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key08] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key08] @oldRecId integer AS
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)) AND (Status < 3)
    ORDER BY Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum = @oldDatum) AND (Nr > @oldNr)) AND (Status < 3)
    ORDER BY Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum > @oldDatum)) AND (Status < 3)
    ORDER BY Datum,Nr,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_WartungsAuftraege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key08] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key08] @oldRecId integer AS
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)) AND (Status < 3)
    ORDER BY Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum = @oldDatum) AND (Nr < @oldNr)) AND (Status < 3)
    ORDER BY Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08)
    WHERE ((Datum < @oldDatum)) AND (Status < 3)
    ORDER BY Datum desc,Nr 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_WartungsAuftraege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key08] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY Datum,Nr,RecId
    RETURN
  END

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


  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key08')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key08) WHERE 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY Datum desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE Status < 3 ORDER BY PrjNr,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key09] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE Status < 3 ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key09] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key09] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldPrjNr = PrjNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)) AND (Status < 3)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr > @oldNr)) AND (Status < 3)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum > @oldDatum)) AND (Status < 3)
    ORDER BY PrjNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr > @oldPrjNr)) AND (Status < 3)
    ORDER BY PrjNr,Datum,Nr,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_WartungsAuftraege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key09] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key09] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldPrjNr = PrjNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)) AND (Status < 3)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum = @oldDatum) AND (Nr < @oldNr)) AND (Status < 3)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr = @oldPrjNr) AND (Datum < @oldDatum)) AND (Status < 3)
    ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09)
    WHERE ((PrjNr < @oldPrjNr)) AND (Status < 3)
    ORDER BY PrjNr desc,Datum desc,Nr 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_WartungsAuftraege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key09] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr
      AND (Status < 3)
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (Status < 3)
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY PrjNr,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key09')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr
      AND (Status < 3)
      ORDER BY PrjNr desc,Datum desc,Nr 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_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (Status < 3)
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key09) WHERE 
      PrjNr = @mPrjNr AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY PrjNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY KdNr,Datum,Nr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  DROP INDEX [Ik_Key10] ON [dbo].[ut_WartungsAuftraege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  CREATE INDEX [Ik_Key10] ON [dbo].[ut_WartungsAuftraege] (KdNr,Datum,Nr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE Status < 3 ORDER BY KdNr,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key10] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE Status < 3 ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key10] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key10] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)) AND (Status < 3)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr > @oldNr)) AND (Status < 3)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum > @oldDatum)) AND (Status < 3)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr > @oldKdNr)) AND (Status < 3)
    ORDER BY KdNr,Datum,Nr,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_WartungsAuftraege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key10] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)) AND (Status < 3)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr < @oldNr)) AND (Status < 3)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr = @oldKdNr) AND (Datum < @oldDatum)) AND (Status < 3)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10)
    WHERE ((KdNr < @oldKdNr)) AND (Status < 3)
    ORDER BY KdNr desc,Datum desc,Nr 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_WartungsAuftraege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key10] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,20)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr
      AND (Status < 3)
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      AND (Status < 3)
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key10')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,20)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      AND (Status < 3)
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      AND (Status < 3)
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr
      AND (Status < 3)
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      AND (Status < 3)
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND (Status < 3)
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) ORDER BY Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key11] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) ORDER BY Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key11] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key11] @oldRecId integer AS
  DECLARE @oldNr char(20)
  SELECT @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11)
    WHERE (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_WartungsAuftraege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key11] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key11] @oldRecId integer AS
  DECLARE @oldNr char(20)
  SELECT @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11)
    WHERE (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_WartungsAuftraege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key11] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) WHERE 
      Nr = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) WHERE 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key11')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) WHERE 
      Nr = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key11) WHERE 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY KdNr,Datum,Nr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  DROP INDEX [Ik_Key12] ON [dbo].[ut_WartungsAuftraege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  CREATE INDEX [Ik_Key12] ON [dbo].[ut_WartungsAuftraege] (KdNr,Datum,Nr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) ORDER BY KdNr,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key12] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key12] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key12] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr > @oldNr)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum > @oldDatum)
    ORDER BY KdNr,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr > @oldKdNr)
    ORDER BY KdNr,Datum,Nr,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_WartungsAuftraege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key12] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldKdNr = KdNr, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum = @oldDatum) AND (Nr < @oldNr)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr = @oldKdNr) AND (Datum < @oldDatum)
    ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12)
    WHERE (KdNr < @oldKdNr)
    ORDER BY KdNr desc,Datum desc,Nr 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_WartungsAuftraege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key12] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,20)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY KdNr,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key12')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,20)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key12) WHERE 
      KdNr = @mKdNr AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY KdNr desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY Monteur,Datum,Nr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  DROP INDEX [Ik_Key13] ON [dbo].[ut_WartungsAuftraege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsAuftraege' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  CREATE INDEX [Ik_Key13] ON [dbo].[ut_WartungsAuftraege] (Monteur,Datum,Nr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_First_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAuftraege_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) ORDER BY Monteur,Datum,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAuftraege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAuftraege_Key13] AS EXECUTE [dbo].[up_First_WartungsAuftraege_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAuftraege_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAuftraege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAuftraege_Key13] AS EXECUTE [dbo].[up_Last_WartungsAuftraege_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsAuftraege_Key13] @oldRecId integer AS
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldMonteur = Monteur, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId > @oldRecId)
    ORDER BY Monteur,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (Nr > @oldNr)
    ORDER BY Monteur,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum > @oldDatum)
    ORDER BY Monteur,Datum,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur > @oldMonteur)
    ORDER BY Monteur,Datum,Nr,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_WartungsAuftraege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsAuftraege_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsAuftraege_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsAuftraege_Key13] @oldRecId integer AS
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldNr char(20)
  SELECT @oldMonteur = Monteur, @oldDatum = Datum, @oldNr = Nr FROM [dbo].[ut_WartungsAuftraege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (Nr = @oldNr) AND (RecId < @oldRecId)
    ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (Nr < @oldNr)
    ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur = @oldMonteur) AND (Datum < @oldDatum)
    ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13)
    WHERE (Monteur < @oldMonteur)
    ORDER BY Monteur desc,Datum desc,Nr 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_WartungsAuftraege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsAuftraege_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsAuftraege_Key13] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAuftraege_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mMonteur char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,5,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,20)

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Monteur,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum
      ORDER BY Monteur,Datum,Nr,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur
      ORDER BY Monteur,Datum,Nr,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur >= @mMonteur AND SUBSTRING(Monteur,1,LEN(@mMonteur)) = @mMonteur
      ORDER BY Monteur,Datum,Nr,RecId
    RETURN
  END


  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Monteur,Datum,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAuftraege_Key13')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAuftraege_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mMonteur char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,5,10)
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,20)

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      Nr = @mNr
      ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum
      ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur
      ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      SUBSTRING(Monteur,1,LEN(@mMonteur)) = @mMonteur
      ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
    RETURN
  END


  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAuftraege] WITH(INDEX = Ik_Key13) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Monteur desc,Datum desc,Nr desc,RecId desc
    RETURN
  END

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


