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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsAnlagen')
BEGIN
CREATE TABLE [dbo].[ut_WartungsAnlagen]
(
  [RecId]  integer NOT NULL
 ,[Ident] integer DEFAULT 0
 ,[Nr] char(20) DEFAULT ''
 ,[ObjektId] integer DEFAULT 0
 ,[AnlTyp] integer DEFAULT 0
 ,[Bez1] char(40) DEFAULT ''
 ,[Bez2] char(40) DEFAULT ''
 ,[Inbetrieb] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Garantie] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Letzte] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Naechste] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Termin] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Uhrzeit] char(5) DEFAULT ''
 ,[Monteur] char(4) DEFAULT ''
 ,[Vorgemerkt] tinyint DEFAULT 0
 ,[Dimension1] char(40) DEFAULT ''
 ,[Dimension2] char(40) DEFAULT ''
 ,[Dimension3] char(40) DEFAULT ''
 ,[Dimension4] char(40) DEFAULT ''
 ,[Dimension5] char(40) DEFAULT ''
 ,[AnzBT] integer DEFAULT 0
 ,[AnzMD] integer DEFAULT 0
 ,[KzCheck] integer DEFAULT 0
 ,[KzMark] integer DEFAULT 0
 ,[Mieter] char(15) DEFAULT ''
 ,[KdNrRng] char(15) DEFAULT ''
 ,[RechDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[LRechDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  CONSTRAINT [Pk_WartungsAnlagen]  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_WartungsAnlagen_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsAnlagen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsAnlagen_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsAnlagen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsAnlagen_Keys')
  DROP TABLE [dbo].[ut_WartungsAnlagen_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_WartungsAnlagen' and sc.[name] = 'Ident')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Ident] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Nr] char(20) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'ObjektId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [ObjektId] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'AnlTyp')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [AnlTyp] 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_WartungsAnlagen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Bez1] 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_WartungsAnlagen' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Bez2] 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_WartungsAnlagen' and sc.[name] = 'Inbetrieb')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Inbetrieb] 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_WartungsAnlagen' and sc.[name] = 'Garantie')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Garantie] 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_WartungsAnlagen' and sc.[name] = 'Letzte')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Letzte] 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_WartungsAnlagen' and sc.[name] = 'Naechste')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Naechste] 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_WartungsAnlagen' and sc.[name] = 'Termin')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Uhrzeit] char(5) 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_WartungsAnlagen' and sc.[name] = 'Monteur')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' and sc.[name] = 'Vorgemerkt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Vorgemerkt] 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_WartungsAnlagen' and sc.[name] = 'Dimension1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Dimension1] 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_WartungsAnlagen' and sc.[name] = 'Dimension2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Dimension2] 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_WartungsAnlagen' and sc.[name] = 'Dimension3')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Dimension3] 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_WartungsAnlagen' and sc.[name] = 'Dimension4')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Dimension4] 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_WartungsAnlagen' and sc.[name] = 'Dimension5')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [Dimension5] 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_WartungsAnlagen' and sc.[name] = 'AnzBT')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [AnzBT] 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_WartungsAnlagen' and sc.[name] = 'AnzMD')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [AnzMD] 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_WartungsAnlagen' and sc.[name] = 'KzCheck')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [KzCheck] 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_WartungsAnlagen' and sc.[name] = 'KzMark')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' and sc.[name] = 'Mieter')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' and sc.[name] = 'KdNrRng')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [KdNrRng] 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_WartungsAnlagen' and sc.[name] = 'RechDatum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [RechDatum] 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_WartungsAnlagen' and sc.[name] = 'LRechDatum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsAnlagen] ADD [LRechDatum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Nr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Nr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Nr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Nr] 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_WartungsAnlagen' and sc.[name] = 'Bez1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Bez1] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Bez1] 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_WartungsAnlagen' and sc.[name] = 'Bez2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Bez2] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Bez2] varchar(40)
END
GO
IF (5 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Uhrzeit] char(5)
END
GO
IF (5 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Uhrzeit'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Uhrzeit] varchar(5)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Monteur'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Monteur] varchar(4)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension1] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension1] 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_WartungsAnlagen' and sc.[name] = 'Dimension2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension2] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension2] 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_WartungsAnlagen' and sc.[name] = 'Dimension3'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension3'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension3] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension3'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension3'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension3] 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_WartungsAnlagen' and sc.[name] = 'Dimension4'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension4'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension4] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension4'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension4'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension4] 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_WartungsAnlagen' and sc.[name] = 'Dimension5'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension5'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension5] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension5'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'Dimension5'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Dimension5] 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_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] 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_WartungsAnlagen' 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_WartungsAnlagen' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [Mieter] 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_WartungsAnlagen' and sc.[name] = 'KdNrRng'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'KdNrRng'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [KdNrRng] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'KdNrRng'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsAnlagen' and sc.[name] = 'KdNrRng'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsAnlagen] ALTER COLUMN [KdNrRng] varchar(15)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsAnlagen')
  DROP VIEW [dbo].[WartungsAnlagen]
GO
CREATE VIEW [dbo].[WartungsAnlagen] AS
  SELECT [RecId]
 ,[Ident]
 ,[Nr]
 ,[ObjektId]
 ,[AnlTyp]
 ,[Bez1]
 ,[Bez2]
 ,[Inbetrieb]
 ,[Garantie]
 ,[Letzte]
 ,[Naechste]
 ,[Termin]
 ,[Uhrzeit]
 ,[Monteur]
 ,[Vorgemerkt]
 ,[Dimension1]
 ,[Dimension2]
 ,[Dimension3]
 ,[Dimension4]
 ,[Dimension5]
 ,[AnzBT]
 ,[AnzMD]
 ,[KzCheck]
 ,[KzMark]
 ,[Mieter]
 ,[KdNrRng]
 ,[RechDatum]
 ,[LRechDatum]
  FROM [dbo].[ut_WartungsAnlagen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_Sel_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsAnlagen]  @mRI integer
AS
  SELECT
    [Ident]
   ,[Nr]
   ,[ObjektId]
   ,[AnlTyp]
   ,[Bez1]
   ,[Bez2]
   ,[Inbetrieb]
   ,[Garantie]
   ,[Letzte]
   ,[Naechste]
   ,[Termin]
   ,[Uhrzeit]
   ,[Monteur]
   ,[Vorgemerkt]
   ,[Dimension1]
   ,[Dimension2]
   ,[Dimension3]
   ,[Dimension4]
   ,[Dimension5]
   ,[AnzBT]
   ,[AnzMD]
   ,[KzCheck]
   ,[KzMark]
   ,[Mieter]
   ,[KdNrRng]
   ,[RechDatum]
   ,[LRechDatum]
  FROM [dbo].[ut_WartungsAnlagen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_Append_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsAnlagen]
   @Param_Ident integer
  ,@Param_Nr char(20)
  ,@Param_ObjektId integer
  ,@Param_AnlTyp integer
  ,@Param_Bez1 char(40)
  ,@Param_Bez2 char(40)
  ,@Param_Inbetrieb datetime
  ,@Param_Garantie datetime
  ,@Param_Letzte datetime
  ,@Param_Naechste datetime
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(5)
  ,@Param_Monteur char(4)
  ,@Param_Vorgemerkt tinyint
  ,@Param_Dimension1 char(40)
  ,@Param_Dimension2 char(40)
  ,@Param_Dimension3 char(40)
  ,@Param_Dimension4 char(40)
  ,@Param_Dimension5 char(40)
  ,@Param_AnzBT integer
  ,@Param_AnzMD integer
  ,@Param_KzCheck integer
  ,@Param_KzMark integer
  ,@Param_Mieter char(15)
  ,@Param_KdNrRng char(15)
  ,@Param_RechDatum datetime
  ,@Param_LRechDatum datetime
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsAnlagen()
      INSERT INTO [dbo].[ut_WartungsAnlagen] (RecId
        ,[Ident]
        ,[Nr]
        ,[ObjektId]
        ,[AnlTyp]
        ,[Bez1]
        ,[Bez2]
        ,[Inbetrieb]
        ,[Garantie]
        ,[Letzte]
        ,[Naechste]
        ,[Termin]
        ,[Uhrzeit]
        ,[Monteur]
        ,[Vorgemerkt]
        ,[Dimension1]
        ,[Dimension2]
        ,[Dimension3]
        ,[Dimension4]
        ,[Dimension5]
        ,[AnzBT]
        ,[AnzMD]
        ,[KzCheck]
        ,[KzMark]
        ,[Mieter]
        ,[KdNrRng]
        ,[RechDatum]
        ,[LRechDatum]
          ) VALUES (@NeueId
        ,@Param_Ident
        ,UPPER(RTRIM(@Param_Nr))
        ,@Param_ObjektId
        ,@Param_AnlTyp
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Bez2)
        ,@Param_Inbetrieb
        ,@Param_Garantie
        ,@Param_Letzte
        ,@Param_Naechste
        ,@Param_Termin
        ,UPPER(RTRIM(@Param_Uhrzeit))
        ,UPPER(RTRIM(@Param_Monteur))
        ,@Param_Vorgemerkt
        ,RTRIM(@Param_Dimension1)
        ,RTRIM(@Param_Dimension2)
        ,RTRIM(@Param_Dimension3)
        ,RTRIM(@Param_Dimension4)
        ,RTRIM(@Param_Dimension5)
        ,@Param_AnzBT
        ,@Param_AnzMD
        ,@Param_KzCheck
        ,@Param_KzMark
        ,UPPER(RTRIM(@Param_Mieter))
        ,UPPER(RTRIM(@Param_KdNrRng))
        ,@Param_RechDatum
        ,@Param_LRechDatum
          )
      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_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_Add_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsAnlagen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsAnlagen()
  INSERT INTO [dbo].[ut_WartungsAnlagen] (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_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_Upd_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsAnlagen] @mRI integer
  ,@Param_Ident integer
  ,@Param_Nr char(20)
  ,@Param_ObjektId integer
  ,@Param_AnlTyp integer
  ,@Param_Bez1 char(40)
  ,@Param_Bez2 char(40)
  ,@Param_Inbetrieb datetime
  ,@Param_Garantie datetime
  ,@Param_Letzte datetime
  ,@Param_Naechste datetime
  ,@Param_Termin datetime
  ,@Param_Uhrzeit char(5)
  ,@Param_Monteur char(4)
  ,@Param_Vorgemerkt tinyint
  ,@Param_Dimension1 char(40)
  ,@Param_Dimension2 char(40)
  ,@Param_Dimension3 char(40)
  ,@Param_Dimension4 char(40)
  ,@Param_Dimension5 char(40)
  ,@Param_AnzBT integer
  ,@Param_AnzMD integer
  ,@Param_KzCheck integer
  ,@Param_KzMark integer
  ,@Param_Mieter char(15)
  ,@Param_KdNrRng char(15)
  ,@Param_RechDatum datetime
  ,@Param_LRechDatum datetime
AS
  UPDATE [dbo].[ut_WartungsAnlagen] SET
    [Ident] = @Param_Ident
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[ObjektId] = @Param_ObjektId
   ,[AnlTyp] = @Param_AnlTyp
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Inbetrieb] = @Param_Inbetrieb
   ,[Garantie] = @Param_Garantie
   ,[Letzte] = @Param_Letzte
   ,[Naechste] = @Param_Naechste
   ,[Termin] = @Param_Termin
   ,[Uhrzeit] = UPPER(RTRIM(@Param_Uhrzeit))
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Vorgemerkt] = @Param_Vorgemerkt
   ,[Dimension1] = RTRIM(@Param_Dimension1)
   ,[Dimension2] = RTRIM(@Param_Dimension2)
   ,[Dimension3] = RTRIM(@Param_Dimension3)
   ,[Dimension4] = RTRIM(@Param_Dimension4)
   ,[Dimension5] = RTRIM(@Param_Dimension5)
   ,[AnzBT] = @Param_AnzBT
   ,[AnzMD] = @Param_AnzMD
   ,[KzCheck] = @Param_KzCheck
   ,[KzMark] = @Param_KzMark
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[KdNrRng] = UPPER(RTRIM(@Param_KdNrRng))
   ,[RechDatum] = @Param_RechDatum
   ,[LRechDatum] = @Param_LRechDatum
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsAnlagen] @Param_RecId varchar(20)
  ,@Param_Ident varchar(8)
  ,@Param_Nr varchar(20)
  ,@Param_ObjektId varchar(8)
  ,@Param_AnlTyp varchar(8)
  ,@Param_Bez1 varchar(40)
  ,@Param_Bez2 varchar(40)
  ,@Param_Inbetrieb varchar(10)
  ,@Param_Garantie varchar(10)
  ,@Param_Letzte varchar(10)
  ,@Param_Naechste varchar(10)
  ,@Param_Termin varchar(10)
  ,@Param_Uhrzeit varchar(5)
  ,@Param_Monteur varchar(4)
  ,@Param_Vorgemerkt varchar(1)
  ,@Param_Dimension1 varchar(40)
  ,@Param_Dimension2 varchar(40)
  ,@Param_Dimension3 varchar(40)
  ,@Param_Dimension4 varchar(40)
  ,@Param_Dimension5 varchar(40)
  ,@Param_AnzBT varchar(2)
  ,@Param_AnzMD varchar(2)
  ,@Param_KzCheck varchar(1)
  ,@Param_KzMark varchar(1)
  ,@Param_Mieter varchar(15)
  ,@Param_KdNrRng varchar(15)
  ,@Param_RechDatum varchar(10)
  ,@Param_LRechDatum varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsAnlagen] SET
    [Ident] = dbo.itcfunc_integer(@Param_Ident)
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[ObjektId] = dbo.itcfunc_integer(@Param_ObjektId)
   ,[AnlTyp] = dbo.itcfunc_integer(@Param_AnlTyp)
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Inbetrieb] = dbo.itcfunc_datetime(@Param_Inbetrieb)
   ,[Garantie] = dbo.itcfunc_datetime(@Param_Garantie)
   ,[Letzte] = dbo.itcfunc_datetime(@Param_Letzte)
   ,[Naechste] = dbo.itcfunc_datetime(@Param_Naechste)
   ,[Termin] = dbo.itcfunc_datetime(@Param_Termin)
   ,[Uhrzeit] = UPPER(RTRIM(@Param_Uhrzeit))
   ,[Monteur] = UPPER(RTRIM(@Param_Monteur))
   ,[Vorgemerkt] = dbo.itcfunc_integer(@Param_Vorgemerkt)
   ,[Dimension1] = RTRIM(@Param_Dimension1)
   ,[Dimension2] = RTRIM(@Param_Dimension2)
   ,[Dimension3] = RTRIM(@Param_Dimension3)
   ,[Dimension4] = RTRIM(@Param_Dimension4)
   ,[Dimension5] = RTRIM(@Param_Dimension5)
   ,[AnzBT] = dbo.itcfunc_integer(@Param_AnzBT)
   ,[AnzMD] = dbo.itcfunc_integer(@Param_AnzMD)
   ,[KzCheck] = dbo.itcfunc_integer(@Param_KzCheck)
   ,[KzMark] = dbo.itcfunc_integer(@Param_KzMark)
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[KdNrRng] = UPPER(RTRIM(@Param_KdNrRng))
   ,[RechDatum] = dbo.itcfunc_datetime(@Param_RechDatum)
   ,[LRechDatum] = dbo.itcfunc_datetime(@Param_LRechDatum)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsAnlagen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsAnlagen')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsAnlagen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsAnlagen]
AS
  SELECT RecId,
    LTRIM(RTRIM([Nr]))
   +LTRIM(RTRIM([Bez1]))
   +LTRIM(RTRIM([Bez2]))
   +LTRIM(RTRIM([Uhrzeit]))
   +LTRIM(RTRIM([Monteur]))
   +LTRIM(RTRIM([Dimension1]))
   +LTRIM(RTRIM([Dimension2]))
   +LTRIM(RTRIM([Dimension3]))
   +LTRIM(RTRIM([Dimension4]))
   +LTRIM(RTRIM([Dimension5]))
   +LTRIM(RTRIM([Mieter]))
   +LTRIM(RTRIM([KdNrRng]))
  AS Data FROM [dbo].[ut_WartungsAnlagen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key00)
    WHERE (Ident > @oldIdent)
    ORDER BY Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key00)
    WHERE (Ident < @oldIdent)
    ORDER BY Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key00) WHERE 
      Ident = @mIdent
      ORDER BY Ident,RecId
    RETURN
  END

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


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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key00) WHERE 
      Ident = @mIdent
      ORDER BY Ident desc,RecId desc
    RETURN
  END

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


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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsAnlagen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsAnlagen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsAnlagen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mIdent 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_WartungsAnlagen] WITH(INDEX = Ik_Key01) WHERE 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY Nr,Ident,RecId
    RETURN
  END

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

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

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsAnlagen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsAnlagen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsAnlagen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mNr char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mIdent 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_WartungsAnlagen] WITH(INDEX = Ik_Key01) WHERE 
      Nr = @mNr AND 
      Ident = @mIdent
      ORDER BY Nr desc,Ident desc,RecId desc
    RETURN
  END

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

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

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


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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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

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

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

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

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

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


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


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


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

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark = @oldKzMark) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY KzMark,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark = @oldKzMark) AND (Ident > @oldIdent)
    ORDER BY KzMark,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark > @oldKzMark)
    ORDER BY KzMark,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark = @oldKzMark) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY KzMark desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark = @oldKzMark) AND (Ident < @oldIdent)
    ORDER BY KzMark desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03)
    WHERE (KzMark < @oldKzMark)
    ORDER BY KzMark desc,Ident desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03) WHERE 
      KzMark = @mKzMark AND 
      Ident = @mIdent
      ORDER BY KzMark,Ident,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark,Ident,RecId
    RETURN
  END

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



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

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

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03) WHERE 
      KzMark = @mKzMark AND 
      Ident = @mIdent
      ORDER BY KzMark desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key03) WHERE 
      KzMark = @mKzMark
      ORDER BY KzMark desc,Ident desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsAnlagen_Key04')
  DROP PROCEDURE [dbo].[up_First_WartungsAnlagen_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsAnlagen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE (Nr <> '') ORDER BY ObjektId,Nr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsAnlagen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsAnlagen_Key04] AS EXECUTE [dbo].[up_First_WartungsAnlagen_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsAnlagen_Key04')
  DROP PROCEDURE [dbo].[up_Last_WartungsAnlagen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsAnlagen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE (Nr <> '') ORDER BY ObjektId desc,Nr desc,Ident desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsAnlagen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsAnlagen_Key04] AS EXECUTE [dbo].[up_Last_WartungsAnlagen_Key04]')
GO

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

  DECLARE @newRecId integer

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

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

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      AND ((Nr <> ''))
      ORDER BY ObjektId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY ObjektId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId
      AND ((Nr <> ''))
      ORDER BY ObjektId,Nr,Ident,RecId
    RETURN
  END

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


  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY ObjektId,Nr,Ident,RecId
    RETURN
  END


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

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

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

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      Nr = @mNr AND 
      Ident = @mIdent
      AND ((Nr <> ''))
      ORDER BY ObjektId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY ObjektId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId
      AND ((Nr <> ''))
      ORDER BY ObjektId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

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


  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsAnlagen] WITH(INDEX = Ik_Key04) WHERE 
      ObjektId = @mObjektId AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY ObjektId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END


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


