------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Kassenbuch')
BEGIN
CREATE TABLE [dbo].[ut_Kassenbuch]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[KtoKasse] char(8) DEFAULT ' '
  ,[Jahr] char(4) DEFAULT ' '
  ,[Monat] char(2) DEFAULT ' '
  ,[Tag] char(2) DEFAULT ' '
  ,[LfdNr] char(4) DEFAULT ' '
  ,[TransNo] char(8) DEFAULT ' '
  ,[KtoSach] char(8) DEFAULT ' '
  ,[UstSchl] char(3) DEFAULT ' '
  ,[Kst] char(20) DEFAULT ' '
  ,[Ktr] char(20) DEFAULT ' '
  ,[KzZA] char(1) DEFAULT ' '
  ,[Waehrung] char(6) DEFAULT ' '
  ,[BetrWaehr] float DEFAULT 0
  ,[BetrEuro] float DEFAULT 0
  ,[BuText] char(30) DEFAULT ' '
  ,[BelegNr] char(8) DEFAULT ' '
  CONSTRAINT [Pk_Kassenbuch]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kassenbuch_KeyDel')
  DROP TRIGGER [dbo].[tr_Kassenbuch_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kassenbuch_KeyUpd')
  DROP TRIGGER [dbo].[tr_Kassenbuch_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Kassenbuch_Keys')
  DROP TABLE [dbo].[ut_Kassenbuch_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'KtoKasse')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [KtoKasse] 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_Kassenbuch' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] 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_Kassenbuch' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] 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_Kassenbuch' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] 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_Kassenbuch' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [LfdNr] char(4) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] 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_Kassenbuch' and sc.[name] = 'KtoSach')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [KtoSach] 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_Kassenbuch' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [UstSchl] char(3) 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_Kassenbuch' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [Kst] 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_Kassenbuch' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [Ktr] 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_Kassenbuch' and sc.[name] = 'KzZA')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [KzZA] 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_Kassenbuch' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] 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_Kassenbuch' and sc.[name] = 'BetrWaehr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [BetrWaehr] 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_Kassenbuch' and sc.[name] = 'BetrEuro')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [BetrEuro] 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_Kassenbuch' and sc.[name] = 'BuText')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [BuText] char(30) 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_Kassenbuch' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ADD [BelegNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'KtoKasse')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [KtoKasse] char(8)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Jahr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Monat] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Tag] char(2)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [LfdNr] char(4)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [TransNo] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'KtoSach')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [KtoSach] char(8)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [UstSchl] char(3)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Kst] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Ktr] char(20)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'KzZA')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [KzZA] char(1)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [Waehrung] char(6)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'BuText')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [BuText] char(30)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Kassenbuch' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_Kassenbuch] ALTER COLUMN [BelegNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Kassenbuch')
  DROP VIEW [dbo].[Kassenbuch]
GO
CREATE VIEW [dbo].[Kassenbuch] AS
  SELECT * FROM [dbo].[ut_Kassenbuch]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Add_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Add_Kassenbuch] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Kassenbuch])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Kassenbuch] (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_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Upd_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Upd_Kassenbuch]
  @RI integer
 ,@Parm_KtoKasse char(8)
 ,@Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Tag char(2)
 ,@Parm_LfdNr char(4)
 ,@Parm_TransNo char(8)
 ,@Parm_KtoSach char(8)
 ,@Parm_UstSchl char(3)
 ,@Parm_Kst char(20)
 ,@Parm_Ktr char(20)
 ,@Parm_KzZA char(1)
 ,@Parm_Waehrung char(6)
 ,@Parm_BetrWaehr float
 ,@Parm_BetrEuro float
 ,@Parm_BuText char(30)
 ,@Parm_BelegNr char(8)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Kassenbuch] SET
     [KtoKasse] = RTRIM(@Parm_KtoKasse)
    ,[Jahr] = RTRIM(@Parm_Jahr)
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[Tag] = RTRIM(@Parm_Tag)
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[TransNo] = RTRIM(@Parm_TransNo)
    ,[KtoSach] = RTRIM(@Parm_KtoSach)
    ,[UstSchl] = RTRIM(@Parm_UstSchl)
    ,[Kst] = RTRIM(@Parm_Kst)
    ,[Ktr] = RTRIM(@Parm_Ktr)
    ,[KzZA] = RTRIM(@Parm_KzZA)
    ,[Waehrung] = RTRIM(@Parm_Waehrung)
    ,[BetrWaehr] = @Parm_BetrWaehr
    ,[BetrEuro] = @Parm_BetrEuro
    ,[BuText] = RTRIM(@Parm_BuText)
    ,[BelegNr] = RTRIM(@Parm_BelegNr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Sel_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Sel_Kassenbuch]
  @RI integer
AS
SELECT
     [KtoKasse]
    ,[Jahr]
    ,[Monat]
    ,[Tag]
    ,[LfdNr]
    ,[TransNo]
    ,[KtoSach]
    ,[UstSchl]
    ,[Kst]
    ,[Ktr]
    ,[KzZA]
    ,[Waehrung]
    ,[BetrWaehr]
    ,[BetrEuro]
    ,[BuText]
    ,[BelegNr]
  FROM [dbo].[ut_Kassenbuch]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Del_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Del_Kassenbuch]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Kassenbuch] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Reorg_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Kassenbuch]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Kassenbuch] 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_Kassenbuch] 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_Kassenbuch] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Kassenbuch')
  DROP PROCEDURE [dbo].[up_Append_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_Append_Kassenbuch]
  @Parm_KtoKasse char(8)
 ,@Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Tag char(2)
 ,@Parm_LfdNr char(4)
 ,@Parm_TransNo char(8)
 ,@Parm_KtoSach char(8)
 ,@Parm_UstSchl char(3)
 ,@Parm_Kst char(20)
 ,@Parm_Ktr char(20)
 ,@Parm_KzZA char(1)
 ,@Parm_Waehrung char(6)
 ,@Parm_BetrWaehr float
 ,@Parm_BetrEuro float
 ,@Parm_BuText char(30)
 ,@Parm_BelegNr char(8)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Kassenbuch])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Kassenbuch] (
    RecId
   ,[KtoKasse]
   ,[Jahr]
   ,[Monat]
   ,[Tag]
   ,[LfdNr]
   ,[TransNo]
   ,[KtoSach]
   ,[UstSchl]
   ,[Kst]
   ,[Ktr]
   ,[KzZA]
   ,[Waehrung]
   ,[BetrWaehr]
   ,[BetrEuro]
   ,[BuText]
   ,[BelegNr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_KtoKasse)
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_Monat)
   ,RTRIM(@Parm_Tag)
   ,RTRIM(@Parm_LfdNr)
   ,RTRIM(@Parm_TransNo)
   ,RTRIM(@Parm_KtoSach)
   ,RTRIM(@Parm_UstSchl)
   ,RTRIM(@Parm_Kst)
   ,RTRIM(@Parm_Ktr)
   ,RTRIM(@Parm_KzZA)
   ,RTRIM(@Parm_Waehrung)
   ,@Parm_BetrWaehr
   ,@Parm_BetrEuro
   ,RTRIM(@Parm_BuText)
   ,RTRIM(@Parm_BelegNr)
       )
  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_Kassenbuch')
  DROP PROCEDURE [dbo].[up_TrSel_Kassenbuch]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Kassenbuch]
AS
  SELECT   RecId,
      LTRIM(RTRIM([KtoKasse]))
      +LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([Monat]))
      +LTRIM(RTRIM([Tag]))
      +LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([TransNo]))
      +LTRIM(RTRIM([KtoSach]))
      +LTRIM(RTRIM([UstSchl]))
      +LTRIM(RTRIM([Kst]))
      +LTRIM(RTRIM([Ktr]))
      +LTRIM(RTRIM([KzZA]))
      +LTRIM(RTRIM([Waehrung]))
      +LTRIM(RTRIM([BuText]))
      +LTRIM(RTRIM([BelegNr]))
    AS Data
  FROM     [dbo].[ut_Kassenbuch]
  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_Kassenbuch'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_Kassenbuch]
(KtoKasse,Jahr,Monat,Tag,LfdNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_First_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_Last_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_Next_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Kassenbuch_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Kassenbuch_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Kassenbuch_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus Kassenbuch in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_Kassenbuch_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch] ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Kassenbuch in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Kassenbuch_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch] ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Kassenbuch_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKtoKasse char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldLfdNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT KtoKasse,Jahr,Monat,Tag,LfdNr FROM [dbo].[ut_Kassenbuch] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKtoKasse,@oldJahr,@oldMonat,@oldTag,@oldLfdNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND LfdNr = @oldLfdNr AND RecId > @oldRecId
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND LfdNr > @oldLfdNr
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag > @oldTag
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr > @oldJahr
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse > @oldKtoKasse
                   ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Kassenbuch_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKtoKasse char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldLfdNr char(4)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT KtoKasse,Jahr,Monat,Tag,LfdNr FROM [dbo].[ut_Kassenbuch] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKtoKasse,@oldJahr,@oldMonat,@oldTag,@oldLfdNr
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND LfdNr = @oldLfdNr AND RecId < @oldRecId
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND LfdNr < @oldLfdNr
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag < @oldTag
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse = @oldKtoKasse AND Jahr < @oldJahr
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
                   WHERE KtoKasse < @oldKtoKasse
                   ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Kassenbuch_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE SUBSTRING(KtoKasse,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND SUBSTRING(Jahr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND SUBSTRING(Monat,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND SUBSTRING(Tag,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND SUBSTRING(LfdNr,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND LfdNr = SUBSTRING(@xKey,17,4)
      ORDER BY KtoKasse,Jahr,Monat,Tag,LfdNr,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Kassenbuch_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE SUBSTRING(KtoKasse,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND SUBSTRING(Jahr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND SUBSTRING(Monat,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND SUBSTRING(Tag,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND SUBSTRING(LfdNr,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      WHERE KtoKasse = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND LfdNr = SUBSTRING(@xKey,17,4)
      ORDER BY KtoKasse DESC,Jahr DESC,Monat DESC,Tag DESC,LfdNr DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Kassenbuch'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_Kassenbuch]
(TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_First_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_Last_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_Next_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Kassenbuch_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Kassenbuch_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Kassenbuch_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus Kassenbuch in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_Kassenbuch_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch] ORDER BY TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Kassenbuch in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Kassenbuch_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch] ORDER BY TransNo DESC,RecId DESC
GO

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

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TransNo FROM [dbo].[ut_Kassenbuch] 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_Kassenbuch]
                   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_Kassenbuch]
                   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_Kassenbuch_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TransNo FROM [dbo].[ut_Kassenbuch] 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_Kassenbuch]
                   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_Kassenbuch]
                   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_Kassenbuch_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      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_Kassenbuch]
      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_Kassenbuch_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kassenbuch]
      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_Kassenbuch]
      WHERE TransNo = SUBSTRING(@xKey,1,8)
      ORDER BY TransNo DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

