-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, 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_Belege')
BEGIN
CREATE TABLE [dbo].[ut_Belege]
(
  [RecId]  integer NOT NULL
 ,[PRJNR] char(8) DEFAULT ''
 ,[BELEGART] char(2) DEFAULT ''
 ,[BELEGNR] char(8) DEFAULT ''
 ,[VERSION] char(1) DEFAULT ''
 ,[LFDNR] char(6) DEFAULT ''
 ,[NRKREIS] integer DEFAULT 0
 ,[BELARTTEXT] char(30) DEFAULT ''
 ,[ANZNACHTR] integer DEFAULT 0
 ,[ANZRAEUME] integer DEFAULT 0
 ,[ANZBAUPHA] integer DEFAULT 0
 ,[BAUPHAAKT] integer DEFAULT 0
 ,[KDNR] char(15) DEFAULT ''
 ,[BLNR] char(15) DEFAULT ''
 ,[TAG] char(2) DEFAULT ''
 ,[TAGDUMMY] char(1) DEFAULT ''
 ,[MONAT] char(2) DEFAULT ''
 ,[MONDUMMY] char(1) DEFAULT ''
 ,[JAHR] char(4) DEFAULT ''
 ,[SUBMISSION] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[BAUBEGINN] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[BAUFERTIG] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[FERTIGJN] tinyint DEFAULT 0
 ,[EIGANSCH] char(2) DEFAULT ''
 ,[PHASE] char(2) DEFAULT ''
 ,[SKONTOTAGE] integer DEFAULT 0
 ,[SKONTOSATZ] float DEFAULT 0
 ,[DRKFFTITEL] tinyint DEFAULT 0
 ,[DRKINCMWST] tinyint DEFAULT 0
 ,[DRKAUFMASS] tinyint DEFAULT 0
 ,[DRKLM] tinyint DEFAULT 0
 ,[DRKZUBEHOE] tinyint DEFAULT 0
 ,[DRKDMRAB] tinyint DEFAULT 0
 ,[DRKRABATT] tinyint DEFAULT 0
 ,[DRKNULLWER] tinyint DEFAULT 0
 ,[AUFLOHN] float DEFAULT 0
 ,[AUFMAT] float DEFAULT 0
 ,[AUFFREMD] float DEFAULT 0
 ,[MITTLOHN] float DEFAULT 0
 ,[EXACT] tinyint DEFAULT 0
 ,[KGRPS] tinyint DEFAULT 0
 ,[KZZUBEHOER] tinyint DEFAULT 0
 ,[LOHNANTEIL] tinyint DEFAULT 0
 ,[KZVK2] tinyint DEFAULT 0
 ,[TITEL] tinyint DEFAULT 0
 ,[MANUELL] tinyint DEFAULT 0
 ,[OZMASKE] char(18) DEFAULT ''
 ,[ANREDE] char(50) DEFAULT ''
 ,[VORNAME] char(50) DEFAULT ''
 ,[NAME1] char(50) DEFAULT ''
 ,[NAME2] char(50) DEFAULT ''
 ,[ZUHAENDEN] char(50) DEFAULT ''
 ,[STRASSE] char(50) DEFAULT ''
 ,[LAND] char(10) DEFAULT ''
 ,[PLZ] char(10) DEFAULT ''
 ,[ORT] char(50) DEFAULT ''
 ,[BRANREDE] char(50) DEFAULT ''
 ,[VORHABEN] char(10) DEFAULT ''
 ,[BETREFF1] char(42) DEFAULT ''
 ,[BETREFF2] char(42) DEFAULT ''
 ,[BETREFF3] char(42) DEFAULT ''
 ,[EDMENGEN] tinyint DEFAULT 0
 ,[EDARTNR] tinyint DEFAULT 0
 ,[EDLPOSNR] tinyint DEFAULT 0
 ,[DRKNETTO] tinyint DEFAULT 0
 ,[UEBERTRAG] tinyint DEFAULT 0
 ,[TITELZUS] tinyint DEFAULT 0
 ,[DRKTEXT] integer DEFAULT 0
 ,[DRKUPOS] tinyint DEFAULT 0
 ,[DRKUPOSPR] tinyint DEFAULT 0
 ,[EDFAKTOR] tinyint DEFAULT 0
 ,[FAELLTAGE] integer DEFAULT 0
 ,[DRKARTNR] tinyint DEFAULT 0
 ,[DRKBRANRE] tinyint DEFAULT 0
 ,[UNSERZEI] char(10) DEFAULT ''
 ,[SICHBEHALT] float DEFAULT 0
 ,[DRKVORTEXT] tinyint DEFAULT 0
 ,[DRKNACTEXT] tinyint DEFAULT 0
 ,[DRKTITSUMM] tinyint DEFAULT 0
 ,[JOURNAL] integer DEFAULT 0
 ,[ZUVERBUCH] tinyint DEFAULT 0
 ,[VERBUCHT] tinyint DEFAULT 0
 ,[NETTO] float DEFAULT 0
 ,[MWST] float DEFAULT 0
 ,[MWSTSATZ] float DEFAULT 0
 ,[BRUTTO] float DEFAULT 0
 ,[VERBUCHTDM] float DEFAULT 0
 ,[SUMMEMAT] float DEFAULT 0
 ,[SUMMELOHN] float DEFAULT 0
 ,[SUMMEFREMD] float DEFAULT 0
 ,[SUMMEZEIT] float DEFAULT 0
 ,[SUMMEEKFR] float DEFAULT 0
 ,[SUMMEEKM] float DEFAULT 0
 ,[GEMEINK] float DEFAULT 0
 ,[GEMEINKVH] float DEFAULT 0
 ,[MAHNENJN] tinyint DEFAULT 0
 ,[MAHNSTUFE] integer DEFAULT 0
 ,[BEZAHLT] float DEFAULT 0
 ,[ZULETZTBEZ] float DEFAULT 0
 ,[ZAHLDATUM] char(10) DEFAULT ''
 ,[WAEHRUNG] char(6) DEFAULT ''
 ,[KURSZUEURO] float DEFAULT 0
 ,[DRKBANK] tinyint DEFAULT 0
 ,[EGRUMAT] integer DEFAULT 0
 ,[EGRULOHN] integer DEFAULT 0
 ,[EGRUFREMD] integer DEFAULT 0
 ,[EGRUGEMEIN] integer DEFAULT 0
 ,[KST] char(20) DEFAULT ''
 ,[KTR] char(20) DEFAULT ''
 ,[DEBKONTO] char(15) DEFAULT ''
 ,[NERECHNEN] tinyint DEFAULT 0
 ,[NEAUSCHLAG] tinyint DEFAULT 0
 ,[NETRENNEN] tinyint DEFAULT 0
 ,[FREMDPROJ] tinyint DEFAULT 0
 ,[GAEBJN] tinyint DEFAULT 0
 ,[GAEBNAME] char(20) DEFAULT ''
 ,[GAEBPHASE] integer DEFAULT 0
 ,[NETTO1] float DEFAULT 0
 ,[NETTO2] float DEFAULT 0
 ,[UST1] float DEFAULT 0
 ,[UST2] float DEFAULT 0
 ,[USTVH2] float DEFAULT 0
 ,[RES4NG2] char(10) DEFAULT ''
 ,[NUMRECS] integer DEFAULT 0
 ,[EDocType] char(10) DEFAULT ''
  CONSTRAINT [Pk_Belege]  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_Belege_KeyDel')
  DROP TRIGGER [dbo].[tr_Belege_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Belege_KeyUpd')
  DROP TRIGGER [dbo].[tr_Belege_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Belege_Keys')
  DROP TABLE [dbo].[ut_Belege_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_Belege' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'BELEGART')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BELEGART] 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_Belege' and sc.[name] = 'BELEGNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BELEGNR] 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_Belege' and sc.[name] = 'VERSION')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [VERSION] 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_Belege' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [LFDNR] 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_Belege' and sc.[name] = 'NRKREIS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NRKREIS] 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_Belege' and sc.[name] = 'BELARTTEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BELARTTEXT] char(30) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ANZNACHTR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ANZNACHTR] 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_Belege' and sc.[name] = 'ANZRAEUME')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ANZRAEUME] 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_Belege' and sc.[name] = 'ANZBAUPHA')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ANZBAUPHA] 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_Belege' and sc.[name] = 'BAUPHAAKT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BAUPHAAKT] 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_Belege' and sc.[name] = 'KDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [KDNR] char(15) 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_Belege' and sc.[name] = 'BLNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BLNR] char(15) 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_Belege' and sc.[name] = 'TAG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'TAGDUMMY')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [TAGDUMMY] 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_Belege' and sc.[name] = 'MONAT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'MONDUMMY')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MONDUMMY] 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_Belege' and sc.[name] = 'JAHR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'SUBMISSION')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUBMISSION] 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_Belege' and sc.[name] = 'BAUBEGINN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BAUBEGINN] 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_Belege' and sc.[name] = 'BAUFERTIG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BAUFERTIG] 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_Belege' and sc.[name] = 'FERTIGJN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [FERTIGJN] 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_Belege' and sc.[name] = 'EIGANSCH')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EIGANSCH] 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_Belege' and sc.[name] = 'PHASE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [PHASE] 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_Belege' and sc.[name] = 'SKONTOTAGE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SKONTOTAGE] 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_Belege' and sc.[name] = 'SKONTOSATZ')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SKONTOSATZ] 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_Belege' and sc.[name] = 'DRKFFTITEL')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKFFTITEL] 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_Belege' and sc.[name] = 'DRKINCMWST')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKINCMWST] 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_Belege' and sc.[name] = 'DRKAUFMASS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKAUFMASS] 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_Belege' and sc.[name] = 'DRKLM')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKLM] 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_Belege' and sc.[name] = 'DRKZUBEHOE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKZUBEHOE] 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_Belege' and sc.[name] = 'DRKDMRAB')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKDMRAB] 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_Belege' and sc.[name] = 'DRKRABATT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKRABATT] 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_Belege' and sc.[name] = 'DRKNULLWER')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKNULLWER] 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_Belege' and sc.[name] = 'AUFLOHN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [AUFLOHN] 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_Belege' and sc.[name] = 'AUFMAT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [AUFMAT] 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_Belege' and sc.[name] = 'AUFFREMD')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [AUFFREMD] 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_Belege' and sc.[name] = 'MITTLOHN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MITTLOHN] 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_Belege' and sc.[name] = 'EXACT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EXACT] 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_Belege' and sc.[name] = 'KGRPS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [KGRPS] 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_Belege' and sc.[name] = 'KZZUBEHOER')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [KZZUBEHOER] 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_Belege' and sc.[name] = 'LOHNANTEIL')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [LOHNANTEIL] 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_Belege' and sc.[name] = 'KZVK2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [KZVK2] 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_Belege' and sc.[name] = 'TITEL')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [TITEL] 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_Belege' and sc.[name] = 'MANUELL')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MANUELL] 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_Belege' and sc.[name] = 'OZMASKE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [OZMASKE] char(18) 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_Belege' and sc.[name] = 'ANREDE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ANREDE] 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_Belege' and sc.[name] = 'VORNAME')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [VORNAME] 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_Belege' and sc.[name] = 'NAME1')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NAME1] 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_Belege' and sc.[name] = 'NAME2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NAME2] 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_Belege' and sc.[name] = 'ZUHAENDEN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ZUHAENDEN] 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_Belege' and sc.[name] = 'STRASSE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [STRASSE] 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_Belege' and sc.[name] = 'LAND')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [LAND] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PLZ')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [PLZ] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ORT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ORT] 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_Belege' and sc.[name] = 'BRANREDE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BRANREDE] 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_Belege' and sc.[name] = 'VORHABEN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [VORHABEN] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF1')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BETREFF1] char(42) 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_Belege' and sc.[name] = 'BETREFF2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BETREFF2] char(42) 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_Belege' and sc.[name] = 'BETREFF3')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BETREFF3] char(42) 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_Belege' and sc.[name] = 'EDMENGEN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EDMENGEN] 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_Belege' and sc.[name] = 'EDARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EDARTNR] 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_Belege' and sc.[name] = 'EDLPOSNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EDLPOSNR] 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_Belege' and sc.[name] = 'DRKNETTO')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKNETTO] 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_Belege' and sc.[name] = 'UEBERTRAG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [UEBERTRAG] 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_Belege' and sc.[name] = 'TITELZUS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [TITELZUS] 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_Belege' and sc.[name] = 'DRKTEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKTEXT] 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_Belege' and sc.[name] = 'DRKUPOS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKUPOS] 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_Belege' and sc.[name] = 'DRKUPOSPR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKUPOSPR] 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_Belege' and sc.[name] = 'EDFAKTOR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EDFAKTOR] 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_Belege' and sc.[name] = 'FAELLTAGE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [FAELLTAGE] 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_Belege' and sc.[name] = 'DRKARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKARTNR] 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_Belege' and sc.[name] = 'DRKBRANRE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKBRANRE] 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_Belege' and sc.[name] = 'UNSERZEI')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [UNSERZEI] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'SICHBEHALT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SICHBEHALT] 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_Belege' and sc.[name] = 'DRKVORTEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKVORTEXT] 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_Belege' and sc.[name] = 'DRKNACTEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKNACTEXT] 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_Belege' and sc.[name] = 'DRKTITSUMM')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKTITSUMM] 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_Belege' and sc.[name] = 'JOURNAL')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [JOURNAL] 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_Belege' and sc.[name] = 'ZUVERBUCH')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ZUVERBUCH] 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_Belege' and sc.[name] = 'VERBUCHT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [VERBUCHT] 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_Belege' and sc.[name] = 'NETTO')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NETTO] 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_Belege' and sc.[name] = 'MWST')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MWST] 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_Belege' and sc.[name] = 'MWSTSATZ')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MWSTSATZ] 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_Belege' and sc.[name] = 'BRUTTO')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BRUTTO] 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_Belege' and sc.[name] = 'VERBUCHTDM')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [VERBUCHTDM] 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_Belege' and sc.[name] = 'SUMMEMAT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMEMAT] 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_Belege' and sc.[name] = 'SUMMELOHN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMELOHN] 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_Belege' and sc.[name] = 'SUMMEFREMD')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMEFREMD] 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_Belege' and sc.[name] = 'SUMMEZEIT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMEZEIT] 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_Belege' and sc.[name] = 'SUMMEEKFR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMEEKFR] 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_Belege' and sc.[name] = 'SUMMEEKM')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [SUMMEEKM] 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_Belege' and sc.[name] = 'GEMEINK')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [GEMEINK] 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_Belege' and sc.[name] = 'GEMEINKVH')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [GEMEINKVH] 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_Belege' and sc.[name] = 'MAHNENJN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MAHNENJN] 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_Belege' and sc.[name] = 'MAHNSTUFE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [MAHNSTUFE] 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_Belege' and sc.[name] = 'BEZAHLT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [BEZAHLT] 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_Belege' and sc.[name] = 'ZULETZTBEZ')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ZULETZTBEZ] 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_Belege' and sc.[name] = 'ZAHLDATUM')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [ZAHLDATUM] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'WAEHRUNG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [WAEHRUNG] char(6) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'KURSZUEURO')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [KURSZUEURO] 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_Belege' and sc.[name] = 'DRKBANK')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DRKBANK] 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_Belege' and sc.[name] = 'EGRUMAT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EGRUMAT] 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_Belege' and sc.[name] = 'EGRULOHN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EGRULOHN] 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_Belege' and sc.[name] = 'EGRUFREMD')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EGRUFREMD] 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_Belege' and sc.[name] = 'EGRUGEMEIN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EGRUGEMEIN] 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_Belege' and sc.[name] = 'KST')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'KTR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'DEBKONTO')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [DEBKONTO] char(15) 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_Belege' and sc.[name] = 'NERECHNEN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NERECHNEN] 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_Belege' and sc.[name] = 'NEAUSCHLAG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NEAUSCHLAG] 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_Belege' and sc.[name] = 'NETRENNEN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NETRENNEN] 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_Belege' and sc.[name] = 'FREMDPROJ')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [FREMDPROJ] 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_Belege' and sc.[name] = 'GAEBJN')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [GAEBJN] 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_Belege' and sc.[name] = 'GAEBNAME')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [GAEBNAME] 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_Belege' and sc.[name] = 'GAEBPHASE')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [GAEBPHASE] 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_Belege' and sc.[name] = 'NETTO1')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NETTO1] 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_Belege' and sc.[name] = 'NETTO2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NETTO2] 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_Belege' and sc.[name] = 'UST1')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [UST1] 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_Belege' and sc.[name] = 'UST2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [UST2] 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_Belege' and sc.[name] = 'USTVH2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [USTVH2] 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_Belege' and sc.[name] = 'RES4NG2')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [RES4NG2] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NUMRECS')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [NUMRECS] 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_Belege' and sc.[name] = 'EDocType')
BEGIN
  ALTER TABLE [dbo].[ut_Belege] ADD [EDocType] char(10) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' 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_Belege' and sc.[name] = 'PRJNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'PRJNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [PRJNR] 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_Belege' and sc.[name] = 'BELEGART'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGART'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELEGART] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGART'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGART'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELEGART] 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_Belege' and sc.[name] = 'BELEGNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELEGNR] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELEGNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELEGNR] varchar(8)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VERSION'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VERSION'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VERSION] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VERSION'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VERSION'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VERSION] varchar(1)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LFDNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LFDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [LFDNR] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LFDNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LFDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [LFDNR] varchar(6)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELARTTEXT'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELARTTEXT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELARTTEXT] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELARTTEXT'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BELARTTEXT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BELARTTEXT] varchar(30)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'KDNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'KDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [KDNR] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'KDNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'KDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [KDNR] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BLNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BLNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BLNR] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BLNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BLNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BLNR] varchar(15)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' 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_Belege' and sc.[name] = 'TAG'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'TAG'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [TAG] varchar(2)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'TAGDUMMY'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'TAGDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [TAGDUMMY] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'TAGDUMMY'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'TAGDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [TAGDUMMY] varchar(1)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' 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_Belege' and sc.[name] = 'MONAT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'MONAT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [MONAT] varchar(2)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'MONDUMMY'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'MONDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [MONDUMMY] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'MONDUMMY'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'MONDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [MONDUMMY] varchar(1)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' 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_Belege' and sc.[name] = 'JAHR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'JAHR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' and sc.[name] = 'EIGANSCH'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EIGANSCH'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [EIGANSCH] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EIGANSCH'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EIGANSCH'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [EIGANSCH] 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_Belege' and sc.[name] = 'PHASE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PHASE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [PHASE] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PHASE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PHASE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [PHASE] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'OZMASKE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'OZMASKE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [OZMASKE] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'OZMASKE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'OZMASKE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [OZMASKE] varchar(18)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ANREDE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ANREDE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ANREDE] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ANREDE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ANREDE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ANREDE] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORNAME'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORNAME'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VORNAME] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORNAME'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORNAME'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VORNAME] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME1'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [NAME1] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME1'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [NAME1] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [NAME2] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'NAME2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [NAME2] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZUHAENDEN'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZUHAENDEN'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ZUHAENDEN] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZUHAENDEN'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZUHAENDEN'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ZUHAENDEN] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'STRASSE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'STRASSE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [STRASSE] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'STRASSE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'STRASSE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [STRASSE] varchar(50)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LAND'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LAND'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [LAND] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LAND'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'LAND'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [LAND] varchar(10)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PLZ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PLZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [PLZ] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PLZ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'PLZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [PLZ] varchar(10)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ORT'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ORT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ORT] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ORT'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ORT'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ORT] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BRANREDE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BRANREDE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BRANREDE] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BRANREDE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BRANREDE'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BRANREDE] varchar(50)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORHABEN'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORHABEN'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VORHABEN] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORHABEN'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'VORHABEN'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [VORHABEN] varchar(10)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF1'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF1] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF1'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF1] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF2] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF2] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF3'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF3'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF3] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF3'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'BETREFF3'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [BETREFF3] varchar(42)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'UNSERZEI'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'UNSERZEI'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [UNSERZEI] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'UNSERZEI'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'UNSERZEI'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [UNSERZEI] varchar(10)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZAHLDATUM'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZAHLDATUM'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ZAHLDATUM] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZAHLDATUM'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'ZAHLDATUM'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [ZAHLDATUM] varchar(10)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'WAEHRUNG'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'WAEHRUNG'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [WAEHRUNG] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'WAEHRUNG'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'WAEHRUNG'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [WAEHRUNG] varchar(6)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' 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_Belege' and sc.[name] = 'KST'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'KST'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'KTR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] 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_Belege' 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_Belege' and sc.[name] = 'KTR'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [KTR] varchar(20)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'DEBKONTO'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'DEBKONTO'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [DEBKONTO] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'DEBKONTO'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'DEBKONTO'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [DEBKONTO] varchar(15)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'GAEBNAME'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'GAEBNAME'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [GAEBNAME] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'GAEBNAME'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'GAEBNAME'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [GAEBNAME] varchar(20)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'RES4NG2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'RES4NG2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [RES4NG2] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'RES4NG2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'RES4NG2'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [RES4NG2] varchar(10)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EDocType'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EDocType'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [EDocType] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EDocType'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege' and sc.[name] = 'EDocType'))
BEGIN
   ALTER TABLE [dbo].[ut_Belege] ALTER COLUMN [EDocType] varchar(10)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Belege')
  DROP VIEW [dbo].[Belege]
GO
CREATE VIEW [dbo].[Belege] AS
  SELECT [RecId]
 ,[PRJNR]
 ,[BELEGART]
 ,[BELEGNR]
 ,[VERSION]
 ,[LFDNR]
 ,[NRKREIS]
 ,[BELARTTEXT]
 ,[ANZNACHTR]
 ,[ANZRAEUME]
 ,[ANZBAUPHA]
 ,[BAUPHAAKT]
 ,[KDNR]
 ,[BLNR]
 ,[TAG]
 ,[TAGDUMMY]
 ,[MONAT]
 ,[MONDUMMY]
 ,[JAHR]
 ,[SUBMISSION]
 ,[BAUBEGINN]
 ,[BAUFERTIG]
 ,[FERTIGJN]
 ,[EIGANSCH]
 ,[PHASE]
 ,[SKONTOTAGE]
 ,[SKONTOSATZ]
 ,[DRKFFTITEL]
 ,[DRKINCMWST]
 ,[DRKAUFMASS]
 ,[DRKLM]
 ,[DRKZUBEHOE]
 ,[DRKDMRAB]
 ,[DRKRABATT]
 ,[DRKNULLWER]
 ,[AUFLOHN]
 ,[AUFMAT]
 ,[AUFFREMD]
 ,[MITTLOHN]
 ,[EXACT]
 ,[KGRPS]
 ,[KZZUBEHOER]
 ,[LOHNANTEIL]
 ,[KZVK2]
 ,[TITEL]
 ,[MANUELL]
 ,[OZMASKE]
 ,[ANREDE]
 ,[VORNAME]
 ,[NAME1]
 ,[NAME2]
 ,[ZUHAENDEN]
 ,[STRASSE]
 ,[LAND]
 ,[PLZ]
 ,[ORT]
 ,[BRANREDE]
 ,[VORHABEN]
 ,[BETREFF1]
 ,[BETREFF2]
 ,[BETREFF3]
 ,[EDMENGEN]
 ,[EDARTNR]
 ,[EDLPOSNR]
 ,[DRKNETTO]
 ,[UEBERTRAG]
 ,[TITELZUS]
 ,[DRKTEXT]
 ,[DRKUPOS]
 ,[DRKUPOSPR]
 ,[EDFAKTOR]
 ,[FAELLTAGE]
 ,[DRKARTNR]
 ,[DRKBRANRE]
 ,[UNSERZEI]
 ,[SICHBEHALT]
 ,[DRKVORTEXT]
 ,[DRKNACTEXT]
 ,[DRKTITSUMM]
 ,[JOURNAL]
 ,[ZUVERBUCH]
 ,[VERBUCHT]
 ,[NETTO]
 ,[MWST]
 ,[MWSTSATZ]
 ,[BRUTTO]
 ,[VERBUCHTDM]
 ,[SUMMEMAT]
 ,[SUMMELOHN]
 ,[SUMMEFREMD]
 ,[SUMMEZEIT]
 ,[SUMMEEKFR]
 ,[SUMMEEKM]
 ,[GEMEINK]
 ,[GEMEINKVH]
 ,[MAHNENJN]
 ,[MAHNSTUFE]
 ,[BEZAHLT]
 ,[ZULETZTBEZ]
 ,[ZAHLDATUM]
 ,[WAEHRUNG]
 ,[KURSZUEURO]
 ,[DRKBANK]
 ,[EGRUMAT]
 ,[EGRULOHN]
 ,[EGRUFREMD]
 ,[EGRUGEMEIN]
 ,[KST]
 ,[KTR]
 ,[DEBKONTO]
 ,[NERECHNEN]
 ,[NEAUSCHLAG]
 ,[NETRENNEN]
 ,[FREMDPROJ]
 ,[GAEBJN]
 ,[GAEBNAME]
 ,[GAEBPHASE]
 ,[NETTO1]
 ,[NETTO2]
 ,[UST1]
 ,[UST2]
 ,[USTVH2]
 ,[RES4NG2]
 ,[NUMRECS]
 ,[EDocType]
  FROM [dbo].[ut_Belege]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Belege')
  DROP PROCEDURE [dbo].[up_Sel_Belege]
GO
CREATE PROCEDURE [dbo].[up_Sel_Belege]  @mRI integer
AS
  SELECT
    [PRJNR]
   ,[BELEGART]
   ,[BELEGNR]
   ,[VERSION]
   ,[LFDNR]
   ,[NRKREIS]
   ,[BELARTTEXT]
   ,[ANZNACHTR]
   ,[ANZRAEUME]
   ,[ANZBAUPHA]
   ,[BAUPHAAKT]
   ,[KDNR]
   ,[BLNR]
   ,[TAG]
   ,[TAGDUMMY]
   ,[MONAT]
   ,[MONDUMMY]
   ,[JAHR]
   ,[SUBMISSION]
   ,[BAUBEGINN]
   ,[BAUFERTIG]
   ,[FERTIGJN]
   ,[EIGANSCH]
   ,[PHASE]
   ,[SKONTOTAGE]
   ,[SKONTOSATZ]
   ,[DRKFFTITEL]
   ,[DRKINCMWST]
   ,[DRKAUFMASS]
   ,[DRKLM]
   ,[DRKZUBEHOE]
   ,[DRKDMRAB]
   ,[DRKRABATT]
   ,[DRKNULLWER]
   ,[AUFLOHN]
   ,[AUFMAT]
   ,[AUFFREMD]
   ,[MITTLOHN]
   ,[EXACT]
   ,[KGRPS]
   ,[KZZUBEHOER]
   ,[LOHNANTEIL]
   ,[KZVK2]
   ,[TITEL]
   ,[MANUELL]
   ,[OZMASKE]
   ,[ANREDE]
   ,[VORNAME]
   ,[NAME1]
   ,[NAME2]
   ,[ZUHAENDEN]
   ,[STRASSE]
   ,[LAND]
   ,[PLZ]
   ,[ORT]
   ,[BRANREDE]
   ,[VORHABEN]
   ,[BETREFF1]
   ,[BETREFF2]
   ,[BETREFF3]
   ,[EDMENGEN]
   ,[EDARTNR]
   ,[EDLPOSNR]
   ,[DRKNETTO]
   ,[UEBERTRAG]
   ,[TITELZUS]
   ,[DRKTEXT]
   ,[DRKUPOS]
   ,[DRKUPOSPR]
   ,[EDFAKTOR]
   ,[FAELLTAGE]
   ,[DRKARTNR]
   ,[DRKBRANRE]
   ,[UNSERZEI]
   ,[SICHBEHALT]
   ,[DRKVORTEXT]
   ,[DRKNACTEXT]
   ,[DRKTITSUMM]
   ,[JOURNAL]
   ,[ZUVERBUCH]
   ,[VERBUCHT]
   ,[NETTO]
   ,[MWST]
   ,[MWSTSATZ]
   ,[BRUTTO]
   ,[VERBUCHTDM]
   ,[SUMMEMAT]
   ,[SUMMELOHN]
   ,[SUMMEFREMD]
   ,[SUMMEZEIT]
   ,[SUMMEEKFR]
   ,[SUMMEEKM]
   ,[GEMEINK]
   ,[GEMEINKVH]
   ,[MAHNENJN]
   ,[MAHNSTUFE]
   ,[BEZAHLT]
   ,[ZULETZTBEZ]
   ,[ZAHLDATUM]
   ,[WAEHRUNG]
   ,[KURSZUEURO]
   ,[DRKBANK]
   ,[EGRUMAT]
   ,[EGRULOHN]
   ,[EGRUFREMD]
   ,[EGRUGEMEIN]
   ,[KST]
   ,[KTR]
   ,[DEBKONTO]
   ,[NERECHNEN]
   ,[NEAUSCHLAG]
   ,[NETRENNEN]
   ,[FREMDPROJ]
   ,[GAEBJN]
   ,[GAEBNAME]
   ,[GAEBPHASE]
   ,[NETTO1]
   ,[NETTO2]
   ,[UST1]
   ,[UST2]
   ,[USTVH2]
   ,[RES4NG2]
   ,[NUMRECS]
   ,[EDocType]
  FROM [dbo].[ut_Belege] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Belege')
  DROP PROCEDURE [dbo].[up_Append_Belege]
GO
CREATE PROCEDURE [dbo].[up_Append_Belege]
   @Param_PRJNR char(8)
  ,@Param_BELEGART char(2)
  ,@Param_BELEGNR char(8)
  ,@Param_VERSION char(1)
  ,@Param_LFDNR char(6)
  ,@Param_NRKREIS integer
  ,@Param_BELARTTEXT char(30)
  ,@Param_ANZNACHTR integer
  ,@Param_ANZRAEUME integer
  ,@Param_ANZBAUPHA integer
  ,@Param_BAUPHAAKT integer
  ,@Param_KDNR char(15)
  ,@Param_BLNR char(15)
  ,@Param_TAG char(2)
  ,@Param_TAGDUMMY char(1)
  ,@Param_MONAT char(2)
  ,@Param_MONDUMMY char(1)
  ,@Param_JAHR char(4)
  ,@Param_SUBMISSION datetime
  ,@Param_BAUBEGINN datetime
  ,@Param_BAUFERTIG datetime
  ,@Param_FERTIGJN tinyint
  ,@Param_EIGANSCH char(2)
  ,@Param_PHASE char(2)
  ,@Param_SKONTOTAGE integer
  ,@Param_SKONTOSATZ float
  ,@Param_DRKFFTITEL tinyint
  ,@Param_DRKINCMWST tinyint
  ,@Param_DRKAUFMASS tinyint
  ,@Param_DRKLM tinyint
  ,@Param_DRKZUBEHOE tinyint
  ,@Param_DRKDMRAB tinyint
  ,@Param_DRKRABATT tinyint
  ,@Param_DRKNULLWER tinyint
  ,@Param_AUFLOHN float
  ,@Param_AUFMAT float
  ,@Param_AUFFREMD float
  ,@Param_MITTLOHN float
  ,@Param_EXACT tinyint
  ,@Param_KGRPS tinyint
  ,@Param_KZZUBEHOER tinyint
  ,@Param_LOHNANTEIL tinyint
  ,@Param_KZVK2 tinyint
  ,@Param_TITEL tinyint
  ,@Param_MANUELL tinyint
  ,@Param_OZMASKE char(18)
  ,@Param_ANREDE char(50)
  ,@Param_VORNAME char(50)
  ,@Param_NAME1 char(50)
  ,@Param_NAME2 char(50)
  ,@Param_ZUHAENDEN char(50)
  ,@Param_STRASSE char(50)
  ,@Param_LAND char(10)
  ,@Param_PLZ char(10)
  ,@Param_ORT char(50)
  ,@Param_BRANREDE char(50)
  ,@Param_VORHABEN char(10)
  ,@Param_BETREFF1 char(42)
  ,@Param_BETREFF2 char(42)
  ,@Param_BETREFF3 char(42)
  ,@Param_EDMENGEN tinyint
  ,@Param_EDARTNR tinyint
  ,@Param_EDLPOSNR tinyint
  ,@Param_DRKNETTO tinyint
  ,@Param_UEBERTRAG tinyint
  ,@Param_TITELZUS tinyint
  ,@Param_DRKTEXT integer
  ,@Param_DRKUPOS tinyint
  ,@Param_DRKUPOSPR tinyint
  ,@Param_EDFAKTOR tinyint
  ,@Param_FAELLTAGE integer
  ,@Param_DRKARTNR tinyint
  ,@Param_DRKBRANRE tinyint
  ,@Param_UNSERZEI char(10)
  ,@Param_SICHBEHALT float
  ,@Param_DRKVORTEXT tinyint
  ,@Param_DRKNACTEXT tinyint
  ,@Param_DRKTITSUMM tinyint
  ,@Param_JOURNAL integer
  ,@Param_ZUVERBUCH tinyint
  ,@Param_VERBUCHT tinyint
  ,@Param_NETTO float
  ,@Param_MWST float
  ,@Param_MWSTSATZ float
  ,@Param_BRUTTO float
  ,@Param_VERBUCHTDM float
  ,@Param_SUMMEMAT float
  ,@Param_SUMMELOHN float
  ,@Param_SUMMEFREMD float
  ,@Param_SUMMEZEIT float
  ,@Param_SUMMEEKFR float
  ,@Param_SUMMEEKM float
  ,@Param_GEMEINK float
  ,@Param_GEMEINKVH float
  ,@Param_MAHNENJN tinyint
  ,@Param_MAHNSTUFE integer
  ,@Param_BEZAHLT float
  ,@Param_ZULETZTBEZ float
  ,@Param_ZAHLDATUM char(10)
  ,@Param_WAEHRUNG char(6)
  ,@Param_KURSZUEURO float
  ,@Param_DRKBANK tinyint
  ,@Param_EGRUMAT integer
  ,@Param_EGRULOHN integer
  ,@Param_EGRUFREMD integer
  ,@Param_EGRUGEMEIN integer
  ,@Param_KST char(20)
  ,@Param_KTR char(20)
  ,@Param_DEBKONTO char(15)
  ,@Param_NERECHNEN tinyint
  ,@Param_NEAUSCHLAG tinyint
  ,@Param_NETRENNEN tinyint
  ,@Param_FREMDPROJ tinyint
  ,@Param_GAEBJN tinyint
  ,@Param_GAEBNAME char(20)
  ,@Param_GAEBPHASE integer
  ,@Param_NETTO1 float
  ,@Param_NETTO2 float
  ,@Param_UST1 float
  ,@Param_UST2 float
  ,@Param_USTVH2 float
  ,@Param_RES4NG2 char(10)
  ,@Param_NUMRECS integer
  ,@Param_EDocType char(10)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Belege()
      INSERT INTO [dbo].[ut_Belege] (RecId
        ,[PRJNR]
        ,[BELEGART]
        ,[BELEGNR]
        ,[VERSION]
        ,[LFDNR]
        ,[NRKREIS]
        ,[BELARTTEXT]
        ,[ANZNACHTR]
        ,[ANZRAEUME]
        ,[ANZBAUPHA]
        ,[BAUPHAAKT]
        ,[KDNR]
        ,[BLNR]
        ,[TAG]
        ,[TAGDUMMY]
        ,[MONAT]
        ,[MONDUMMY]
        ,[JAHR]
        ,[SUBMISSION]
        ,[BAUBEGINN]
        ,[BAUFERTIG]
        ,[FERTIGJN]
        ,[EIGANSCH]
        ,[PHASE]
        ,[SKONTOTAGE]
        ,[SKONTOSATZ]
        ,[DRKFFTITEL]
        ,[DRKINCMWST]
        ,[DRKAUFMASS]
        ,[DRKLM]
        ,[DRKZUBEHOE]
        ,[DRKDMRAB]
        ,[DRKRABATT]
        ,[DRKNULLWER]
        ,[AUFLOHN]
        ,[AUFMAT]
        ,[AUFFREMD]
        ,[MITTLOHN]
        ,[EXACT]
        ,[KGRPS]
        ,[KZZUBEHOER]
        ,[LOHNANTEIL]
        ,[KZVK2]
        ,[TITEL]
        ,[MANUELL]
        ,[OZMASKE]
        ,[ANREDE]
        ,[VORNAME]
        ,[NAME1]
        ,[NAME2]
        ,[ZUHAENDEN]
        ,[STRASSE]
        ,[LAND]
        ,[PLZ]
        ,[ORT]
        ,[BRANREDE]
        ,[VORHABEN]
        ,[BETREFF1]
        ,[BETREFF2]
        ,[BETREFF3]
        ,[EDMENGEN]
        ,[EDARTNR]
        ,[EDLPOSNR]
        ,[DRKNETTO]
        ,[UEBERTRAG]
        ,[TITELZUS]
        ,[DRKTEXT]
        ,[DRKUPOS]
        ,[DRKUPOSPR]
        ,[EDFAKTOR]
        ,[FAELLTAGE]
        ,[DRKARTNR]
        ,[DRKBRANRE]
        ,[UNSERZEI]
        ,[SICHBEHALT]
        ,[DRKVORTEXT]
        ,[DRKNACTEXT]
        ,[DRKTITSUMM]
        ,[JOURNAL]
        ,[ZUVERBUCH]
        ,[VERBUCHT]
        ,[NETTO]
        ,[MWST]
        ,[MWSTSATZ]
        ,[BRUTTO]
        ,[VERBUCHTDM]
        ,[SUMMEMAT]
        ,[SUMMELOHN]
        ,[SUMMEFREMD]
        ,[SUMMEZEIT]
        ,[SUMMEEKFR]
        ,[SUMMEEKM]
        ,[GEMEINK]
        ,[GEMEINKVH]
        ,[MAHNENJN]
        ,[MAHNSTUFE]
        ,[BEZAHLT]
        ,[ZULETZTBEZ]
        ,[ZAHLDATUM]
        ,[WAEHRUNG]
        ,[KURSZUEURO]
        ,[DRKBANK]
        ,[EGRUMAT]
        ,[EGRULOHN]
        ,[EGRUFREMD]
        ,[EGRUGEMEIN]
        ,[KST]
        ,[KTR]
        ,[DEBKONTO]
        ,[NERECHNEN]
        ,[NEAUSCHLAG]
        ,[NETRENNEN]
        ,[FREMDPROJ]
        ,[GAEBJN]
        ,[GAEBNAME]
        ,[GAEBPHASE]
        ,[NETTO1]
        ,[NETTO2]
        ,[UST1]
        ,[UST2]
        ,[USTVH2]
        ,[RES4NG2]
        ,[NUMRECS]
        ,[EDocType]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PRJNR))
        ,UPPER(RTRIM(@Param_BELEGART))
        ,UPPER(RTRIM(@Param_BELEGNR))
        ,UPPER(RTRIM(@Param_VERSION))
        ,UPPER(RTRIM(@Param_LFDNR))
        ,@Param_NRKREIS
        ,RTRIM(@Param_BELARTTEXT)
        ,@Param_ANZNACHTR
        ,@Param_ANZRAEUME
        ,@Param_ANZBAUPHA
        ,@Param_BAUPHAAKT
        ,UPPER(RTRIM(@Param_KDNR))
        ,UPPER(RTRIM(@Param_BLNR))
        ,RTRIM(@Param_TAG)
        ,RTRIM(@Param_TAGDUMMY)
        ,RTRIM(@Param_MONAT)
        ,RTRIM(@Param_MONDUMMY)
        ,RTRIM(@Param_JAHR)
        ,@Param_SUBMISSION
        ,@Param_BAUBEGINN
        ,@Param_BAUFERTIG
        ,@Param_FERTIGJN
        ,UPPER(RTRIM(@Param_EIGANSCH))
        ,RTRIM(@Param_PHASE)
        ,@Param_SKONTOTAGE
        ,@Param_SKONTOSATZ
        ,@Param_DRKFFTITEL
        ,@Param_DRKINCMWST
        ,@Param_DRKAUFMASS
        ,@Param_DRKLM
        ,@Param_DRKZUBEHOE
        ,@Param_DRKDMRAB
        ,@Param_DRKRABATT
        ,@Param_DRKNULLWER
        ,@Param_AUFLOHN
        ,@Param_AUFMAT
        ,@Param_AUFFREMD
        ,@Param_MITTLOHN
        ,@Param_EXACT
        ,@Param_KGRPS
        ,@Param_KZZUBEHOER
        ,@Param_LOHNANTEIL
        ,@Param_KZVK2
        ,@Param_TITEL
        ,@Param_MANUELL
        ,RTRIM(@Param_OZMASKE)
        ,RTRIM(@Param_ANREDE)
        ,RTRIM(@Param_VORNAME)
        ,RTRIM(@Param_NAME1)
        ,RTRIM(@Param_NAME2)
        ,RTRIM(@Param_ZUHAENDEN)
        ,RTRIM(@Param_STRASSE)
        ,RTRIM(@Param_LAND)
        ,RTRIM(@Param_PLZ)
        ,RTRIM(@Param_ORT)
        ,RTRIM(@Param_BRANREDE)
        ,UPPER(RTRIM(@Param_VORHABEN))
        ,RTRIM(@Param_BETREFF1)
        ,RTRIM(@Param_BETREFF2)
        ,RTRIM(@Param_BETREFF3)
        ,@Param_EDMENGEN
        ,@Param_EDARTNR
        ,@Param_EDLPOSNR
        ,@Param_DRKNETTO
        ,@Param_UEBERTRAG
        ,@Param_TITELZUS
        ,@Param_DRKTEXT
        ,@Param_DRKUPOS
        ,@Param_DRKUPOSPR
        ,@Param_EDFAKTOR
        ,@Param_FAELLTAGE
        ,@Param_DRKARTNR
        ,@Param_DRKBRANRE
        ,RTRIM(@Param_UNSERZEI)
        ,@Param_SICHBEHALT
        ,@Param_DRKVORTEXT
        ,@Param_DRKNACTEXT
        ,@Param_DRKTITSUMM
        ,@Param_JOURNAL
        ,@Param_ZUVERBUCH
        ,@Param_VERBUCHT
        ,@Param_NETTO
        ,@Param_MWST
        ,@Param_MWSTSATZ
        ,@Param_BRUTTO
        ,@Param_VERBUCHTDM
        ,@Param_SUMMEMAT
        ,@Param_SUMMELOHN
        ,@Param_SUMMEFREMD
        ,@Param_SUMMEZEIT
        ,@Param_SUMMEEKFR
        ,@Param_SUMMEEKM
        ,@Param_GEMEINK
        ,@Param_GEMEINKVH
        ,@Param_MAHNENJN
        ,@Param_MAHNSTUFE
        ,@Param_BEZAHLT
        ,@Param_ZULETZTBEZ
        ,RTRIM(@Param_ZAHLDATUM)
        ,RTRIM(@Param_WAEHRUNG)
        ,@Param_KURSZUEURO
        ,@Param_DRKBANK
        ,@Param_EGRUMAT
        ,@Param_EGRULOHN
        ,@Param_EGRUFREMD
        ,@Param_EGRUGEMEIN
        ,UPPER(RTRIM(@Param_KST))
        ,UPPER(RTRIM(@Param_KTR))
        ,RTRIM(@Param_DEBKONTO)
        ,@Param_NERECHNEN
        ,@Param_NEAUSCHLAG
        ,@Param_NETRENNEN
        ,@Param_FREMDPROJ
        ,@Param_GAEBJN
        ,RTRIM(@Param_GAEBNAME)
        ,@Param_GAEBPHASE
        ,@Param_NETTO1
        ,@Param_NETTO2
        ,@Param_UST1
        ,@Param_UST2
        ,@Param_USTVH2
        ,RTRIM(@Param_RES4NG2)
        ,@Param_NUMRECS
        ,UPPER(RTRIM(@Param_EDocType))
          )
      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_Belege')
  DROP PROCEDURE [dbo].[up_Add_Belege]
GO
CREATE PROCEDURE [dbo].[up_Add_Belege]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Belege()
  INSERT INTO [dbo].[ut_Belege] (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_Belege')
  DROP PROCEDURE [dbo].[up_Upd_Belege]
GO
CREATE PROCEDURE [dbo].[up_Upd_Belege] @mRI integer
  ,@Param_PRJNR char(8)
  ,@Param_BELEGART char(2)
  ,@Param_BELEGNR char(8)
  ,@Param_VERSION char(1)
  ,@Param_LFDNR char(6)
  ,@Param_NRKREIS integer
  ,@Param_BELARTTEXT char(30)
  ,@Param_ANZNACHTR integer
  ,@Param_ANZRAEUME integer
  ,@Param_ANZBAUPHA integer
  ,@Param_BAUPHAAKT integer
  ,@Param_KDNR char(15)
  ,@Param_BLNR char(15)
  ,@Param_TAG char(2)
  ,@Param_TAGDUMMY char(1)
  ,@Param_MONAT char(2)
  ,@Param_MONDUMMY char(1)
  ,@Param_JAHR char(4)
  ,@Param_SUBMISSION datetime
  ,@Param_BAUBEGINN datetime
  ,@Param_BAUFERTIG datetime
  ,@Param_FERTIGJN tinyint
  ,@Param_EIGANSCH char(2)
  ,@Param_PHASE char(2)
  ,@Param_SKONTOTAGE integer
  ,@Param_SKONTOSATZ float
  ,@Param_DRKFFTITEL tinyint
  ,@Param_DRKINCMWST tinyint
  ,@Param_DRKAUFMASS tinyint
  ,@Param_DRKLM tinyint
  ,@Param_DRKZUBEHOE tinyint
  ,@Param_DRKDMRAB tinyint
  ,@Param_DRKRABATT tinyint
  ,@Param_DRKNULLWER tinyint
  ,@Param_AUFLOHN float
  ,@Param_AUFMAT float
  ,@Param_AUFFREMD float
  ,@Param_MITTLOHN float
  ,@Param_EXACT tinyint
  ,@Param_KGRPS tinyint
  ,@Param_KZZUBEHOER tinyint
  ,@Param_LOHNANTEIL tinyint
  ,@Param_KZVK2 tinyint
  ,@Param_TITEL tinyint
  ,@Param_MANUELL tinyint
  ,@Param_OZMASKE char(18)
  ,@Param_ANREDE char(50)
  ,@Param_VORNAME char(50)
  ,@Param_NAME1 char(50)
  ,@Param_NAME2 char(50)
  ,@Param_ZUHAENDEN char(50)
  ,@Param_STRASSE char(50)
  ,@Param_LAND char(10)
  ,@Param_PLZ char(10)
  ,@Param_ORT char(50)
  ,@Param_BRANREDE char(50)
  ,@Param_VORHABEN char(10)
  ,@Param_BETREFF1 char(42)
  ,@Param_BETREFF2 char(42)
  ,@Param_BETREFF3 char(42)
  ,@Param_EDMENGEN tinyint
  ,@Param_EDARTNR tinyint
  ,@Param_EDLPOSNR tinyint
  ,@Param_DRKNETTO tinyint
  ,@Param_UEBERTRAG tinyint
  ,@Param_TITELZUS tinyint
  ,@Param_DRKTEXT integer
  ,@Param_DRKUPOS tinyint
  ,@Param_DRKUPOSPR tinyint
  ,@Param_EDFAKTOR tinyint
  ,@Param_FAELLTAGE integer
  ,@Param_DRKARTNR tinyint
  ,@Param_DRKBRANRE tinyint
  ,@Param_UNSERZEI char(10)
  ,@Param_SICHBEHALT float
  ,@Param_DRKVORTEXT tinyint
  ,@Param_DRKNACTEXT tinyint
  ,@Param_DRKTITSUMM tinyint
  ,@Param_JOURNAL integer
  ,@Param_ZUVERBUCH tinyint
  ,@Param_VERBUCHT tinyint
  ,@Param_NETTO float
  ,@Param_MWST float
  ,@Param_MWSTSATZ float
  ,@Param_BRUTTO float
  ,@Param_VERBUCHTDM float
  ,@Param_SUMMEMAT float
  ,@Param_SUMMELOHN float
  ,@Param_SUMMEFREMD float
  ,@Param_SUMMEZEIT float
  ,@Param_SUMMEEKFR float
  ,@Param_SUMMEEKM float
  ,@Param_GEMEINK float
  ,@Param_GEMEINKVH float
  ,@Param_MAHNENJN tinyint
  ,@Param_MAHNSTUFE integer
  ,@Param_BEZAHLT float
  ,@Param_ZULETZTBEZ float
  ,@Param_ZAHLDATUM char(10)
  ,@Param_WAEHRUNG char(6)
  ,@Param_KURSZUEURO float
  ,@Param_DRKBANK tinyint
  ,@Param_EGRUMAT integer
  ,@Param_EGRULOHN integer
  ,@Param_EGRUFREMD integer
  ,@Param_EGRUGEMEIN integer
  ,@Param_KST char(20)
  ,@Param_KTR char(20)
  ,@Param_DEBKONTO char(15)
  ,@Param_NERECHNEN tinyint
  ,@Param_NEAUSCHLAG tinyint
  ,@Param_NETRENNEN tinyint
  ,@Param_FREMDPROJ tinyint
  ,@Param_GAEBJN tinyint
  ,@Param_GAEBNAME char(20)
  ,@Param_GAEBPHASE integer
  ,@Param_NETTO1 float
  ,@Param_NETTO2 float
  ,@Param_UST1 float
  ,@Param_UST2 float
  ,@Param_USTVH2 float
  ,@Param_RES4NG2 char(10)
  ,@Param_NUMRECS integer
  ,@Param_EDocType char(10)
AS
  UPDATE [dbo].[ut_Belege] SET
    [PRJNR] = UPPER(RTRIM(@Param_PRJNR))
   ,[BELEGART] = UPPER(RTRIM(@Param_BELEGART))
   ,[BELEGNR] = UPPER(RTRIM(@Param_BELEGNR))
   ,[VERSION] = UPPER(RTRIM(@Param_VERSION))
   ,[LFDNR] = UPPER(RTRIM(@Param_LFDNR))
   ,[NRKREIS] = @Param_NRKREIS
   ,[BELARTTEXT] = RTRIM(@Param_BELARTTEXT)
   ,[ANZNACHTR] = @Param_ANZNACHTR
   ,[ANZRAEUME] = @Param_ANZRAEUME
   ,[ANZBAUPHA] = @Param_ANZBAUPHA
   ,[BAUPHAAKT] = @Param_BAUPHAAKT
   ,[KDNR] = UPPER(RTRIM(@Param_KDNR))
   ,[BLNR] = UPPER(RTRIM(@Param_BLNR))
   ,[TAG] = RTRIM(@Param_TAG)
   ,[TAGDUMMY] = RTRIM(@Param_TAGDUMMY)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[MONDUMMY] = RTRIM(@Param_MONDUMMY)
   ,[JAHR] = RTRIM(@Param_JAHR)
   ,[SUBMISSION] = @Param_SUBMISSION
   ,[BAUBEGINN] = @Param_BAUBEGINN
   ,[BAUFERTIG] = @Param_BAUFERTIG
   ,[FERTIGJN] = @Param_FERTIGJN
   ,[EIGANSCH] = UPPER(RTRIM(@Param_EIGANSCH))
   ,[PHASE] = RTRIM(@Param_PHASE)
   ,[SKONTOTAGE] = @Param_SKONTOTAGE
   ,[SKONTOSATZ] = @Param_SKONTOSATZ
   ,[DRKFFTITEL] = @Param_DRKFFTITEL
   ,[DRKINCMWST] = @Param_DRKINCMWST
   ,[DRKAUFMASS] = @Param_DRKAUFMASS
   ,[DRKLM] = @Param_DRKLM
   ,[DRKZUBEHOE] = @Param_DRKZUBEHOE
   ,[DRKDMRAB] = @Param_DRKDMRAB
   ,[DRKRABATT] = @Param_DRKRABATT
   ,[DRKNULLWER] = @Param_DRKNULLWER
   ,[AUFLOHN] = @Param_AUFLOHN
   ,[AUFMAT] = @Param_AUFMAT
   ,[AUFFREMD] = @Param_AUFFREMD
   ,[MITTLOHN] = @Param_MITTLOHN
   ,[EXACT] = @Param_EXACT
   ,[KGRPS] = @Param_KGRPS
   ,[KZZUBEHOER] = @Param_KZZUBEHOER
   ,[LOHNANTEIL] = @Param_LOHNANTEIL
   ,[KZVK2] = @Param_KZVK2
   ,[TITEL] = @Param_TITEL
   ,[MANUELL] = @Param_MANUELL
   ,[OZMASKE] = RTRIM(@Param_OZMASKE)
   ,[ANREDE] = RTRIM(@Param_ANREDE)
   ,[VORNAME] = RTRIM(@Param_VORNAME)
   ,[NAME1] = RTRIM(@Param_NAME1)
   ,[NAME2] = RTRIM(@Param_NAME2)
   ,[ZUHAENDEN] = RTRIM(@Param_ZUHAENDEN)
   ,[STRASSE] = RTRIM(@Param_STRASSE)
   ,[LAND] = RTRIM(@Param_LAND)
   ,[PLZ] = RTRIM(@Param_PLZ)
   ,[ORT] = RTRIM(@Param_ORT)
   ,[BRANREDE] = RTRIM(@Param_BRANREDE)
   ,[VORHABEN] = UPPER(RTRIM(@Param_VORHABEN))
   ,[BETREFF1] = RTRIM(@Param_BETREFF1)
   ,[BETREFF2] = RTRIM(@Param_BETREFF2)
   ,[BETREFF3] = RTRIM(@Param_BETREFF3)
   ,[EDMENGEN] = @Param_EDMENGEN
   ,[EDARTNR] = @Param_EDARTNR
   ,[EDLPOSNR] = @Param_EDLPOSNR
   ,[DRKNETTO] = @Param_DRKNETTO
   ,[UEBERTRAG] = @Param_UEBERTRAG
   ,[TITELZUS] = @Param_TITELZUS
   ,[DRKTEXT] = @Param_DRKTEXT
   ,[DRKUPOS] = @Param_DRKUPOS
   ,[DRKUPOSPR] = @Param_DRKUPOSPR
   ,[EDFAKTOR] = @Param_EDFAKTOR
   ,[FAELLTAGE] = @Param_FAELLTAGE
   ,[DRKARTNR] = @Param_DRKARTNR
   ,[DRKBRANRE] = @Param_DRKBRANRE
   ,[UNSERZEI] = RTRIM(@Param_UNSERZEI)
   ,[SICHBEHALT] = @Param_SICHBEHALT
   ,[DRKVORTEXT] = @Param_DRKVORTEXT
   ,[DRKNACTEXT] = @Param_DRKNACTEXT
   ,[DRKTITSUMM] = @Param_DRKTITSUMM
   ,[JOURNAL] = @Param_JOURNAL
   ,[ZUVERBUCH] = @Param_ZUVERBUCH
   ,[VERBUCHT] = @Param_VERBUCHT
   ,[NETTO] = @Param_NETTO
   ,[MWST] = @Param_MWST
   ,[MWSTSATZ] = @Param_MWSTSATZ
   ,[BRUTTO] = @Param_BRUTTO
   ,[VERBUCHTDM] = @Param_VERBUCHTDM
   ,[SUMMEMAT] = @Param_SUMMEMAT
   ,[SUMMELOHN] = @Param_SUMMELOHN
   ,[SUMMEFREMD] = @Param_SUMMEFREMD
   ,[SUMMEZEIT] = @Param_SUMMEZEIT
   ,[SUMMEEKFR] = @Param_SUMMEEKFR
   ,[SUMMEEKM] = @Param_SUMMEEKM
   ,[GEMEINK] = @Param_GEMEINK
   ,[GEMEINKVH] = @Param_GEMEINKVH
   ,[MAHNENJN] = @Param_MAHNENJN
   ,[MAHNSTUFE] = @Param_MAHNSTUFE
   ,[BEZAHLT] = @Param_BEZAHLT
   ,[ZULETZTBEZ] = @Param_ZULETZTBEZ
   ,[ZAHLDATUM] = RTRIM(@Param_ZAHLDATUM)
   ,[WAEHRUNG] = RTRIM(@Param_WAEHRUNG)
   ,[KURSZUEURO] = @Param_KURSZUEURO
   ,[DRKBANK] = @Param_DRKBANK
   ,[EGRUMAT] = @Param_EGRUMAT
   ,[EGRULOHN] = @Param_EGRULOHN
   ,[EGRUFREMD] = @Param_EGRUFREMD
   ,[EGRUGEMEIN] = @Param_EGRUGEMEIN
   ,[KST] = UPPER(RTRIM(@Param_KST))
   ,[KTR] = UPPER(RTRIM(@Param_KTR))
   ,[DEBKONTO] = RTRIM(@Param_DEBKONTO)
   ,[NERECHNEN] = @Param_NERECHNEN
   ,[NEAUSCHLAG] = @Param_NEAUSCHLAG
   ,[NETRENNEN] = @Param_NETRENNEN
   ,[FREMDPROJ] = @Param_FREMDPROJ
   ,[GAEBJN] = @Param_GAEBJN
   ,[GAEBNAME] = RTRIM(@Param_GAEBNAME)
   ,[GAEBPHASE] = @Param_GAEBPHASE
   ,[NETTO1] = @Param_NETTO1
   ,[NETTO2] = @Param_NETTO2
   ,[UST1] = @Param_UST1
   ,[UST2] = @Param_UST2
   ,[USTVH2] = @Param_USTVH2
   ,[RES4NG2] = RTRIM(@Param_RES4NG2)
   ,[NUMRECS] = @Param_NUMRECS
   ,[EDocType] = UPPER(RTRIM(@Param_EDocType))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Belege')
  DROP PROCEDURE [dbo].[up_Upd2_Belege]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Belege] @Param_RecId varchar(20)
  ,@Param_PRJNR varchar(8)
  ,@Param_BELEGART varchar(2)
  ,@Param_BELEGNR varchar(8)
  ,@Param_VERSION varchar(1)
  ,@Param_LFDNR varchar(6)
  ,@Param_NRKREIS varchar(2)
  ,@Param_BELARTTEXT varchar(30)
  ,@Param_ANZNACHTR varchar(2)
  ,@Param_ANZRAEUME varchar(2)
  ,@Param_ANZBAUPHA varchar(2)
  ,@Param_BAUPHAAKT varchar(2)
  ,@Param_KDNR varchar(15)
  ,@Param_BLNR varchar(15)
  ,@Param_TAG varchar(2)
  ,@Param_TAGDUMMY varchar(1)
  ,@Param_MONAT varchar(2)
  ,@Param_MONDUMMY varchar(1)
  ,@Param_JAHR varchar(4)
  ,@Param_SUBMISSION varchar(10)
  ,@Param_BAUBEGINN varchar(10)
  ,@Param_BAUFERTIG varchar(10)
  ,@Param_FERTIGJN varchar(1)
  ,@Param_EIGANSCH varchar(2)
  ,@Param_PHASE varchar(2)
  ,@Param_SKONTOTAGE varchar(2)
  ,@Param_SKONTOSATZ varchar(4)
  ,@Param_DRKFFTITEL varchar(1)
  ,@Param_DRKINCMWST varchar(1)
  ,@Param_DRKAUFMASS varchar(1)
  ,@Param_DRKLM varchar(1)
  ,@Param_DRKZUBEHOE varchar(1)
  ,@Param_DRKDMRAB varchar(1)
  ,@Param_DRKRABATT varchar(1)
  ,@Param_DRKNULLWER varchar(1)
  ,@Param_AUFLOHN varchar(6)
  ,@Param_AUFMAT varchar(6)
  ,@Param_AUFFREMD varchar(6)
  ,@Param_MITTLOHN varchar(6)
  ,@Param_EXACT varchar(1)
  ,@Param_KGRPS varchar(1)
  ,@Param_KZZUBEHOER varchar(1)
  ,@Param_LOHNANTEIL varchar(1)
  ,@Param_KZVK2 varchar(1)
  ,@Param_TITEL varchar(1)
  ,@Param_MANUELL varchar(1)
  ,@Param_OZMASKE varchar(18)
  ,@Param_ANREDE varchar(50)
  ,@Param_VORNAME varchar(50)
  ,@Param_NAME1 varchar(50)
  ,@Param_NAME2 varchar(50)
  ,@Param_ZUHAENDEN varchar(50)
  ,@Param_STRASSE varchar(50)
  ,@Param_LAND varchar(10)
  ,@Param_PLZ varchar(10)
  ,@Param_ORT varchar(50)
  ,@Param_BRANREDE varchar(50)
  ,@Param_VORHABEN varchar(10)
  ,@Param_BETREFF1 varchar(42)
  ,@Param_BETREFF2 varchar(42)
  ,@Param_BETREFF3 varchar(42)
  ,@Param_EDMENGEN varchar(1)
  ,@Param_EDARTNR varchar(1)
  ,@Param_EDLPOSNR varchar(1)
  ,@Param_DRKNETTO varchar(1)
  ,@Param_UEBERTRAG varchar(1)
  ,@Param_TITELZUS varchar(1)
  ,@Param_DRKTEXT varchar(2)
  ,@Param_DRKUPOS varchar(1)
  ,@Param_DRKUPOSPR varchar(1)
  ,@Param_EDFAKTOR varchar(1)
  ,@Param_FAELLTAGE varchar(2)
  ,@Param_DRKARTNR varchar(1)
  ,@Param_DRKBRANRE varchar(1)
  ,@Param_UNSERZEI varchar(10)
  ,@Param_SICHBEHALT varchar(6)
  ,@Param_DRKVORTEXT varchar(1)
  ,@Param_DRKNACTEXT varchar(1)
  ,@Param_DRKTITSUMM varchar(1)
  ,@Param_JOURNAL varchar(1)
  ,@Param_ZUVERBUCH varchar(1)
  ,@Param_VERBUCHT varchar(1)
  ,@Param_NETTO varchar(14)
  ,@Param_MWST varchar(14)
  ,@Param_MWSTSATZ varchar(6)
  ,@Param_BRUTTO varchar(14)
  ,@Param_VERBUCHTDM varchar(14)
  ,@Param_SUMMEMAT varchar(14)
  ,@Param_SUMMELOHN varchar(14)
  ,@Param_SUMMEFREMD varchar(14)
  ,@Param_SUMMEZEIT varchar(14)
  ,@Param_SUMMEEKFR varchar(14)
  ,@Param_SUMMEEKM varchar(14)
  ,@Param_GEMEINK varchar(14)
  ,@Param_GEMEINKVH varchar(14)
  ,@Param_MAHNENJN varchar(1)
  ,@Param_MAHNSTUFE varchar(2)
  ,@Param_BEZAHLT varchar(14)
  ,@Param_ZULETZTBEZ varchar(14)
  ,@Param_ZAHLDATUM varchar(10)
  ,@Param_WAEHRUNG varchar(6)
  ,@Param_KURSZUEURO varchar(12)
  ,@Param_DRKBANK varchar(1)
  ,@Param_EGRUMAT varchar(2)
  ,@Param_EGRULOHN varchar(2)
  ,@Param_EGRUFREMD varchar(2)
  ,@Param_EGRUGEMEIN varchar(2)
  ,@Param_KST varchar(20)
  ,@Param_KTR varchar(20)
  ,@Param_DEBKONTO varchar(15)
  ,@Param_NERECHNEN varchar(1)
  ,@Param_NEAUSCHLAG varchar(1)
  ,@Param_NETRENNEN varchar(1)
  ,@Param_FREMDPROJ varchar(1)
  ,@Param_GAEBJN varchar(1)
  ,@Param_GAEBNAME varchar(20)
  ,@Param_GAEBPHASE varchar(2)
  ,@Param_NETTO1 varchar(14)
  ,@Param_NETTO2 varchar(14)
  ,@Param_UST1 varchar(14)
  ,@Param_UST2 varchar(14)
  ,@Param_USTVH2 varchar(5)
  ,@Param_RES4NG2 varchar(10)
  ,@Param_NUMRECS varchar(5)
  ,@Param_EDocType varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Belege] SET
    [PRJNR] = UPPER(RTRIM(@Param_PRJNR))
   ,[BELEGART] = UPPER(RTRIM(@Param_BELEGART))
   ,[BELEGNR] = UPPER(RTRIM(@Param_BELEGNR))
   ,[VERSION] = UPPER(RTRIM(@Param_VERSION))
   ,[LFDNR] = UPPER(RTRIM(@Param_LFDNR))
   ,[NRKREIS] = dbo.itcfunc_integer(@Param_NRKREIS)
   ,[BELARTTEXT] = RTRIM(@Param_BELARTTEXT)
   ,[ANZNACHTR] = dbo.itcfunc_integer(@Param_ANZNACHTR)
   ,[ANZRAEUME] = dbo.itcfunc_integer(@Param_ANZRAEUME)
   ,[ANZBAUPHA] = dbo.itcfunc_integer(@Param_ANZBAUPHA)
   ,[BAUPHAAKT] = dbo.itcfunc_integer(@Param_BAUPHAAKT)
   ,[KDNR] = UPPER(RTRIM(@Param_KDNR))
   ,[BLNR] = UPPER(RTRIM(@Param_BLNR))
   ,[TAG] = RTRIM(@Param_TAG)
   ,[TAGDUMMY] = RTRIM(@Param_TAGDUMMY)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[MONDUMMY] = RTRIM(@Param_MONDUMMY)
   ,[JAHR] = RTRIM(@Param_JAHR)
   ,[SUBMISSION] = dbo.itcfunc_datetime(@Param_SUBMISSION)
   ,[BAUBEGINN] = dbo.itcfunc_datetime(@Param_BAUBEGINN)
   ,[BAUFERTIG] = dbo.itcfunc_datetime(@Param_BAUFERTIG)
   ,[FERTIGJN] = dbo.itcfunc_integer(@Param_FERTIGJN)
   ,[EIGANSCH] = UPPER(RTRIM(@Param_EIGANSCH))
   ,[PHASE] = RTRIM(@Param_PHASE)
   ,[SKONTOTAGE] = dbo.itcfunc_integer(@Param_SKONTOTAGE)
   ,[SKONTOSATZ] = dbo.itcfunc_float(@Param_SKONTOSATZ)
   ,[DRKFFTITEL] = dbo.itcfunc_integer(@Param_DRKFFTITEL)
   ,[DRKINCMWST] = dbo.itcfunc_integer(@Param_DRKINCMWST)
   ,[DRKAUFMASS] = dbo.itcfunc_integer(@Param_DRKAUFMASS)
   ,[DRKLM] = dbo.itcfunc_integer(@Param_DRKLM)
   ,[DRKZUBEHOE] = dbo.itcfunc_integer(@Param_DRKZUBEHOE)
   ,[DRKDMRAB] = dbo.itcfunc_integer(@Param_DRKDMRAB)
   ,[DRKRABATT] = dbo.itcfunc_integer(@Param_DRKRABATT)
   ,[DRKNULLWER] = dbo.itcfunc_integer(@Param_DRKNULLWER)
   ,[AUFLOHN] = dbo.itcfunc_float(@Param_AUFLOHN)
   ,[AUFMAT] = dbo.itcfunc_float(@Param_AUFMAT)
   ,[AUFFREMD] = dbo.itcfunc_float(@Param_AUFFREMD)
   ,[MITTLOHN] = dbo.itcfunc_float(@Param_MITTLOHN)
   ,[EXACT] = dbo.itcfunc_integer(@Param_EXACT)
   ,[KGRPS] = dbo.itcfunc_integer(@Param_KGRPS)
   ,[KZZUBEHOER] = dbo.itcfunc_integer(@Param_KZZUBEHOER)
   ,[LOHNANTEIL] = dbo.itcfunc_integer(@Param_LOHNANTEIL)
   ,[KZVK2] = dbo.itcfunc_integer(@Param_KZVK2)
   ,[TITEL] = dbo.itcfunc_integer(@Param_TITEL)
   ,[MANUELL] = dbo.itcfunc_integer(@Param_MANUELL)
   ,[OZMASKE] = RTRIM(@Param_OZMASKE)
   ,[ANREDE] = RTRIM(@Param_ANREDE)
   ,[VORNAME] = RTRIM(@Param_VORNAME)
   ,[NAME1] = RTRIM(@Param_NAME1)
   ,[NAME2] = RTRIM(@Param_NAME2)
   ,[ZUHAENDEN] = RTRIM(@Param_ZUHAENDEN)
   ,[STRASSE] = RTRIM(@Param_STRASSE)
   ,[LAND] = RTRIM(@Param_LAND)
   ,[PLZ] = RTRIM(@Param_PLZ)
   ,[ORT] = RTRIM(@Param_ORT)
   ,[BRANREDE] = RTRIM(@Param_BRANREDE)
   ,[VORHABEN] = UPPER(RTRIM(@Param_VORHABEN))
   ,[BETREFF1] = RTRIM(@Param_BETREFF1)
   ,[BETREFF2] = RTRIM(@Param_BETREFF2)
   ,[BETREFF3] = RTRIM(@Param_BETREFF3)
   ,[EDMENGEN] = dbo.itcfunc_integer(@Param_EDMENGEN)
   ,[EDARTNR] = dbo.itcfunc_integer(@Param_EDARTNR)
   ,[EDLPOSNR] = dbo.itcfunc_integer(@Param_EDLPOSNR)
   ,[DRKNETTO] = dbo.itcfunc_integer(@Param_DRKNETTO)
   ,[UEBERTRAG] = dbo.itcfunc_integer(@Param_UEBERTRAG)
   ,[TITELZUS] = dbo.itcfunc_integer(@Param_TITELZUS)
   ,[DRKTEXT] = dbo.itcfunc_integer(@Param_DRKTEXT)
   ,[DRKUPOS] = dbo.itcfunc_integer(@Param_DRKUPOS)
   ,[DRKUPOSPR] = dbo.itcfunc_integer(@Param_DRKUPOSPR)
   ,[EDFAKTOR] = dbo.itcfunc_integer(@Param_EDFAKTOR)
   ,[FAELLTAGE] = dbo.itcfunc_integer(@Param_FAELLTAGE)
   ,[DRKARTNR] = dbo.itcfunc_integer(@Param_DRKARTNR)
   ,[DRKBRANRE] = dbo.itcfunc_integer(@Param_DRKBRANRE)
   ,[UNSERZEI] = RTRIM(@Param_UNSERZEI)
   ,[SICHBEHALT] = dbo.itcfunc_float(@Param_SICHBEHALT)
   ,[DRKVORTEXT] = dbo.itcfunc_integer(@Param_DRKVORTEXT)
   ,[DRKNACTEXT] = dbo.itcfunc_integer(@Param_DRKNACTEXT)
   ,[DRKTITSUMM] = dbo.itcfunc_integer(@Param_DRKTITSUMM)
   ,[JOURNAL] = dbo.itcfunc_integer(@Param_JOURNAL)
   ,[ZUVERBUCH] = dbo.itcfunc_integer(@Param_ZUVERBUCH)
   ,[VERBUCHT] = dbo.itcfunc_integer(@Param_VERBUCHT)
   ,[NETTO] = dbo.itcfunc_float(@Param_NETTO)
   ,[MWST] = dbo.itcfunc_float(@Param_MWST)
   ,[MWSTSATZ] = dbo.itcfunc_float(@Param_MWSTSATZ)
   ,[BRUTTO] = dbo.itcfunc_float(@Param_BRUTTO)
   ,[VERBUCHTDM] = dbo.itcfunc_float(@Param_VERBUCHTDM)
   ,[SUMMEMAT] = dbo.itcfunc_float(@Param_SUMMEMAT)
   ,[SUMMELOHN] = dbo.itcfunc_float(@Param_SUMMELOHN)
   ,[SUMMEFREMD] = dbo.itcfunc_float(@Param_SUMMEFREMD)
   ,[SUMMEZEIT] = dbo.itcfunc_float(@Param_SUMMEZEIT)
   ,[SUMMEEKFR] = dbo.itcfunc_float(@Param_SUMMEEKFR)
   ,[SUMMEEKM] = dbo.itcfunc_float(@Param_SUMMEEKM)
   ,[GEMEINK] = dbo.itcfunc_float(@Param_GEMEINK)
   ,[GEMEINKVH] = dbo.itcfunc_float(@Param_GEMEINKVH)
   ,[MAHNENJN] = dbo.itcfunc_integer(@Param_MAHNENJN)
   ,[MAHNSTUFE] = dbo.itcfunc_integer(@Param_MAHNSTUFE)
   ,[BEZAHLT] = dbo.itcfunc_float(@Param_BEZAHLT)
   ,[ZULETZTBEZ] = dbo.itcfunc_float(@Param_ZULETZTBEZ)
   ,[ZAHLDATUM] = RTRIM(@Param_ZAHLDATUM)
   ,[WAEHRUNG] = RTRIM(@Param_WAEHRUNG)
   ,[KURSZUEURO] = dbo.itcfunc_float(@Param_KURSZUEURO)
   ,[DRKBANK] = dbo.itcfunc_integer(@Param_DRKBANK)
   ,[EGRUMAT] = dbo.itcfunc_integer(@Param_EGRUMAT)
   ,[EGRULOHN] = dbo.itcfunc_integer(@Param_EGRULOHN)
   ,[EGRUFREMD] = dbo.itcfunc_integer(@Param_EGRUFREMD)
   ,[EGRUGEMEIN] = dbo.itcfunc_integer(@Param_EGRUGEMEIN)
   ,[KST] = UPPER(RTRIM(@Param_KST))
   ,[KTR] = UPPER(RTRIM(@Param_KTR))
   ,[DEBKONTO] = RTRIM(@Param_DEBKONTO)
   ,[NERECHNEN] = dbo.itcfunc_integer(@Param_NERECHNEN)
   ,[NEAUSCHLAG] = dbo.itcfunc_integer(@Param_NEAUSCHLAG)
   ,[NETRENNEN] = dbo.itcfunc_integer(@Param_NETRENNEN)
   ,[FREMDPROJ] = dbo.itcfunc_integer(@Param_FREMDPROJ)
   ,[GAEBJN] = dbo.itcfunc_integer(@Param_GAEBJN)
   ,[GAEBNAME] = RTRIM(@Param_GAEBNAME)
   ,[GAEBPHASE] = dbo.itcfunc_integer(@Param_GAEBPHASE)
   ,[NETTO1] = dbo.itcfunc_float(@Param_NETTO1)
   ,[NETTO2] = dbo.itcfunc_float(@Param_NETTO2)
   ,[UST1] = dbo.itcfunc_float(@Param_UST1)
   ,[UST2] = dbo.itcfunc_float(@Param_UST2)
   ,[USTVH2] = dbo.itcfunc_float(@Param_USTVH2)
   ,[RES4NG2] = RTRIM(@Param_RES4NG2)
   ,[NUMRECS] = dbo.itcfunc_integer(@Param_NUMRECS)
   ,[EDocType] = UPPER(RTRIM(@Param_EDocType))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Belege] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Belege')
  DROP PROCEDURE [dbo].[up_TrSel_Belege]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Belege]
AS
  SELECT RecId,
    LTRIM(RTRIM([PRJNR]))
   +LTRIM(RTRIM([BELEGART]))
   +LTRIM(RTRIM([BELEGNR]))
   +LTRIM(RTRIM([VERSION]))
   +LTRIM(RTRIM([LFDNR]))
   +LTRIM(RTRIM([BELARTTEXT]))
   +LTRIM(RTRIM([KDNR]))
   +LTRIM(RTRIM([BLNR]))
   +LTRIM(RTRIM([TAG]))
   +LTRIM(RTRIM([TAGDUMMY]))
   +LTRIM(RTRIM([MONAT]))
   +LTRIM(RTRIM([MONDUMMY]))
   +LTRIM(RTRIM([JAHR]))
   +LTRIM(RTRIM([EIGANSCH]))
   +LTRIM(RTRIM([PHASE]))
   +LTRIM(RTRIM([OZMASKE]))
   +LTRIM(RTRIM([ANREDE]))
   +LTRIM(RTRIM([VORNAME]))
   +LTRIM(RTRIM([NAME1]))
   +LTRIM(RTRIM([NAME2]))
   +LTRIM(RTRIM([ZUHAENDEN]))
   +LTRIM(RTRIM([STRASSE]))
   +LTRIM(RTRIM([LAND]))
   +LTRIM(RTRIM([PLZ]))
   +LTRIM(RTRIM([ORT]))
   +LTRIM(RTRIM([BRANREDE]))
   +LTRIM(RTRIM([VORHABEN]))
   +LTRIM(RTRIM([BETREFF1]))
   +LTRIM(RTRIM([BETREFF2]))
   +LTRIM(RTRIM([BETREFF3]))
   +LTRIM(RTRIM([UNSERZEI]))
   +LTRIM(RTRIM([ZAHLDATUM]))
   +LTRIM(RTRIM([WAEHRUNG]))
   +LTRIM(RTRIM([KST]))
   +LTRIM(RTRIM([KTR]))
   +LTRIM(RTRIM([DEBKONTO]))
   +LTRIM(RTRIM([GAEBNAME]))
   +LTRIM(RTRIM([RES4NG2]))
   +LTRIM(RTRIM([EDocType]))
  AS Data FROM [dbo].[ut_Belege] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key00')
  DROP PROCEDURE [dbo].[up_First_Belege_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) ORDER BY PRJNR,BELEGART,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key00] AS EXECUTE [dbo].[up_First_Belege_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key00')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key00] AS EXECUTE [dbo].[up_Last_Belege_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key00')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key00] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR > @oldBELEGNR)
    ORDER BY PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART > @oldBELEGART)
    ORDER BY PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,BELEGART,BELEGNR,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_Belege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key00] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR < @oldBELEGNR)
    ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART < @oldBELEGART)
    ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,BELEGART desc,BELEGNR 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_Belege_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

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

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

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR 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_Belege] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key00) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key01')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key01] @oldRecId integer AS
  DECLARE @oldBELEGNR char(8)
  DECLARE @oldBELEGART char(2)
  SELECT @oldBELEGNR = BELEGNR, @oldBELEGART = BELEGART FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR = @oldBELEGNR) AND (BELEGART = @oldBELEGART) AND (RecId > @oldRecId)
    ORDER BY BELEGNR,BELEGART,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR = @oldBELEGNR) AND (BELEGART > @oldBELEGART)
    ORDER BY BELEGNR,BELEGART,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR > @oldBELEGNR)
    ORDER BY BELEGNR,BELEGART,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_Belege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key01] @oldRecId integer AS
  DECLARE @oldBELEGNR char(8)
  DECLARE @oldBELEGART char(2)
  SELECT @oldBELEGNR = BELEGNR, @oldBELEGART = BELEGART FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR = @oldBELEGNR) AND (BELEGART = @oldBELEGART) AND (RecId < @oldRecId)
    ORDER BY BELEGNR desc,BELEGART desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR = @oldBELEGNR) AND (BELEGART < @oldBELEGART)
    ORDER BY BELEGNR desc,BELEGART desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE (BELEGNR < @oldBELEGNR)
    ORDER BY BELEGNR desc,BELEGART 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_Belege_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR AND 
      BELEGART = @mBELEGART
      ORDER BY BELEGNR,BELEGART,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGNR,BELEGART,RecId
    RETURN
  END

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

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR AND 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGNR,BELEGART,RecId
    RETURN
  END

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR AND 
      BELEGART = @mBELEGART
      ORDER BY BELEGNR desc,BELEGART desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGNR desc,BELEGART 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_Belege]  WHERE 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY BELEGNR desc,BELEGART desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR = @mBELEGNR AND 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGNR desc,BELEGART desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key02')
  DROP PROCEDURE [dbo].[up_First_Belege_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) ORDER BY BELEGART,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key02] AS EXECUTE [dbo].[up_First_Belege_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key02')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) ORDER BY BELEGART desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key02] AS EXECUTE [dbo].[up_Last_Belege_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key02')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key02] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART = @oldBELEGART) AND (BELEGNR > @oldBELEGNR)
    ORDER BY BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART > @oldBELEGART)
    ORDER BY BELEGART,BELEGNR,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_Belege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key02] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART = @oldBELEGART) AND (BELEGNR < @oldBELEGNR)
    ORDER BY BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02)
    WHERE (BELEGART < @oldBELEGART)
    ORDER BY BELEGART desc,BELEGNR 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_Belege_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key02] @oldRecId')
GO

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

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY BELEGART,BELEGNR,RecId
    RETURN
  END

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key02) WHERE 
      BELEGART = @mBELEGART AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key03')
  DROP PROCEDURE [dbo].[up_First_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key03] AS EXECUTE [dbo].[up_First_Belege_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key03')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key03] AS EXECUTE [dbo].[up_Last_Belege_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key03')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key03] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldVORHABEN char(10)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldVORHABEN = VORHABEN, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR > @oldJAHR)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN > @oldVORHABEN)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART > @oldBELEGART)
    ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,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_Belege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key03')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key03] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldVORHABEN char(10)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldVORHABEN = VORHABEN, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN = @oldVORHABEN) AND (JAHR < @oldJAHR)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART = @oldBELEGART) AND (VORHABEN < @oldVORHABEN)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03)
    WHERE (BELEGART < @oldBELEGART)
    ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG 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_Belege_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key03')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mVORHABEN char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,10)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,2)

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN >= @mVORHABEN AND SUBSTRING(VORHABEN,1,LEN(@mVORHABEN)) = @mVORHABEN
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART,VORHABEN,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key03')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mVORHABEN char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,10)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,2)

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      SUBSTRING(VORHABEN,1,LEN(@mVORHABEN)) = @mVORHABEN
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key03) WHERE 
      BELEGART = @mBELEGART AND 
      VORHABEN = @mVORHABEN AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART desc,VORHABEN desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  DROP INDEX [Ik_Key04] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key04')
  CREATE INDEX [Ik_Key04] ON [dbo].[ut_Belege] (PRJNR,JAHR,MONAT,TAG,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key04')
  DROP PROCEDURE [dbo].[up_First_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key04] AS EXECUTE [dbo].[up_First_Belege_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key04')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key04] AS EXECUTE [dbo].[up_Last_Belege_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key04')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key04] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR > @oldJAHR)
    ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,JAHR,MONAT,TAG,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_Belege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key04')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key04] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR < @oldJAHR)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG 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_Belege_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key04] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key04')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR 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)

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

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key04')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR 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)

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

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG 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_Belege] WITH(INDEX = Ik_Key04) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key04) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  DROP INDEX [Ik_Key05] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key05')
  CREATE INDEX [Ik_Key05] ON [dbo].[ut_Belege] (KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key05')
  DROP PROCEDURE [dbo].[up_First_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key05] AS EXECUTE [dbo].[up_First_Belege_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key05')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key05] AS EXECUTE [dbo].[up_Last_Belege_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key05')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key05] @oldRecId integer AS
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR > @oldBELEGNR)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART > @oldBELEGART)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR > @oldPRJNR)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR > @oldJAHR)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR > @oldKDNR)
    ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,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_Belege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key05')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key05] @oldRecId integer AS
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (BELEGNR < @oldBELEGNR)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR = @oldPRJNR) AND (BELEGART < @oldBELEGART)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (PRJNR < @oldPRJNR)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR = @oldKDNR) AND (JAHR < @oldJAHR)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05)
    WHERE (KDNR < @oldKDNR)
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR 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_Belege_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key05] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key05')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,8)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,34,8)

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

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR >= @mKDNR AND SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY KDNR,JAHR,MONAT,TAG,PRJNR,BELEGART,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key05')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,8)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,34,8)

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

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key05) WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,PRJNR desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key06')
  DROP INDEX [Ik_Key06] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key06')
  CREATE INDEX [Ik_Key06] ON [dbo].[ut_Belege] (PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key06')
  DROP PROCEDURE [dbo].[up_First_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key06] AS EXECUTE [dbo].[up_First_Belege_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key06')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key06] AS EXECUTE [dbo].[up_Last_Belege_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key06')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key06] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART = @oldBELEGART) AND (BELEGNR > @oldBELEGNR)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART > @oldBELEGART)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR > @oldJAHR)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR > @oldKDNR)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,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_Belege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key06] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key06')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key06] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGART = BELEGART, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART = @oldBELEGART) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART = @oldBELEGART) AND (BELEGNR < @oldBELEGNR)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGART < @oldBELEGART)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR = @oldKDNR) AND (JAHR < @oldJAHR)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR = @oldPRJNR) AND (KDNR < @oldKDNR)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR 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_Belege_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key06] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key06] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key06')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,28,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,30,2)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,34,8)

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

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR >= @mKDNR AND SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR,KDNR,JAHR,MONAT,TAG,BELEGART,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key06')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key06] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,28,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,30,2)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,32,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,34,8)

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

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR 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_Belege] WITH(INDEX = Ik_Key06) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 33 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key06) WHERE 
      PRJNR = @mPRJNR AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGART = @mBELEGART AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGART desc,BELEGNR desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  DROP INDEX [Ik_Key07] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  CREATE INDEX [Ik_Key07] ON [dbo].[ut_Belege] (BELEGART,JAHR,MONAT,TAG,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key07')
  DROP PROCEDURE [dbo].[up_First_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key07] AS EXECUTE [dbo].[up_First_Belege_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key07')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key07] AS EXECUTE [dbo].[up_Last_Belege_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key07')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key07] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR > @oldJAHR)
    ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART > @oldBELEGART)
    ORDER BY BELEGART,JAHR,MONAT,TAG,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_Belege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key07')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key07] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART = @oldBELEGART) AND (JAHR < @oldJAHR)
    ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07)
    WHERE (BELEGART < @oldBELEGART)
    ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG 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_Belege_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key07] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key07')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key07')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key07] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key07) WHERE 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  DROP INDEX [Ik_Key08] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key08')
  CREATE INDEX [Ik_Key08] ON [dbo].[ut_Belege] (BELEGART,KDNR,JAHR,MONAT,TAG,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key08')
  DROP PROCEDURE [dbo].[up_First_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key08] AS EXECUTE [dbo].[up_First_Belege_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key08')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key08] AS EXECUTE [dbo].[up_Last_Belege_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key08')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key08] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR > @oldJAHR)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR > @oldKDNR)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART > @oldBELEGART)
    ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,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_Belege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key08] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key08')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key08] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR = @oldKDNR) AND (JAHR < @oldJAHR)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART = @oldBELEGART) AND (KDNR < @oldKDNR)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08)
    WHERE (BELEGART < @oldBELEGART)
    ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG 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_Belege_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key08] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key08')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,15)
  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)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR >= @mKDNR AND SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART,KDNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key08')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key08]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key08] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,15)
  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)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key08) WHERE 
      BELEGART = @mBELEGART AND 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY BELEGART desc,KDNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key09')
  DROP INDEX [Ik_Key09] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key09')
  CREATE INDEX [Ik_Key09] ON [dbo].[ut_Belege] (PRJNR,BELEGART,JAHR,MONAT,TAG,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key09')
  DROP PROCEDURE [dbo].[up_First_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key09] AS EXECUTE [dbo].[up_First_Belege_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key09')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key09] AS EXECUTE [dbo].[up_Last_Belege_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key09')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key09] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR > @oldJAHR)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART > @oldBELEGART)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,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_Belege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key09] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key09')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key09] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGART char(2)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldPRJNR = PRJNR, @oldBELEGART = BELEGART, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART = @oldBELEGART) AND (JAHR < @oldJAHR)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGART < @oldBELEGART)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG 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_Belege_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key09] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key09')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR,BELEGART,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key09')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key09]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key09] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG 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_Belege] WITH(INDEX = Ik_Key09) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key09) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGART = @mBELEGART AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR desc,BELEGART desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key10')
  DROP PROCEDURE [dbo].[up_First_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key10] AS EXECUTE [dbo].[up_First_Belege_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key10')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key10] AS EXECUTE [dbo].[up_Last_Belege_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key10')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key10] @oldRecId integer AS
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR > @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT > @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR > @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR,MONAT,TAG,BELEGNR,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_Belege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key10')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key10] @oldRecId integer AS
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR < @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR = @oldJAHR) AND (MONAT < @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((JAHR < @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR 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_Belege_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key10] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key10')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key10] @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 @mBELEGNR 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_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key10')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key10] @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 @mBELEGNR 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_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR 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_Belege]  WHERE 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key11')
  DROP PROCEDURE [dbo].[up_First_Belege_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key11] AS EXECUTE [dbo].[up_First_Belege_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key11')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key11] AS EXECUTE [dbo].[up_Last_Belege_Key11]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((BELEGNR > @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY BELEGNR,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_Belege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key11] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((BELEGNR < @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY BELEGNR 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_Belege_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key11] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key11')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGNR 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_Belege]  WHERE 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key11')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGNR 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_Belege]  WHERE 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY BELEGNR 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_Belege]  WHERE 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY BELEGNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key12')
  DROP PROCEDURE [dbo].[up_First_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY PRJNR,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key12] AS EXECUTE [dbo].[up_First_Belege_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key12')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY PRJNR desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key12] AS EXECUTE [dbo].[up_Last_Belege_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key12')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key12] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (BELEGNR > @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR > @oldPRJNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,BELEGNR,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_Belege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key12')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key12] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (BELEGNR < @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR < @oldPRJNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,BELEGNR 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_Belege_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key12] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key12')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGNR 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_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key12')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGNR 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_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,BELEGNR 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_Belege]  WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  DROP INDEX [Ik_Key13] ON [dbo].[ut_Belege]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key13')
  DROP PROCEDURE [dbo].[up_First_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key13] AS EXECUTE [dbo].[up_First_Belege_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key13')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key13] AS EXECUTE [dbo].[up_Last_Belege_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key13')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key13] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR > @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR > @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR > @oldPRJNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,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_Belege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key13')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key13] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR < @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR = @oldPRJNR) AND (JAHR < @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((PRJNR < @oldPRJNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR 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_Belege_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key13] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key13')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR 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 @mBELEGNR 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_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key13')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR 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 @mBELEGNR 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_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR 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_Belege]  WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key14')
  DROP INDEX [Ik_Key14] ON [dbo].[ut_Belege]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key14')
  DROP PROCEDURE [dbo].[up_First_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key14] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key14] AS EXECUTE [dbo].[up_First_Belege_Key14]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key14')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key14] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE (BELEGART >= ' 1') AND (BELEGART <= ' 4') ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key14] AS EXECUTE [dbo].[up_Last_Belege_Key14]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key14')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key14] @oldRecId integer AS
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR > @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR > @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR > @oldKDNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,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_Belege_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key14] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key14] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key14')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key14] @oldRecId integer AS
  DECLARE @oldKDNR char(15)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldKDNR = KDNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR < @oldBELEGNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR = @oldKDNR) AND (JAHR < @oldJAHR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] 
    WHERE ((KDNR < @oldKDNR)) AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
    ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR 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_Belege_Key14')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key14] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key14] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key14')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key14] @xKey char(250), @xLen integer AS
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,8)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR >= @mKDNR AND SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key14')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key14]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key14] @xKey char(250), @xLen integer AS
  DECLARE @mKDNR char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,24,8)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      SUBSTRING(KDNR,1,LEN(@mKDNR)) = @mKDNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege]  WHERE 
      KDNR = @mKDNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      AND ((BELEGART >= ' 1') AND (BELEGART <= ' 4'))
      ORDER BY KDNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key15')
  DROP INDEX [Ik_Key15] ON [dbo].[ut_Belege]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege' and so.[id] = si.[id] and si.[name] = 'Ik_Key15')
  CREATE INDEX [Ik_Key15] ON [dbo].[ut_Belege] (BELEGART,PRJNR,FERTIGJN,JAHR,MONAT,TAG,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Key15')
  DROP PROCEDURE [dbo].[up_First_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Key15] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE (0 = FERTIGJN) ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Key15] AS EXECUTE [dbo].[up_First_Belege_Key15]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Key15')
  DROP PROCEDURE [dbo].[up_Last_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Key15] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE (0 = FERTIGJN) ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Key15] AS EXECUTE [dbo].[up_Last_Belege_Key15]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Key15')
  DROP PROCEDURE [dbo].[up_Next_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Key15] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR > @oldJAHR)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR > @oldPRJNR)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART > @oldBELEGART)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,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_Belege_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Key15] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Key15] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Key15')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Key15] @oldRecId integer AS
  DECLARE @oldBELEGART char(2)
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldBELEGART = BELEGART, @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Belege] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR = @oldPRJNR) AND (JAHR < @oldJAHR)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART = @oldBELEGART) AND (PRJNR < @oldPRJNR)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15)
    WHERE ((BELEGART < @oldBELEGART)) AND ((0 = FERTIGJN))
    ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG 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_Belege_Key15')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Key15] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Key15] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Key15')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Key15] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,8)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART >= @mBELEGART AND SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART,PRJNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Key15')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Key15]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Key15] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGART char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,8)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,2)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      SUBSTRING(BELEGART,1,LEN(@mBELEGART)) = @mBELEGART
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege] WITH(INDEX = Ik_Key15) WHERE 
      BELEGART = @mBELEGART AND 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      AND ((0 = FERTIGJN))
      ORDER BY BELEGART desc,PRJNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


