------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Wartungszeilen')
BEGIN
CREATE TABLE [dbo].[ut_Wartungszeilen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[KUNDENNR] char(15) DEFAULT ' '
  ,[ANLAGENNR] char(20) DEFAULT ' '
  ,[TEXT1] char(55) DEFAULT ' '
  ,[TEXT2] char(55) DEFAULT ' '
  ,[TEXT3] char(55) DEFAULT ' '
  ,[TEXT4] char(55) DEFAULT ' '
  ,[TEXT5] char(55) DEFAULT ' '
  ,[OBJNR] char(20) DEFAULT ' '
  ,[Name1] char(40) DEFAULT ' '
  ,[Name2] char(40) DEFAULT ' '
  ,[Strasse] char(40) DEFAULT ' '
  ,[Plz] char(8) DEFAULT ' '
  ,[Ort] char(40) DEFAULT ' '
  ,[DATUM] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[MONTEUR] char(40) DEFAULT ' '
  ,[nWARTSUMME] float DEFAULT 0
  ,[HISTORYIE] char(254) DEFAULT ' '
  ,[VERTNR] char(20) DEFAULT ' '
  ,[nVERTSUMME] float DEFAULT 0
  ,[cBEGINN] char(40) DEFAULT ' '
  ,[cENDE] char(40) DEFAULT ' '
  ,[STAPEL] char(20) DEFAULT ' '
  ,[OBJNAME1] char(40) DEFAULT ' '
  ,[OBJName2] char(40) DEFAULT ' '
  ,[OBJStrasse] char(40) DEFAULT ' '
  ,[OBJPlz] char(8) DEFAULT ' '
  ,[OBJOrt] char(40) DEFAULT ' '
  ,[Sammeln] tinyint DEFAULT 0
  ,[Status] char(1) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  CONSTRAINT [Pk_Wartungszeilen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Wartungszeilen_KeyDel')
  DROP TRIGGER [dbo].[tr_Wartungszeilen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Wartungszeilen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Wartungszeilen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Wartungszeilen_Keys')
  DROP TABLE [dbo].[ut_Wartungszeilen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'KUNDENNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [KUNDENNR] 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_Wartungszeilen' and sc.[name] = 'ANLAGENNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] 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_Wartungszeilen' and sc.[name] = 'TEXT1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [TEXT1] char(55) 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_Wartungszeilen' and sc.[name] = 'TEXT2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [TEXT2] char(55) 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_Wartungszeilen' and sc.[name] = 'TEXT3')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [TEXT3] char(55) 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_Wartungszeilen' and sc.[name] = 'TEXT4')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [TEXT4] char(55) 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_Wartungszeilen' and sc.[name] = 'TEXT5')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [TEXT5] char(55) 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_Wartungszeilen' and sc.[name] = 'OBJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJNR] 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_Wartungszeilen' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Name1] 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_Wartungszeilen' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Name2] 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_Wartungszeilen' and sc.[name] = 'Strasse')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Strasse] 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_Wartungszeilen' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Plz] 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_Wartungszeilen' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Ort] 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_Wartungszeilen' and sc.[name] = 'DATUM')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] 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_Wartungszeilen' and sc.[name] = 'MONTEUR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [MONTEUR] 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_Wartungszeilen' and sc.[name] = 'nWARTSUMME')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [nWARTSUMME] 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_Wartungszeilen' and sc.[name] = 'HISTORYIE')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [HISTORYIE] char(254) 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_Wartungszeilen' and sc.[name] = 'VERTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [VERTNR] 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_Wartungszeilen' and sc.[name] = 'nVERTSUMME')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [nVERTSUMME] 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_Wartungszeilen' and sc.[name] = 'cBEGINN')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [cBEGINN] 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_Wartungszeilen' and sc.[name] = 'cENDE')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [cENDE] 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_Wartungszeilen' and sc.[name] = 'STAPEL')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [STAPEL] 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_Wartungszeilen' and sc.[name] = 'OBJNAME1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJNAME1] 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_Wartungszeilen' and sc.[name] = 'OBJName2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJName2] 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_Wartungszeilen' and sc.[name] = 'OBJStrasse')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJStrasse] 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_Wartungszeilen' and sc.[name] = 'OBJPlz')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJPlz] 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_Wartungszeilen' and sc.[name] = 'OBJOrt')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [OBJOrt] 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_Wartungszeilen' and sc.[name] = 'Sammeln')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Sammeln] 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_Wartungszeilen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [Status] char(1) 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_Wartungszeilen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] 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_Wartungszeilen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [BelArt] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ADD [BelNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'KUNDENNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [KUNDENNR] char(15)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'ANLAGENNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [ANLAGENNR] char(20)
END
GO
IF 55 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'TEXT1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [TEXT1] char(55)
END
GO
IF 55 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'TEXT2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [TEXT2] char(55)
END
GO
IF 55 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'TEXT3')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [TEXT3] char(55)
END
GO
IF 55 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'TEXT4')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [TEXT4] char(55)
END
GO
IF 55 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'TEXT5')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [TEXT5] char(55)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJNR] char(20)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Name1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Name2] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Strasse')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Strasse] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Plz] char(8)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Ort] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'MONTEUR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [MONTEUR] char(40)
END
GO
IF 254 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'HISTORYIE')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [HISTORYIE] char(254)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'VERTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [VERTNR] char(20)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'cBEGINN')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [cBEGINN] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'cENDE')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [cENDE] char(40)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'STAPEL')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [STAPEL] char(20)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJNAME1')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJNAME1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJName2')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJName2] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJStrasse')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJStrasse] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJPlz')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJPlz] char(8)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'OBJOrt')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [OBJOrt] char(40)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [Status] char(1)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Wartungszeilen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Wartungszeilen] ALTER COLUMN [BelNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Wartungszeilen')
  DROP VIEW [dbo].[Wartungszeilen]
GO
CREATE VIEW [dbo].[Wartungszeilen] AS
  SELECT * FROM [dbo].[ut_Wartungszeilen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Add_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Add_Wartungszeilen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Wartungszeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Wartungszeilen] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Upd_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Wartungszeilen]
  @RI integer
 ,@Parm_KUNDENNR char(15)
 ,@Parm_ANLAGENNR char(20)
 ,@Parm_TEXT1 char(55)
 ,@Parm_TEXT2 char(55)
 ,@Parm_TEXT3 char(55)
 ,@Parm_TEXT4 char(55)
 ,@Parm_TEXT5 char(55)
 ,@Parm_OBJNR char(20)
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Strasse char(40)
 ,@Parm_Plz char(8)
 ,@Parm_Ort char(40)
 ,@Parm_DATUM datetime
 ,@Parm_MONTEUR char(40)
 ,@Parm_nWARTSUMME float
 ,@Parm_HISTORYIE char(254)
 ,@Parm_VERTNR char(20)
 ,@Parm_nVERTSUMME float
 ,@Parm_cBEGINN char(40)
 ,@Parm_cENDE char(40)
 ,@Parm_STAPEL char(20)
 ,@Parm_OBJNAME1 char(40)
 ,@Parm_OBJName2 char(40)
 ,@Parm_OBJStrasse char(40)
 ,@Parm_OBJPlz char(8)
 ,@Parm_OBJOrt char(40)
 ,@Parm_Sammeln tinyint
 ,@Parm_Status char(1)
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Wartungszeilen] SET
     [KUNDENNR] = RTRIM(@Parm_KUNDENNR)
    ,[ANLAGENNR] = RTRIM(@Parm_ANLAGENNR)
    ,[TEXT1] = RTRIM(@Parm_TEXT1)
    ,[TEXT2] = RTRIM(@Parm_TEXT2)
    ,[TEXT3] = RTRIM(@Parm_TEXT3)
    ,[TEXT4] = RTRIM(@Parm_TEXT4)
    ,[TEXT5] = RTRIM(@Parm_TEXT5)
    ,[OBJNR] = RTRIM(@Parm_OBJNR)
    ,[Name1] = RTRIM(@Parm_Name1)
    ,[Name2] = RTRIM(@Parm_Name2)
    ,[Strasse] = RTRIM(@Parm_Strasse)
    ,[Plz] = RTRIM(@Parm_Plz)
    ,[Ort] = RTRIM(@Parm_Ort)
    ,[DATUM] = @Parm_DATUM
    ,[MONTEUR] = RTRIM(@Parm_MONTEUR)
    ,[nWARTSUMME] = @Parm_nWARTSUMME
    ,[HISTORYIE] = RTRIM(@Parm_HISTORYIE)
    ,[VERTNR] = RTRIM(@Parm_VERTNR)
    ,[nVERTSUMME] = @Parm_nVERTSUMME
    ,[cBEGINN] = RTRIM(@Parm_cBEGINN)
    ,[cENDE] = RTRIM(@Parm_cENDE)
    ,[STAPEL] = RTRIM(@Parm_STAPEL)
    ,[OBJNAME1] = RTRIM(@Parm_OBJNAME1)
    ,[OBJName2] = RTRIM(@Parm_OBJName2)
    ,[OBJStrasse] = RTRIM(@Parm_OBJStrasse)
    ,[OBJPlz] = RTRIM(@Parm_OBJPlz)
    ,[OBJOrt] = RTRIM(@Parm_OBJOrt)
    ,[Sammeln] = @Parm_Sammeln
    ,[Status] = RTRIM(@Parm_Status)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Sel_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Wartungszeilen]
  @RI integer
AS
SELECT
     [KUNDENNR]
    ,[ANLAGENNR]
    ,[TEXT1]
    ,[TEXT2]
    ,[TEXT3]
    ,[TEXT4]
    ,[TEXT5]
    ,[OBJNR]
    ,[Name1]
    ,[Name2]
    ,[Strasse]
    ,[Plz]
    ,[Ort]
    ,[DATUM]
    ,[MONTEUR]
    ,[nWARTSUMME]
    ,[HISTORYIE]
    ,[VERTNR]
    ,[nVERTSUMME]
    ,[cBEGINN]
    ,[cENDE]
    ,[STAPEL]
    ,[OBJNAME1]
    ,[OBJName2]
    ,[OBJStrasse]
    ,[OBJPlz]
    ,[OBJOrt]
    ,[Sammeln]
    ,[Status]
    ,[PrjNr]
    ,[BelArt]
    ,[BelNr]
  FROM [dbo].[ut_Wartungszeilen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Del_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Del_Wartungszeilen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Wartungszeilen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Reorg_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Wartungszeilen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Wartungszeilen] 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_Wartungszeilen] 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_Wartungszeilen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_Append_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Append_Wartungszeilen]
  @Parm_KUNDENNR char(15)
 ,@Parm_ANLAGENNR char(20)
 ,@Parm_TEXT1 char(55)
 ,@Parm_TEXT2 char(55)
 ,@Parm_TEXT3 char(55)
 ,@Parm_TEXT4 char(55)
 ,@Parm_TEXT5 char(55)
 ,@Parm_OBJNR char(20)
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Strasse char(40)
 ,@Parm_Plz char(8)
 ,@Parm_Ort char(40)
 ,@Parm_DATUM datetime
 ,@Parm_MONTEUR char(40)
 ,@Parm_nWARTSUMME float
 ,@Parm_HISTORYIE char(254)
 ,@Parm_VERTNR char(20)
 ,@Parm_nVERTSUMME float
 ,@Parm_cBEGINN char(40)
 ,@Parm_cENDE char(40)
 ,@Parm_STAPEL char(20)
 ,@Parm_OBJNAME1 char(40)
 ,@Parm_OBJName2 char(40)
 ,@Parm_OBJStrasse char(40)
 ,@Parm_OBJPlz char(8)
 ,@Parm_OBJOrt char(40)
 ,@Parm_Sammeln tinyint
 ,@Parm_Status char(1)
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Wartungszeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Wartungszeilen] (
    RecId
   ,[KUNDENNR]
   ,[ANLAGENNR]
   ,[TEXT1]
   ,[TEXT2]
   ,[TEXT3]
   ,[TEXT4]
   ,[TEXT5]
   ,[OBJNR]
   ,[Name1]
   ,[Name2]
   ,[Strasse]
   ,[Plz]
   ,[Ort]
   ,[DATUM]
   ,[MONTEUR]
   ,[nWARTSUMME]
   ,[HISTORYIE]
   ,[VERTNR]
   ,[nVERTSUMME]
   ,[cBEGINN]
   ,[cENDE]
   ,[STAPEL]
   ,[OBJNAME1]
   ,[OBJName2]
   ,[OBJStrasse]
   ,[OBJPlz]
   ,[OBJOrt]
   ,[Sammeln]
   ,[Status]
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_KUNDENNR)
   ,RTRIM(@Parm_ANLAGENNR)
   ,RTRIM(@Parm_TEXT1)
   ,RTRIM(@Parm_TEXT2)
   ,RTRIM(@Parm_TEXT3)
   ,RTRIM(@Parm_TEXT4)
   ,RTRIM(@Parm_TEXT5)
   ,RTRIM(@Parm_OBJNR)
   ,RTRIM(@Parm_Name1)
   ,RTRIM(@Parm_Name2)
   ,RTRIM(@Parm_Strasse)
   ,RTRIM(@Parm_Plz)
   ,RTRIM(@Parm_Ort)
   ,@Parm_DATUM
   ,RTRIM(@Parm_MONTEUR)
   ,@Parm_nWARTSUMME
   ,RTRIM(@Parm_HISTORYIE)
   ,RTRIM(@Parm_VERTNR)
   ,@Parm_nVERTSUMME
   ,RTRIM(@Parm_cBEGINN)
   ,RTRIM(@Parm_cENDE)
   ,RTRIM(@Parm_STAPEL)
   ,RTRIM(@Parm_OBJNAME1)
   ,RTRIM(@Parm_OBJName2)
   ,RTRIM(@Parm_OBJStrasse)
   ,RTRIM(@Parm_OBJPlz)
   ,RTRIM(@Parm_OBJOrt)
   ,@Parm_Sammeln
   ,RTRIM(@Parm_Status)
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Wartungszeilen')
  DROP PROCEDURE [dbo].[up_TrSel_Wartungszeilen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Wartungszeilen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([KUNDENNR]))
      +LTRIM(RTRIM([ANLAGENNR]))
      +LTRIM(RTRIM([TEXT1]))
      +LTRIM(RTRIM([TEXT2]))
      +LTRIM(RTRIM([TEXT3]))
      +LTRIM(RTRIM([TEXT4]))
      +LTRIM(RTRIM([TEXT5]))
      +LTRIM(RTRIM([OBJNR]))
      +LTRIM(RTRIM([Name1]))
      +LTRIM(RTRIM([Name2]))
      +LTRIM(RTRIM([Strasse]))
      +LTRIM(RTRIM([Plz]))
      +LTRIM(RTRIM([Ort]))
      +LTRIM(RTRIM([MONTEUR]))
      +LTRIM(RTRIM([HISTORYIE]))
      +LTRIM(RTRIM([VERTNR]))
      +LTRIM(RTRIM([cBEGINN]))
      +LTRIM(RTRIM([cENDE]))
      +LTRIM(RTRIM([STAPEL]))
      +LTRIM(RTRIM([OBJNAME1]))
      +LTRIM(RTRIM([OBJName2]))
      +LTRIM(RTRIM([OBJStrasse]))
      +LTRIM(RTRIM([OBJPlz]))
      +LTRIM(RTRIM([OBJOrt]))
      +LTRIM(RTRIM([Status]))
      +LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
    AS Data
  FROM     [dbo].[ut_Wartungszeilen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Wartungszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_Wartungszeilen]
(KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_First_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Wartungszeilen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Wartungszeilen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Wartungszeilen_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus Wartungszeilen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_Wartungszeilen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen] ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Wartungszeilen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Wartungszeilen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen] ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Wartungszeilen_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKUNDENNR char(15)
  DECLARE @oldVERTNR char(20)
  DECLARE @oldANLAGENNR char(20)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr FROM [dbo].[ut_Wartungszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKUNDENNR,@oldVERTNR,@oldANLAGENNR,@oldPrjNr,@oldBelArt,@oldBelNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RecId > @oldRecId
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr > @oldPrjNr
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR > @oldANLAGENNR
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR > @oldVERTNR
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR > @oldKUNDENNR
                   ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Wartungszeilen_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKUNDENNR char(15)
  DECLARE @oldVERTNR char(20)
  DECLARE @oldANLAGENNR char(20)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr FROM [dbo].[ut_Wartungszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKUNDENNR,@oldVERTNR,@oldANLAGENNR,@oldPrjNr,@oldBelArt,@oldBelNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND RecId < @oldRecId
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND PrjNr < @oldPrjNr
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR < @oldANLAGENNR
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR = @oldKUNDENNR AND VERTNR < @oldVERTNR
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE KUNDENNR < @oldKUNDENNR
                   ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Wartungszeilen_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 15
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE SUBSTRING(KUNDENNR,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 15
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 35
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND SUBSTRING(VERTNR,1,@xLen-15) = SUBSTRING(@xKey,16,@xLen-15)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 35
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 55
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND SUBSTRING(ANLAGENNR,1,@xLen-35) = SUBSTRING(@xKey,36,@xLen-35)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 55
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 63
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND SUBSTRING(PrjNr,1,@xLen-55) = SUBSTRING(@xKey,56,@xLen-55)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 63
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 65
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND SUBSTRING(BelArt,1,@xLen-63) = SUBSTRING(@xKey,64,@xLen-63)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 65
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 73
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2) AND SUBSTRING(BelNr,1,@xLen-65) = SUBSTRING(@xKey,66,@xLen-65)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 73
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2) AND BelNr = SUBSTRING(@xKey,66,8)
      ORDER BY KUNDENNR,VERTNR,ANLAGENNR,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Wartungszeilen_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 15
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE SUBSTRING(KUNDENNR,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 15
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 35
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND SUBSTRING(VERTNR,1,@xLen-15) = SUBSTRING(@xKey,16,@xLen-15)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 35
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 55
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND SUBSTRING(ANLAGENNR,1,@xLen-35) = SUBSTRING(@xKey,36,@xLen-35)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 55
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 63
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND SUBSTRING(PrjNr,1,@xLen-55) = SUBSTRING(@xKey,56,@xLen-55)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 63
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 65
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND SUBSTRING(BelArt,1,@xLen-63) = SUBSTRING(@xKey,64,@xLen-63)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 65
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 73
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2) AND SUBSTRING(BelNr,1,@xLen-65) = SUBSTRING(@xKey,66,@xLen-65)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 73
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE KUNDENNR = SUBSTRING(@xKey,1,15) AND VERTNR = SUBSTRING(@xKey,16,20) AND ANLAGENNR = SUBSTRING(@xKey,36,20) AND PrjNr = SUBSTRING(@xKey,56,8) AND BelArt = SUBSTRING(@xKey,64,2) AND BelNr = SUBSTRING(@xKey,66,8)
      ORDER BY KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,PrjNr DESC,BelArt DESC,BelNr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Wartungszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_Wartungszeilen]
(Status,KUNDENNR,VERTNR,ANLAGENNR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_First_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_Last_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_Next_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Wartungszeilen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Wartungszeilen_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Wartungszeilen_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus Wartungszeilen in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_Wartungszeilen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen] ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Wartungszeilen in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Wartungszeilen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen] ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Wartungszeilen_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldKUNDENNR char(15)
  DECLARE @oldVERTNR char(20)
  DECLARE @oldANLAGENNR char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,KUNDENNR,VERTNR,ANLAGENNR FROM [dbo].[ut_Wartungszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldKUNDENNR,@oldVERTNR,@oldANLAGENNR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND RecId > @oldRecId
                   ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR > @oldANLAGENNR
                   ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR > @oldVERTNR
                   ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR > @oldKUNDENNR
                   ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status > @oldStatus
                   ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Wartungszeilen_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldKUNDENNR char(15)
  DECLARE @oldVERTNR char(20)
  DECLARE @oldANLAGENNR char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,KUNDENNR,VERTNR,ANLAGENNR FROM [dbo].[ut_Wartungszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldKUNDENNR,@oldVERTNR,@oldANLAGENNR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR = @oldANLAGENNR AND RecId < @oldRecId
                   ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR = @oldVERTNR AND ANLAGENNR < @oldANLAGENNR
                   ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR = @oldKUNDENNR AND VERTNR < @oldVERTNR
                   ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status = @oldStatus AND KUNDENNR < @oldKUNDENNR
                   ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
                   WHERE Status < @oldStatus
                   ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Wartungszeilen_Key1] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND SUBSTRING(KUNDENNR,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND SUBSTRING(VERTNR,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen < 56
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20) AND SUBSTRING(ANLAGENNR,1,@xLen-36) = SUBSTRING(@xKey,37,@xLen-36)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  IF @xLen = 56
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20) AND ANLAGENNR = SUBSTRING(@xKey,37,20)
      ORDER BY Status,KUNDENNR,VERTNR,ANLAGENNR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Wartungszeilen_Key1] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND SUBSTRING(KUNDENNR,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND SUBSTRING(VERTNR,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 56
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20) AND SUBSTRING(ANLAGENNR,1,@xLen-36) = SUBSTRING(@xKey,37,@xLen-36)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 56
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Wartungszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND KUNDENNR = SUBSTRING(@xKey,2,15) AND VERTNR = SUBSTRING(@xKey,17,20) AND ANLAGENNR = SUBSTRING(@xKey,37,20)
      ORDER BY Status DESC,KUNDENNR DESC,VERTNR DESC,ANLAGENNR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

