-- 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_WartungsObjekte')
BEGIN
CREATE TABLE [dbo].[ut_WartungsObjekte]
(
  [RecId]  integer NOT NULL
 ,[Ident] integer DEFAULT 0
 ,[Nr] char(20) DEFAULT ''
 ,[VertragId] integer DEFAULT 0
 ,[Bez1] char(40) DEFAULT ''
 ,[Bez2] char(40) DEFAULT ''
 ,[KdNr] char(15) DEFAULT ''
 ,[KdNrRng] char(15) DEFAULT ''
 ,[Standort] char(15) DEFAULT ''
 ,[HausMstr] char(15) DEFAULT ''
 ,[Mieter] char(15) DEFAULT ''
 ,[GeoDaten] char(40) DEFAULT ''
 ,[Entfernung] float DEFAULT 0
 ,[Sammel] integer DEFAULT 0
  CONSTRAINT [Pk_WartungsObjekte]  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_WartungsObjekte_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsObjekte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsObjekte_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsObjekte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsObjekte_Keys')
  DROP TABLE [dbo].[ut_WartungsObjekte_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_WartungsObjekte' and sc.[name] = 'Ident')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'VertragId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [VertragId] 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_WartungsObjekte' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [KdNr] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'KdNrRng')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'Standort')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [Standort] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'HausMstr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [HausMstr] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'Mieter')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' and sc.[name] = 'GeoDaten')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [GeoDaten] 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_WartungsObjekte' and sc.[name] = 'Entfernung')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [Entfernung] 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_WartungsObjekte' and sc.[name] = 'Sammel')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsObjekte] ADD [Sammel] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [Bez2] 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_WartungsObjekte' and sc.[name] = 'KdNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [KdNr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'KdNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'KdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [KdNr] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'KdNrRng'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'KdNrRng'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [KdNrRng] 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_WartungsObjekte' and sc.[name] = 'Standort'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'Standort'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [Standort] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'Standort'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'Standort'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [Standort] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'HausMstr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'HausMstr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [HausMstr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'HausMstr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'HausMstr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [HausMstr] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] 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_WartungsObjekte' 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_WartungsObjekte' and sc.[name] = 'Mieter'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [Mieter] varchar(15)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'GeoDaten'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'GeoDaten'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [GeoDaten] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'GeoDaten'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsObjekte' and sc.[name] = 'GeoDaten'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsObjekte] ALTER COLUMN [GeoDaten] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsObjekte')
  DROP VIEW [dbo].[WartungsObjekte]
GO
CREATE VIEW [dbo].[WartungsObjekte] AS
  SELECT [RecId]
 ,[Ident]
 ,[Nr]
 ,[VertragId]
 ,[Bez1]
 ,[Bez2]
 ,[KdNr]
 ,[KdNrRng]
 ,[Standort]
 ,[HausMstr]
 ,[Mieter]
 ,[GeoDaten]
 ,[Entfernung]
 ,[Sammel]
  FROM [dbo].[ut_WartungsObjekte]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Sel_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsObjekte]  @mRI integer
AS
  SELECT
    [Ident]
   ,[Nr]
   ,[VertragId]
   ,[Bez1]
   ,[Bez2]
   ,[KdNr]
   ,[KdNrRng]
   ,[Standort]
   ,[HausMstr]
   ,[Mieter]
   ,[GeoDaten]
   ,[Entfernung]
   ,[Sammel]
  FROM [dbo].[ut_WartungsObjekte] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Append_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsObjekte]
   @Param_Ident integer
  ,@Param_Nr char(20)
  ,@Param_VertragId integer
  ,@Param_Bez1 char(40)
  ,@Param_Bez2 char(40)
  ,@Param_KdNr char(15)
  ,@Param_KdNrRng char(15)
  ,@Param_Standort char(15)
  ,@Param_HausMstr char(15)
  ,@Param_Mieter char(15)
  ,@Param_GeoDaten char(40)
  ,@Param_Entfernung float
  ,@Param_Sammel integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsObjekte()
      INSERT INTO [dbo].[ut_WartungsObjekte] (RecId
        ,[Ident]
        ,[Nr]
        ,[VertragId]
        ,[Bez1]
        ,[Bez2]
        ,[KdNr]
        ,[KdNrRng]
        ,[Standort]
        ,[HausMstr]
        ,[Mieter]
        ,[GeoDaten]
        ,[Entfernung]
        ,[Sammel]
          ) VALUES (@NeueId
        ,@Param_Ident
        ,UPPER(RTRIM(@Param_Nr))
        ,@Param_VertragId
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Bez2)
        ,UPPER(RTRIM(@Param_KdNr))
        ,UPPER(RTRIM(@Param_KdNrRng))
        ,UPPER(RTRIM(@Param_Standort))
        ,UPPER(RTRIM(@Param_HausMstr))
        ,UPPER(RTRIM(@Param_Mieter))
        ,RTRIM(@Param_GeoDaten)
        ,@Param_Entfernung
        ,@Param_Sammel
          )
      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_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Add_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsObjekte]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsObjekte()
  INSERT INTO [dbo].[ut_WartungsObjekte] (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_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Upd_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsObjekte] @mRI integer
  ,@Param_Ident integer
  ,@Param_Nr char(20)
  ,@Param_VertragId integer
  ,@Param_Bez1 char(40)
  ,@Param_Bez2 char(40)
  ,@Param_KdNr char(15)
  ,@Param_KdNrRng char(15)
  ,@Param_Standort char(15)
  ,@Param_HausMstr char(15)
  ,@Param_Mieter char(15)
  ,@Param_GeoDaten char(40)
  ,@Param_Entfernung float
  ,@Param_Sammel integer
AS
  UPDATE [dbo].[ut_WartungsObjekte] SET
    [Ident] = @Param_Ident
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[VertragId] = @Param_VertragId
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[KdNrRng] = UPPER(RTRIM(@Param_KdNrRng))
   ,[Standort] = UPPER(RTRIM(@Param_Standort))
   ,[HausMstr] = UPPER(RTRIM(@Param_HausMstr))
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[GeoDaten] = RTRIM(@Param_GeoDaten)
   ,[Entfernung] = @Param_Entfernung
   ,[Sammel] = @Param_Sammel
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsObjekte] @Param_RecId varchar(20)
  ,@Param_Ident varchar(8)
  ,@Param_Nr varchar(20)
  ,@Param_VertragId varchar(8)
  ,@Param_Bez1 varchar(40)
  ,@Param_Bez2 varchar(40)
  ,@Param_KdNr varchar(15)
  ,@Param_KdNrRng varchar(15)
  ,@Param_Standort varchar(15)
  ,@Param_HausMstr varchar(15)
  ,@Param_Mieter varchar(15)
  ,@Param_GeoDaten varchar(40)
  ,@Param_Entfernung varchar(6)
  ,@Param_Sammel varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsObjekte] SET
    [Ident] = dbo.itcfunc_integer(@Param_Ident)
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[VertragId] = dbo.itcfunc_integer(@Param_VertragId)
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
   ,[KdNrRng] = UPPER(RTRIM(@Param_KdNrRng))
   ,[Standort] = UPPER(RTRIM(@Param_Standort))
   ,[HausMstr] = UPPER(RTRIM(@Param_HausMstr))
   ,[Mieter] = UPPER(RTRIM(@Param_Mieter))
   ,[GeoDaten] = RTRIM(@Param_GeoDaten)
   ,[Entfernung] = dbo.itcfunc_float(@Param_Entfernung)
   ,[Sammel] = dbo.itcfunc_integer(@Param_Sammel)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsObjekte] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsObjekte]
AS
  SELECT RecId,
    LTRIM(RTRIM([Nr]))
   +LTRIM(RTRIM([Bez1]))
   +LTRIM(RTRIM([Bez2]))
   +LTRIM(RTRIM([KdNr]))
   +LTRIM(RTRIM([KdNrRng]))
   +LTRIM(RTRIM([Standort]))
   +LTRIM(RTRIM([HausMstr]))
   +LTRIM(RTRIM([Mieter]))
   +LTRIM(RTRIM([GeoDaten]))
  AS Data FROM [dbo].[ut_WartungsObjekte] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsObjekte')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsObjekte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsObjekte] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte] )
  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_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_WartungsObjekte]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsObjekte] (Ident,RecId)
GO

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsObjekte_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsObjekte_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsObjekte_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_WartungsObjekte] 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_WartungsObjekte_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsObjekte_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsObjekte_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsObjekte_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsObjekte_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsObjekte_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_WartungsObjekte] 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_WartungsObjekte_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_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_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_WartungsObjekte]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_WartungsObjekte] (Nr,Ident,RecId)
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsObjekte_Key01')
  DROP PROCEDURE [dbo].[up_Last_WartungsObjekte_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsObjekte_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsObjekte_Key01] AS EXECUTE [dbo].[up_Last_WartungsObjekte_Key01]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key01] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsObjekte_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsObjekte_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsObjekte_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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsObjekte_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsObjekte_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsObjekte_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsObjekte_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsObjekte_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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte] 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_WartungsObjekte_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key01] @xKey, @xLen')
GO


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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key02) WHERE 
      KdNr = @mKdNr AND 
      Nr = @mNr
      ORDER BY KdNr,Nr,RecId
    RETURN
  END

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

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

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

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

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

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

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

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key02) WHERE 
      KdNr = @mKdNr AND 
      Nr = @mNr
      ORDER BY KdNr desc,Nr desc,RecId desc
    RETURN
  END

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

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

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

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

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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03)
    WHERE (Standort > @oldStandort)
    ORDER BY Standort,KdNr,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03)
    WHERE (Standort < @oldStandort)
    ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr = @mNr
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

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

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

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr = @mNr
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(Standort,1,LEN(@mStandort)) = @mStandort
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key03) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04)
    WHERE (VertragId > @oldVertragId)
    ORDER BY VertragId,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_WartungsObjekte_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key04] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04)
    WHERE (VertragId = @oldVertragId) AND (Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY VertragId 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_WartungsObjekte] WITH(INDEX = Ik_Key04)
    WHERE (VertragId = @oldVertragId) AND (Nr = @oldNr) AND (Ident < @oldIdent)
    ORDER BY VertragId 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_WartungsObjekte] WITH(INDEX = Ik_Key04)
    WHERE (VertragId = @oldVertragId) AND (Nr < @oldNr)
    ORDER BY VertragId 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_WartungsObjekte] WITH(INDEX = Ik_Key04)
    WHERE (VertragId < @oldVertragId)
    ORDER BY VertragId 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_WartungsObjekte_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key04] @oldRecId')
GO

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

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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId AND 
      Nr = @mNr
      ORDER BY VertragId,Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId
      ORDER BY VertragId,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_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY VertragId,Nr,Ident,RecId
    RETURN
  END


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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId AND 
      Nr = @mNr
      ORDER BY VertragId desc,Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId
      ORDER BY VertragId 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_WartungsObjekte] WITH(INDEX = Ik_Key04) WHERE 
      VertragId = @mVertragId AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY VertragId 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_WartungsObjekte_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key04] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key04] @xKey, @xLen')
GO


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

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

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

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

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key05)
    WHERE (Sammel > @oldSammel)
    ORDER BY Sammel,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_WartungsObjekte_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key05] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key05)
    WHERE (Sammel = @oldSammel) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY Sammel 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_WartungsObjekte] WITH(INDEX = Ik_Key05)
    WHERE (Sammel = @oldSammel) AND (Ident < @oldIdent)
    ORDER BY Sammel 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_WartungsObjekte] WITH(INDEX = Ik_Key05)
    WHERE (Sammel < @oldSammel)
    ORDER BY Sammel 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_WartungsObjekte_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key05] @oldRecId')
GO

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

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

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

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key05) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel,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_WartungsObjekte_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsObjekte_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsObjekte_Key05] @xKey, @xLen')
GO

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

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key05) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel 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_WartungsObjekte_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key05] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsObjekte_Key06')
  DROP PROCEDURE [dbo].[up_First_WartungsObjekte_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsObjekte_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) ORDER BY Sammel,KdNr,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsObjekte_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsObjekte_Key06] AS EXECUTE [dbo].[up_First_WartungsObjekte_Key06]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsObjekte_Key06')
  DROP PROCEDURE [dbo].[up_Next_WartungsObjekte_Key06]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsObjekte_Key06] @oldRecId integer AS
  DECLARE @oldSammel integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldIdent integer
  SELECT @oldSammel = Sammel, @oldKdNr = KdNr, @oldIdent = Ident FROM [dbo].[ut_WartungsObjekte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr = @oldKdNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY Sammel,KdNr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr = @oldKdNr) AND (Ident > @oldIdent)
    ORDER BY Sammel,KdNr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr > @oldKdNr)
    ORDER BY Sammel,KdNr,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel > @oldSammel)
    ORDER BY Sammel,KdNr,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_WartungsObjekte_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key06] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsObjekte_Key06')
  DROP PROCEDURE [dbo].[up_Prior_WartungsObjekte_Key06]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsObjekte_Key06] @oldRecId integer AS
  DECLARE @oldSammel integer
  DECLARE @oldKdNr char(15)
  DECLARE @oldIdent integer
  SELECT @oldSammel = Sammel, @oldKdNr = KdNr, @oldIdent = Ident FROM [dbo].[ut_WartungsObjekte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr = @oldKdNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY Sammel desc,KdNr 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_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr = @oldKdNr) AND (Ident < @oldIdent)
    ORDER BY Sammel desc,KdNr 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_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel = @oldSammel) AND (KdNr < @oldKdNr)
    ORDER BY Sammel desc,KdNr 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_WartungsObjekte] WITH(INDEX = Ik_Key06)
    WHERE (Sammel < @oldSammel)
    ORDER BY Sammel desc,KdNr 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_WartungsObjekte_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key06] @oldRecId')
GO

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

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

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

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      KdNr = @mKdNr AND 
      Ident = @mIdent
      ORDER BY Sammel,KdNr,Ident,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      KdNr = @mKdNr
      ORDER BY Sammel,KdNr,Ident,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel,KdNr,Ident,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Sammel,KdNr,Ident,RecId
    RETURN
  END


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

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

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      KdNr = @mKdNr AND 
      Ident = @mIdent
      ORDER BY Sammel desc,KdNr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      KdNr = @mKdNr
      ORDER BY Sammel desc,KdNr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel desc,KdNr desc,Ident desc,RecId desc
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key06) WHERE 
      Sammel = @mSammel AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      ORDER BY Sammel desc,KdNr 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_WartungsObjekte_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key06] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key06] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsObjekte_Key07')
  DROP PROCEDURE [dbo].[up_First_WartungsObjekte_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsObjekte_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) ORDER BY Sammel,Standort,Ident,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsObjekte_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsObjekte_Key07] AS EXECUTE [dbo].[up_First_WartungsObjekte_Key07]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsObjekte_Key07')
  DROP PROCEDURE [dbo].[up_Next_WartungsObjekte_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsObjekte_Key07] @oldRecId integer AS
  DECLARE @oldSammel integer
  DECLARE @oldStandort char(15)
  DECLARE @oldIdent integer
  SELECT @oldSammel = Sammel, @oldStandort = Standort, @oldIdent = Ident FROM [dbo].[ut_WartungsObjekte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort = @oldStandort) AND (Ident = @oldIdent) AND (RecId > @oldRecId)
    ORDER BY Sammel,Standort,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort = @oldStandort) AND (Ident > @oldIdent)
    ORDER BY Sammel,Standort,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort > @oldStandort)
    ORDER BY Sammel,Standort,Ident,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel > @oldSammel)
    ORDER BY Sammel,Standort,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_WartungsObjekte_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsObjekte_Key07')
  DROP PROCEDURE [dbo].[up_Prior_WartungsObjekte_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsObjekte_Key07] @oldRecId integer AS
  DECLARE @oldSammel integer
  DECLARE @oldStandort char(15)
  DECLARE @oldIdent integer
  SELECT @oldSammel = Sammel, @oldStandort = Standort, @oldIdent = Ident FROM [dbo].[ut_WartungsObjekte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort = @oldStandort) AND (Ident = @oldIdent) AND (RecId < @oldRecId)
    ORDER BY Sammel desc,Standort 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_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort = @oldStandort) AND (Ident < @oldIdent)
    ORDER BY Sammel desc,Standort 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_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel = @oldSammel) AND (Standort < @oldStandort)
    ORDER BY Sammel desc,Standort 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_WartungsObjekte] WITH(INDEX = Ik_Key07)
    WHERE (Sammel < @oldSammel)
    ORDER BY Sammel desc,Standort 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_WartungsObjekte_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key07] @oldRecId')
GO

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

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

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

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      Standort = @mStandort AND 
      Ident = @mIdent
      ORDER BY Sammel,Standort,Ident,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      Standort = @mStandort
      ORDER BY Sammel,Standort,Ident,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel,Standort,Ident,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      Standort >= @mStandort AND SUBSTRING(Standort,1,LEN(@mStandort)) = @mStandort
      ORDER BY Sammel,Standort,Ident,RecId
    RETURN
  END


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

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

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      Standort = @mStandort AND 
      Ident = @mIdent
      ORDER BY Sammel desc,Standort desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      Standort = @mStandort
      ORDER BY Sammel desc,Standort desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel
      ORDER BY Sammel desc,Standort desc,Ident desc,RecId desc
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key07) WHERE 
      Sammel = @mSammel AND 
      SUBSTRING(Standort,1,LEN(@mStandort)) = @mStandort
      ORDER BY Sammel desc,Standort 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_WartungsObjekte_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key07] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key07] @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_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  DROP INDEX [Ik_Key08] ON [dbo].[ut_WartungsObjekte]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_WartungsObjekte' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  CREATE INDEX [Ik_Key08] ON [dbo].[ut_WartungsObjekte] (Nr,Ident,RecId)
GO

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId > @oldRecId)) AND ((Nr <> ''))
    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_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr = @oldNr) AND (Ident > @oldIdent)) AND ((Nr <> ''))
    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_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr > @oldNr)) AND ((Nr <> ''))
    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_WartungsObjekte_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsObjekte_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsObjekte_Key08] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr = @oldNr) AND (Ident = @oldIdent) AND (RecId < @oldRecId)) AND ((Nr <> ''))
    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_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr = @oldNr) AND (Ident < @oldIdent)) AND ((Nr <> ''))
    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_WartungsObjekte] WITH(INDEX = Ik_Key08)
    WHERE ((Nr < @oldNr)) AND ((Nr <> ''))
    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_WartungsObjekte_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsObjekte_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsObjekte_Key08] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsObjekte_Key08')
  DROP PROCEDURE [dbo].[up_FindF_WartungsObjekte_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsObjekte_Key08] @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_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      Nr = @mNr AND 
      Ident = @mIdent
      AND ((Nr <> ''))
      ORDER BY Nr,Ident,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      Nr = @mNr
      AND ((Nr <> ''))
      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_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      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_WartungsObjekte_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsObjekte_Key08] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsObjekte_Key08] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsObjekte_Key08')
  DROP PROCEDURE [dbo].[up_FindL_WartungsObjekte_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsObjekte_Key08] @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_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      Nr = @mNr AND 
      Ident = @mIdent
      AND ((Nr <> ''))
      ORDER BY Nr desc,Ident desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      Nr = @mNr
      AND ((Nr <> ''))
      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_WartungsObjekte] WITH(INDEX = Ik_Key08) WHERE 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      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_WartungsObjekte_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsObjekte_Key08] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsObjekte_Key08] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsObjekte_Key09')
  DROP PROCEDURE [dbo].[up_First_WartungsObjekte_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsObjekte_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE (Nr <> '') ORDER BY KdNr,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsObjekte_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsObjekte_Key09] AS EXECUTE [dbo].[up_First_WartungsObjekte_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsObjekte_Key09')
  DROP PROCEDURE [dbo].[up_Last_WartungsObjekte_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsObjekte_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE (Nr <> '') ORDER BY KdNr desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsObjekte_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsObjekte_Key09] AS EXECUTE [dbo].[up_Last_WartungsObjekte_Key09]')
GO

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

  DECLARE @newRecId integer

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY KdNr,Nr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr
      AND ((Nr <> ''))
      ORDER BY KdNr,Nr,RecId
    RETURN
  END

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

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

  IF @xLen < 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY KdNr,Nr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr
      AND ((Nr <> ''))
      ORDER BY KdNr desc,Nr desc,RecId desc
    RETURN
  END

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

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

  IF @xLen < 35 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key09) WHERE 
      KdNr = @mKdNr AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY KdNr desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsObjekte_Key10')
  DROP PROCEDURE [dbo].[up_First_WartungsObjekte_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsObjekte_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE (Nr <> '') ORDER BY Standort,KdNr,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsObjekte_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsObjekte_Key10] AS EXECUTE [dbo].[up_First_WartungsObjekte_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsObjekte_Key10')
  DROP PROCEDURE [dbo].[up_Last_WartungsObjekte_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsObjekte_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE (Nr <> '') ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsObjekte_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsObjekte_Key10] AS EXECUTE [dbo].[up_Last_WartungsObjekte_Key10]')
GO

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

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10)
    WHERE ((Standort = @oldStandort) AND (KdNr = @oldKdNr) AND (Nr > @oldNr)) AND ((Nr <> ''))
    ORDER BY Standort,KdNr,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10)
    WHERE ((Standort = @oldStandort) AND (KdNr > @oldKdNr)) AND ((Nr <> ''))
    ORDER BY Standort,KdNr,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10)
    WHERE ((Standort > @oldStandort)) AND ((Nr <> ''))
    ORDER BY Standort,KdNr,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10)
    WHERE ((Standort = @oldStandort) AND (KdNr < @oldKdNr)) AND ((Nr <> ''))
    ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10)
    WHERE ((Standort < @oldStandort)) AND ((Nr <> ''))
    ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort >= @mStandort AND SUBSTRING(Standort,1,LEN(@mStandort)) = @mStandort
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr >= @mKdNr AND SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY Standort,KdNr,Nr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      Nr = @mNr
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(Standort,1,LEN(@mStandort)) = @mStandort
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      SUBSTRING(KdNr,1,LEN(@mKdNr)) = @mKdNr
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 50 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsObjekte] WITH(INDEX = Ik_Key10) WHERE 
      Standort = @mStandort AND 
      KdNr = @mKdNr AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      AND ((Nr <> ''))
      ORDER BY Standort desc,KdNr desc,Nr desc,RecId desc
    RETURN
  END

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


