------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Ausgleichszeilen')
BEGIN
CREATE TABLE [dbo].[ut_Ausgleichszeilen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Status] char(1) DEFAULT ' '
  ,[Typ] char(1) DEFAULT ' '
  ,[Konto] char(8) DEFAULT ' '
  ,[TransNo] char(8) DEFAULT ' '
  ,[DstTyp] char(1) DEFAULT ' '
  ,[DstKonto] char(8) DEFAULT ' '
  ,[DstBetrag] float DEFAULT 0
  ,[SkoKonto] char(8) DEFAULT ' '
  ,[SkoBetrag] float DEFAULT 0
  ,[RestKonto] char(8) DEFAULT ' '
  ,[RestBetrag] float DEFAULT 0
  CONSTRAINT [Pk_Ausgleichszeilen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Ausgleichszeilen_KeyDel')
  DROP TRIGGER [dbo].[tr_Ausgleichszeilen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Ausgleichszeilen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Ausgleichszeilen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Ausgleichszeilen_Keys')
  DROP TABLE [dbo].[ut_Ausgleichszeilen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] 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_Ausgleichszeilen' and sc.[name] = 'Typ')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [Typ] 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_Ausgleichszeilen' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [Konto] 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_Ausgleichszeilen' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [TransNo] 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_Ausgleichszeilen' and sc.[name] = 'DstTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [DstTyp] 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_Ausgleichszeilen' and sc.[name] = 'DstKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [DstKonto] 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_Ausgleichszeilen' and sc.[name] = 'DstBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [DstBetrag] 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_Ausgleichszeilen' and sc.[name] = 'SkoKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [SkoKonto] 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_Ausgleichszeilen' and sc.[name] = 'SkoBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [SkoBetrag] 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_Ausgleichszeilen' and sc.[name] = 'RestKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [RestKonto] 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_Ausgleichszeilen' and sc.[name] = 'RestBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ADD [RestBetrag] float NOT NULL DEFAULT 0
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [Status] char(1)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'Typ')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [Typ] char(1)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [Konto] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [TransNo] char(8)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'DstTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [DstTyp] char(1)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'DstKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [DstKonto] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'SkoKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [SkoKonto] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Ausgleichszeilen' and sc.[name] = 'RestKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Ausgleichszeilen] ALTER COLUMN [RestKonto] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Ausgleichszeilen')
  DROP VIEW [dbo].[Ausgleichszeilen]
GO
CREATE VIEW [dbo].[Ausgleichszeilen] AS
  SELECT * FROM [dbo].[ut_Ausgleichszeilen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Add_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Add_Ausgleichszeilen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Ausgleichszeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Ausgleichszeilen] (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_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Upd_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Ausgleichszeilen]
  @RI integer
 ,@Parm_Status char(1)
 ,@Parm_Typ char(1)
 ,@Parm_Konto char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_DstTyp char(1)
 ,@Parm_DstKonto char(8)
 ,@Parm_DstBetrag float
 ,@Parm_SkoKonto char(8)
 ,@Parm_SkoBetrag float
 ,@Parm_RestKonto char(8)
 ,@Parm_RestBetrag float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Ausgleichszeilen] SET
     [Status] = RTRIM(@Parm_Status)
    ,[Typ] = RTRIM(@Parm_Typ)
    ,[Konto] = RTRIM(@Parm_Konto)
    ,[TransNo] = RTRIM(@Parm_TransNo)
    ,[DstTyp] = RTRIM(@Parm_DstTyp)
    ,[DstKonto] = RTRIM(@Parm_DstKonto)
    ,[DstBetrag] = @Parm_DstBetrag
    ,[SkoKonto] = RTRIM(@Parm_SkoKonto)
    ,[SkoBetrag] = @Parm_SkoBetrag
    ,[RestKonto] = RTRIM(@Parm_RestKonto)
    ,[RestBetrag] = @Parm_RestBetrag
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Sel_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Ausgleichszeilen]
  @RI integer
AS
SELECT
     [Status]
    ,[Typ]
    ,[Konto]
    ,[TransNo]
    ,[DstTyp]
    ,[DstKonto]
    ,[DstBetrag]
    ,[SkoKonto]
    ,[SkoBetrag]
    ,[RestKonto]
    ,[RestBetrag]
  FROM [dbo].[ut_Ausgleichszeilen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Del_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Del_Ausgleichszeilen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Reorg_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Ausgleichszeilen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Ausgleichszeilen] 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_Ausgleichszeilen] 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_Ausgleichszeilen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_Append_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_Append_Ausgleichszeilen]
  @Parm_Status char(1)
 ,@Parm_Typ char(1)
 ,@Parm_Konto char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_DstTyp char(1)
 ,@Parm_DstKonto char(8)
 ,@Parm_DstBetrag float
 ,@Parm_SkoKonto char(8)
 ,@Parm_SkoBetrag float
 ,@Parm_RestKonto char(8)
 ,@Parm_RestBetrag float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Ausgleichszeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Ausgleichszeilen] (
    RecId
   ,[Status]
   ,[Typ]
   ,[Konto]
   ,[TransNo]
   ,[DstTyp]
   ,[DstKonto]
   ,[DstBetrag]
   ,[SkoKonto]
   ,[SkoBetrag]
   ,[RestKonto]
   ,[RestBetrag]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Status)
   ,RTRIM(@Parm_Typ)
   ,RTRIM(@Parm_Konto)
   ,RTRIM(@Parm_TransNo)
   ,RTRIM(@Parm_DstTyp)
   ,RTRIM(@Parm_DstKonto)
   ,@Parm_DstBetrag
   ,RTRIM(@Parm_SkoKonto)
   ,@Parm_SkoBetrag
   ,RTRIM(@Parm_RestKonto)
   ,@Parm_RestBetrag
       )
  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_Ausgleichszeilen')
  DROP PROCEDURE [dbo].[up_TrSel_Ausgleichszeilen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Ausgleichszeilen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Status]))
      +LTRIM(RTRIM([Typ]))
      +LTRIM(RTRIM([Konto]))
      +LTRIM(RTRIM([TransNo]))
      +LTRIM(RTRIM([DstTyp]))
      +LTRIM(RTRIM([DstKonto]))
      +LTRIM(RTRIM([SkoKonto]))
      +LTRIM(RTRIM([RestKonto]))
    AS Data
  FROM     [dbo].[ut_Ausgleichszeilen]
  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_Ausgleichszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_Ausgleichszeilen]
(Status,Typ,Konto,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Ausgleichszeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus Ausgleichszeilen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Status,Typ,Konto,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Ausgleichszeilen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Status,Typ,Konto,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,Typ,Konto,TransNo FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldTyp,@oldKonto,@oldTransNo
  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_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY Status,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo > @oldTransNo
                   ORDER BY Status,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto > @oldKonto
                   ORDER BY Status,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ > @oldTyp
                   ORDER BY Status,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status > @oldStatus
                   ORDER BY Status,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,Typ,Konto,TransNo FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldTyp,@oldKonto,@oldTransNo
  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_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo < @oldTransNo
                   ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ = @oldTyp AND Konto < @oldKonto
                   ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND Typ < @oldTyp
                   ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status < @oldStatus
                   ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Status,Typ,Konto,TransNo,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key00] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND SUBSTRING(Konto,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8) AND SUBSTRING(TransNo,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8) AND TransNo = SUBSTRING(@xKey,11,8)
      ORDER BY Status,Typ,Konto,TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key00] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND SUBSTRING(Konto,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8) AND SUBSTRING(TransNo,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND Typ = SUBSTRING(@xKey,2,1) AND Konto = SUBSTRING(@xKey,3,8) AND TransNo = SUBSTRING(@xKey,11,8)
      ORDER BY Status DESC,Typ DESC,Konto DESC,TransNo 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_Ausgleichszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_Ausgleichszeilen]
(Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Ausgleichszeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus Ausgleichszeilen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Ausgleichszeilen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldDstTyp char(1)
  DECLARE @oldDstKonto char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,DstTyp,DstKonto,Typ,Konto,TransNo FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldDstTyp,@oldDstKonto,@oldTyp,@oldKonto,@oldTransNo
  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_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo > @oldTransNo
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto > @oldKonto
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ > @oldTyp
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto > @oldDstKonto
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp > @oldDstTyp
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status > @oldStatus
                   ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldStatus char(1)
  DECLARE @oldDstTyp char(1)
  DECLARE @oldDstKonto char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Status,DstTyp,DstKonto,Typ,Konto,TransNo FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldStatus,@oldDstTyp,@oldDstKonto,@oldTyp,@oldKonto,@oldTransNo
  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_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto = @oldKonto AND TransNo < @oldTransNo
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ = @oldTyp AND Konto < @oldKonto
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto = @oldDstKonto AND Typ < @oldTyp
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp = @oldDstTyp AND DstKonto < @oldDstKonto
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status = @oldStatus AND DstTyp < @oldDstTyp
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Status < @oldStatus
                   ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND SUBSTRING(DstKonto,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 19
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND SUBSTRING(Konto,1,@xLen-11) = SUBSTRING(@xKey,12,@xLen-11)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 19
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8) AND SUBSTRING(TransNo,1,@xLen-19) = SUBSTRING(@xKey,20,@xLen-19)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8) AND TransNo = SUBSTRING(@xKey,20,8)
      ORDER BY Status,DstTyp,DstKonto,Typ,Konto,TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND SUBSTRING(DstKonto,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 11
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 19
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND SUBSTRING(Konto,1,@xLen-11) = SUBSTRING(@xKey,12,@xLen-11)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 19
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8) AND SUBSTRING(TransNo,1,@xLen-19) = SUBSTRING(@xKey,20,@xLen-19)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Status = SUBSTRING(@xKey,1,1) AND DstTyp = SUBSTRING(@xKey,2,1) AND DstKonto = SUBSTRING(@xKey,3,8) AND Typ = SUBSTRING(@xKey,11,1) AND Konto = SUBSTRING(@xKey,12,8) AND TransNo = SUBSTRING(@xKey,20,8)
      ORDER BY Status DESC,DstTyp DESC,DstKonto DESC,Typ DESC,Konto DESC,TransNo 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_Ausgleichszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key02')
CREATE INDEX [Ik_Key02] ON [dbo].[ut_Ausgleichszeilen]
(Typ,Konto,TransNo,Status,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next50_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Ausgleichszeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key02]
GO

------------------------------------------------------------
/* ersten Datensatz aus Ausgleichszeilen in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Typ,Konto,TransNo,Status,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Ausgleichszeilen in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Typ,Konto,TransNo,Status,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  DECLARE @oldStatus char(1)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Typ,Konto,TransNo,Status FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTyp,@oldKonto,@oldTransNo,@oldStatus
  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_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND Status = @oldStatus AND RecId > @oldRecId
                   ORDER BY Typ,Konto,TransNo,Status,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND Status > @oldStatus
                   ORDER BY Typ,Konto,TransNo,Status,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo > @oldTransNo
                   ORDER BY Typ,Konto,TransNo,Status,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto > @oldKonto
                   ORDER BY Typ,Konto,TransNo,Status,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ > @oldTyp
                   ORDER BY Typ,Konto,TransNo,Status,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 Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  DECLARE @oldStatus char(1)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Typ,Konto,TransNo,Status FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTyp,@oldKonto,@oldTransNo,@oldStatus
  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_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND Status = @oldStatus AND RecId < @oldRecId
                   ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo = @oldTransNo AND Status < @oldStatus
                   ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto = @oldKonto AND TransNo < @oldTransNo
                   ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ = @oldTyp AND Konto < @oldKonto
                   ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE Typ < @oldTyp
                   ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status 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 Typ,Konto,TransNo,Status,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key02] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  IF @xLen < 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND SUBSTRING(Konto,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  IF @xLen = 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  IF @xLen < 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND SUBSTRING(TransNo,1,@xLen-9) = SUBSTRING(@xKey,10,@xLen-9)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  IF @xLen = 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND TransNo = SUBSTRING(@xKey,10,8)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND TransNo = SUBSTRING(@xKey,10,8) AND Status = SUBSTRING(@xKey,18,1)
      ORDER BY Typ,Konto,TransNo,Status,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key02] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
    RETURN
  END

  IF @xLen < 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND SUBSTRING(Konto,1,@xLen-1) = SUBSTRING(@xKey,2,@xLen-1)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
    RETURN
  END

  IF @xLen = 9
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
    RETURN
  END

  IF @xLen < 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND SUBSTRING(TransNo,1,@xLen-9) = SUBSTRING(@xKey,10,@xLen-9)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
    RETURN
  END

  IF @xLen = 17
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND TransNo = SUBSTRING(@xKey,10,8)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
      WHERE Typ = SUBSTRING(@xKey,1,1) AND Konto = SUBSTRING(@xKey,2,8) AND TransNo = SUBSTRING(@xKey,10,8) AND Status = SUBSTRING(@xKey,18,1)
      ORDER BY Typ DESC,Konto DESC,TransNo DESC,Status 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_Ausgleichszeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key03')
CREATE INDEX [Ik_Key03] ON [dbo].[ut_Ausgleichszeilen]
(TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next50_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindL_Ausgleichszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Ausgleichszeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindF_Ausgleichszeilen_Key03]
GO

------------------------------------------------------------
/* ersten Datensatz aus Ausgleichszeilen in Folge Key03 ermittln */
CREATE PROCEDURE [dbo].[up_First_Ausgleichszeilen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Ausgleichszeilen in Folge Key03 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Ausgleichszeilen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen] ORDER BY TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Ausgleichszeilen_Key03] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TransNo FROM [dbo].[ut_Ausgleichszeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTransNo
  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_Ausgleichszeilen]
                   WHERE TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Ausgleichszeilen]
                   WHERE TransNo > @oldTransNo
                   ORDER BY TransNo,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 TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Ausgleichszeilen_Key03] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTransNo char(8)

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

