------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Mahnstufen')
BEGIN
CREATE TABLE [dbo].[ut_Mahnstufen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nummer] char(2) DEFAULT ' '
  ,[Stufe] char(2) DEFAULT ' '
  ,[FaelligIn] integer DEFAULT 0
  ,[Warten] integer DEFAULT 0
  ,[Vortext] char(250) DEFAULT ' '
  ,[Vortext1] char(250) DEFAULT ' '
  ,[Vortext2] char(12) DEFAULT ' '
  ,[Nachtext] char(250) DEFAULT ' '
  ,[Nachtext1] char(250) DEFAULT ' '
  ,[Nachtext2] char(12) DEFAULT ' '
  ,[Gebuehr] float DEFAULT 0
  ,[Reserv] char(100) DEFAULT ' '
  CONSTRAINT [Pk_Mahnstufen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mahnstufen_KeyDel')
  DROP TRIGGER [dbo].[tr_Mahnstufen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mahnstufen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Mahnstufen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Mahnstufen_Keys')
  DROP TABLE [dbo].[ut_Mahnstufen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Nummer] 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_Mahnstufen' and sc.[name] = 'Stufe')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Stufe] 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_Mahnstufen' and sc.[name] = 'FaelligIn')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [FaelligIn] 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_Mahnstufen' and sc.[name] = 'Warten')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Warten] 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_Mahnstufen' and sc.[name] = 'Vortext')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Vortext] char(250) 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_Mahnstufen' and sc.[name] = 'Vortext1')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Vortext1] char(250) 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_Mahnstufen' and sc.[name] = 'Vortext2')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Vortext2] char(12) 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_Mahnstufen' and sc.[name] = 'Nachtext')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Nachtext] char(250) 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_Mahnstufen' and sc.[name] = 'Nachtext1')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Nachtext1] char(250) 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_Mahnstufen' and sc.[name] = 'Nachtext2')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Nachtext2] char(12) 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_Mahnstufen' and sc.[name] = 'Gebuehr')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Gebuehr] 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_Mahnstufen' and sc.[name] = 'Reserv')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ADD [Reserv] char(100) NOT NULL DEFAULT ' '
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Nummer] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Stufe')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Stufe] char(2)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Vortext')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Vortext] char(250)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Vortext1')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Vortext1] char(250)
END
GO
IF 12 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Vortext2')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Vortext2] char(12)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Nachtext')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Nachtext] char(250)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Nachtext1')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Nachtext1] char(250)
END
GO
IF 12 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Nachtext2')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Nachtext2] char(12)
END
GO
IF 100 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnstufen' and sc.[name] = 'Reserv')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnstufen] ALTER COLUMN [Reserv] char(100)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Mahnstufen')
  DROP VIEW [dbo].[Mahnstufen]
GO
CREATE VIEW [dbo].[Mahnstufen] AS
  SELECT * FROM [dbo].[ut_Mahnstufen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Add_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Add_Mahnstufen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Mahnstufen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mahnstufen] (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_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Upd_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Mahnstufen]
  @RI integer
 ,@Parm_Nummer char(2)
 ,@Parm_Stufe char(2)
 ,@Parm_FaelligIn integer
 ,@Parm_Warten integer
 ,@Parm_Vortext char(250)
 ,@Parm_Vortext1 char(250)
 ,@Parm_Vortext2 char(12)
 ,@Parm_Nachtext char(250)
 ,@Parm_Nachtext1 char(250)
 ,@Parm_Nachtext2 char(12)
 ,@Parm_Gebuehr float
 ,@Parm_Reserv char(100)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Mahnstufen] SET
     [Nummer] = RTRIM(@Parm_Nummer)
    ,[Stufe] = RTRIM(@Parm_Stufe)
    ,[FaelligIn] = @Parm_FaelligIn
    ,[Warten] = @Parm_Warten
    ,[Vortext] = RTRIM(@Parm_Vortext)
    ,[Vortext1] = RTRIM(@Parm_Vortext1)
    ,[Vortext2] = RTRIM(@Parm_Vortext2)
    ,[Nachtext] = RTRIM(@Parm_Nachtext)
    ,[Nachtext1] = RTRIM(@Parm_Nachtext1)
    ,[Nachtext2] = RTRIM(@Parm_Nachtext2)
    ,[Gebuehr] = @Parm_Gebuehr
    ,[Reserv] = RTRIM(@Parm_Reserv)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Sel_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Mahnstufen]
  @RI integer
AS
SELECT
     [Nummer]
    ,[Stufe]
    ,[FaelligIn]
    ,[Warten]
    ,[Vortext]
    ,[Vortext1]
    ,[Vortext2]
    ,[Nachtext]
    ,[Nachtext1]
    ,[Nachtext2]
    ,[Gebuehr]
    ,[Reserv]
  FROM [dbo].[ut_Mahnstufen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Del_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Del_Mahnstufen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Mahnstufen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Reorg_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Mahnstufen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Mahnstufen] 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_Mahnstufen] 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_Mahnstufen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Mahnstufen')
  DROP PROCEDURE [dbo].[up_Append_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_Append_Mahnstufen]
  @Parm_Nummer char(2)
 ,@Parm_Stufe char(2)
 ,@Parm_FaelligIn integer
 ,@Parm_Warten integer
 ,@Parm_Vortext char(250)
 ,@Parm_Vortext1 char(250)
 ,@Parm_Vortext2 char(12)
 ,@Parm_Nachtext char(250)
 ,@Parm_Nachtext1 char(250)
 ,@Parm_Nachtext2 char(12)
 ,@Parm_Gebuehr float
 ,@Parm_Reserv char(100)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Mahnstufen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mahnstufen] (
    RecId
   ,[Nummer]
   ,[Stufe]
   ,[FaelligIn]
   ,[Warten]
   ,[Vortext]
   ,[Vortext1]
   ,[Vortext2]
   ,[Nachtext]
   ,[Nachtext1]
   ,[Nachtext2]
   ,[Gebuehr]
   ,[Reserv]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nummer)
   ,RTRIM(@Parm_Stufe)
   ,@Parm_FaelligIn
   ,@Parm_Warten
   ,RTRIM(@Parm_Vortext)
   ,RTRIM(@Parm_Vortext1)
   ,RTRIM(@Parm_Vortext2)
   ,RTRIM(@Parm_Nachtext)
   ,RTRIM(@Parm_Nachtext1)
   ,RTRIM(@Parm_Nachtext2)
   ,@Parm_Gebuehr
   ,RTRIM(@Parm_Reserv)
       )
  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_Mahnstufen')
  DROP PROCEDURE [dbo].[up_TrSel_Mahnstufen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Mahnstufen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nummer]))
      +LTRIM(RTRIM([Stufe]))
      +LTRIM(RTRIM([Vortext]))
      +LTRIM(RTRIM([Vortext1]))
      +LTRIM(RTRIM([Vortext2]))
      +LTRIM(RTRIM([Nachtext]))
      +LTRIM(RTRIM([Nachtext1]))
      +LTRIM(RTRIM([Nachtext2]))
      +LTRIM(RTRIM([Reserv]))
    AS Data
  FROM     [dbo].[ut_Mahnstufen]
  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_Mahnstufen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_Mahnstufen]
(Nummer,Stufe,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_First_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Mahnstufen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mahnstufen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Mahnstufen_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus Mahnstufen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_Mahnstufen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen] ORDER BY Nummer,Stufe,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Mahnstufen in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Mahnstufen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen] ORDER BY Nummer DESC,Stufe DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Nummer,Stufe,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Mahnstufen_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNummer char(2)
  DECLARE @oldStufe char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nummer,Stufe FROM [dbo].[ut_Mahnstufen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNummer,@oldStufe
  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_Mahnstufen]
                   WHERE Nummer = @oldNummer AND Stufe = @oldStufe AND RecId > @oldRecId
                   ORDER BY Nummer,Stufe,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
                   WHERE Nummer = @oldNummer AND Stufe > @oldStufe
                   ORDER BY Nummer,Stufe,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
                   WHERE Nummer > @oldNummer
                   ORDER BY Nummer,Stufe,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 Nummer DESC,Stufe DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Mahnstufen_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNummer char(2)
  DECLARE @oldStufe char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nummer,Stufe FROM [dbo].[ut_Mahnstufen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNummer,@oldStufe
  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_Mahnstufen]
                   WHERE Nummer = @oldNummer AND Stufe = @oldStufe AND RecId < @oldRecId
                   ORDER BY Nummer DESC,Stufe DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
                   WHERE Nummer = @oldNummer AND Stufe < @oldStufe
                   ORDER BY Nummer DESC,Stufe DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
                   WHERE Nummer < @oldNummer
                   ORDER BY Nummer DESC,Stufe 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 Nummer,Stufe,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Mahnstufen_Key0] @xKey char(128),@xLen integer AS

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

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

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
      WHERE Nummer = SUBSTRING(@xKey,1,2) AND SUBSTRING(Stufe,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Nummer,Stufe,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
      WHERE Nummer = SUBSTRING(@xKey,1,2) AND Stufe = SUBSTRING(@xKey,3,2)
      ORDER BY Nummer,Stufe,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
      WHERE Nummer = SUBSTRING(@xKey,1,2) AND SUBSTRING(Stufe,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Nummer DESC,Stufe DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Mahnstufen]
      WHERE Nummer = SUBSTRING(@xKey,1,2) AND Stufe = SUBSTRING(@xKey,3,2)
      ORDER BY Nummer DESC,Stufe DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

