-- 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_WartungsMeldungen')
BEGIN
CREATE TABLE [dbo].[ut_WartungsMeldungen]
(
  [RecId]  integer NOT NULL
 ,[AnlagenId] integer DEFAULT 0
 ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Monteur] char(4) DEFAULT ''
 ,[Bearbeiter] char(10) DEFAULT ''
 ,[Status] integer DEFAULT 0
 ,[Dringend] tinyint DEFAULT 0
 ,[Wiedervorl] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Termin] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Uhrzeit] char(20) DEFAULT ''
 ,[Dauer] float DEFAULT 0
 ,[AnlagenNr] char(20) DEFAULT ''
 ,[AnlagenBez] char(40) DEFAULT ''
 ,[ObjektNr] char(20) DEFAULT ''
 ,[ObjektBez] char(40) DEFAULT ''
 ,[KdNr] char(15) DEFAULT ''
 ,[KdName] char(40) DEFAULT ''
 ,[Standort] char(15) DEFAULT ''
 ,[HausMstr] char(15) DEFAULT ''
 ,[Mieter] char(15) DEFAULT ''
 ,[SoName] char(40) DEFAULT ''
 ,[SoStrasse] char(40) DEFAULT ''
 ,[SoOrt] char(40) DEFAULT ''
 ,[SoPlz] char(10) DEFAULT ''
 ,[AuftragId] integer DEFAULT 0
 ,[Vorgemerkt] integer DEFAULT 0
 ,[KzStoerung] integer DEFAULT 0
 ,[BelNr] char(8) DEFAULT ''
 ,[PrjNr] char(8) DEFAULT ''
 ,[KzMark] integer DEFAULT 0
 ,[KzWartung] integer DEFAULT 0
 ,[ObjektSam] integer DEFAULT 0
 ,[LfdNr] integer DEFAULT 0
  CONSTRAINT [Pk_WartungsMeldungen]  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_WartungsMeldungen_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsMeldungen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsMeldungen_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsMeldungen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsMeldungen_Keys')
  DROP TABLE [dbo].[ut_WartungsMeldungen_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_WartungsMeldungen' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Monteur')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Bearbeiter')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Bearbeiter] char(10) 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_WartungsMeldungen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Dringend')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Dringend] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Wiedervorl')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Wiedervorl] 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_WartungsMeldungen' and sc.[name] = 'Termin')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'Dauer')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Dauer] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [AnlagenNr] 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_WartungsMeldungen' and sc.[name] = 'AnlagenBez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [AnlagenBez] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [ObjektNr] 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_WartungsMeldungen' and sc.[name] = 'ObjektBez')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [ObjektBez] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'KdName')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [KdName] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Standort')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Standort] 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_WartungsMeldungen' and sc.[name] = 'HausMstr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [HausMstr] 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_WartungsMeldungen' and sc.[name] = 'Mieter')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Mieter] 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_WartungsMeldungen' and sc.[name] = 'SoName')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [SoName] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoStrasse')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [SoStrasse] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoOrt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [SoOrt] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoPlz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [SoPlz] char(10) 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_WartungsMeldungen' and sc.[name] = 'AuftragId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [AuftragId] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Vorgemerkt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [Vorgemerkt] 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_WartungsMeldungen' and sc.[name] = 'KzStoerung')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [KzStoerung] 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_WartungsMeldungen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' and sc.[name] = 'KzMark')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [KzMark] 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_WartungsMeldungen' and sc.[name] = 'KzWartung')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [KzWartung] 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_WartungsMeldungen' and sc.[name] = 'ObjektSam')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [ObjektSam] 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_WartungsMeldungen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsMeldungen] ADD [LfdNr] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Monteur] varchar(4)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Bearbeiter'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Bearbeiter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Bearbeiter] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Bearbeiter'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Bearbeiter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Bearbeiter] varchar(10)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Uhrzeit] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [AnlagenNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [AnlagenNr] varchar(20)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenBez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenBez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [AnlagenBez] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenBez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'AnlagenBez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [AnlagenBez] varchar(40)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [ObjektNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [ObjektNr] varchar(20)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektBez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektBez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [ObjektBez] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektBez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'ObjektBez'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [ObjektBez] varchar(40)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [KdNr] varchar(15)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'KdName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'KdName'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [KdName] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'KdName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'KdName'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [KdName] varchar(40)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Standort'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Standort'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Standort] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Standort'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Standort'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Standort] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'HausMstr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'HausMstr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [HausMstr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'HausMstr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'HausMstr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [HausMstr] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Mieter'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Mieter] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Mieter'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [Mieter] varchar(15)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoName'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoName] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoName'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoName] varchar(40)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoStrasse'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoStrasse'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoStrasse] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoStrasse'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoStrasse'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoStrasse] varchar(40)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoOrt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoOrt'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoOrt] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoOrt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoOrt'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoOrt] varchar(40)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoPlz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoPlz'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoPlz] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoPlz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' and sc.[name] = 'SoPlz'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [SoPlz] varchar(10)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [BelNr] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] 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_WartungsMeldungen' 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_WartungsMeldungen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsMeldungen] ALTER COLUMN [PrjNr] varchar(8)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsMeldungen')
  DROP VIEW [dbo].[WartungsMeldungen]
GO
CREATE VIEW [dbo].[WartungsMeldungen] AS
  SELECT [RecId]
 ,[AnlagenId]
 ,[Datum]
 ,[Monteur]
 ,[Bearbeiter]
 ,[Status]
 ,[Dringend]
 ,[Wiedervorl]
 ,[Termin]
 ,[Uhrzeit]
 ,[Dauer]
 ,[AnlagenNr]
 ,[AnlagenBez]
 ,[ObjektNr]
 ,[ObjektBez]
 ,[KdNr]
 ,[KdName]
 ,[Standort]
 ,[HausMstr]
 ,[Mieter]
 ,[SoName]
 ,[SoStrasse]
 ,[SoOrt]
 ,[SoPlz]
 ,[AuftragId]
 ,[Vorgemerkt]
 ,[KzStoerung]
 ,[BelNr]
 ,[PrjNr]
 ,[KzMark]
 ,[KzWartung]
 ,[ObjektSam]
 ,[LfdNr]
  FROM [dbo].[ut_WartungsMeldungen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_Sel_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsMeldungen]  @mRI integer
AS
  SELECT
    [AnlagenId]
   ,[Datum]
   ,[Monteur]
   ,[Bearbeiter]
   ,[Status]
   ,[Dringend]
   ,[Wiedervorl]
   ,[Termin]
   ,[Uhrzeit]
   ,[Dauer]
   ,[AnlagenNr]
   ,[AnlagenBez]
   ,[ObjektNr]
   ,[ObjektBez]
   ,[KdNr]
   ,[KdName]
   ,[Standort]
   ,[HausMstr]
   ,[Mieter]
   ,[SoName]
   ,[SoStrasse]
   ,[SoOrt]
   ,[SoPlz]
   ,[AuftragId]
   ,[Vorgemerkt]
   ,[KzStoerung]
   ,[BelNr]
   ,[PrjNr]
   ,[KzMark]
   ,[KzWartung]
   ,[ObjektSam]
   ,[LfdNr]
  FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_Append_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsMeldungen]
   @Param_AnlagenId integer
  ,@Param_Datum datetime
  ,@Param_Monteur char(4)
  ,@Param_Bearbeiter char(10)
  ,@Param_Status integer
  ,@Param_Dringend tinyint
  ,@Param_Wiedervorl datetime
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(20)
  ,@Param_Dauer float
  ,@Param_AnlagenNr char(20)
  ,@Param_AnlagenBez char(40)
  ,@Param_ObjektNr char(20)
  ,@Param_ObjektBez char(40)
  ,@Param_KdNr char(15)
  ,@Param_KdName char(40)
  ,@Param_Standort char(15)
  ,@Param_HausMstr char(15)
  ,@Param_Mieter char(15)
  ,@Param_SoName char(40)
  ,@Param_SoStrasse char(40)
  ,@Param_SoOrt char(40)
  ,@Param_SoPlz char(10)
  ,@Param_AuftragId integer
  ,@Param_Vorgemerkt integer
  ,@Param_KzStoerung integer
  ,@Param_BelNr char(8)
  ,@Param_PrjNr char(8)
  ,@Param_KzMark integer
  ,@Param_KzWartung integer
  ,@Param_ObjektSam integer
  ,@Param_LfdNr integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsMeldungen()
      INSERT INTO [dbo].[ut_WartungsMeldungen] (RecId
        ,[AnlagenId]
        ,[Datum]
        ,[Monteur]
        ,[Bearbeiter]
        ,[Status]
        ,[Dringend]
        ,[Wiedervorl]
        ,[Termin]
        ,[Uhrzeit]
        ,[Dauer]
        ,[AnlagenNr]
        ,[AnlagenBez]
        ,[ObjektNr]
        ,[ObjektBez]
        ,[KdNr]
        ,[KdName]
        ,[Standort]
        ,[HausMstr]
        ,[Mieter]
        ,[SoName]
        ,[SoStrasse]
        ,[SoOrt]
        ,[SoPlz]
        ,[AuftragId]
        ,[Vorgemerkt]
        ,[KzStoerung]
        ,[BelNr]
        ,[PrjNr]
        ,[KzMark]
        ,[KzWartung]
        ,[ObjektSam]
        ,[LfdNr]
          ) VALUES (@NeueId
        ,@Param_AnlagenId
        ,@Param_Datum
        ,UPPER(RTRIM(@Param_Monteur))
        ,RTRIM(@Param_Bearbeiter)
        ,@Param_Status
        ,@Param_Dringend
        ,@Param_Wiedervorl
        ,@Param_Termin
        ,RTRIM(@Param_Uhrzeit)
        ,@Param_Dauer
        ,UPPER(RTRIM(@Param_AnlagenNr))
        ,RTRIM(@Param_AnlagenBez)
        ,UPPER(RTRIM(@Param_ObjektNr))
        ,RTRIM(@Param_ObjektBez)
        ,UPPER(RTRIM(@Param_KdNr))
        ,RTRIM(@Param_KdName)
        ,UPPER(RTRIM(@Param_Standort))
        ,UPPER(RTRIM(@Param_HausMstr))
        ,UPPER(RTRIM(@Param_Mieter))
        ,RTRIM(@Param_SoName)
        ,RTRIM(@Param_SoStrasse)
        ,RTRIM(@Param_SoOrt)
        ,RTRIM(@Param_SoPlz)
        ,@Param_AuftragId
        ,@Param_Vorgemerkt
        ,@Param_KzStoerung
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_PrjNr))
        ,@Param_KzMark
        ,@Param_KzWartung
        ,@Param_ObjektSam
        ,@Param_LfdNr
          )
      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_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_Add_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsMeldungen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsMeldungen()
  INSERT INTO [dbo].[ut_WartungsMeldungen] (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_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_Upd_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsMeldungen] @mRI integer
  ,@Param_AnlagenId integer
  ,@Param_Datum datetime
  ,@Param_Monteur char(4)
  ,@Param_Bearbeiter char(10)
  ,@Param_Status integer
  ,@Param_Dringend tinyint
  ,@Param_Wiedervorl datetime
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(20)
  ,@Param_Dauer float
  ,@Param_AnlagenNr char(20)
  ,@Param_AnlagenBez char(40)
  ,@Param_ObjektNr char(20)
  ,@Param_ObjektBez char(40)
  ,@Param_KdNr char(15)
  ,@Param_KdName char(40)
  ,@Param_Standort char(15)
  ,@Param_HausMstr char(15)
  ,@Param_Mieter char(15)
  ,@Param_SoName char(40)
  ,@Param_SoStrasse char(40)
  ,@Param_SoOrt char(40)
  ,@Param_SoPlz char(10)
  ,@Param_AuftragId integer
  ,@Param_Vorgemerkt integer
  ,@Param_KzStoerung integer
  ,@Param_BelNr char(8)
  ,@Param_PrjNr char(8)
  ,@Param_KzMark integer
  ,@Param_KzWartung integer
  ,@Param_ObjektSam integer
  ,@Param_LfdNr integer
AS
  UPDATE [dbo].[ut_WartungsMeldungen] SET
    [AnlagenId] = @Param_AnlagenId
   ,[Datum] = @Param_Datum
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Bearbeiter] = RTRIM(@Param_Bearbeiter)
   ,[Status] = @Param_Status
   ,[Dringend] = @Param_Dringend
   ,[Wiedervorl] = @Param_Wiedervorl
   ,[Termin] = @Param_Termin
   ,[Uhrzeit] = RTRIM(@Param_Uhrzeit)
   ,[Dauer] = @Param_Dauer
   ,[AnlagenNr] = UPPER(RTRIM(@Param_AnlagenNr))
   ,[AnlagenBez] = RTRIM(@Param_AnlagenBez)
   ,[ObjektNr] = UPPER(RTRIM(@Param_ObjektNr))
   ,[ObjektBez] = RTRIM(@Param_ObjektBez)
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[KdName] = RTRIM(@Param_KdName)
   ,[Standort] = UPPER(RTRIM(@Param_Standort))
   ,[HausMstr] = UPPER(RTRIM(@Param_HausMstr))
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[SoName] = RTRIM(@Param_SoName)
   ,[SoStrasse] = RTRIM(@Param_SoStrasse)
   ,[SoOrt] = RTRIM(@Param_SoOrt)
   ,[SoPlz] = RTRIM(@Param_SoPlz)
   ,[AuftragId] = @Param_AuftragId
   ,[Vorgemerkt] = @Param_Vorgemerkt
   ,[KzStoerung] = @Param_KzStoerung
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[KzMark] = @Param_KzMark
   ,[KzWartung] = @Param_KzWartung
   ,[ObjektSam] = @Param_ObjektSam
   ,[LfdNr] = @Param_LfdNr
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsMeldungen] @Param_RecId varchar(20)
  ,@Param_AnlagenId varchar(8)
  ,@Param_Datum varchar(10)
  ,@Param_Monteur varchar(4)
  ,@Param_Bearbeiter varchar(10)
  ,@Param_Status varchar(1)
  ,@Param_Dringend varchar(1)
  ,@Param_Wiedervorl varchar(10)
  ,@Param_Termin varchar(10)
  ,@Param_Uhrzeit varchar(20)
  ,@Param_Dauer varchar(4)
  ,@Param_AnlagenNr varchar(20)
  ,@Param_AnlagenBez varchar(40)
  ,@Param_ObjektNr varchar(20)
  ,@Param_ObjektBez varchar(40)
  ,@Param_KdNr varchar(15)
  ,@Param_KdName varchar(40)
  ,@Param_Standort varchar(15)
  ,@Param_HausMstr varchar(15)
  ,@Param_Mieter varchar(15)
  ,@Param_SoName varchar(40)
  ,@Param_SoStrasse varchar(40)
  ,@Param_SoOrt varchar(40)
  ,@Param_SoPlz varchar(10)
  ,@Param_AuftragId varchar(8)
  ,@Param_Vorgemerkt varchar(1)
  ,@Param_KzStoerung varchar(1)
  ,@Param_BelNr varchar(8)
  ,@Param_PrjNr varchar(8)
  ,@Param_KzMark varchar(1)
  ,@Param_KzWartung varchar(1)
  ,@Param_ObjektSam varchar(1)
  ,@Param_LfdNr varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsMeldungen] SET
    [AnlagenId] = dbo.itcfunc_integer(@Param_AnlagenId)
   ,[Datum] = dbo.itcfunc_datetime(@Param_Datum)
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Bearbeiter] = RTRIM(@Param_Bearbeiter)
   ,[Status] = dbo.itcfunc_integer(@Param_Status)
   ,[Dringend] = dbo.itcfunc_integer(@Param_Dringend)
   ,[Wiedervorl] = dbo.itcfunc_datetime(@Param_Wiedervorl)
   ,[Termin] = dbo.itcfunc_datetime(@Param_Termin)
   ,[Uhrzeit] = RTRIM(@Param_Uhrzeit)
   ,[Dauer] = dbo.itcfunc_float(@Param_Dauer)
   ,[AnlagenNr] = UPPER(RTRIM(@Param_AnlagenNr))
   ,[AnlagenBez] = RTRIM(@Param_AnlagenBez)
   ,[ObjektNr] = UPPER(RTRIM(@Param_ObjektNr))
   ,[ObjektBez] = RTRIM(@Param_ObjektBez)
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[KdName] = RTRIM(@Param_KdName)
   ,[Standort] = UPPER(RTRIM(@Param_Standort))
   ,[HausMstr] = UPPER(RTRIM(@Param_HausMstr))
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[SoName] = RTRIM(@Param_SoName)
   ,[SoStrasse] = RTRIM(@Param_SoStrasse)
   ,[SoOrt] = RTRIM(@Param_SoOrt)
   ,[SoPlz] = RTRIM(@Param_SoPlz)
   ,[AuftragId] = dbo.itcfunc_integer(@Param_AuftragId)
   ,[Vorgemerkt] = dbo.itcfunc_integer(@Param_Vorgemerkt)
   ,[KzStoerung] = dbo.itcfunc_integer(@Param_KzStoerung)
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[KzMark] = dbo.itcfunc_integer(@Param_KzMark)
   ,[KzWartung] = dbo.itcfunc_integer(@Param_KzWartung)
   ,[ObjektSam] = dbo.itcfunc_integer(@Param_ObjektSam)
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsMeldungen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsMeldungen')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsMeldungen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsMeldungen]
AS
  SELECT RecId,
    LTRIM(RTRIM([Monteur]))
   +LTRIM(RTRIM([Bearbeiter]))
   +LTRIM(RTRIM([Uhrzeit]))
   +LTRIM(RTRIM([AnlagenNr]))
   +LTRIM(RTRIM([AnlagenBez]))
   +LTRIM(RTRIM([ObjektNr]))
   +LTRIM(RTRIM([ObjektBez]))
   +LTRIM(RTRIM([KdNr]))
   +LTRIM(RTRIM([KdName]))
   +LTRIM(RTRIM([Standort]))
   +LTRIM(RTRIM([HausMstr]))
   +LTRIM(RTRIM([Mieter]))
   +LTRIM(RTRIM([SoName]))
   +LTRIM(RTRIM([SoStrasse]))
   +LTRIM(RTRIM([SoOrt]))
   +LTRIM(RTRIM([SoPlz]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([PrjNr]))
  AS Data FROM [dbo].[ut_WartungsMeldungen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

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

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




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

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

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key00) WHERE 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

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

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




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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01)
    WHERE (Datum > @oldDatum)
    ORDER BY Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01)
    WHERE (Datum < @oldDatum)
    ORDER BY Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01) WHERE 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01) WHERE 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01) WHERE 
      Datum = @mDatum
      ORDER BY Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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




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

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

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01) WHERE 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key01) WHERE 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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

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




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


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key02] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum > @oldDatum)
    ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status > @oldStatus)
    ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key02] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status = @oldStatus) AND (Datum < @oldDatum)
    ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus
      ORDER BY Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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





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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key02) WHERE 
      Status = @mStatus
      ORDER BY Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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





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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key03] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key03] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key03] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum > @oldDatum)
    ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId > @oldAuftragId)
    ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key03] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId = @oldAuftragId) AND (Datum < @oldDatum)
    ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03)
    WHERE (AuftragId < @oldAuftragId)
    ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,8)

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum
      ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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





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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key03')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,9,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,8)

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Datum = @mDatum
      ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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





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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key04] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key04] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key04] @oldRecId integer AS
  DECLARE @oldTermin datetime
  DECLARE @oldUhrzeit char(20)
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldTermin = Termin, @oldUhrzeit = Uhrzeit, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum > @oldDatum)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt > @oldSoOrt)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz > @oldSoPlz)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit > @oldUhrzeit)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin > @oldTermin)
    ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key04] @oldRecId integer AS
  DECLARE @oldTermin datetime
  DECLARE @oldUhrzeit char(20)
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldTermin = Termin, @oldUhrzeit = Uhrzeit, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum < @oldDatum)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt < @oldSoOrt)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz < @oldSoPlz)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin = @oldTermin) AND (Uhrzeit < @oldUhrzeit)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04)
    WHERE (Termin < @oldTermin)
    ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mTermin datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mUhrzeit char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,31,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,41,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,81,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,91,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,99,8)

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

  IF @xLen = 106 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 98 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit >= @mUhrzeit AND SUBSTRING(Uhrzeit,1,LEN(@mUhrzeit)) = @mUhrzeit
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz >= @mSoPlz AND SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt >= @mSoOrt AND SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key04')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mTermin datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,1,10)
  DECLARE @mUhrzeit char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,20)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,31,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,41,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,81,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,91,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,99,8)

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

  IF @xLen = 106 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 98 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      SUBSTRING(Uhrzeit,1,LEN(@mUhrzeit)) = @mUhrzeit
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key04) WHERE 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key05] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key05] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key05] @oldRecId integer AS
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldMonteur = Monteur, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur > @oldMonteur)
    ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key05] @oldRecId integer AS
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldMonteur = Monteur, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05)
    WHERE (Monteur < @oldMonteur)
    ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key05] @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 @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,15,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,23,8)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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

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

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

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




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key05')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key05] @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 @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,15,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,23,8)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      Monteur = @mMonteur
      ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(Monteur,1,LEN(@mMonteur)) = @mMonteur
      ORDER BY Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key06] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key06] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key06] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldMonteur = Monteur, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum > @oldDatum)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur > @oldMonteur)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status > @oldStatus)
    ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key06] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldMonteur char(4)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldMonteur = Monteur, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur = @oldMonteur) AND (Datum < @oldDatum)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status = @oldStatus) AND (Monteur < @oldMonteur)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mMonteur char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,6,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,16,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,24,8)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur >= @mMonteur AND SUBSTRING(Monteur,1,LEN(@mMonteur)) = @mMonteur
      ORDER BY Status,Monteur,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key06')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mMonteur char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,6,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,16,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,24,8)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur AND 
      Datum = @mDatum
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      Monteur = @mMonteur
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key06) WHERE 
      Status = @mStatus AND 
      SUBSTRING(Monteur,1,LEN(@mMonteur)) = @mMonteur
      ORDER BY Status desc,Monteur desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key07] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key07] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key07] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldTermin datetime
  DECLARE @oldUhrzeit char(20)
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldTermin = Termin, @oldUhrzeit = Uhrzeit, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum > @oldDatum)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt > @oldSoOrt)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz > @oldSoPlz)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit > @oldUhrzeit)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin > @oldTermin)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status > @oldStatus)
    ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key07] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldTermin datetime
  DECLARE @oldUhrzeit char(20)
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldTermin = Termin, @oldUhrzeit = Uhrzeit, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (Datum < @oldDatum)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz = @oldSoPlz) AND (SoOrt < @oldSoOrt)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit = @oldUhrzeit) AND (SoPlz < @oldSoPlz)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin = @oldTermin) AND (Uhrzeit < @oldUhrzeit)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status = @oldStatus) AND (Termin < @oldTermin)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mTermin datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mUhrzeit char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,12,20)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,42,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,82,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,92,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,8)

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

  IF @xLen = 107 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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



  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit >= @mUhrzeit AND SUBSTRING(Uhrzeit,1,LEN(@mUhrzeit)) = @mUhrzeit
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz >= @mSoPlz AND SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt >= @mSoOrt AND SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Status,Termin,Uhrzeit,SoPlz,SoOrt,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key07')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mTermin datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mUhrzeit char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,12,20)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,42,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,82,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,92,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,100,8)

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

  IF @xLen = 107 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 99 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      Datum = @mDatum
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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



  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      SUBSTRING(Uhrzeit,1,LEN(@mUhrzeit)) = @mUhrzeit
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key07) WHERE 
      Status = @mStatus AND 
      Termin = @mTermin AND 
      Uhrzeit = @mUhrzeit AND 
      SoPlz = @mSoPlz AND 
      SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Status desc,Termin desc,Uhrzeit desc,SoPlz desc,SoOrt desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key08] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key08] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key08] @oldRecId integer AS
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldSoStrasse char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldSoStrasse = SoStrasse, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum > @oldDatum)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse > @oldSoStrasse)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt > @oldSoOrt)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz > @oldSoPlz)
    ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key08] @oldRecId integer AS
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldSoStrasse char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldSoStrasse = SoStrasse, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum < @oldDatum)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse < @oldSoStrasse)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz = @oldSoPlz) AND (SoOrt < @oldSoOrt)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08)
    WHERE (SoPlz < @oldSoPlz)
    ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,40)
  DECLARE @mSoStrasse char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,51,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,91,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,101,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,109,8)

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

  IF @xLen = 116 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 108 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 100 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz >= @mSoPlz AND SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt >= @mSoOrt AND SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse >= @mSoStrasse AND SUBSTRING(SoStrasse,1,LEN(@mSoStrasse)) = @mSoStrasse
      ORDER BY SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key08')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,40)
  DECLARE @mSoStrasse char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,51,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,91,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,101,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,109,8)

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

  IF @xLen = 116 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 108 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 100 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key08) WHERE 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SUBSTRING(SoStrasse,1,LEN(@mSoStrasse)) = @mSoStrasse
      ORDER BY SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key09] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key09] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key09] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldSoStrasse char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldSoStrasse = SoStrasse, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum > @oldDatum)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse > @oldSoStrasse)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt > @oldSoOrt)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz > @oldSoPlz)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status > @oldStatus)
    ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key09] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldSoPlz char(10)
  DECLARE @oldSoOrt char(40)
  DECLARE @oldSoStrasse char(40)
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldSoPlz = SoPlz, @oldSoOrt = SoOrt, @oldSoStrasse = SoStrasse, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse = @oldSoStrasse) AND (Datum < @oldDatum)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt = @oldSoOrt) AND (SoStrasse < @oldSoStrasse)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz = @oldSoPlz) AND (SoOrt < @oldSoOrt)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status = @oldStatus) AND (SoPlz < @oldSoPlz)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,12,40)
  DECLARE @mSoStrasse char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,52,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,92,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,102,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,110,8)

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

  IF @xLen = 117 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 109 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 101 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 51 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz >= @mSoPlz AND SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 51 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt >= @mSoOrt AND SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen < 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse >= @mSoStrasse AND SUBSTRING(SoStrasse,1,LEN(@mSoStrasse)) = @mSoStrasse
      ORDER BY Status,SoPlz,SoOrt,SoStrasse,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key09')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mSoPlz char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,10)
  DECLARE @mSoOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,12,40)
  DECLARE @mSoStrasse char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,52,40)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,92,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,102,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,110,8)

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

  IF @xLen = 117 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 109 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 101 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse AND 
      Datum = @mDatum
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SoStrasse = @mSoStrasse
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 51 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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


  IF @xLen < 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SUBSTRING(SoPlz,1,LEN(@mSoPlz)) = @mSoPlz
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 51 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SUBSTRING(SoOrt,1,LEN(@mSoOrt)) = @mSoOrt
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 91 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key09) WHERE 
      Status = @mStatus AND 
      SoPlz = @mSoPlz AND 
      SoOrt = @mSoOrt AND 
      SUBSTRING(SoStrasse,1,LEN(@mSoStrasse)) = @mSoStrasse
      ORDER BY Status desc,SoPlz desc,SoOrt desc,SoStrasse desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key10] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key10] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key10] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektNr char(20)
  DECLARE @oldAnlagenNr char(20)
  DECLARE @oldAnlagenId integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldKdNr = KdNr, @oldObjektNr = ObjektNr, @oldAnlagenNr = AnlagenNr, @oldAnlagenId = AnlagenId, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr > @oldLfdNr)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum > @oldDatum)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId > @oldAnlagenId)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr > @oldAnlagenNr)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr > @oldObjektNr)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr > @oldKdNr)
    ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key10] @oldRecId integer AS
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektNr char(20)
  DECLARE @oldAnlagenNr char(20)
  DECLARE @oldAnlagenId integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldKdNr = KdNr, @oldObjektNr = ObjektNr, @oldAnlagenNr = AnlagenNr, @oldAnlagenId = AnlagenId, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr < @oldLfdNr)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum < @oldDatum)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId < @oldAnlagenId)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr < @oldAnlagenNr)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr = @oldKdNr) AND (ObjektNr < @oldObjektNr)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10)
    WHERE (KdNr < @oldKdNr)
    ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mObjektNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,20)
  DECLARE @mAnlagenNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,36,20)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,56,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,64,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,74,8)

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

  IF @xLen = 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 73 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 63 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen < 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr >= @mObjektNr AND SUBSTRING(ObjektNr,1,LEN(@mObjektNr)) = @mObjektNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen < 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr >= @mAnlagenNr AND SUBSTRING(AnlagenNr,1,LEN(@mAnlagenNr)) = @mAnlagenNr
      ORDER BY KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key10')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mObjektNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,20)
  DECLARE @mAnlagenNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,36,20)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,56,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,64,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,74,8)

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

  IF @xLen = 81 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 73 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 63 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      SUBSTRING(ObjektNr,1,LEN(@mObjektNr)) = @mObjektNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key10) WHERE 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      SUBSTRING(AnlagenNr,1,LEN(@mAnlagenNr)) = @mAnlagenNr
      ORDER BY KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key11] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key11] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key11] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektNr char(20)
  DECLARE @oldAnlagenNr char(20)
  DECLARE @oldAnlagenId integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldKdNr = KdNr, @oldObjektNr = ObjektNr, @oldAnlagenNr = AnlagenNr, @oldAnlagenId = AnlagenId, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr > @oldLfdNr)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum > @oldDatum)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId > @oldAnlagenId)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr > @oldAnlagenNr)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr > @oldObjektNr)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr > @oldKdNr)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status > @oldStatus)
    ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key11] @oldRecId integer AS
  DECLARE @oldStatus integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektNr char(20)
  DECLARE @oldAnlagenNr char(20)
  DECLARE @oldAnlagenId integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldStatus = Status, @oldKdNr = KdNr, @oldObjektNr = ObjektNr, @oldAnlagenNr = AnlagenNr, @oldAnlagenId = AnlagenId, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum = @oldDatum) AND (LfdNr < @oldLfdNr)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId = @oldAnlagenId) AND (Datum < @oldDatum)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr = @oldAnlagenNr) AND (AnlagenId < @oldAnlagenId)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr = @oldObjektNr) AND (AnlagenNr < @oldAnlagenNr)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr = @oldKdNr) AND (ObjektNr < @oldObjektNr)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status = @oldStatus) AND (KdNr < @oldKdNr)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11)
    WHERE (Status < @oldStatus)
    ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mObjektNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,20)
  DECLARE @mAnlagenNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,37,20)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,57,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,65,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,75,8)

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

  IF @xLen = 82 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 74 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 56 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen < 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr >= @mObjektNr AND SUBSTRING(ObjektNr,1,LEN(@mObjektNr)) = @mObjektNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen < 56 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr >= @mAnlagenNr AND SUBSTRING(AnlagenNr,1,LEN(@mAnlagenNr)) = @mAnlagenNr
      ORDER BY Status,KdNr,ObjektNr,AnlagenNr,AnlagenId,Datum,LfdNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key11')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mObjektNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,20)
  DECLARE @mAnlagenNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,37,20)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,57,8)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,65,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,75,8)

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

  IF @xLen = 82 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 74 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId AND 
      Datum = @mDatum
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 64 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr AND 
      AnlagenId = @mAnlagenId
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 56 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      AnlagenNr = @mAnlagenNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      SUBSTRING(ObjektNr,1,LEN(@mObjektNr)) = @mObjektNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 56 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key11) WHERE 
      Status = @mStatus AND 
      KdNr = @mKdNr AND 
      ObjektNr = @mObjektNr AND 
      SUBSTRING(AnlagenNr,1,LEN(@mAnlagenNr)) = @mAnlagenNr
      ORDER BY Status desc,KdNr desc,ObjektNr desc,AnlagenNr desc,AnlagenId desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key12] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key12] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key12] @oldRecId integer AS
  DECLARE @oldKzStoerung integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzStoerung = KzStoerung, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum > @oldDatum)
    ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung > @oldKzStoerung)
    ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key12] @oldRecId integer AS
  DECLARE @oldKzStoerung integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzStoerung = KzStoerung, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung = @oldKzStoerung) AND (Datum < @oldDatum)
    ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12)
    WHERE (KzStoerung < @oldKzStoerung)
    ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mKzStoerung integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum
      ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung
      ORDER BY KzStoerung,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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





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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key12')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mKzStoerung integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung AND 
      Datum = @mDatum
      ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key12) WHERE 
      KzStoerung = @mKzStoerung
      ORDER BY KzStoerung desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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





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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key13] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key13] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key13] @oldRecId integer AS
  DECLARE @oldKzMark integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzMark = KzMark, @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum > @oldDatum)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status > @oldStatus)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark > @oldKzMark)
    ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key13] @oldRecId integer AS
  DECLARE @oldKzMark integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzMark = KzMark, @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status = @oldStatus) AND (Datum < @oldDatum)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark = @oldKzMark) AND (Status < @oldStatus)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13)
    WHERE (KzMark < @oldKzMark)
    ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mKzMark integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,3,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,13,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,21,8)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus
      ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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






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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key13')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mKzMark integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,3,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,13,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,21,8)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark AND 
      Status = @mStatus
      ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key13) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

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






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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key14] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key14] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key14]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key14] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key14] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key14]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key14] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldStatus = Status, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (LfdNr > @oldLfdNr)
    ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum > @oldDatum)
    ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId > @oldAnlagenId)
    ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key14] @oldRecId integer AS
  DECLARE @oldAnlagenId integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldLfdNr integer
  SELECT @oldAnlagenId = AnlagenId, @oldStatus = Status, @oldDatum = Datum, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (LfdNr < @oldLfdNr)
    ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId = @oldAnlagenId) AND (Status = @oldStatus) AND (Datum < @oldDatum)
    ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14)
    WHERE (AnlagenId < @oldAnlagenId)
    ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key14] @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 @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus
      ORDER BY AnlagenId,Status,Datum,LfdNr,RecId
    RETURN
  END

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

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





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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key14')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key14]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key14] @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 @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      LfdNr = @mLfdNr
      ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key14) WHERE 
      AnlagenId = @mAnlagenId AND 
      Status = @mStatus
      ORDER BY AnlagenId desc,Status desc,Datum desc,LfdNr desc,RecId desc
    RETURN
  END

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

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





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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_First_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsMeldungen_Key15] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsMeldungen_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsMeldungen_Key15] AS EXECUTE [dbo].[up_First_WartungsMeldungen_Key15]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsMeldungen_Key15] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsMeldungen_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsMeldungen_Key15] AS EXECUTE [dbo].[up_Last_WartungsMeldungen_Key15]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsMeldungen_Key15] @oldRecId integer AS
  DECLARE @oldKzMark integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektSam integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzMark = KzMark, @oldKdNr = KdNr, @oldObjektSam = ObjektSam, @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr > @oldLfdNr)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId > @oldAnlagenId)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum > @oldDatum)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status > @oldStatus)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam > @oldObjektSam)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr > @oldKdNr)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark > @oldKzMark)
    ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsMeldungen_Key15] @oldRecId integer AS
  DECLARE @oldKzMark integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldObjektSam integer
  DECLARE @oldStatus integer
  DECLARE @oldDatum datetime
  DECLARE @oldAnlagenId integer
  DECLARE @oldLfdNr integer
  SELECT @oldKzMark = KzMark, @oldKdNr = KdNr, @oldObjektSam = ObjektSam, @oldStatus = Status, @oldDatum = Datum, @oldAnlagenId = AnlagenId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsMeldungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId = @oldAnlagenId) AND (LfdNr < @oldLfdNr)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum = @oldDatum) AND (AnlagenId < @oldAnlagenId)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status = @oldStatus) AND (Datum < @oldDatum)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam = @oldObjektSam) AND (Status < @oldStatus)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr = @oldKdNr) AND (ObjektSam < @oldObjektSam)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark = @oldKzMark) AND (KdNr < @oldKdNr)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15)
    WHERE (KzMark < @oldKzMark)
    ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsMeldungen_Key15] @xKey char(250), @xLen integer AS
  DECLARE @mKzMark integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mObjektSam integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,17,1)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,18,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,19,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)
  DECLARE @mLfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KzMark,KdNr,ObjektSam,Status,Datum,AnlagenId,LfdNr,RecId
    RETURN
  END






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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsMeldungen_Key15')
  DROP PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key15]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsMeldungen_Key15] @xKey char(250), @xLen integer AS
  DECLARE @mKzMark integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mKdNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mObjektSam integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,17,1)
  DECLARE @mStatus integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,18,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,19,10)
  DECLARE @mAnlagenId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)
  DECLARE @mLfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId AND 
      LfdNr = @mLfdNr
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum AND 
      AnlagenId = @mAnlagenId
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus AND 
      Datum = @mDatum
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam AND 
      Status = @mStatus
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr AND 
      ObjektSam = @mObjektSam
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      KdNr = @mKdNr
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr 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_WartungsMeldungen] WITH(INDEX = Ik_Key15) WHERE 
      KzMark = @mKzMark AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY KzMark desc,KdNr desc,ObjektSam desc,Status desc,Datum desc,AnlagenId desc,LfdNr desc,RecId desc
    RETURN
  END






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


