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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_LagerEinKopf')
BEGIN
CREATE TABLE [dbo].[ut_LagerEinKopf]
(
  [RecId]  integer NOT NULL
 ,[BelNr] integer DEFAULT 0
 ,[PrjNr] char(8) DEFAULT ''
 ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[AdrNr] char(15) DEFAULT ''
 ,[UZ] char(10) DEFAULT ''
 ,[ExtBelnr] char(20) DEFAULT ''
 ,[KredKto] char(8) DEFAULT ''
 ,[Name1] char(40) DEFAULT ''
 ,[Waehrung] char(6) DEFAULT ''
 ,[Eurokurs] float DEFAULT 0
 ,[SummeBrutto] float DEFAULT 0
 ,[FaelligTage] integer DEFAULT 0
 ,[SkontoTage] integer DEFAULT 0
 ,[SkontoSatz] float DEFAULT 0
 ,[TransNo] char(8) DEFAULT ''
 ,[Typ] char(1) DEFAULT ''
 ,[KladCode] char(20) DEFAULT ''
 ,[KladLfdNr] integer DEFAULT 0
 ,[BuText] char(50) DEFAULT ''
 ,[ArtPosten] tinyint DEFAULT 0
 ,[VerbrKonto] char(8) DEFAULT ''
 ,[SummeNetto] float DEFAULT 0
 ,[Genehmigt] tinyint DEFAULT 0
 ,[SummeExt] float DEFAULT 0
 ,[ERDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[LogName] char(20) DEFAULT ''
 ,[Hinweis] varchar(50) DEFAULT ''
  CONSTRAINT [Pk_LagerEinKopf]  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_LagerEinKopf_KeyDel')
  DROP TRIGGER [dbo].[tr_LagerEinKopf_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_LagerEinKopf_KeyUpd')
  DROP TRIGGER [dbo].[tr_LagerEinKopf_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_LagerEinKopf_Keys')
  DROP TABLE [dbo].[ut_LagerEinKopf_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_LagerEinKopf' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [BelNr] 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_LagerEinKopf' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Datum] 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_LagerEinKopf' and sc.[name] = 'AdrNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [AdrNr] 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_LagerEinKopf' and sc.[name] = 'UZ')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [UZ] 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_LagerEinKopf' and sc.[name] = 'ExtBelnr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [ExtBelnr] 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_LagerEinKopf' and sc.[name] = 'KredKto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [KredKto] 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_LagerEinKopf' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Name1] char(40) 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_LagerEinKopf' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' and sc.[name] = 'Eurokurs')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Eurokurs] 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_LagerEinKopf' and sc.[name] = 'SummeBrutto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [SummeBrutto] 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_LagerEinKopf' and sc.[name] = 'FaelligTage')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [FaelligTage] 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_LagerEinKopf' and sc.[name] = 'SkontoTage')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' and sc.[name] = 'SkontoSatz')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [TransNo] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Typ')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Typ] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KladCode')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [KladCode] 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_LagerEinKopf' and sc.[name] = 'KladLfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [KladLfdNr] 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_LagerEinKopf' and sc.[name] = 'BuText')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [BuText] 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_LagerEinKopf' and sc.[name] = 'ArtPosten')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [ArtPosten] 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_LagerEinKopf' and sc.[name] = 'VerbrKonto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [VerbrKonto] 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_LagerEinKopf' and sc.[name] = 'SummeNetto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [SummeNetto] 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_LagerEinKopf' and sc.[name] = 'Genehmigt')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Genehmigt] 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_LagerEinKopf' and sc.[name] = 'SummeExt')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [SummeExt] 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_LagerEinKopf' and sc.[name] = 'ERDatum')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [ERDatum] 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_LagerEinKopf' and sc.[name] = 'LogName')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [LogName] 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_LagerEinKopf' and sc.[name] = 'Hinweis')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinKopf] ADD [Hinweis] varchar(50) 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_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'AdrNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'AdrNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [AdrNr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'AdrNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'AdrNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [AdrNr] varchar(15)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'UZ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'UZ'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [UZ] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'UZ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'UZ'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [UZ] varchar(10)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'ExtBelnr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'ExtBelnr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [ExtBelnr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'ExtBelnr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'ExtBelnr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [ExtBelnr] varchar(20)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KredKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KredKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [KredKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KredKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KredKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [KredKto] varchar(8)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'Name1'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Name1] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'Name1'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Name1] varchar(40)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'Waehrung'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] 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_LagerEinKopf' 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_LagerEinKopf' and sc.[name] = 'Waehrung'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Waehrung] varchar(6)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'TransNo'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'TransNo'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [TransNo] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'TransNo'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'TransNo'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [TransNo] 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_LagerEinKopf' and sc.[name] = 'Typ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Typ'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Typ] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Typ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Typ'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Typ] varchar(1)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KladCode'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KladCode'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [KladCode] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KladCode'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'KladCode'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [KladCode] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'BuText'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'BuText'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [BuText] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'BuText'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'BuText'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [BuText] varchar(50)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'VerbrKonto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'VerbrKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [VerbrKonto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'VerbrKonto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'VerbrKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [VerbrKonto] varchar(8)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'LogName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [LogName] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'LogName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [LogName] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Hinweis'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Hinweis'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Hinweis] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Hinweis'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinKopf' and sc.[name] = 'Hinweis'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinKopf] ALTER COLUMN [Hinweis] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'LagerEinKopf')
  DROP VIEW [dbo].[LagerEinKopf]
GO
CREATE VIEW [dbo].[LagerEinKopf] AS
  SELECT [RecId]
 ,[BelNr]
 ,[PrjNr]
 ,[Datum]
 ,[AdrNr]
 ,[UZ]
 ,[ExtBelnr]
 ,[KredKto]
 ,[Name1]
 ,[Waehrung]
 ,[Eurokurs]
 ,[SummeBrutto]
 ,[FaelligTage]
 ,[SkontoTage]
 ,[SkontoSatz]
 ,[TransNo]
 ,[Typ]
 ,[KladCode]
 ,[KladLfdNr]
 ,[BuText]
 ,[ArtPosten]
 ,[VerbrKonto]
 ,[SummeNetto]
 ,[Genehmigt]
 ,[SummeExt]
 ,[ERDatum]
 ,[LogName]
 ,[Hinweis]
  FROM [dbo].[ut_LagerEinKopf]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_Sel_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_Sel_LagerEinKopf]  @mRI integer
AS
  SELECT
    [BelNr]
   ,[PrjNr]
   ,[Datum]
   ,[AdrNr]
   ,[UZ]
   ,[ExtBelnr]
   ,[KredKto]
   ,[Name1]
   ,[Waehrung]
   ,[Eurokurs]
   ,[SummeBrutto]
   ,[FaelligTage]
   ,[SkontoTage]
   ,[SkontoSatz]
   ,[TransNo]
   ,[Typ]
   ,[KladCode]
   ,[KladLfdNr]
   ,[BuText]
   ,[ArtPosten]
   ,[VerbrKonto]
   ,[SummeNetto]
   ,[Genehmigt]
   ,[SummeExt]
   ,[ERDatum]
   ,[LogName]
   ,[Hinweis]
  FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_Append_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_Append_LagerEinKopf]
   @Param_BelNr integer
  ,@Param_PrjNr char(8)
  ,@Param_Datum datetime
  ,@Param_AdrNr char(15)
  ,@Param_UZ char(10)
  ,@Param_ExtBelnr char(20)
  ,@Param_KredKto char(8)
  ,@Param_Name1 char(40)
  ,@Param_Waehrung char(6)
  ,@Param_Eurokurs float
  ,@Param_SummeBrutto float
  ,@Param_FaelligTage integer
  ,@Param_SkontoTage integer
  ,@Param_SkontoSatz float
  ,@Param_TransNo char(8)
  ,@Param_Typ char(1)
  ,@Param_KladCode char(20)
  ,@Param_KladLfdNr integer
  ,@Param_BuText char(50)
  ,@Param_ArtPosten tinyint
  ,@Param_VerbrKonto char(8)
  ,@Param_SummeNetto float
  ,@Param_Genehmigt tinyint
  ,@Param_SummeExt float
  ,@Param_ERDatum datetime
  ,@Param_LogName char(20)
  ,@Param_Hinweis varchar(50)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_LagerEinKopf()
      INSERT INTO [dbo].[ut_LagerEinKopf] (RecId
        ,[BelNr]
        ,[PrjNr]
        ,[Datum]
        ,[AdrNr]
        ,[UZ]
        ,[ExtBelnr]
        ,[KredKto]
        ,[Name1]
        ,[Waehrung]
        ,[Eurokurs]
        ,[SummeBrutto]
        ,[FaelligTage]
        ,[SkontoTage]
        ,[SkontoSatz]
        ,[TransNo]
        ,[Typ]
        ,[KladCode]
        ,[KladLfdNr]
        ,[BuText]
        ,[ArtPosten]
        ,[VerbrKonto]
        ,[SummeNetto]
        ,[Genehmigt]
        ,[SummeExt]
        ,[ERDatum]
        ,[LogName]
        ,[Hinweis]
          ) VALUES (@NeueId
        ,@Param_BelNr
        ,UPPER(RTRIM(@Param_PrjNr))
        ,@Param_Datum
        ,UPPER(RTRIM(@Param_AdrNr))
        ,RTRIM(@Param_UZ)
        ,UPPER(RTRIM(@Param_ExtBelnr))
        ,UPPER(RTRIM(@Param_KredKto))
        ,RTRIM(@Param_Name1)
        ,UPPER(RTRIM(@Param_Waehrung))
        ,@Param_Eurokurs
        ,@Param_SummeBrutto
        ,@Param_FaelligTage
        ,@Param_SkontoTage
        ,@Param_SkontoSatz
        ,UPPER(RTRIM(@Param_TransNo))
        ,UPPER(RTRIM(@Param_Typ))
        ,UPPER(RTRIM(@Param_KladCode))
        ,@Param_KladLfdNr
        ,RTRIM(@Param_BuText)
        ,@Param_ArtPosten
        ,UPPER(RTRIM(@Param_VerbrKonto))
        ,@Param_SummeNetto
        ,@Param_Genehmigt
        ,@Param_SummeExt
        ,@Param_ERDatum
        ,UPPER(RTRIM(@Param_LogName))
        ,RTRIM(@Param_Hinweis)
          )
      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_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_Add_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_Add_LagerEinKopf]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_LagerEinKopf()
  INSERT INTO [dbo].[ut_LagerEinKopf] (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_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_Upd_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd_LagerEinKopf] @mRI integer
  ,@Param_BelNr integer
  ,@Param_PrjNr char(8)
  ,@Param_Datum datetime
  ,@Param_AdrNr char(15)
  ,@Param_UZ char(10)
  ,@Param_ExtBelnr char(20)
  ,@Param_KredKto char(8)
  ,@Param_Name1 char(40)
  ,@Param_Waehrung char(6)
  ,@Param_Eurokurs float
  ,@Param_SummeBrutto float
  ,@Param_FaelligTage integer
  ,@Param_SkontoTage integer
  ,@Param_SkontoSatz float
  ,@Param_TransNo char(8)
  ,@Param_Typ char(1)
  ,@Param_KladCode char(20)
  ,@Param_KladLfdNr integer
  ,@Param_BuText char(50)
  ,@Param_ArtPosten tinyint
  ,@Param_VerbrKonto char(8)
  ,@Param_SummeNetto float
  ,@Param_Genehmigt tinyint
  ,@Param_SummeExt float
  ,@Param_ERDatum datetime
  ,@Param_LogName char(20)
  ,@Param_Hinweis varchar(50)
AS
  UPDATE [dbo].[ut_LagerEinKopf] SET
    [BelNr] = @Param_BelNr
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[Datum] = @Param_Datum
   ,[AdrNr] = UPPER(RTRIM(@Param_AdrNr))
   ,[UZ] = RTRIM(@Param_UZ)
   ,[ExtBelnr] = UPPER(RTRIM(@Param_ExtBelnr))
   ,[KredKto] = UPPER(RTRIM(@Param_KredKto))
   ,[Name1] = RTRIM(@Param_Name1)
   ,[Waehrung] = UPPER(RTRIM(@Param_Waehrung))
   ,[Eurokurs] = @Param_Eurokurs
   ,[SummeBrutto] = @Param_SummeBrutto
   ,[FaelligTage] = @Param_FaelligTage
   ,[SkontoTage] = @Param_SkontoTage
   ,[SkontoSatz] = @Param_SkontoSatz
   ,[TransNo] = UPPER(RTRIM(@Param_TransNo))
   ,[Typ] = UPPER(RTRIM(@Param_Typ))
   ,[KladCode] = UPPER(RTRIM(@Param_KladCode))
   ,[KladLfdNr] = @Param_KladLfdNr
   ,[BuText] = RTRIM(@Param_BuText)
   ,[ArtPosten] = @Param_ArtPosten
   ,[VerbrKonto] = UPPER(RTRIM(@Param_VerbrKonto))
   ,[SummeNetto] = @Param_SummeNetto
   ,[Genehmigt] = @Param_Genehmigt
   ,[SummeExt] = @Param_SummeExt
   ,[ERDatum] = @Param_ERDatum
   ,[LogName] = UPPER(RTRIM(@Param_LogName))
   ,[Hinweis] = RTRIM(@Param_Hinweis)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_Upd2_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd2_LagerEinKopf] @Param_RecId varchar(20)
  ,@Param_BelNr varchar(8)
  ,@Param_PrjNr varchar(8)
  ,@Param_Datum varchar(10)
  ,@Param_AdrNr varchar(15)
  ,@Param_UZ varchar(10)
  ,@Param_ExtBelnr varchar(20)
  ,@Param_KredKto varchar(8)
  ,@Param_Name1 varchar(40)
  ,@Param_Waehrung varchar(6)
  ,@Param_Eurokurs varchar(12)
  ,@Param_SummeBrutto varchar(12)
  ,@Param_FaelligTage varchar(3)
  ,@Param_SkontoTage varchar(3)
  ,@Param_SkontoSatz varchar(4)
  ,@Param_TransNo varchar(8)
  ,@Param_Typ varchar(1)
  ,@Param_KladCode varchar(20)
  ,@Param_KladLfdNr varchar(8)
  ,@Param_BuText varchar(50)
  ,@Param_ArtPosten varchar(1)
  ,@Param_VerbrKonto varchar(8)
  ,@Param_SummeNetto varchar(12)
  ,@Param_Genehmigt varchar(1)
  ,@Param_SummeExt varchar(12)
  ,@Param_ERDatum varchar(10)
  ,@Param_LogName varchar(20)
  ,@Param_Hinweis varchar(50)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_LagerEinKopf] SET
    [BelNr] = dbo.itcfunc_integer(@Param_BelNr)
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[Datum] = dbo.itcfunc_datetime(@Param_Datum)
   ,[AdrNr] = UPPER(RTRIM(@Param_AdrNr))
   ,[UZ] = RTRIM(@Param_UZ)
   ,[ExtBelnr] = UPPER(RTRIM(@Param_ExtBelnr))
   ,[KredKto] = UPPER(RTRIM(@Param_KredKto))
   ,[Name1] = RTRIM(@Param_Name1)
   ,[Waehrung] = UPPER(RTRIM(@Param_Waehrung))
   ,[Eurokurs] = dbo.itcfunc_float(@Param_Eurokurs)
   ,[SummeBrutto] = dbo.itcfunc_float(@Param_SummeBrutto)
   ,[FaelligTage] = dbo.itcfunc_integer(@Param_FaelligTage)
   ,[SkontoTage] = dbo.itcfunc_integer(@Param_SkontoTage)
   ,[SkontoSatz] = dbo.itcfunc_float(@Param_SkontoSatz)
   ,[TransNo] = UPPER(RTRIM(@Param_TransNo))
   ,[Typ] = UPPER(RTRIM(@Param_Typ))
   ,[KladCode] = UPPER(RTRIM(@Param_KladCode))
   ,[KladLfdNr] = dbo.itcfunc_integer(@Param_KladLfdNr)
   ,[BuText] = RTRIM(@Param_BuText)
   ,[ArtPosten] = dbo.itcfunc_integer(@Param_ArtPosten)
   ,[VerbrKonto] = UPPER(RTRIM(@Param_VerbrKonto))
   ,[SummeNetto] = dbo.itcfunc_float(@Param_SummeNetto)
   ,[Genehmigt] = dbo.itcfunc_integer(@Param_Genehmigt)
   ,[SummeExt] = dbo.itcfunc_float(@Param_SummeExt)
   ,[ERDatum] = dbo.itcfunc_datetime(@Param_ERDatum)
   ,[LogName] = UPPER(RTRIM(@Param_LogName))
   ,[Hinweis] = RTRIM(@Param_Hinweis)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_LagerEinKopf] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_LagerEinKopf')
  DROP PROCEDURE [dbo].[up_TrSel_LagerEinKopf]
GO
CREATE PROCEDURE [dbo].[up_TrSel_LagerEinKopf]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([AdrNr]))
   +LTRIM(RTRIM([UZ]))
   +LTRIM(RTRIM([ExtBelnr]))
   +LTRIM(RTRIM([KredKto]))
   +LTRIM(RTRIM([Name1]))
   +LTRIM(RTRIM([Waehrung]))
   +LTRIM(RTRIM([TransNo]))
   +LTRIM(RTRIM([Typ]))
   +LTRIM(RTRIM([KladCode]))
   +LTRIM(RTRIM([BuText]))
   +LTRIM(RTRIM([VerbrKonto]))
   +LTRIM(RTRIM([LogName]))
   +LTRIM(RTRIM([Hinweis]))
  AS Data FROM [dbo].[ut_LagerEinKopf] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key00')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) ORDER BY Typ,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key00] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key00')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) ORDER BY Typ desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key00] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key00')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key00] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)
    ORDER BY Typ,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (BelNr > @oldBelNr)
    ORDER BY Typ,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ > @oldTyp)
    ORDER BY Typ,BelNr,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_LagerEinKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key00')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key00] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)
    ORDER BY Typ desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ = @oldTyp) AND (BelNr < @oldBelNr)
    ORDER BY Typ desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00)
    WHERE (Typ < @oldTyp)
    ORDER BY Typ desc,BelNr 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_LagerEinKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      BelNr = @mBelNr
      ORDER BY Typ,BelNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp
      ORDER BY Typ,BelNr,RecId
    RETURN
  END

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

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


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

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

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp AND 
      BelNr = @mBelNr
      ORDER BY Typ desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) WHERE 
      Typ = @mTyp
      ORDER BY Typ desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Typ desc,BelNr desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) ORDER BY Typ,Datum,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key01] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key01] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key01] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr > @oldBelNr)
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum > @oldDatum)
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ > @oldTyp)
    ORDER BY Typ,Datum,BelNr,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_LagerEinKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key01] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr < @oldBelNr)
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ = @oldTyp) AND (Datum < @oldDatum)
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01)
    WHERE (Typ < @oldTyp)
    ORDER BY Typ desc,Datum desc,BelNr 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_LagerEinKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinKopf_Key01')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      Typ = @mTyp
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE (0 = Genehmigt) AND (0 = KladLfdNr) ORDER BY Typ,Datum,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key02] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE (0 = Genehmigt) AND (0 = KladLfdNr) ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key02] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key02] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr > @oldBelNr)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum > @oldDatum)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ > @oldTyp)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,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_LagerEinKopf_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key02] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr < @oldBelNr)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum < @oldDatum)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ < @oldTyp)) AND ((0 = Genehmigt) AND (0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr 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_LagerEinKopf_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinKopf_Key02')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = Genehmigt) AND (0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE (0 = KladLfdNr) ORDER BY Typ,Datum,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key03] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE (0 = KladLfdNr) ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key03] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key03] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)) AND ((0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr > @oldBelNr)) AND ((0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum > @oldDatum)) AND ((0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ > @oldTyp)) AND ((0 = KladLfdNr))
    ORDER BY Typ,Datum,BelNr,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_LagerEinKopf_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key03] @oldRecId integer AS
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)) AND ((0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr < @oldBelNr)) AND ((0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ = @oldTyp) AND (Datum < @oldDatum)) AND ((0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03)
    WHERE ((Typ < @oldTyp)) AND ((0 = KladLfdNr))
    ORDER BY Typ desc,Datum desc,BelNr 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_LagerEinKopf_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY Typ,Datum,BelNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinKopf_Key03')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,2,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,12,8)

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      Typ = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE (0 = KladLfdNr) ORDER BY LogName,Typ,Datum,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key04] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE (0 = KladLfdNr) ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key04] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key04] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldLogName = LogName, @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)) AND ((0 = KladLfdNr))
    ORDER BY LogName,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr > @oldBelNr)) AND ((0 = KladLfdNr))
    ORDER BY LogName,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum > @oldDatum)) AND ((0 = KladLfdNr))
    ORDER BY LogName,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ > @oldTyp)) AND ((0 = KladLfdNr))
    ORDER BY LogName,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName > @oldLogName)) AND ((0 = KladLfdNr))
    ORDER BY LogName,Typ,Datum,BelNr,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_LagerEinKopf_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key04] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key04] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldLogName = LogName, @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)) AND ((0 = KladLfdNr))
    ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr < @oldBelNr)) AND ((0 = KladLfdNr))
    ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ = @oldTyp) AND (Datum < @oldDatum)) AND ((0 = KladLfdNr))
    ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName = @oldLogName) AND (Typ < @oldTyp)) AND ((0 = KladLfdNr))
    ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04)
    WHERE ((LogName < @oldLogName)) AND ((0 = KladLfdNr))
    ORDER BY LogName desc,Typ desc,Datum desc,BelNr 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_LagerEinKopf_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key04] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mLogName char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,22,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,32,8)

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

  IF @xLen = 39 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName >= @mLogName AND SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY LogName,Typ,Datum,BelNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinKopf_Key04')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key04] @xKey char(250), @xLen integer AS
  DECLARE @mLogName char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,22,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,32,8)

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

  IF @xLen = 39 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp AND 
      Datum = @mDatum
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      Typ = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key04) WHERE 
      LogName = @mLogName AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      AND ((0 = KladLfdNr))
      ORDER BY LogName desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_First_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinKopf_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) ORDER BY KredKto,Typ,Datum,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinKopf_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinKopf_Key05] AS EXECUTE [dbo].[up_First_LagerEinKopf_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_Last_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinKopf_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinKopf_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinKopf_Key05] AS EXECUTE [dbo].[up_Last_LagerEinKopf_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_Next_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinKopf_Key05] @oldRecId integer AS
  DECLARE @oldKredKto char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldKredKto = KredKto, @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)
    ORDER BY KredKto,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr > @oldBelNr)
    ORDER BY KredKto,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum > @oldDatum)
    ORDER BY KredKto,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ > @oldTyp)
    ORDER BY KredKto,Typ,Datum,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto > @oldKredKto)
    ORDER BY KredKto,Typ,Datum,BelNr,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_LagerEinKopf_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinKopf_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinKopf_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinKopf_Key05] @oldRecId integer AS
  DECLARE @oldKredKto char(8)
  DECLARE @oldTyp char(1)
  DECLARE @oldDatum datetime
  DECLARE @oldBelNr integer
  SELECT @oldKredKto = KredKto, @oldTyp = Typ, @oldDatum = Datum, @oldBelNr = BelNr FROM [dbo].[ut_LagerEinKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)
    ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum = @oldDatum) AND (BelNr < @oldBelNr)
    ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ = @oldTyp) AND (Datum < @oldDatum)
    ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto = @oldKredKto) AND (Typ < @oldTyp)
    ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05)
    WHERE (KredKto < @oldKredKto)
    ORDER BY KredKto desc,Typ desc,Datum desc,BelNr 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_LagerEinKopf_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinKopf_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinKopf_Key05] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinKopf_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mKredKto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp AND 
      Datum = @mDatum
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto >= @mKredKto AND SUBSTRING(KredKto,1,LEN(@mKredKto)) = @mKredKto
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ >= @mTyp AND SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY KredKto,Typ,Datum,BelNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinKopf_Key05')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinKopf_Key05] @xKey char(250), @xLen integer AS
  DECLARE @mKredKto char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,1)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp AND 
      Datum = @mDatum AND 
      BelNr = @mBelNr
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp AND 
      Datum = @mDatum
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      Typ = @mTyp
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr 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_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(KredKto,1,LEN(@mKredKto)) = @mKredKto
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinKopf] WITH(INDEX = Ik_Key05) WHERE 
      KredKto = @mKredKto AND 
      SUBSTRING(Typ,1,LEN(@mTyp)) = @mTyp
      ORDER BY KredKto desc,Typ desc,Datum desc,BelNr desc,RecId desc
    RETURN
  END



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


