------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_ZahlOp')
BEGIN
CREATE TABLE [dbo].[ut_ZahlOp]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[BuchNr] char(8) DEFAULT ' '
  ,[Datum] char(10) DEFAULT ' '
  ,[Betrag] float DEFAULT 0
  ,[Waehrung] char(6) DEFAULT ' '
  ,[Kurs] float DEFAULT 0
  ,[Ausgl] integer DEFAULT 0
  ,[BuchText] char(100) DEFAULT ' '
  ,[FibuKtoVon] char(8) DEFAULT ' '
  ,[FibuKtoAuf] char(8) DEFAULT ' '
  ,[BankKtoVon] char(20) DEFAULT ' '
  ,[BankKtoAuf] char(20) DEFAULT ' '
  ,[BLZVon] char(8) DEFAULT ' '
  ,[BLZAuf] char(8) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  CONSTRAINT [Pk_ZahlOp]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ZahlOp_KeyDel')
  DROP TRIGGER [dbo].[tr_ZahlOp_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ZahlOp_KeyUpd')
  DROP TRIGGER [dbo].[tr_ZahlOp_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ZahlOp_Keys')
  DROP TABLE [dbo].[ut_ZahlOp_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BelArt] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BelNr] 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_ZahlOp' and sc.[name] = 'BuchNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BuchNr] 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_ZahlOp' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [Datum] char(10) 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_ZahlOp' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [Betrag] 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_ZahlOp' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [Waehrung] char(6) 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_ZahlOp' and sc.[name] = 'Kurs')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [Kurs] 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_ZahlOp' and sc.[name] = 'Ausgl')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [Ausgl] 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_ZahlOp' and sc.[name] = 'BuchText')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BuchText] char(100) 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_ZahlOp' and sc.[name] = 'FibuKtoVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [FibuKtoVon] 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_ZahlOp' and sc.[name] = 'FibuKtoAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [FibuKtoAuf] 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_ZahlOp' and sc.[name] = 'BankKtoVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BankKtoVon] 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_ZahlOp' and sc.[name] = 'BankKtoAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BankKtoAuf] 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_ZahlOp' and sc.[name] = 'BLZVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BLZVon] 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_ZahlOp' and sc.[name] = 'BLZAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [BLZAuf] 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_ZahlOp' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ADD [PrjNr] char(8) 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_ZahlOp' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BelNr] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BuchNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BuchNr] char(8)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [Datum] char(10)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [Waehrung] char(6)
END
GO
IF 100 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BuchText')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BuchText] char(100)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'FibuKtoVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [FibuKtoVon] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'FibuKtoAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [FibuKtoAuf] char(8)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BankKtoVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BankKtoVon] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BankKtoAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BankKtoAuf] char(20)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BLZVon')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BLZVon] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'BLZAuf')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [BLZAuf] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZahlOp' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZahlOp] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ZahlOp')
  DROP VIEW [dbo].[ZahlOp]
GO
CREATE VIEW [dbo].[ZahlOp] AS
  SELECT * FROM [dbo].[ut_ZahlOp]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_ZahlOp')
  DROP PROCEDURE [dbo].[up_Add_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Add_ZahlOp] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_ZahlOp])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ZahlOp] (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_ZahlOp')
  DROP PROCEDURE [dbo].[up_Upd_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Upd_ZahlOp]
  @RI integer
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BuchNr char(8)
 ,@Parm_Datum char(10)
 ,@Parm_Betrag float
 ,@Parm_Waehrung char(6)
 ,@Parm_Kurs float
 ,@Parm_Ausgl integer
 ,@Parm_BuchText char(100)
 ,@Parm_FibuKtoVon char(8)
 ,@Parm_FibuKtoAuf char(8)
 ,@Parm_BankKtoVon char(20)
 ,@Parm_BankKtoAuf char(20)
 ,@Parm_BLZVon char(8)
 ,@Parm_BLZAuf char(8)
 ,@Parm_PrjNr char(8)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_ZahlOp] SET
     [BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[BuchNr] = RTRIM(@Parm_BuchNr)
    ,[Datum] = RTRIM(@Parm_Datum)
    ,[Betrag] = @Parm_Betrag
    ,[Waehrung] = RTRIM(@Parm_Waehrung)
    ,[Kurs] = @Parm_Kurs
    ,[Ausgl] = @Parm_Ausgl
    ,[BuchText] = RTRIM(@Parm_BuchText)
    ,[FibuKtoVon] = RTRIM(@Parm_FibuKtoVon)
    ,[FibuKtoAuf] = RTRIM(@Parm_FibuKtoAuf)
    ,[BankKtoVon] = RTRIM(@Parm_BankKtoVon)
    ,[BankKtoAuf] = RTRIM(@Parm_BankKtoAuf)
    ,[BLZVon] = RTRIM(@Parm_BLZVon)
    ,[BLZAuf] = RTRIM(@Parm_BLZAuf)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ZahlOp')
  DROP PROCEDURE [dbo].[up_Sel_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Sel_ZahlOp]
  @RI integer
AS
SELECT
     [BelArt]
    ,[BelNr]
    ,[BuchNr]
    ,[Datum]
    ,[Betrag]
    ,[Waehrung]
    ,[Kurs]
    ,[Ausgl]
    ,[BuchText]
    ,[FibuKtoVon]
    ,[FibuKtoAuf]
    ,[BankKtoVon]
    ,[BankKtoAuf]
    ,[BLZVon]
    ,[BLZAuf]
    ,[PrjNr]
  FROM [dbo].[ut_ZahlOp]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_ZahlOp')
  DROP PROCEDURE [dbo].[up_Del_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Del_ZahlOp]
  @RI integer
AS
  DELETE FROM [dbo].[ut_ZahlOp] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_ZahlOp')
  DROP PROCEDURE [dbo].[up_Reorg_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Reorg_ZahlOp]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_ZahlOp] 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_ZahlOp] 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_ZahlOp] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ZahlOp')
  DROP PROCEDURE [dbo].[up_Append_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_Append_ZahlOp]
  @Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BuchNr char(8)
 ,@Parm_Datum char(10)
 ,@Parm_Betrag float
 ,@Parm_Waehrung char(6)
 ,@Parm_Kurs float
 ,@Parm_Ausgl integer
 ,@Parm_BuchText char(100)
 ,@Parm_FibuKtoVon char(8)
 ,@Parm_FibuKtoAuf char(8)
 ,@Parm_BankKtoVon char(20)
 ,@Parm_BankKtoAuf char(20)
 ,@Parm_BLZVon char(8)
 ,@Parm_BLZAuf char(8)
 ,@Parm_PrjNr char(8)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_ZahlOp])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ZahlOp] (
    RecId
   ,[BelArt]
   ,[BelNr]
   ,[BuchNr]
   ,[Datum]
   ,[Betrag]
   ,[Waehrung]
   ,[Kurs]
   ,[Ausgl]
   ,[BuchText]
   ,[FibuKtoVon]
   ,[FibuKtoAuf]
   ,[BankKtoVon]
   ,[BankKtoAuf]
   ,[BLZVon]
   ,[BLZAuf]
   ,[PrjNr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_BuchNr)
   ,RTRIM(@Parm_Datum)
   ,@Parm_Betrag
   ,RTRIM(@Parm_Waehrung)
   ,@Parm_Kurs
   ,@Parm_Ausgl
   ,RTRIM(@Parm_BuchText)
   ,RTRIM(@Parm_FibuKtoVon)
   ,RTRIM(@Parm_FibuKtoAuf)
   ,RTRIM(@Parm_BankKtoVon)
   ,RTRIM(@Parm_BankKtoAuf)
   ,RTRIM(@Parm_BLZVon)
   ,RTRIM(@Parm_BLZAuf)
   ,RTRIM(@Parm_PrjNr)
       )
  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_ZahlOp')
  DROP PROCEDURE [dbo].[up_TrSel_ZahlOp]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ZahlOp]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([BuchNr]))
      +LTRIM(RTRIM([Datum]))
      +LTRIM(RTRIM([Waehrung]))
      +LTRIM(RTRIM([BuchText]))
      +LTRIM(RTRIM([FibuKtoVon]))
      +LTRIM(RTRIM([FibuKtoAuf]))
      +LTRIM(RTRIM([BankKtoVon]))
      +LTRIM(RTRIM([BankKtoAuf]))
      +LTRIM(RTRIM([BLZVon]))
      +LTRIM(RTRIM([BLZAuf]))
      +LTRIM(RTRIM([PrjNr]))
    AS Data
  FROM     [dbo].[ut_ZahlOp]
  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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_ZahlOp]
(BelArt,BelNr,BuchNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key0')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BelArt,BelNr,BuchNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BelArt,BelNr,BuchNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId > @oldRecId
                   ORDER BY BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr > @oldBuchNr
                   ORDER BY BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt > @oldBelArt
                   ORDER BY BelArt,BelNr,BuchNr,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 BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

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

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BelArt,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2)
      ORDER BY BelArt,BelNr,BuchNr,RecId
    RETURN
  END

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

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(BuchNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND BuchNr = SUBSTRING(@xKey,11,8)
      ORDER BY BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BelArt,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2)
      ORDER BY BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

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

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(BuchNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND BuchNr = SUBSTRING(@xKey,11,8)
      ORDER BY BelArt DESC,BelNr DESC,BuchNr 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_ZahlOp]
(Datum,BelArt,BelNr,BuchNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key1')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Datum,BelArt,BelNr,BuchNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldDatum char(10)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Datum,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldDatum,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId > @oldRecId
                   ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr > @oldBuchNr
                   ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt > @oldBelArt
                   ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum > @oldDatum
                   ORDER BY Datum,BelArt,BelNr,BuchNr,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 Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldDatum char(10)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Datum,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldDatum,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId < @oldRecId
                   ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr < @oldBuchNr
                   ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND BelArt < @oldBelArt
                   ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum < @oldDatum
                   ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr 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 Datum,BelArt,BelNr,BuchNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key1] @xKey char(128),@xLen integer AS

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(Datum,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND SUBSTRING(BelArt,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND SUBSTRING(BelNr,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8) AND SUBSTRING(BuchNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8) AND BuchNr = SUBSTRING(@xKey,21,8)
      ORDER BY Datum,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(Datum,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND SUBSTRING(BelArt,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND SUBSTRING(BelNr,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8) AND SUBSTRING(BuchNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND BelArt = SUBSTRING(@xKey,11,2) AND BelNr = SUBSTRING(@xKey,13,8) AND BuchNr = SUBSTRING(@xKey,21,8)
      ORDER BY Datum DESC,BelArt DESC,BelNr DESC,BuchNr 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key2')
CREATE INDEX [Ik_Key2] ON [dbo].[ut_ZahlOp]
(BuchNr,BelArt,BelNr,Datum,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key2')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key2]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BuchNr,BelArt,BelNr,Datum,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchNr,BelArt,BelNr,Datum FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchNr,@oldBelArt,@oldBelNr,@oldDatum
  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_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND RecId > @oldRecId
                   ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum > @oldDatum
                   ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt > @oldBelArt
                   ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr > @oldBuchNr
                   ORDER BY BuchNr,BelArt,BelNr,Datum,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 BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchNr,BelArt,BelNr,Datum FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchNr,@oldBelArt,@oldBelNr,@oldDatum
  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_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND RecId < @oldRecId
                   ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum < @oldDatum
                   ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND BelArt < @oldBelArt
                   ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr < @oldBuchNr
                   ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum 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 BuchNr,BelArt,BelNr,Datum,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BuchNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(Datum,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Datum = SUBSTRING(@xKey,19,10)
      ORDER BY BuchNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BuchNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(Datum,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Datum = SUBSTRING(@xKey,19,10)
      ORDER BY BuchNr DESC,BelArt DESC,BelNr DESC,Datum 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key3')
CREATE INDEX [Ik_Key3] ON [dbo].[ut_ZahlOp]
(PrjNr,BelArt,BelNr,BuchNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key3')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key3]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key3 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key3 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key3] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId > @oldRecId
                   ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr > @oldBuchNr
                   ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr > @oldPrjNr
                   ORDER BY PrjNr,BelArt,BelNr,BuchNr,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 PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key3] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId < @oldRecId
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr < @oldBuchNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE PrjNr < @oldPrjNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr 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 PrjNr,BelArt,BelNr,BuchNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key3] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(BuchNr,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND BuchNr = SUBSTRING(@xKey,19,8)
      ORDER BY PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(BuchNr,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND BuchNr = SUBSTRING(@xKey,19,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key4')
CREATE INDEX [Ik_Key4] ON [dbo].[ut_ZahlOp]
(Datum,PrjNr,BelArt,BelNr,BuchNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key4]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key4')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key4]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key4 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key4 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key4] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldDatum char(10)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Datum,PrjNr,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldDatum,@oldPrjNr,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId > @oldRecId
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr > @oldBuchNr
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr > @oldPrjNr
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum > @oldDatum
                   ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,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 Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key4] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldDatum char(10)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Datum,PrjNr,BelArt,BelNr,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldDatum,@oldPrjNr,@oldBelArt,@oldBelNr,@oldBuchNr
  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_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr = @oldBuchNr AND RecId < @oldRecId
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND BuchNr < @oldBuchNr
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum = @oldDatum AND PrjNr < @oldPrjNr
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE Datum < @oldDatum
                   ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr 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 Datum,PrjNr,BelArt,BelNr,BuchNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key4] @xKey char(128),@xLen integer AS

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(Datum,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND SUBSTRING(PrjNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(BelArt,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND SUBSTRING(BelNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8) AND SUBSTRING(BuchNr,1,@xLen-28) = SUBSTRING(@xKey,29,@xLen-28)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8) AND BuchNr = SUBSTRING(@xKey,29,8)
      ORDER BY Datum,PrjNr,BelArt,BelNr,BuchNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(Datum,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND SUBSTRING(PrjNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(BelArt,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND SUBSTRING(BelNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8) AND SUBSTRING(BuchNr,1,@xLen-28) = SUBSTRING(@xKey,29,@xLen-28)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE Datum = SUBSTRING(@xKey,1,10) AND PrjNr = SUBSTRING(@xKey,11,8) AND BelArt = SUBSTRING(@xKey,19,2) AND BelNr = SUBSTRING(@xKey,21,8) AND BuchNr = SUBSTRING(@xKey,29,8)
      ORDER BY Datum DESC,PrjNr DESC,BelArt DESC,BelNr DESC,BuchNr 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key5')
CREATE INDEX [Ik_Key5] ON [dbo].[ut_ZahlOp]
(BuchNr,PrjNr,BelArt,BelNr,Datum,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key5]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key5')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key5]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key5 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key5 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key5] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchNr char(8)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchNr,PrjNr,BelArt,BelNr,Datum FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchNr,@oldPrjNr,@oldBelArt,@oldBelNr,@oldDatum
  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_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND RecId > @oldRecId
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum > @oldDatum
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr > @oldPrjNr
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr > @oldBuchNr
                   ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,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 BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key5] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchNr char(8)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchNr,PrjNr,BelArt,BelNr,Datum FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchNr,@oldPrjNr,@oldBelArt,@oldBelNr,@oldDatum
  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_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND RecId < @oldRecId
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum < @oldDatum
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr = @oldBuchNr AND PrjNr < @oldPrjNr
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BuchNr < @oldBuchNr
                   ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum 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 BuchNr,PrjNr,BelArt,BelNr,Datum,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key5] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BuchNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(PrjNr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND SUBSTRING(BelArt,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND SUBSTRING(BelNr,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8) AND SUBSTRING(Datum,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8) AND Datum = SUBSTRING(@xKey,27,10)
      ORDER BY BuchNr,PrjNr,BelArt,BelNr,Datum,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BuchNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(PrjNr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND SUBSTRING(BelArt,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND SUBSTRING(BelNr,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen < 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8) AND SUBSTRING(Datum,1,@xLen-26) = SUBSTRING(@xKey,27,@xLen-26)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum DESC,RecId DESC
    RETURN
  END

  IF @xLen = 36
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BuchNr = SUBSTRING(@xKey,1,8) AND PrjNr = SUBSTRING(@xKey,9,8) AND BelArt = SUBSTRING(@xKey,17,2) AND BelNr = SUBSTRING(@xKey,19,8) AND Datum = SUBSTRING(@xKey,27,10)
      ORDER BY BuchNr DESC,PrjNr DESC,BelArt DESC,BelNr DESC,Datum 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_ZahlOp'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key6')
CREATE INDEX [Ik_Key6] ON [dbo].[ut_ZahlOp]
(BelArt,BelNr,Datum,BuchNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_First_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_Last_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_Prior_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_Next_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_Prior50_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_Next50_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_FindL_ZahlOp_Key6]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZahlOp_Key6')
  DROP PROCEDURE [dbo].[up_FindF_ZahlOp_Key6]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZahlOp in Folge Key6 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZahlOp_Key6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZahlOp in Folge Key6 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZahlOp_Key6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp] ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BelArt,BelNr,Datum,BuchNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZahlOp_Key6] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BelArt,BelNr,Datum,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBelArt,@oldBelNr,@oldDatum,@oldBuchNr
  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_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND BuchNr = @oldBuchNr AND RecId > @oldRecId
                   ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND BuchNr > @oldBuchNr
                   ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum > @oldDatum
                   ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt > @oldBelArt
                   ORDER BY BelArt,BelNr,Datum,BuchNr,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 BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZahlOp_Key6] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldDatum char(10)
  DECLARE @oldBuchNr char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BelArt,BelNr,Datum,BuchNr FROM [dbo].[ut_ZahlOp] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBelArt,@oldBelNr,@oldDatum,@oldBuchNr
  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_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND BuchNr = @oldBuchNr AND RecId < @oldRecId
                   ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum = @oldDatum AND BuchNr < @oldBuchNr
                   ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr = @oldBelNr AND Datum < @oldDatum
                   ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
                   WHERE BelArt < @oldBelArt
                   ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr 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 BelArt,BelNr,Datum,BuchNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZahlOp_Key6] @xKey char(128),@xLen integer AS

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BelArt,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND SUBSTRING(BelNr,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(Datum,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10) AND SUBSTRING(BuchNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10) AND BuchNr = SUBSTRING(@xKey,21,8)
      ORDER BY BelArt,BelNr,Datum,BuchNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE SUBSTRING(BelArt,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND SUBSTRING(BelNr,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(Datum,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10) AND SUBSTRING(BuchNr,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 28
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZahlOp]
      WHERE BelArt = SUBSTRING(@xKey,1,2) AND BelNr = SUBSTRING(@xKey,3,8) AND Datum = SUBSTRING(@xKey,11,10) AND BuchNr = SUBSTRING(@xKey,21,8)
      ORDER BY BelArt DESC,BelNr DESC,Datum DESC,BuchNr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

