------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Monteurberichte')
BEGIN
CREATE TABLE [dbo].[ut_Monteurberichte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BERINR] char(8) DEFAULT ' '
  ,[LFDNR] char(4) DEFAULT ' '
  ,[SATZART] char(2) DEFAULT ' '
  ,[PRJNR] char(8) DEFAULT ' '
  ,[KDNR] char(15) DEFAULT ' '
  ,[STATUS] char(1) DEFAULT ' '
  ,[DATAKEY] char(20) DEFAULT ' '
  ,[DATATEXT] char(42) DEFAULT ' '
  ,[DATAMENGE] float DEFAULT 0
  ,[DATAPREIS] float DEFAULT 0
  ,[DATAEK] float DEFAULT 0
  ,[DATAZEIT] float DEFAULT 0
  ,[DATAEINH] integer DEFAULT 0
  ,[DATAMWST] integer DEFAULT 0
  ,[DATAKW] integer DEFAULT 0
  ,[DATAMIN0] integer DEFAULT 0
  ,[DATAMIN1] integer DEFAULT 0
  ,[DATAMIN2] integer DEFAULT 0
  ,[DATAMIN3] integer DEFAULT 0
  ,[DATAMIN4] integer DEFAULT 0
  ,[DATAMIN5] integer DEFAULT 0
  ,[DATAMIN6] integer DEFAULT 0
  CONSTRAINT [Pk_Monteurberichte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Monteurberichte_KeyDel')
  DROP TRIGGER [dbo].[tr_Monteurberichte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Monteurberichte_KeyUpd')
  DROP TRIGGER [dbo].[tr_Monteurberichte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Monteurberichte_Keys')
  DROP TABLE [dbo].[ut_Monteurberichte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'BERINR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [BERINR] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [LFDNR] char(4) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'SATZART')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [SATZART] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [PRJNR] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'KDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] 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_Monteurberichte' and sc.[name] = 'STATUS')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [STATUS] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'DATAKEY')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAKEY] 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_Monteurberichte' and sc.[name] = 'DATATEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATATEXT] char(42) 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_Monteurberichte' and sc.[name] = 'DATAMENGE')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMENGE] 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_Monteurberichte' and sc.[name] = 'DATAPREIS')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAPREIS] 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_Monteurberichte' and sc.[name] = 'DATAEK')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAEK] 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_Monteurberichte' and sc.[name] = 'DATAZEIT')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAZEIT] 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_Monteurberichte' and sc.[name] = 'DATAEINH')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAEINH] 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_Monteurberichte' and sc.[name] = 'DATAMWST')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMWST] 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_Monteurberichte' and sc.[name] = 'DATAKW')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAKW] 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_Monteurberichte' and sc.[name] = 'DATAMIN0')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN0] 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_Monteurberichte' and sc.[name] = 'DATAMIN1')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN1] 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_Monteurberichte' and sc.[name] = 'DATAMIN2')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN2] 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_Monteurberichte' and sc.[name] = 'DATAMIN3')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN3] 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_Monteurberichte' and sc.[name] = 'DATAMIN4')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN4] 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_Monteurberichte' and sc.[name] = 'DATAMIN5')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN5] 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_Monteurberichte' and sc.[name] = 'DATAMIN6')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ADD [DATAMIN6] integer NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'BERINR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [BERINR] char(8)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [LFDNR] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'SATZART')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [SATZART] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [PRJNR] char(8)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'KDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [KDNR] char(15)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'STATUS')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [STATUS] char(1)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'DATAKEY')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [DATAKEY] char(20)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Monteurberichte' and sc.[name] = 'DATATEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Monteurberichte] ALTER COLUMN [DATATEXT] char(42)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Monteurberichte')
  DROP VIEW [dbo].[Monteurberichte]
GO
CREATE VIEW [dbo].[Monteurberichte] AS
  SELECT * FROM [dbo].[ut_Monteurberichte]
GO

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

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Monteurberichte')
  DROP PROCEDURE [dbo].[up_Upd_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_Upd_Monteurberichte]
  @RI integer
 ,@Parm_BERINR char(8)
 ,@Parm_LFDNR char(4)
 ,@Parm_SATZART char(2)
 ,@Parm_PRJNR char(8)
 ,@Parm_KDNR char(15)
 ,@Parm_STATUS char(1)
 ,@Parm_DATAKEY char(20)
 ,@Parm_DATATEXT char(42)
 ,@Parm_DATAMENGE float
 ,@Parm_DATAPREIS float
 ,@Parm_DATAEK float
 ,@Parm_DATAZEIT float
 ,@Parm_DATAEINH integer
 ,@Parm_DATAMWST integer
 ,@Parm_DATAKW integer
 ,@Parm_DATAMIN0 integer
 ,@Parm_DATAMIN1 integer
 ,@Parm_DATAMIN2 integer
 ,@Parm_DATAMIN3 integer
 ,@Parm_DATAMIN4 integer
 ,@Parm_DATAMIN5 integer
 ,@Parm_DATAMIN6 integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Monteurberichte] SET
     [BERINR] = RTRIM(@Parm_BERINR)
    ,[LFDNR] = RTRIM(@Parm_LFDNR)
    ,[SATZART] = RTRIM(@Parm_SATZART)
    ,[PRJNR] = RTRIM(@Parm_PRJNR)
    ,[KDNR] = RTRIM(@Parm_KDNR)
    ,[STATUS] = RTRIM(@Parm_STATUS)
    ,[DATAKEY] = RTRIM(@Parm_DATAKEY)
    ,[DATATEXT] = RTRIM(@Parm_DATATEXT)
    ,[DATAMENGE] = @Parm_DATAMENGE
    ,[DATAPREIS] = @Parm_DATAPREIS
    ,[DATAEK] = @Parm_DATAEK
    ,[DATAZEIT] = @Parm_DATAZEIT
    ,[DATAEINH] = @Parm_DATAEINH
    ,[DATAMWST] = @Parm_DATAMWST
    ,[DATAKW] = @Parm_DATAKW
    ,[DATAMIN0] = @Parm_DATAMIN0
    ,[DATAMIN1] = @Parm_DATAMIN1
    ,[DATAMIN2] = @Parm_DATAMIN2
    ,[DATAMIN3] = @Parm_DATAMIN3
    ,[DATAMIN4] = @Parm_DATAMIN4
    ,[DATAMIN5] = @Parm_DATAMIN5
    ,[DATAMIN6] = @Parm_DATAMIN6
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Monteurberichte')
  DROP PROCEDURE [dbo].[up_Sel_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_Sel_Monteurberichte]
  @RI integer
AS
SELECT
     [BERINR]
    ,[LFDNR]
    ,[SATZART]
    ,[PRJNR]
    ,[KDNR]
    ,[STATUS]
    ,[DATAKEY]
    ,[DATATEXT]
    ,[DATAMENGE]
    ,[DATAPREIS]
    ,[DATAEK]
    ,[DATAZEIT]
    ,[DATAEINH]
    ,[DATAMWST]
    ,[DATAKW]
    ,[DATAMIN0]
    ,[DATAMIN1]
    ,[DATAMIN2]
    ,[DATAMIN3]
    ,[DATAMIN4]
    ,[DATAMIN5]
    ,[DATAMIN6]
  FROM [dbo].[ut_Monteurberichte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Monteurberichte')
  DROP PROCEDURE [dbo].[up_Del_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_Del_Monteurberichte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Monteurberichte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Monteurberichte')
  DROP PROCEDURE [dbo].[up_Reorg_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Monteurberichte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Monteurberichte] 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_Monteurberichte] 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_Monteurberichte] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Monteurberichte')
  DROP PROCEDURE [dbo].[up_Append_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_Append_Monteurberichte]
  @Parm_BERINR char(8)
 ,@Parm_LFDNR char(4)
 ,@Parm_SATZART char(2)
 ,@Parm_PRJNR char(8)
 ,@Parm_KDNR char(15)
 ,@Parm_STATUS char(1)
 ,@Parm_DATAKEY char(20)
 ,@Parm_DATATEXT char(42)
 ,@Parm_DATAMENGE float
 ,@Parm_DATAPREIS float
 ,@Parm_DATAEK float
 ,@Parm_DATAZEIT float
 ,@Parm_DATAEINH integer
 ,@Parm_DATAMWST integer
 ,@Parm_DATAKW integer
 ,@Parm_DATAMIN0 integer
 ,@Parm_DATAMIN1 integer
 ,@Parm_DATAMIN2 integer
 ,@Parm_DATAMIN3 integer
 ,@Parm_DATAMIN4 integer
 ,@Parm_DATAMIN5 integer
 ,@Parm_DATAMIN6 integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Monteurberichte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Monteurberichte] (
    RecId
   ,[BERINR]
   ,[LFDNR]
   ,[SATZART]
   ,[PRJNR]
   ,[KDNR]
   ,[STATUS]
   ,[DATAKEY]
   ,[DATATEXT]
   ,[DATAMENGE]
   ,[DATAPREIS]
   ,[DATAEK]
   ,[DATAZEIT]
   ,[DATAEINH]
   ,[DATAMWST]
   ,[DATAKW]
   ,[DATAMIN0]
   ,[DATAMIN1]
   ,[DATAMIN2]
   ,[DATAMIN3]
   ,[DATAMIN4]
   ,[DATAMIN5]
   ,[DATAMIN6]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BERINR)
   ,RTRIM(@Parm_LFDNR)
   ,RTRIM(@Parm_SATZART)
   ,RTRIM(@Parm_PRJNR)
   ,RTRIM(@Parm_KDNR)
   ,RTRIM(@Parm_STATUS)
   ,RTRIM(@Parm_DATAKEY)
   ,RTRIM(@Parm_DATATEXT)
   ,@Parm_DATAMENGE
   ,@Parm_DATAPREIS
   ,@Parm_DATAEK
   ,@Parm_DATAZEIT
   ,@Parm_DATAEINH
   ,@Parm_DATAMWST
   ,@Parm_DATAKW
   ,@Parm_DATAMIN0
   ,@Parm_DATAMIN1
   ,@Parm_DATAMIN2
   ,@Parm_DATAMIN3
   ,@Parm_DATAMIN4
   ,@Parm_DATAMIN5
   ,@Parm_DATAMIN6
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Monteurberichte')
  DROP PROCEDURE [dbo].[up_TrSel_Monteurberichte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Monteurberichte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BERINR]))
      +LTRIM(RTRIM([LFDNR]))
      +LTRIM(RTRIM([SATZART]))
      +LTRIM(RTRIM([PRJNR]))
      +LTRIM(RTRIM([KDNR]))
      +LTRIM(RTRIM([STATUS]))
      +LTRIM(RTRIM([DATAKEY]))
      +LTRIM(RTRIM([DATATEXT]))
    AS Data
  FROM     [dbo].[ut_Monteurberichte]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_Monteurberichte]
(BERINR,LFDNR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY BERINR,LFDNR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BERINR,LFDNR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBERINR char(8)
  DECLARE @oldLFDNR char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BERINR,LFDNR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBERINR,@oldLFDNR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR = @oldBERINR AND LFDNR = @oldLFDNR AND RecId > @oldRecId
                   ORDER BY BERINR,LFDNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR = @oldBERINR AND LFDNR > @oldLFDNR
                   ORDER BY BERINR,LFDNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR > @oldBERINR
                   ORDER BY BERINR,LFDNR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY BERINR DESC,LFDNR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBERINR char(8)
  DECLARE @oldLFDNR char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BERINR,LFDNR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBERINR,@oldLFDNR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR = @oldBERINR AND LFDNR = @oldLFDNR AND RecId < @oldRecId
                   ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR = @oldBERINR AND LFDNR < @oldLFDNR
                   ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE BERINR < @oldBERINR
                   ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY BERINR,LFDNR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(BERINR,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BERINR,LFDNR,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8)
      ORDER BY BERINR,LFDNR,RecId
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8) AND SUBSTRING(LFDNR,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BERINR,LFDNR,RecId
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8) AND LFDNR = SUBSTRING(@xKey,9,4)
      ORDER BY BERINR,LFDNR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY BERINR DESC,LFDNR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(BERINR,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8)
      ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8) AND SUBSTRING(LFDNR,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE BERINR = SUBSTRING(@xKey,1,8) AND LFDNR = SUBSTRING(@xKey,9,4)
      ORDER BY BERINR DESC,LFDNR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_Monteurberichte]
(SATZART,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND BERINR > @oldBERINR
                   ORDER BY SATZART,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key1] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,BERINR,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(BERINR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART,BERINR,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND BERINR = SUBSTRING(@xKey,3,8)
      ORDER BY SATZART,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key1] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(BERINR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND BERINR = SUBSTRING(@xKey,3,8)
      ORDER BY SATZART DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key2')
CREATE INDEX [Ik_Key2] ON [dbo].[ut_Monteurberichte]
(SATZART,PRJNR,KDNR,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key2')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key2]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,PRJNR,KDNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldPRJNR,@oldKDNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR > @oldBERINR
                   ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR > @oldKDNR
                   ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR > @oldPRJNR
                   ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,PRJNR,KDNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldPRJNR,@oldKDNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR = @oldPRJNR AND KDNR < @oldKDNR
                   ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND PRJNR < @oldPRJNR
                   ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(PRJNR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND SUBSTRING(KDNR,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15) AND SUBSTRING(BERINR,1,@xLen-25) = SUBSTRING(@xKey,26,@xLen-25)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15) AND BERINR = SUBSTRING(@xKey,26,8)
      ORDER BY SATZART,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(PRJNR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND SUBSTRING(KDNR,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15) AND SUBSTRING(BERINR,1,@xLen-25) = SUBSTRING(@xKey,26,@xLen-25)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND PRJNR = SUBSTRING(@xKey,3,8) AND KDNR = SUBSTRING(@xKey,11,15) AND BERINR = SUBSTRING(@xKey,26,8)
      ORDER BY SATZART DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key3')
CREATE INDEX [Ik_Key3] ON [dbo].[ut_Monteurberichte]
(SATZART,KDNR,PRJNR,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key3')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key3]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key3 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key3 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key3] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldKDNR char(15)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,KDNR,PRJNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldKDNR,@oldPRJNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR > @oldBERINR
                   ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR > @oldPRJNR
                   ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR > @oldKDNR
                   ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key3] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldKDNR char(15)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,KDNR,PRJNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldKDNR,@oldPRJNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR = @oldKDNR AND PRJNR < @oldPRJNR
                   ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND KDNR < @oldKDNR
                   ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key3] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(KDNR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND SUBSTRING(PRJNR,1,@xLen-17) = SUBSTRING(@xKey,18,@xLen-17)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8) AND SUBSTRING(BERINR,1,@xLen-25) = SUBSTRING(@xKey,26,@xLen-25)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8) AND BERINR = SUBSTRING(@xKey,26,8)
      ORDER BY SATZART,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key3] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(KDNR,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND SUBSTRING(PRJNR,1,@xLen-17) = SUBSTRING(@xKey,18,@xLen-17)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 25
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8) AND SUBSTRING(BERINR,1,@xLen-25) = SUBSTRING(@xKey,26,@xLen-25)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 33
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND KDNR = SUBSTRING(@xKey,3,15) AND PRJNR = SUBSTRING(@xKey,18,8) AND BERINR = SUBSTRING(@xKey,26,8)
      ORDER BY SATZART DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key4')
CREATE INDEX [Ik_Key4] ON [dbo].[ut_Monteurberichte]
(SATZART,STATUS,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key4')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key4]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key4 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,STATUS,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key4 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,STATUS,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key4] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,STATUS,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND BERINR > @oldBERINR
                   ORDER BY SATZART,STATUS,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS > @oldSTATUS
                   ORDER BY SATZART,STATUS,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,STATUS,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key4] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS < @oldSTATUS
                   ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,STATUS,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key4] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,STATUS,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,STATUS,BERINR,RecId
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART,STATUS,BERINR,RecId
    RETURN
  END

  IF @xLen < 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(BERINR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART,STATUS,BERINR,RecId
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND BERINR = SUBSTRING(@xKey,4,8)
      ORDER BY SATZART,STATUS,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key4] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(BERINR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND BERINR = SUBSTRING(@xKey,4,8)
      ORDER BY SATZART DESC,STATUS DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key5')
CREATE INDEX [Ik_Key5] ON [dbo].[ut_Monteurberichte]
(SATZART,STATUS,PRJNR,KDNR,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key5')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key5]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key5 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key5 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key5] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,PRJNR,KDNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldPRJNR,@oldKDNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR > @oldBERINR
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR > @oldKDNR
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR > @oldPRJNR
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS > @oldSTATUS
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key5] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,PRJNR,KDNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldPRJNR,@oldKDNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR = @oldKDNR AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR = @oldPRJNR AND KDNR < @oldKDNR
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND PRJNR < @oldPRJNR
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS < @oldSTATUS
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key5] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(PRJNR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND SUBSTRING(KDNR,1,@xLen-11) = SUBSTRING(@xKey,12,@xLen-11)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15) AND SUBSTRING(BERINR,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15) AND BERINR = SUBSTRING(@xKey,27,8)
      ORDER BY SATZART,STATUS,PRJNR,KDNR,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key5] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(PRJNR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND SUBSTRING(KDNR,1,@xLen-11) = SUBSTRING(@xKey,12,@xLen-11)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15) AND SUBSTRING(BERINR,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND PRJNR = SUBSTRING(@xKey,4,8) AND KDNR = SUBSTRING(@xKey,12,15) AND BERINR = SUBSTRING(@xKey,27,8)
      ORDER BY SATZART DESC,STATUS DESC,PRJNR DESC,KDNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key6')
CREATE INDEX [Ik_Key6] ON [dbo].[ut_Monteurberichte]
(SATZART,STATUS,KDNR,PRJNR,BERINR,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key6')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key6]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key6 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key6 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key6] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldKDNR char(15)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,KDNR,PRJNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldKDNR,@oldPRJNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR = @oldBERINR AND RecId > @oldRecId
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR > @oldBERINR
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR > @oldPRJNR
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR > @oldKDNR
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS > @oldSTATUS
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key6] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldSTATUS char(1)
  DECLARE @oldKDNR char(15)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBERINR char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,STATUS,KDNR,PRJNR,BERINR FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldSTATUS,@oldKDNR,@oldPRJNR,@oldBERINR
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR = @oldBERINR AND RecId < @oldRecId
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR = @oldPRJNR AND BERINR < @oldBERINR
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR = @oldKDNR AND PRJNR < @oldPRJNR
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS = @oldSTATUS AND KDNR < @oldKDNR
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND STATUS < @oldSTATUS
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key6] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(KDNR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND SUBSTRING(PRJNR,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8) AND SUBSTRING(BERINR,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8) AND BERINR = SUBSTRING(@xKey,27,8)
      ORDER BY SATZART,STATUS,KDNR,PRJNR,BERINR,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key6] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND SUBSTRING(KDNR,1,@xLen-3) = SUBSTRING(@xKey,4,@xLen-3)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND SUBSTRING(PRJNR,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen < 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8) AND SUBSTRING(BERINR,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  IF @xLen = 34
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND STATUS = SUBSTRING(@xKey,3,1) AND KDNR = SUBSTRING(@xKey,4,15) AND PRJNR = SUBSTRING(@xKey,19,8) AND BERINR = SUBSTRING(@xKey,27,8)
      ORDER BY SATZART DESC,STATUS DESC,KDNR DESC,PRJNR DESC,BERINR DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Monteurberichte'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key7')
CREATE INDEX [Ik_Key7] ON [dbo].[ut_Monteurberichte]
(SATZART,DATATEXT,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_First_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_Last_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_Prior_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_Next_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_Prior50_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_Next50_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_FindL_Monteurberichte_Key7]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Monteurberichte_Key7')
  DROP PROCEDURE [dbo].[up_FindF_Monteurberichte_Key7]
GO

------------------------------------------------------------
/* ersten Datensatz aus Monteurberichte in Folge Key7 ermittln */
CREATE PROCEDURE [dbo].[up_First_Monteurberichte_Key7] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART,DATATEXT,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Monteurberichte in Folge Key7 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Monteurberichte_Key7] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte] ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SATZART,DATATEXT,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Monteurberichte_Key7] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldDATATEXT char(42)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,DATATEXT FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldDATATEXT
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND DATATEXT = @oldDATATEXT AND RecId > @oldRecId
                   ORDER BY SATZART,DATATEXT,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND DATATEXT > @oldDATATEXT
                   ORDER BY SATZART,DATATEXT,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART > @oldSATZART
                   ORDER BY SATZART,DATATEXT,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Monteurberichte_Key7] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSATZART char(2)
  DECLARE @oldDATATEXT char(42)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SATZART,DATATEXT FROM [dbo].[ut_Monteurberichte] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSATZART,@oldDATATEXT
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND DATATEXT = @oldDATATEXT AND RecId < @oldRecId
                   ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART = @oldSATZART AND DATATEXT < @oldDATATEXT
                   ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
                   WHERE SATZART < @oldSATZART
                   ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY SATZART,DATATEXT,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Monteurberichte_Key7] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART,DATATEXT,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART,DATATEXT,RecId
    RETURN
  END

  IF @xLen < 44
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(DATATEXT,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART,DATATEXT,RecId
    RETURN
  END

  IF @xLen = 44
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND DATATEXT = SUBSTRING(@xKey,3,42)
      ORDER BY SATZART,DATATEXT,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Monteurberichte_Key7] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SUBSTRING(SATZART,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2)
      ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
    RETURN
  END

  IF @xLen < 44
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND SUBSTRING(DATATEXT,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
    RETURN
  END

  IF @xLen = 44
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Monteurberichte]
      WHERE SATZART = SUBSTRING(@xKey,1,2) AND DATATEXT = SUBSTRING(@xKey,3,42)
      ORDER BY SATZART DESC,DATATEXT DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

