-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Buchungszeilen')
BEGIN
CREATE TABLE [dbo].[ut_Buchungszeilen]
(
  [RecId]  integer NOT NULL
 ,[Typ] char(1) DEFAULT ''
 ,[Konto] char(8) DEFAULT ''
 ,[Jahr] char(4) DEFAULT ''
 ,[Monat] char(2) DEFAULT ''
 ,[Tag] char(2) DEFAULT ''
 ,[TransNo] char(8) DEFAULT ''
 ,[Soll] float DEFAULT 0
 ,[Haben] float DEFAULT 0
 ,[SaldoAlt] float DEFAULT 0
 ,[SaldoNeu] float DEFAULT 0
 ,[BtrOrg] float DEFAULT 0
 ,[BtrWae] char(6) DEFAULT ''
 ,[Saldiert] tinyint DEFAULT 0
 ,[Abgeholt] tinyint DEFAULT 0
 ,[Text] char(50) DEFAULT ''
 ,[SaldoUms] float DEFAULT 0
 ,[Kst] char(20) DEFAULT ''
 ,[Ktr] char(20) DEFAULT ''
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[FJahr] char(4) DEFAULT ''
 ,[FMonat] char(2) DEFAULT ''
 ,[FTag] char(2) DEFAULT ''
 ,[AusglTRN] char(8) DEFAULT ''
 ,[AusglBtr] float DEFAULT 0
 ,[SkoSatz] float DEFAULT 0
 ,[SkoFrist] integer DEFAULT 0
 ,[BelNrExt] char(20) DEFAULT ''
 ,[BelDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Vorgang] char(20) DEFAULT ''
 ,[Status1] integer DEFAULT 0
 ,[Status2] integer DEFAULT 0
 ,[StatusDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[JnlLine] integer DEFAULT 0
 ,[RefTRN] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
  CONSTRAINT [Pk_Buchungszeilen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Buchungszeilen_KeyDel')
  DROP TRIGGER [dbo].[tr_Buchungszeilen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Buchungszeilen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Buchungszeilen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Buchungszeilen_Keys')
  DROP TABLE [dbo].[ut_Buchungszeilen_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Typ')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Typ] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Konto] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Soll')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Soll] 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_Buchungszeilen' and sc.[name] = 'Haben')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Haben] 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_Buchungszeilen' and sc.[name] = 'SaldoAlt')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [SaldoAlt] 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_Buchungszeilen' and sc.[name] = 'SaldoNeu')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [SaldoNeu] 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_Buchungszeilen' and sc.[name] = 'BtrOrg')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BtrOrg] 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_Buchungszeilen' and sc.[name] = 'BtrWae')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BtrWae] 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_Buchungszeilen' and sc.[name] = 'Saldiert')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Saldiert] tinyint 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_Buchungszeilen' and sc.[name] = 'Abgeholt')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Abgeholt] tinyint 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_Buchungszeilen' and sc.[name] = 'Text')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Text] char(50) 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_Buchungszeilen' and sc.[name] = 'SaldoUms')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [SaldoUms] 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_Buchungszeilen' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [PrjNr] 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_Buchungszeilen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BelNr] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FJahr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [FJahr] 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_Buchungszeilen' and sc.[name] = 'FMonat')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [FMonat] 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_Buchungszeilen' and sc.[name] = 'FTag')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [FTag] 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_Buchungszeilen' and sc.[name] = 'AusglTRN')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [AusglTRN] 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_Buchungszeilen' and sc.[name] = 'AusglBtr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [AusglBtr] 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_Buchungszeilen' and sc.[name] = 'SkoSatz')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [SkoSatz] 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_Buchungszeilen' and sc.[name] = 'SkoFrist')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [SkoFrist] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNrExt')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BelNrExt] 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_Buchungszeilen' and sc.[name] = 'BelDatum')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BelDatum] 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_Buchungszeilen' and sc.[name] = 'Vorgang')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Vorgang] 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_Buchungszeilen' and sc.[name] = 'Status1')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Status1] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Status2')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [Status2] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'StatusDate')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [StatusDate] 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_Buchungszeilen' and sc.[name] = 'JnlLine')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [JnlLine] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'RefTRN')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [RefTRN] 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_Buchungszeilen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungszeilen] ADD [BelArt] char(2) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Typ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Typ'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Typ] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Typ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Typ'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Typ] varchar(1)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Konto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Konto'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Konto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Konto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Konto'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Konto] varchar(8)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Jahr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Jahr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Jahr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Jahr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Jahr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Jahr] varchar(4)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Monat'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Monat'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Monat'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Monat'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Monat] varchar(2)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Tag'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Tag'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Tag'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Tag'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Tag] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'TransNo'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'TransNo'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'TransNo'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'TransNo'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [TransNo] varchar(8)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BtrWae'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BtrWae'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BtrWae] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BtrWae'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BtrWae'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BtrWae] varchar(6)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Text'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Text'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Text] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Text'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Text'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Text] varchar(50)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Kst'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] 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_Buchungszeilen' and sc.[name] = 'Kst'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Kst] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Ktr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Ktr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Ktr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Ktr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Ktr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Ktr] varchar(20)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'PrjNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [PrjNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'PrjNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FJahr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FJahr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FJahr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FJahr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FJahr'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FJahr] varchar(4)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FMonat'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FMonat'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FMonat] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FMonat'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FMonat'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FMonat] varchar(2)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FTag'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FTag'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FTag] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FTag'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'FTag'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [FTag] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'AusglTRN'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'AusglTRN'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [AusglTRN] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'AusglTRN'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'AusglTRN'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [AusglTRN] varchar(8)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNrExt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNrExt'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelNrExt] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNrExt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelNrExt'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelNrExt] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Vorgang'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Vorgang'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Vorgang] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Vorgang'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'Vorgang'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [Vorgang] varchar(20)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'RefTRN'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'RefTRN'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [RefTRN] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'RefTRN'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'RefTRN'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [RefTRN] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelArt] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Buchungszeilen' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Buchungszeilen] ALTER COLUMN [BelArt] varchar(2)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Buchungszeilen')
  DROP VIEW [dbo].[Buchungszeilen]
GO
CREATE VIEW [dbo].[Buchungszeilen] AS
  SELECT [RecId]
 ,[Typ]
 ,[Konto]
 ,[Jahr]
 ,[Monat]
 ,[Tag]
 ,[TransNo]
 ,[Soll]
 ,[Haben]
 ,[SaldoAlt]
 ,[SaldoNeu]
 ,[BtrOrg]
 ,[BtrWae]
 ,[Saldiert]
 ,[Abgeholt]
 ,[Text]
 ,[SaldoUms]
 ,[Kst]
 ,[Ktr]
 ,[PrjNr]
 ,[BelNr]
 ,[FJahr]
 ,[FMonat]
 ,[FTag]
 ,[AusglTRN]
 ,[AusglBtr]
 ,[SkoSatz]
 ,[SkoFrist]
 ,[BelNrExt]
 ,[BelDatum]
 ,[Vorgang]
 ,[Status1]
 ,[Status2]
 ,[StatusDate]
 ,[JnlLine]
 ,[RefTRN]
 ,[BelArt]
  FROM [dbo].[ut_Buchungszeilen]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_Buchungszeilen', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_Buchungszeilen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Buchungszeilen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Buchungszeilen] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Sel_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Buchungszeilen]  @mRI integer
AS
  SELECT
    [Typ]
   ,[Konto]
   ,[Jahr]
   ,[Monat]
   ,[Tag]
   ,[TransNo]
   ,[Soll]
   ,[Haben]
   ,[SaldoAlt]
   ,[SaldoNeu]
   ,[BtrOrg]
   ,[BtrWae]
   ,[Saldiert]
   ,[Abgeholt]
   ,[Text]
   ,[SaldoUms]
   ,[Kst]
   ,[Ktr]
   ,[PrjNr]
   ,[BelNr]
   ,[FJahr]
   ,[FMonat]
   ,[FTag]
   ,[AusglTRN]
   ,[AusglBtr]
   ,[SkoSatz]
   ,[SkoFrist]
   ,[BelNrExt]
   ,[BelDatum]
   ,[Vorgang]
   ,[Status1]
   ,[Status2]
   ,[StatusDate]
   ,[JnlLine]
   ,[RefTRN]
   ,[BelArt]
  FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Sel2_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel2_Buchungszeilen]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[Buchungszeilen] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Append_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Append_Buchungszeilen]
   @Param_Typ char(1)
  ,@Param_Konto char(8)
  ,@Param_Jahr char(4)
  ,@Param_Monat char(2)
  ,@Param_Tag char(2)
  ,@Param_TransNo char(8)
  ,@Param_Soll float
  ,@Param_Haben float
  ,@Param_SaldoAlt float
  ,@Param_SaldoNeu float
  ,@Param_BtrOrg float
  ,@Param_BtrWae char(6)
  ,@Param_Saldiert tinyint
  ,@Param_Abgeholt tinyint
  ,@Param_Text char(50)
  ,@Param_SaldoUms float
  ,@Param_Kst char(20)
  ,@Param_Ktr char(20)
  ,@Param_PrjNr char(8)
  ,@Param_BelNr char(8)
  ,@Param_FJahr char(4)
  ,@Param_FMonat char(2)
  ,@Param_FTag char(2)
  ,@Param_AusglTRN char(8)
  ,@Param_AusglBtr float
  ,@Param_SkoSatz float
  ,@Param_SkoFrist integer
  ,@Param_BelNrExt char(20)
  ,@Param_BelDatum datetime
  ,@Param_Vorgang char(20)
  ,@Param_Status1 integer
  ,@Param_Status2 integer
  ,@Param_StatusDate datetime
  ,@Param_JnlLine integer
  ,@Param_RefTRN char(8)
  ,@Param_BelArt char(2)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Buchungszeilen()
      INSERT INTO [dbo].[ut_Buchungszeilen] (RecId
        ,[Typ]
        ,[Konto]
        ,[Jahr]
        ,[Monat]
        ,[Tag]
        ,[TransNo]
        ,[Soll]
        ,[Haben]
        ,[SaldoAlt]
        ,[SaldoNeu]
        ,[BtrOrg]
        ,[BtrWae]
        ,[Saldiert]
        ,[Abgeholt]
        ,[Text]
        ,[SaldoUms]
        ,[Kst]
        ,[Ktr]
        ,[PrjNr]
        ,[BelNr]
        ,[FJahr]
        ,[FMonat]
        ,[FTag]
        ,[AusglTRN]
        ,[AusglBtr]
        ,[SkoSatz]
        ,[SkoFrist]
        ,[BelNrExt]
        ,[BelDatum]
        ,[Vorgang]
        ,[Status1]
        ,[Status2]
        ,[StatusDate]
        ,[JnlLine]
        ,[RefTRN]
        ,[BelArt]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Typ))
        ,UPPER(RTRIM(@Param_Konto))
        ,UPPER(RTRIM(@Param_Jahr))
        ,UPPER(RTRIM(@Param_Monat))
        ,UPPER(RTRIM(@Param_Tag))
        ,UPPER(RTRIM(@Param_TransNo))
        ,@Param_Soll
        ,@Param_Haben
        ,@Param_SaldoAlt
        ,@Param_SaldoNeu
        ,@Param_BtrOrg
        ,UPPER(RTRIM(@Param_BtrWae))
        ,@Param_Saldiert
        ,@Param_Abgeholt
        ,RTRIM(@Param_Text)
        ,@Param_SaldoUms
        ,UPPER(RTRIM(@Param_Kst))
        ,UPPER(RTRIM(@Param_Ktr))
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_FJahr))
        ,UPPER(RTRIM(@Param_FMonat))
        ,UPPER(RTRIM(@Param_FTag))
        ,UPPER(RTRIM(@Param_AusglTRN))
        ,@Param_AusglBtr
        ,@Param_SkoSatz
        ,@Param_SkoFrist
        ,UPPER(RTRIM(@Param_BelNrExt))
        ,@Param_BelDatum
        ,UPPER(RTRIM(@Param_Vorgang))
        ,@Param_Status1
        ,@Param_Status2
        ,@Param_StatusDate
        ,@Param_JnlLine
        ,UPPER(RTRIM(@Param_RefTRN))
        ,UPPER(RTRIM(@Param_BelArt))
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Add_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Add_Buchungszeilen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Buchungszeilen()
  INSERT INTO [dbo].[ut_Buchungszeilen] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Upd_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Buchungszeilen] @mRI integer
  ,@Param_Typ char(1)
  ,@Param_Konto char(8)
  ,@Param_Jahr char(4)
  ,@Param_Monat char(2)
  ,@Param_Tag char(2)
  ,@Param_TransNo char(8)
  ,@Param_Soll float
  ,@Param_Haben float
  ,@Param_SaldoAlt float
  ,@Param_SaldoNeu float
  ,@Param_BtrOrg float
  ,@Param_BtrWae char(6)
  ,@Param_Saldiert tinyint
  ,@Param_Abgeholt tinyint
  ,@Param_Text char(50)
  ,@Param_SaldoUms float
  ,@Param_Kst char(20)
  ,@Param_Ktr char(20)
  ,@Param_PrjNr char(8)
  ,@Param_BelNr char(8)
  ,@Param_FJahr char(4)
  ,@Param_FMonat char(2)
  ,@Param_FTag char(2)
  ,@Param_AusglTRN char(8)
  ,@Param_AusglBtr float
  ,@Param_SkoSatz float
  ,@Param_SkoFrist integer
  ,@Param_BelNrExt char(20)
  ,@Param_BelDatum datetime
  ,@Param_Vorgang char(20)
  ,@Param_Status1 integer
  ,@Param_Status2 integer
  ,@Param_StatusDate datetime
  ,@Param_JnlLine integer
  ,@Param_RefTRN char(8)
  ,@Param_BelArt char(2)
AS
  UPDATE [dbo].[ut_Buchungszeilen] SET
    [Typ] = UPPER(RTRIM(@Param_Typ))
   ,[Konto] = UPPER(RTRIM(@Param_Konto))
   ,[Jahr] = UPPER(RTRIM(@Param_Jahr))
   ,[Monat] = UPPER(RTRIM(@Param_Monat))
   ,[Tag] = UPPER(RTRIM(@Param_Tag))
   ,[TransNo] = UPPER(RTRIM(@Param_TransNo))
   ,[Soll] = @Param_Soll
   ,[Haben] = @Param_Haben
   ,[SaldoAlt] = @Param_SaldoAlt
   ,[SaldoNeu] = @Param_SaldoNeu
   ,[BtrOrg] = @Param_BtrOrg
   ,[BtrWae] = UPPER(RTRIM(@Param_BtrWae))
   ,[Saldiert] = @Param_Saldiert
   ,[Abgeholt] = @Param_Abgeholt
   ,[Text] = RTRIM(@Param_Text)
   ,[SaldoUms] = @Param_SaldoUms
   ,[Kst] = UPPER(RTRIM(@Param_Kst))
   ,[Ktr] = UPPER(RTRIM(@Param_Ktr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[FJahr] = UPPER(RTRIM(@Param_FJahr))
   ,[FMonat] = UPPER(RTRIM(@Param_FMonat))
   ,[FTag] = UPPER(RTRIM(@Param_FTag))
   ,[AusglTRN] = UPPER(RTRIM(@Param_AusglTRN))
   ,[AusglBtr] = @Param_AusglBtr
   ,[SkoSatz] = @Param_SkoSatz
   ,[SkoFrist] = @Param_SkoFrist
   ,[BelNrExt] = UPPER(RTRIM(@Param_BelNrExt))
   ,[BelDatum] = @Param_BelDatum
   ,[Vorgang] = UPPER(RTRIM(@Param_Vorgang))
   ,[Status1] = @Param_Status1
   ,[Status2] = @Param_Status2
   ,[StatusDate] = @Param_StatusDate
   ,[JnlLine] = @Param_JnlLine
   ,[RefTRN] = UPPER(RTRIM(@Param_RefTRN))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Upd2_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Buchungszeilen] @Param_RecId varchar(20)
  ,@Param_Typ varchar(1)
  ,@Param_Konto varchar(8)
  ,@Param_Jahr varchar(4)
  ,@Param_Monat varchar(2)
  ,@Param_Tag varchar(2)
  ,@Param_TransNo varchar(8)
  ,@Param_Soll varchar(12)
  ,@Param_Haben varchar(12)
  ,@Param_SaldoAlt varchar(14)
  ,@Param_SaldoNeu varchar(14)
  ,@Param_BtrOrg varchar(12)
  ,@Param_BtrWae varchar(6)
  ,@Param_Saldiert varchar(1)
  ,@Param_Abgeholt varchar(1)
  ,@Param_Text varchar(50)
  ,@Param_SaldoUms varchar(14)
  ,@Param_Kst varchar(20)
  ,@Param_Ktr varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelNr varchar(8)
  ,@Param_FJahr varchar(4)
  ,@Param_FMonat varchar(2)
  ,@Param_FTag varchar(2)
  ,@Param_AusglTRN varchar(8)
  ,@Param_AusglBtr varchar(12)
  ,@Param_SkoSatz varchar(4)
  ,@Param_SkoFrist varchar(3)
  ,@Param_BelNrExt varchar(20)
  ,@Param_BelDatum varchar(10)
  ,@Param_Vorgang varchar(20)
  ,@Param_Status1 varchar(1)
  ,@Param_Status2 varchar(1)
  ,@Param_StatusDate varchar(10)
  ,@Param_JnlLine varchar(8)
  ,@Param_RefTRN varchar(8)
  ,@Param_BelArt varchar(2)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Buchungszeilen] SET
    [Typ] = UPPER(RTRIM(@Param_Typ))
   ,[Konto] = UPPER(RTRIM(@Param_Konto))
   ,[Jahr] = UPPER(RTRIM(@Param_Jahr))
   ,[Monat] = UPPER(RTRIM(@Param_Monat))
   ,[Tag] = UPPER(RTRIM(@Param_Tag))
   ,[TransNo] = UPPER(RTRIM(@Param_TransNo))
   ,[Soll] = dbo.itcfunc_float(@Param_Soll)
   ,[Haben] = dbo.itcfunc_float(@Param_Haben)
   ,[SaldoAlt] = dbo.itcfunc_float(@Param_SaldoAlt)
   ,[SaldoNeu] = dbo.itcfunc_float(@Param_SaldoNeu)
   ,[BtrOrg] = dbo.itcfunc_float(@Param_BtrOrg)
   ,[BtrWae] = UPPER(RTRIM(@Param_BtrWae))
   ,[Saldiert] = dbo.itcfunc_integer(@Param_Saldiert)
   ,[Abgeholt] = dbo.itcfunc_integer(@Param_Abgeholt)
   ,[Text] = RTRIM(@Param_Text)
   ,[SaldoUms] = dbo.itcfunc_float(@Param_SaldoUms)
   ,[Kst] = UPPER(RTRIM(@Param_Kst))
   ,[Ktr] = UPPER(RTRIM(@Param_Ktr))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[FJahr] = UPPER(RTRIM(@Param_FJahr))
   ,[FMonat] = UPPER(RTRIM(@Param_FMonat))
   ,[FTag] = UPPER(RTRIM(@Param_FTag))
   ,[AusglTRN] = UPPER(RTRIM(@Param_AusglTRN))
   ,[AusglBtr] = dbo.itcfunc_float(@Param_AusglBtr)
   ,[SkoSatz] = dbo.itcfunc_float(@Param_SkoSatz)
   ,[SkoFrist] = dbo.itcfunc_integer(@Param_SkoFrist)
   ,[BelNrExt] = UPPER(RTRIM(@Param_BelNrExt))
   ,[BelDatum] = dbo.itcfunc_datetime(@Param_BelDatum)
   ,[Vorgang] = UPPER(RTRIM(@Param_Vorgang))
   ,[Status1] = dbo.itcfunc_integer(@Param_Status1)
   ,[Status2] = dbo.itcfunc_integer(@Param_Status2)
   ,[StatusDate] = dbo.itcfunc_datetime(@Param_StatusDate)
   ,[JnlLine] = dbo.itcfunc_integer(@Param_JnlLine)
   ,[RefTRN] = UPPER(RTRIM(@Param_RefTRN))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Buchungszeilen] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Del_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Del_Buchungszeilen] @mRI integer
AS
  DELETE FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_TrSel_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Buchungszeilen]
AS
  SELECT RecId,
    LTRIM(RTRIM([Typ]))
   +LTRIM(RTRIM([Konto]))
   +LTRIM(RTRIM([Jahr]))
   +LTRIM(RTRIM([Monat]))
   +LTRIM(RTRIM([Tag]))
   +LTRIM(RTRIM([TransNo]))
   +LTRIM(RTRIM([BtrWae]))
   +LTRIM(RTRIM([Text]))
   +LTRIM(RTRIM([Kst]))
   +LTRIM(RTRIM([Ktr]))
   +LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([FJahr]))
   +LTRIM(RTRIM([FMonat]))
   +LTRIM(RTRIM([FTag]))
   +LTRIM(RTRIM([AusglTRN]))
   +LTRIM(RTRIM([BelNrExt]))
   +LTRIM(RTRIM([Vorgang]))
   +LTRIM(RTRIM([RefTRN]))
   +LTRIM(RTRIM([BelArt]))
  AS Data FROM [dbo].[ut_Buchungszeilen] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Buchungszeilen')
  DROP PROCEDURE [dbo].[up_Reorg_Buchungszeilen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Buchungszeilen] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Buchungszeilen] 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_Buchungszeilen] 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_Buchungszeilen] )
  END
GO

-- logical key  ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_Buchungszeilen] (Typ,Konto,Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key00] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key00] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key00] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldTyp = Typ, @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat > @oldMonat)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr > @oldJahr)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto > @oldKonto)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ > @oldTyp)
    ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key00] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldTyp = Typ, @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat < @oldMonat)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr < @oldJahr)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (Konto < @oldKonto)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00)
    WHERE (Typ < @oldTyp)
    ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,14,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,14,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key00] @xKey, @xLen')
GO


-- logical key  ORDER BY TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_Buchungszeilen]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  ORDER BY TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key01] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  ORDER BY TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key01] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key01] @oldRecId integer AS
  DECLARE @oldTransNo char(8)
  SELECT @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (TransNo > @oldTransNo)
    ORDER BY TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key01] @oldRecId integer AS
  DECLARE @oldTransNo char(8)
  SELECT @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (TransNo < @oldTransNo)
    ORDER BY TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      TransNo = @mTransNo
      ORDER BY TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      TransNo = @mTransNo
      ORDER BY TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

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

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key01] @xKey, @xLen')
GO


-- logical key  ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  DROP INDEX [Ik_Key02] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  CREATE INDEX [Ik_Key02] ON [dbo].[ut_Buchungszeilen] (Jahr,Monat,Tag,Typ,Konto,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key02] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key02] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key02] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId > @oldRecId)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ = @oldTyp) AND (Konto > @oldKonto)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ > @oldTyp)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat > @oldMonat)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr > @oldJahr)
    ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key02] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId < @oldRecId)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ = @oldTyp) AND (Konto < @oldKonto)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Typ < @oldTyp)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr = @oldJahr) AND (Monat < @oldMonat)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02)
    WHERE (Jahr < @oldJahr)
    ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key02]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key02]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY Jahr,Monat,Tag,Typ,Konto,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key02] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key02) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Typ = @mTyp AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY Jahr desc,Monat desc,Tag desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key02] @xKey, @xLen')
GO


-- logical key  ORDER BY AusglTRN,Typ,Konto,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  DROP INDEX [Ik_Key03] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  CREATE INDEX [Ik_Key03] ON [dbo].[ut_Buchungszeilen] (AusglTRN,Typ,Konto,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) ORDER BY AusglTRN,Typ,Konto,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key03] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key03] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key03] @oldRecId integer AS
  DECLARE @oldAusglTRN char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldAusglTRN = AusglTRN, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId > @oldRecId)
    ORDER BY AusglTRN,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (Konto > @oldKonto)
    ORDER BY AusglTRN,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ > @oldTyp)
    ORDER BY AusglTRN,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN > @oldAusglTRN)
    ORDER BY AusglTRN,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key03] @oldRecId integer AS
  DECLARE @oldAusglTRN char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldAusglTRN = AusglTRN, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId < @oldRecId)
    ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (Konto < @oldKonto)
    ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ < @oldTyp)
    ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03)
    WHERE (AusglTRN < @oldAusglTRN)
    ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key03]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key03]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAusglTRN char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN >= @mAusglTRN AND SUBSTRING(AusglTRN,1,LEN(@mAusglTRN)) = @mAusglTRN
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY AusglTRN,Typ,Konto,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key03] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key03] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key03')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAusglTRN char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(AusglTRN,1,LEN(@mAusglTRN)) = @mAusglTRN
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key03) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY AusglTRN desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key03] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key03] @xKey, @xLen')
GO


-- logical key  ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  DROP INDEX [Ik_Key04] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  CREATE INDEX [Ik_Key04] ON [dbo].[ut_Buchungszeilen] (AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key04] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key04] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key04] @oldRecId integer AS
  DECLARE @oldAusglTRN char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldFJahr char(4)
  DECLARE @oldFMonat char(2)
  DECLARE @oldFTag char(2)
  DECLARE @oldKonto char(8)
  SELECT @oldAusglTRN = AusglTRN, @oldTyp = Typ, @oldFJahr = FJahr, @oldFMonat = FMonat, @oldFTag = FTag, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (RecId > @oldRecId)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto > @oldKonto)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag > @oldFTag)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat > @oldFMonat)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr > @oldFJahr)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ > @oldTyp)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN > @oldAusglTRN)
    ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key04] @oldRecId integer AS
  DECLARE @oldAusglTRN char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldFJahr char(4)
  DECLARE @oldFMonat char(2)
  DECLARE @oldFTag char(2)
  DECLARE @oldKonto char(8)
  SELECT @oldAusglTRN = AusglTRN, @oldTyp = Typ, @oldFJahr = FJahr, @oldFMonat = FMonat, @oldFTag = FTag, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (RecId < @oldRecId)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto < @oldKonto)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag < @oldFTag)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr = @oldFJahr) AND (FMonat < @oldFMonat)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ = @oldTyp) AND (FJahr < @oldFJahr)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN = @oldAusglTRN) AND (Typ < @oldTyp)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04)
    WHERE (AusglTRN < @oldAusglTRN)
    ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key04]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key04]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key04]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mAusglTRN char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mFJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,4)
  DECLARE @mFMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,14,2)
  DECLARE @mFTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN >= @mAusglTRN AND SUBSTRING(AusglTRN,1,LEN(@mAusglTRN)) = @mAusglTRN
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr >= @mFJahr AND SUBSTRING(FJahr,1,LEN(@mFJahr)) = @mFJahr
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat >= @mFMonat AND SUBSTRING(FMonat,1,LEN(@mFMonat)) = @mFMonat
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag >= @mFTag AND SUBSTRING(FTag,1,LEN(@mFTag)) = @mFTag
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY AusglTRN,Typ,FJahr,FMonat,FTag,Konto,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key04] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key04] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key04')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mAusglTRN char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mFJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,4)
  DECLARE @mFMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,14,2)
  DECLARE @mFTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      SUBSTRING(AusglTRN,1,LEN(@mAusglTRN)) = @mAusglTRN
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      SUBSTRING(FJahr,1,LEN(@mFJahr)) = @mFJahr
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      SUBSTRING(FMonat,1,LEN(@mFMonat)) = @mFMonat
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      SUBSTRING(FTag,1,LEN(@mFTag)) = @mFTag
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key04) WHERE 
      AusglTRN = @mAusglTRN AND 
      Typ = @mTyp AND 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY AusglTRN desc,Typ desc,FJahr desc,FMonat desc,FTag desc,Konto desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key04] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key04] @xKey, @xLen')
GO


-- logical key  ORDER BY TransNo,Typ,Konto,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  DROP INDEX [Ik_Key05] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  CREATE INDEX [Ik_Key05] ON [dbo].[ut_Buchungszeilen] (TransNo,Typ,Konto,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) ORDER BY TransNo,Typ,Konto,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key05] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key05] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key05] @oldRecId integer AS
  DECLARE @oldTransNo char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldTransNo = TransNo, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId > @oldRecId)
    ORDER BY TransNo,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ = @oldTyp) AND (Konto > @oldKonto)
    ORDER BY TransNo,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ > @oldTyp)
    ORDER BY TransNo,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo > @oldTransNo)
    ORDER BY TransNo,Typ,Konto,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key05] @oldRecId integer AS
  DECLARE @oldTransNo char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  SELECT @oldTransNo = TransNo, @oldTyp = Typ, @oldKonto = Konto FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (RecId < @oldRecId)
    ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ = @oldTyp) AND (Konto < @oldKonto)
    ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo = @oldTransNo) AND (Typ < @oldTyp)
    ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05)
    WHERE (TransNo < @oldTransNo)
    ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key05]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key05]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key05]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key05]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY TransNo,Typ,Konto,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key05] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key05')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key05) WHERE 
      TransNo = @mTransNo AND 
      Typ = @mTyp AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY TransNo desc,Typ desc,Konto desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key05] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key05] @xKey, @xLen')
GO


-- logical key  ORDER BY Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key06')
  DROP INDEX [Ik_Key06] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key06')
  CREATE INDEX [Ik_Key06] ON [dbo].[ut_Buchungszeilen] (Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) ORDER BY Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key06] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key06] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key06] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)
    ORDER BY Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)
    ORDER BY Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat > @oldMonat)
    ORDER BY Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr > @oldJahr)
    ORDER BY Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key06] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key06] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)
    ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)
    ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr = @oldJahr) AND (Monat < @oldMonat)
    ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06)
    WHERE (Jahr < @oldJahr)
    ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key06] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key06]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key06]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key06]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key06]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key06] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key06] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key06')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key06) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key06] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key06] @xKey, @xLen')
GO


-- logical key  ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  DROP INDEX [Ik_Key07] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  CREATE INDEX [Ik_Key07] ON [dbo].[ut_Buchungszeilen] (Konto,Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key07] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key07] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key07] @oldRecId integer AS
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat > @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr > @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto > @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key07] @oldRecId integer AS
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat < @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto = @oldKonto) AND (Jahr < @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07)
    WHERE ((Konto < @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key07]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key07]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key07]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key07]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key07] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key07] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key07')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key07) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key07] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key07] @xKey, @xLen')
GO


-- logical key  ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  DROP INDEX [Ik_Key08] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  CREATE INDEX [Ik_Key08] ON [dbo].[ut_Buchungszeilen] (Jahr,Monat,Tag,Konto,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key08] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key08] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key08] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldKonto = Konto, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto = @oldKonto) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto = @oldKonto) AND (TransNo > @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto > @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat > @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr > @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key08] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key08] @oldRecId integer AS
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  SELECT @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldKonto = Konto, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto = @oldKonto) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto = @oldKonto) AND (TransNo < @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (Konto < @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr = @oldJahr) AND (Monat < @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08)
    WHERE ((Jahr < @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key08] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key08]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key08]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key08]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key08]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr,Monat,Tag,Konto,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key08] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key08] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key08')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key08) WHERE 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      Konto = @mKonto AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY Jahr desc,Monat desc,Tag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key08] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key08] @xKey, @xLen')
GO


-- logical key  ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key09')
  DROP INDEX [Ik_Key09] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key09')
  CREATE INDEX [Ik_Key09] ON [dbo].[ut_Buchungszeilen] (FJahr,FMonat,FTag,Konto,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key09] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key09] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key09] @oldRecId integer AS
  DECLARE @oldFJahr char(4)
  DECLARE @oldFMonat char(2)
  DECLARE @oldFTag char(2)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  SELECT @oldFJahr = FJahr, @oldFMonat = FMonat, @oldFTag = FTag, @oldKonto = Konto, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (TransNo > @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto > @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag > @oldFTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat > @oldFMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr > @oldFJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key09] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key09] @oldRecId integer AS
  DECLARE @oldFJahr char(4)
  DECLARE @oldFMonat char(2)
  DECLARE @oldFTag char(2)
  DECLARE @oldKonto char(8)
  DECLARE @oldTransNo char(8)
  SELECT @oldFJahr = FJahr, @oldFMonat = FMonat, @oldFTag = FTag, @oldKonto = Konto, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto = @oldKonto) AND (TransNo < @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag = @oldFTag) AND (Konto < @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat = @oldFMonat) AND (FTag < @oldFTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr = @oldFJahr) AND (FMonat < @oldFMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09)
    WHERE ((FJahr < @oldFJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
    ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key09] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key09]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key09]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key09]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key09]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mFJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mFMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mFTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr >= @mFJahr AND SUBSTRING(FJahr,1,LEN(@mFJahr)) = @mFJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat >= @mFMonat AND SUBSTRING(FMonat,1,LEN(@mFMonat)) = @mFMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag >= @mFTag AND SUBSTRING(FTag,1,LEN(@mFTag)) = @mFTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr,FMonat,FTag,Konto,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key09] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key09] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key09')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mFJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mFMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mFTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      SUBSTRING(FJahr,1,LEN(@mFJahr)) = @mFJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      SUBSTRING(FMonat,1,LEN(@mFMonat)) = @mFMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      SUBSTRING(FTag,1,LEN(@mFTag)) = @mFTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key09) WHERE 
      FJahr = @mFJahr AND 
      FMonat = @mFMonat AND 
      FTag = @mFTag AND 
      Konto = @mKonto AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = ''))
      ORDER BY FJahr desc,FMonat desc,FTag desc,Konto desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key09] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key09] @xKey, @xLen')
GO


-- logical key  ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  DROP INDEX [Ik_Key10] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  CREATE INDEX [Ik_Key10] ON [dbo].[ut_Buchungszeilen] (Konto,Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0) ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key10] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE (Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0) ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key10] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key10] @oldRecId integer AS
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat > @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr > @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto > @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key10] @oldRecId integer AS
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat < @oldMonat)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto = @oldKonto) AND (Jahr < @oldJahr)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10)
    WHERE ((Konto < @oldKonto)) AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
    ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key10]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key10]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key10]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key10]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key10] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key10] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key10')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key10) WHERE 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      AND ((Status1 = 1) AND (Typ = 'P') AND (AusglTRN = '') AND (SkoFrist > 0))
      ORDER BY Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key10] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key10] @xKey, @xLen')
GO


-- logical key  ORDER BY Vorgang,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key11')
  DROP INDEX [Ik_Key11] ON [dbo].[ut_Buchungszeilen]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  ORDER BY Vorgang,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key11] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  ORDER BY Vorgang desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key11] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key11] @oldRecId integer AS
  DECLARE @oldVorgang char(20)
  DECLARE @oldTransNo char(8)
  SELECT @oldVorgang = Vorgang, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang = @oldVorgang) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY Vorgang,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang = @oldVorgang) AND (TransNo > @oldTransNo)
    ORDER BY Vorgang,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang > @oldVorgang)
    ORDER BY Vorgang,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key11] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key11] @oldRecId integer AS
  DECLARE @oldVorgang char(20)
  DECLARE @oldTransNo char(8)
  SELECT @oldVorgang = Vorgang, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang = @oldVorgang) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY Vorgang desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang = @oldVorgang) AND (TransNo < @oldTransNo)
    ORDER BY Vorgang desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] 
    WHERE (Vorgang < @oldVorgang)
    ORDER BY Vorgang desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key11] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key11]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key11]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key11]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key11]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mVorgang char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang AND 
      TransNo = @mTransNo
      ORDER BY Vorgang,TransNo,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang
      ORDER BY Vorgang,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang >= @mVorgang AND SUBSTRING(Vorgang,1,LEN(@mVorgang)) = @mVorgang
      ORDER BY Vorgang,TransNo,RecId
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Vorgang,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key11] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key11] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key11')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mVorgang char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang AND 
      TransNo = @mTransNo
      ORDER BY Vorgang desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang
      ORDER BY Vorgang desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      SUBSTRING(Vorgang,1,LEN(@mVorgang)) = @mVorgang
      ORDER BY Vorgang desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen]  WHERE 
      Vorgang = @mVorgang AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY Vorgang desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key11] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key11] @xKey, @xLen')
GO


-- logical key  ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  DROP INDEX [Ik_Key12] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  CREATE INDEX [Ik_Key12] ON [dbo].[ut_Buchungszeilen] (JnlLine,Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key12] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key12] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key12] @oldRecId integer AS
  DECLARE @oldJnlLine integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldJnlLine = JnlLine, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat > @oldMonat)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr > @oldJahr)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine > @oldJnlLine)
    ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key12] @oldRecId integer AS
  DECLARE @oldJnlLine integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldJnlLine = JnlLine, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr = @oldJahr) AND (Monat < @oldMonat)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine = @oldJnlLine) AND (Jahr < @oldJahr)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12)
    WHERE (JnlLine < @oldJnlLine)
    ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key12]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key12]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key12]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key12]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mJnlLine integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY JnlLine,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key12] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key12] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key12')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mJnlLine integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key12) WHERE 
      JnlLine = @mJnlLine AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY JnlLine desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key12] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key12] @xKey, @xLen')
GO


-- logical key  ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  DROP INDEX [Ik_Key13] ON [dbo].[ut_Buchungszeilen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Buchungszeilen' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  CREATE INDEX [Ik_Key13] ON [dbo].[ut_Buchungszeilen] (PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_First_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_Buchungszeilen_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Buchungszeilen_Key13] AS EXECUTE [dbo].[up_First_Buchungszeilen_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_Last_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_Buchungszeilen_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Buchungszeilen_Key13] AS EXECUTE [dbo].[up_Last_Buchungszeilen_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_Next_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_Buchungszeilen_Key13] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldPrjNr = PrjNr, @oldTyp = Typ, @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId > @oldRecId)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo > @oldTransNo)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag > @oldTag)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat > @oldMonat)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr > @oldJahr)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto > @oldKonto)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ > @oldTyp)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Buchungszeilen_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Next_Buchungszeilen_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_Buchungszeilen_Key13] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldKonto char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldTransNo char(8)
  SELECT @oldPrjNr = PrjNr, @oldTyp = Typ, @oldKonto = Konto, @oldJahr = Jahr, @oldMonat = Monat, @oldTag = Tag, @oldTransNo = TransNo FROM [dbo].[ut_Buchungszeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo = @oldTransNo) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag = @oldTag) AND (TransNo < @oldTransNo)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat = @oldMonat) AND (Tag < @oldTag)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr = @oldJahr) AND (Monat < @oldMonat)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto = @oldKonto) AND (Jahr < @oldJahr)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ = @oldTyp) AND (Konto < @oldKonto)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr = @oldPrjNr) AND (Typ < @oldTyp)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Buchungszeilen_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Buchungszeilen_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_Next50_Buchungszeilen_Key13]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[qp_Next50_Buchungszeilen_Key13]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungszeilen_Key13]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[qp_Prior50_Buchungszeilen_Key13]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_Buchungszeilen_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto >= @mKonto AND SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr >= @mJahr AND SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat >= @mMonat AND SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag >= @mTag AND SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo >= @mTransNo AND SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY PrjNr,Typ,Konto,Jahr,Monat,Tag,TransNo,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Buchungszeilen_Key13] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Buchungszeilen_Key13] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungszeilen_Key13')
  DROP PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_Buchungszeilen_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mKonto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,8)
  DECLARE @mJahr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,18,4)
  DECLARE @mMonat char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mTag char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,2)
  DECLARE @mTransNo char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      TransNo = @mTransNo
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      SUBSTRING(Konto,1,LEN(@mKonto)) = @mKonto
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      SUBSTRING(Jahr,1,LEN(@mJahr)) = @mJahr
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      SUBSTRING(Monat,1,LEN(@mMonat)) = @mMonat
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      SUBSTRING(Tag,1,LEN(@mTag)) = @mTag
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Buchungszeilen] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      Typ = @mTyp AND 
      Konto = @mKonto AND 
      Jahr = @mJahr AND 
      Monat = @mMonat AND 
      Tag = @mTag AND 
      SUBSTRING(TransNo,1,LEN(@mTransNo)) = @mTransNo
      ORDER BY PrjNr desc,Typ desc,Konto desc,Jahr desc,Monat desc,Tag desc,TransNo desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Buchungszeilen_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Buchungszeilen_Key13] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Buchungszeilen_Key13] @xKey, @xLen')
GO


