------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BuchblattZeilen')
BEGIN
CREATE TABLE [dbo].[ut_BuchblattZeilen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BBKtoTyp] char(1) DEFAULT ' '
  ,[BBKtoNr] char(8) DEFAULT ' '
  ,[TransNo] char(8) DEFAULT ' '
  ,[State] char(1) DEFAULT ' '
  ,[Tag] char(2) DEFAULT ' '
  ,[Monat] char(2) DEFAULT ' '
  ,[Jahr] char(4) DEFAULT ' '
  ,[InsertDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[InsertBy] char(10) DEFAULT ' '
  ,[DeleteDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[DeleteBy] char(10) DEFAULT ' '
  CONSTRAINT [Pk_BuchblattZeilen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchblattZeilen_KeyDel')
  DROP TRIGGER [dbo].[tr_BuchblattZeilen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchblattZeilen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BuchblattZeilen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BuchblattZeilen_Keys')
  DROP TABLE [dbo].[ut_BuchblattZeilen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'BBKtoTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [BBKtoTyp] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'BBKtoNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [BBKtoNr] 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_BuchblattZeilen' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [TransNo] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'State')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [State] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [Tag] 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_BuchblattZeilen' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [Monat] 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_BuchblattZeilen' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [Jahr] char(4) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'InsertDate')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [InsertDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'InsertBy')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [InsertBy] 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_BuchblattZeilen' and sc.[name] = 'DeleteDate')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [DeleteDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'DeleteBy')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ADD [DeleteBy] char(10) NOT NULL DEFAULT ' '
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'BBKtoTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [BBKtoTyp] char(1)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'BBKtoNr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [BBKtoNr] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [TransNo] char(8)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'State')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [State] char(1)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [Tag] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [Monat] char(2)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [Jahr] char(4)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'InsertBy')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [InsertBy] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchblattZeilen' and sc.[name] = 'DeleteBy')
BEGIN
  ALTER TABLE [dbo].[ut_BuchblattZeilen] ALTER COLUMN [DeleteBy] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BuchblattZeilen')
  DROP VIEW [dbo].[BuchblattZeilen]
GO
CREATE VIEW [dbo].[BuchblattZeilen] AS
  SELECT * FROM [dbo].[ut_BuchblattZeilen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Add_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Add_BuchblattZeilen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BuchblattZeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchblattZeilen] (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_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Upd_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BuchblattZeilen]
  @RI integer
 ,@Parm_BBKtoTyp char(1)
 ,@Parm_BBKtoNr char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_State char(1)
 ,@Parm_Tag char(2)
 ,@Parm_Monat char(2)
 ,@Parm_Jahr char(4)
 ,@Parm_InsertDate datetime
 ,@Parm_InsertBy char(10)
 ,@Parm_DeleteDate datetime
 ,@Parm_DeleteBy char(10)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BuchblattZeilen] SET
     [BBKtoTyp] = RTRIM(@Parm_BBKtoTyp)
    ,[BBKtoNr] = RTRIM(@Parm_BBKtoNr)
    ,[TransNo] = RTRIM(@Parm_TransNo)
    ,[State] = RTRIM(@Parm_State)
    ,[Tag] = RTRIM(@Parm_Tag)
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[Jahr] = RTRIM(@Parm_Jahr)
    ,[InsertDate] = @Parm_InsertDate
    ,[InsertBy] = RTRIM(@Parm_InsertBy)
    ,[DeleteDate] = @Parm_DeleteDate
    ,[DeleteBy] = RTRIM(@Parm_DeleteBy)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Sel_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BuchblattZeilen]
  @RI integer
AS
SELECT
     [BBKtoTyp]
    ,[BBKtoNr]
    ,[TransNo]
    ,[State]
    ,[Tag]
    ,[Monat]
    ,[Jahr]
    ,[InsertDate]
    ,[InsertBy]
    ,[DeleteDate]
    ,[DeleteBy]
  FROM [dbo].[ut_BuchblattZeilen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Del_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Del_BuchblattZeilen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BuchblattZeilen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Reorg_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BuchblattZeilen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BuchblattZeilen] 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_BuchblattZeilen] 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_BuchblattZeilen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_Append_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_Append_BuchblattZeilen]
  @Parm_BBKtoTyp char(1)
 ,@Parm_BBKtoNr char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_State char(1)
 ,@Parm_Tag char(2)
 ,@Parm_Monat char(2)
 ,@Parm_Jahr char(4)
 ,@Parm_InsertDate datetime
 ,@Parm_InsertBy char(10)
 ,@Parm_DeleteDate datetime
 ,@Parm_DeleteBy char(10)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BuchblattZeilen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchblattZeilen] (
    RecId
   ,[BBKtoTyp]
   ,[BBKtoNr]
   ,[TransNo]
   ,[State]
   ,[Tag]
   ,[Monat]
   ,[Jahr]
   ,[InsertDate]
   ,[InsertBy]
   ,[DeleteDate]
   ,[DeleteBy]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BBKtoTyp)
   ,RTRIM(@Parm_BBKtoNr)
   ,RTRIM(@Parm_TransNo)
   ,RTRIM(@Parm_State)
   ,RTRIM(@Parm_Tag)
   ,RTRIM(@Parm_Monat)
   ,RTRIM(@Parm_Jahr)
   ,@Parm_InsertDate
   ,RTRIM(@Parm_InsertBy)
   ,@Parm_DeleteDate
   ,RTRIM(@Parm_DeleteBy)
       )
  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_BuchblattZeilen')
  DROP PROCEDURE [dbo].[up_TrSel_BuchblattZeilen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BuchblattZeilen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BBKtoTyp]))
      +LTRIM(RTRIM([BBKtoNr]))
      +LTRIM(RTRIM([TransNo]))
      +LTRIM(RTRIM([State]))
      +LTRIM(RTRIM([Tag]))
      +LTRIM(RTRIM([Monat]))
      +LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([InsertBy]))
      +LTRIM(RTRIM([DeleteBy]))
    AS Data
  FROM     [dbo].[ut_BuchblattZeilen]
  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_BuchblattZeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_BuchblattZeilen]
(BBKtoTyp,BBKtoNr,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_First_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BuchblattZeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchblattZeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus BuchblattZeilen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_BuchblattZeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY BBKtoTyp,BBKtoNr,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BuchblattZeilen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
GO

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

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BBKtoTyp,BBKtoNr,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBBKtoTyp,@oldBBKtoNr,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo > @oldTransNo
                   ORDER BY BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr > @oldBBKtoNr
                   ORDER BY BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp > @oldBBKtoTyp
                   ORDER BY BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BBKtoTyp,BBKtoNr,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBBKtoTyp,@oldBBKtoNr,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo < @oldTransNo
                   ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp = @oldBBKtoTyp AND BBKtoNr < @oldBBKtoNr
                   ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE BBKtoTyp < @oldBBKtoTyp
                   ORDER BY BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BuchblattZeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_BuchblattZeilen]
(State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_First_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BuchblattZeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchblattZeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus BuchblattZeilen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_BuchblattZeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BuchblattZeilen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldState char(1)
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldState,@oldBBKtoTyp,@oldBBKtoNr,@oldJahr,@oldMonat,@oldTag,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND TransNo > @oldTransNo
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag > @oldTag
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr > @oldJahr
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr > @oldBBKtoNr
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp > @oldBBKtoTyp
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State > @oldState
                   ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldState char(1)
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldState,@oldBBKtoTyp,@oldBBKtoNr,@oldJahr,@oldMonat,@oldTag,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND TransNo < @oldTransNo
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag < @oldTag
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND Jahr < @oldJahr
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr < @oldBBKtoNr
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp < @oldBBKtoTyp
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State < @oldState
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND SUBSTRING(BBKtoNr,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(Jahr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND SUBSTRING(Monat,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND SUBSTRING(Tag,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2) AND SUBSTRING(TransNo,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2) AND TransNo = SUBSTRING(@xKey,19,8)
      ORDER BY State,BBKtoTyp,BBKtoNr,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_BuchblattZeilen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen = 1
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND SUBSTRING(BBKtoNr,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND SUBSTRING(Jahr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND SUBSTRING(Monat,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND SUBSTRING(Tag,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen < 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2) AND SUBSTRING(TransNo,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND Jahr = SUBSTRING(@xKey,11,4) AND Monat = SUBSTRING(@xKey,15,2) AND Tag = SUBSTRING(@xKey,17,2) AND TransNo = SUBSTRING(@xKey,19,8)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,Jahr DESC,Monat DESC,Tag DESC,TransNo DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BuchblattZeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key02')
CREATE INDEX [Ik_Key02] ON [dbo].[ut_BuchblattZeilen]
(State,BBKtoTyp,BBKtoNr,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_First_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next50_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BuchblattZeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchblattZeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key02]
GO

------------------------------------------------------------
/* ersten Datensatz aus BuchblattZeilen in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_First_BuchblattZeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BuchblattZeilen in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen] ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldState char(1)
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT State,BBKtoTyp,BBKtoNr,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldState,@oldBBKtoTyp,@oldBBKtoNr,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo > @oldTransNo
                   ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr > @oldBBKtoNr
                   ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp > @oldBBKtoTyp
                   ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State > @oldState
                   ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldState char(1)
  DECLARE @oldBBKtoTyp char(1)
  DECLARE @oldBBKtoNr char(8)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT State,BBKtoTyp,BBKtoNr,TransNo FROM [dbo].[ut_BuchblattZeilen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldState,@oldBBKtoTyp,@oldBBKtoNr,@oldTransNo
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr = @oldBBKtoNr AND TransNo < @oldTransNo
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp = @oldBBKtoTyp AND BBKtoNr < @oldBBKtoNr
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State = @oldState AND BBKtoTyp < @oldBBKtoTyp
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
                   WHERE State < @oldState
                   ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key02] @xKey char(128),@xLen integer AS

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

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1)
      ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND SUBSTRING(BBKtoNr,1,@xLen-2) = SUBSTRING(@xKey,3,@xLen-2)
      ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8)
      ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
    RETURN
  END

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

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND TransNo = SUBSTRING(@xKey,11,8)
      ORDER BY State,BBKtoTyp,BBKtoNr,TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

  IF @xLen = 2
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchblattZeilen]
      WHERE State = SUBSTRING(@xKey,1,1) AND BBKtoTyp = SUBSTRING(@xKey,2,1) AND BBKtoNr = SUBSTRING(@xKey,3,8) AND TransNo = SUBSTRING(@xKey,11,8)
      ORDER BY State DESC,BBKtoTyp DESC,BBKtoNr DESC,TransNo DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BuchblattZeilen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key03')
CREATE INDEX [Ik_Key03] ON [dbo].[ut_BuchblattZeilen]
(TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_First_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_Last_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next50_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BuchblattZeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchblattZeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindF_BuchblattZeilen_Key03]
GO

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

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

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

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

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

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

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

