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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Lagerbewegungen')
BEGIN
CREATE TABLE [dbo].[ut_Lagerbewegungen]
(
  [RecId]  integer NOT NULL
 ,[ARTNR] char(20) DEFAULT ''
 ,[TAG] char(2) DEFAULT ''
 ,[TAGDUMMY] char(1) DEFAULT ''
 ,[MONAT] char(2) DEFAULT ''
 ,[MONDUMMY] char(1) DEFAULT ''
 ,[JAHR] char(4) DEFAULT ''
 ,[KZ_ZA] char(1) DEFAULT ''
 ,[INFO] varchar(20) DEFAULT ''
 ,[MENGE] float DEFAULT 0
 ,[VERBUCHT] tinyint DEFAULT 0
 ,[EKPREIS] float DEFAULT 0
 ,[GEBINDE] float DEFAULT 0
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[PosRefKey] char(6) DEFAULT ''
 ,[Wert] float DEFAULT 0
 ,[ZuBuchen] tinyint DEFAULT 0
 ,[UrsprMenge] float DEFAULT 0
 ,[Bemerkung] varchar(40) DEFAULT ''
 ,[VersDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Bez1] varchar(42) DEFAULT ''
 ,[Merkmal] varchar(42) DEFAULT ''
 ,[SollBest] float DEFAULT 0
 ,[SollWert] float DEFAULT 0
 ,[LastInvBes] float DEFAULT 0
 ,[Lagerort] char(15) DEFAULT ''
 ,[Einheit] varchar(6) DEFAULT ''
 ,[ManSollBes] tinyint DEFAULT 0
 ,[ManEkPreis] tinyint DEFAULT 0
 ,[VerbFibu] tinyint DEFAULT 0
 ,[Konto] char(8) DEFAULT ''
 ,[GKonto] char(8) DEFAULT ''
 ,[BuchDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Kladde] char(20) DEFAULT ''
 ,[NRHW] char(15) DEFAULT ''
  CONSTRAINT [Pk_Lagerbewegungen]  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_Lagerbewegungen_KeyDel')
  DROP TRIGGER [dbo].[tr_Lagerbewegungen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Lagerbewegungen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Lagerbewegungen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Lagerbewegungen_Keys')
  DROP TABLE [dbo].[ut_Lagerbewegungen_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_Lagerbewegungen' and sc.[name] = 'ARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [ARTNR] 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_Lagerbewegungen' and sc.[name] = 'TAG')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [TAG] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAGDUMMY')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [TAGDUMMY] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONAT')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [MONAT] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONDUMMY')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [MONDUMMY] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'JAHR')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [JAHR] char(4) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'KZ_ZA')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [KZ_ZA] 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_Lagerbewegungen' and sc.[name] = 'INFO')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [INFO] varchar(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_Lagerbewegungen' and sc.[name] = 'MENGE')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [MENGE] 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_Lagerbewegungen' and sc.[name] = 'VERBUCHT')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [VERBUCHT] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'EKPREIS')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [EKPREIS] 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_Lagerbewegungen' and sc.[name] = 'GEBINDE')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [GEBINDE] 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_Lagerbewegungen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] 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_Lagerbewegungen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [BelArt] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [BelNr] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [PosRefKey] 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_Lagerbewegungen' and sc.[name] = 'Wert')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Wert] 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_Lagerbewegungen' and sc.[name] = 'ZuBuchen')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [ZuBuchen] 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_Lagerbewegungen' and sc.[name] = 'UrsprMenge')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [UrsprMenge] 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_Lagerbewegungen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Bemerkung] varchar(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_Lagerbewegungen' and sc.[name] = 'VersDate')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [VersDate] 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_Lagerbewegungen' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Bez1] varchar(42) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Merkmal] varchar(42) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'SollBest')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [SollBest] 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_Lagerbewegungen' and sc.[name] = 'SollWert')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [SollWert] 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_Lagerbewegungen' and sc.[name] = 'LastInvBes')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [LastInvBes] 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_Lagerbewegungen' and sc.[name] = 'Lagerort')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Lagerort] 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_Lagerbewegungen' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Einheit] varchar(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_Lagerbewegungen' and sc.[name] = 'ManSollBes')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [ManSollBes] 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_Lagerbewegungen' and sc.[name] = 'ManEkPreis')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [ManEkPreis] 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_Lagerbewegungen' and sc.[name] = 'VerbFibu')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [VerbFibu] 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_Lagerbewegungen' and sc.[name] = 'Konto')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Konto] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'GKonto')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [GKonto] 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_Lagerbewegungen' and sc.[name] = 'BuchDatum')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [BuchDatum] 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_Lagerbewegungen' and sc.[name] = 'Kladde')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [Kladde] 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_Lagerbewegungen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Lagerbewegungen] ADD [NRHW] char(15) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'ARTNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'ARTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [ARTNR] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'ARTNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'ARTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [ARTNR] varchar(20)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAG'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAG'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [TAG] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAG'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAG'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [TAG] varchar(2)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAGDUMMY'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAGDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [TAGDUMMY] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAGDUMMY'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'TAGDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [TAGDUMMY] varchar(1)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONAT'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONAT'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [MONAT] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONAT'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONAT'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [MONAT] varchar(2)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONDUMMY'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [MONDUMMY] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONDUMMY'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'MONDUMMY'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [MONDUMMY] varchar(1)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'JAHR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'JAHR'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [JAHR] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'JAHR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'JAHR'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [JAHR] varchar(4)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'KZ_ZA'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'KZ_ZA'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [KZ_ZA] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'KZ_ZA'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'KZ_ZA'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [KZ_ZA] 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_Lagerbewegungen' and sc.[name] = 'INFO'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'INFO'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [INFO] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'INFO'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'INFO'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [INFO] 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_Lagerbewegungen' 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_Lagerbewegungen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] 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_Lagerbewegungen' 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_Lagerbewegungen' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [BelArt] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [BelArt] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [BelNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'PosRefKey'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [PosRefKey] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'PosRefKey'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [PosRefKey] varchar(6)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bemerkung'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bemerkung'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Bemerkung] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bemerkung'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bemerkung'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Bemerkung] varchar(40)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bez1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Bez1] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bez1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Bez1] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Merkmal'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Merkmal] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Merkmal'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Merkmal] varchar(42)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Lagerort'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Lagerort'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Lagerort] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Lagerort'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Lagerort'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Lagerort] varchar(15)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Einheit'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Einheit'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Einheit] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Einheit'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Einheit'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Einheit] 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_Lagerbewegungen' and sc.[name] = 'Konto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Konto'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Konto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Konto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Konto'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Konto] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'GKonto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'GKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [GKonto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'GKonto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'GKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [GKonto] 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_Lagerbewegungen' and sc.[name] = 'Kladde'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Kladde'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Kladde] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Kladde'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'Kladde'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [Kladde] varchar(20)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'NRHW'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'NRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [NRHW] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'NRHW'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Lagerbewegungen' and sc.[name] = 'NRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_Lagerbewegungen] ALTER COLUMN [NRHW] varchar(15)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Lagerbewegungen')
  DROP VIEW [dbo].[Lagerbewegungen]
GO
CREATE VIEW [dbo].[Lagerbewegungen] AS
  SELECT [RecId]
 ,[ARTNR]
 ,[TAG]
 ,[TAGDUMMY]
 ,[MONAT]
 ,[MONDUMMY]
 ,[JAHR]
 ,[KZ_ZA]
 ,[INFO]
 ,[MENGE]
 ,[VERBUCHT]
 ,[EKPREIS]
 ,[GEBINDE]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[PosRefKey]
 ,[Wert]
 ,[ZuBuchen]
 ,[UrsprMenge]
 ,[Bemerkung]
 ,[VersDate]
 ,[Bez1]
 ,[Merkmal]
 ,[SollBest]
 ,[SollWert]
 ,[LastInvBes]
 ,[Lagerort]
 ,[Einheit]
 ,[ManSollBes]
 ,[ManEkPreis]
 ,[VerbFibu]
 ,[Konto]
 ,[GKonto]
 ,[BuchDatum]
 ,[Kladde]
 ,[NRHW]
  FROM [dbo].[ut_Lagerbewegungen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_Sel_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Lagerbewegungen]  @mRI integer
AS
  SELECT
    [ARTNR]
   ,[TAG]
   ,[TAGDUMMY]
   ,[MONAT]
   ,[MONDUMMY]
   ,[JAHR]
   ,[KZ_ZA]
   ,[INFO]
   ,[MENGE]
   ,[VERBUCHT]
   ,[EKPREIS]
   ,[GEBINDE]
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[PosRefKey]
   ,[Wert]
   ,[ZuBuchen]
   ,[UrsprMenge]
   ,[Bemerkung]
   ,[VersDate]
   ,[Bez1]
   ,[Merkmal]
   ,[SollBest]
   ,[SollWert]
   ,[LastInvBes]
   ,[Lagerort]
   ,[Einheit]
   ,[ManSollBes]
   ,[ManEkPreis]
   ,[VerbFibu]
   ,[Konto]
   ,[GKonto]
   ,[BuchDatum]
   ,[Kladde]
   ,[NRHW]
  FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_Append_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_Append_Lagerbewegungen]
   @Param_ARTNR char(20)
  ,@Param_TAG char(2)
  ,@Param_TAGDUMMY char(1)
  ,@Param_MONAT char(2)
  ,@Param_MONDUMMY char(1)
  ,@Param_JAHR char(4)
  ,@Param_KZ_ZA char(1)
  ,@Param_INFO varchar(20)
  ,@Param_MENGE float
  ,@Param_VERBUCHT tinyint
  ,@Param_EKPREIS float
  ,@Param_GEBINDE float
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_PosRefKey char(6)
  ,@Param_Wert float
  ,@Param_ZuBuchen tinyint
  ,@Param_UrsprMenge float
  ,@Param_Bemerkung varchar(40)
  ,@Param_VersDate datetime
  ,@Param_Bez1 varchar(42)
  ,@Param_Merkmal varchar(42)
  ,@Param_SollBest float
  ,@Param_SollWert float
  ,@Param_LastInvBes float
  ,@Param_Lagerort char(15)
  ,@Param_Einheit varchar(6)
  ,@Param_ManSollBes tinyint
  ,@Param_ManEkPreis tinyint
  ,@Param_VerbFibu tinyint
  ,@Param_Konto char(8)
  ,@Param_GKonto char(8)
  ,@Param_BuchDatum datetime
  ,@Param_Kladde char(20)
  ,@Param_NRHW char(15)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Lagerbewegungen()
      INSERT INTO [dbo].[ut_Lagerbewegungen] (RecId
        ,[ARTNR]
        ,[TAG]
        ,[TAGDUMMY]
        ,[MONAT]
        ,[MONDUMMY]
        ,[JAHR]
        ,[KZ_ZA]
        ,[INFO]
        ,[MENGE]
        ,[VERBUCHT]
        ,[EKPREIS]
        ,[GEBINDE]
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[PosRefKey]
        ,[Wert]
        ,[ZuBuchen]
        ,[UrsprMenge]
        ,[Bemerkung]
        ,[VersDate]
        ,[Bez1]
        ,[Merkmal]
        ,[SollBest]
        ,[SollWert]
        ,[LastInvBes]
        ,[Lagerort]
        ,[Einheit]
        ,[ManSollBes]
        ,[ManEkPreis]
        ,[VerbFibu]
        ,[Konto]
        ,[GKonto]
        ,[BuchDatum]
        ,[Kladde]
        ,[NRHW]
          ) VALUES (@NeueId
        ,RTRIM(@Param_ARTNR)
        ,RTRIM(@Param_TAG)
        ,RTRIM(@Param_TAGDUMMY)
        ,RTRIM(@Param_MONAT)
        ,RTRIM(@Param_MONDUMMY)
        ,RTRIM(@Param_JAHR)
        ,RTRIM(@Param_KZ_ZA)
        ,RTRIM(@Param_INFO)
        ,@Param_MENGE
        ,@Param_VERBUCHT
        ,@Param_EKPREIS
        ,@Param_GEBINDE
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_PosRefKey))
        ,@Param_Wert
        ,@Param_ZuBuchen
        ,@Param_UrsprMenge
        ,RTRIM(@Param_Bemerkung)
        ,@Param_VersDate
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Merkmal)
        ,@Param_SollBest
        ,@Param_SollWert
        ,@Param_LastInvBes
        ,UPPER(RTRIM(@Param_Lagerort))
        ,RTRIM(@Param_Einheit)
        ,@Param_ManSollBes
        ,@Param_ManEkPreis
        ,@Param_VerbFibu
        ,UPPER(RTRIM(@Param_Konto))
        ,UPPER(RTRIM(@Param_GKonto))
        ,@Param_BuchDatum
        ,UPPER(RTRIM(@Param_Kladde))
        ,UPPER(RTRIM(@Param_NRHW))
          )
      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_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_Add_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_Add_Lagerbewegungen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Lagerbewegungen()
  INSERT INTO [dbo].[ut_Lagerbewegungen] (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_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_Upd_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Lagerbewegungen] @mRI integer
  ,@Param_ARTNR char(20)
  ,@Param_TAG char(2)
  ,@Param_TAGDUMMY char(1)
  ,@Param_MONAT char(2)
  ,@Param_MONDUMMY char(1)
  ,@Param_JAHR char(4)
  ,@Param_KZ_ZA char(1)
  ,@Param_INFO varchar(20)
  ,@Param_MENGE float
  ,@Param_VERBUCHT tinyint
  ,@Param_EKPREIS float
  ,@Param_GEBINDE float
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_PosRefKey char(6)
  ,@Param_Wert float
  ,@Param_ZuBuchen tinyint
  ,@Param_UrsprMenge float
  ,@Param_Bemerkung varchar(40)
  ,@Param_VersDate datetime
  ,@Param_Bez1 varchar(42)
  ,@Param_Merkmal varchar(42)
  ,@Param_SollBest float
  ,@Param_SollWert float
  ,@Param_LastInvBes float
  ,@Param_Lagerort char(15)
  ,@Param_Einheit varchar(6)
  ,@Param_ManSollBes tinyint
  ,@Param_ManEkPreis tinyint
  ,@Param_VerbFibu tinyint
  ,@Param_Konto char(8)
  ,@Param_GKonto char(8)
  ,@Param_BuchDatum datetime
  ,@Param_Kladde char(20)
  ,@Param_NRHW char(15)
AS
  UPDATE [dbo].[ut_Lagerbewegungen] SET
    [ARTNR] = RTRIM(@Param_ARTNR)
   ,[TAG] = RTRIM(@Param_TAG)
   ,[TAGDUMMY] = RTRIM(@Param_TAGDUMMY)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[MONDUMMY] = RTRIM(@Param_MONDUMMY)
   ,[JAHR] = RTRIM(@Param_JAHR)
   ,[KZ_ZA] = RTRIM(@Param_KZ_ZA)
   ,[INFO] = RTRIM(@Param_INFO)
   ,[MENGE] = @Param_MENGE
   ,[VERBUCHT] = @Param_VERBUCHT
   ,[EKPREIS] = @Param_EKPREIS
   ,[GEBINDE] = @Param_GEBINDE
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[Wert] = @Param_Wert
   ,[ZuBuchen] = @Param_ZuBuchen
   ,[UrsprMenge] = @Param_UrsprMenge
   ,[Bemerkung] = RTRIM(@Param_Bemerkung)
   ,[VersDate] = @Param_VersDate
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[SollBest] = @Param_SollBest
   ,[SollWert] = @Param_SollWert
   ,[LastInvBes] = @Param_LastInvBes
   ,[Lagerort] = UPPER(RTRIM(@Param_Lagerort))
   ,[Einheit] = RTRIM(@Param_Einheit)
   ,[ManSollBes] = @Param_ManSollBes
   ,[ManEkPreis] = @Param_ManEkPreis
   ,[VerbFibu] = @Param_VerbFibu
   ,[Konto] = UPPER(RTRIM(@Param_Konto))
   ,[GKonto] = UPPER(RTRIM(@Param_GKonto))
   ,[BuchDatum] = @Param_BuchDatum
   ,[Kladde] = UPPER(RTRIM(@Param_Kladde))
   ,[NRHW] = UPPER(RTRIM(@Param_NRHW))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_Upd2_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Lagerbewegungen] @Param_RecId varchar(20)
  ,@Param_ARTNR varchar(20)
  ,@Param_TAG varchar(2)
  ,@Param_TAGDUMMY varchar(1)
  ,@Param_MONAT varchar(2)
  ,@Param_MONDUMMY varchar(1)
  ,@Param_JAHR varchar(4)
  ,@Param_KZ_ZA varchar(1)
  ,@Param_INFO varchar(20)
  ,@Param_MENGE varchar(12)
  ,@Param_VERBUCHT varchar(1)
  ,@Param_EKPREIS varchar(10)
  ,@Param_GEBINDE varchar(12)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_PosRefKey varchar(6)
  ,@Param_Wert varchar(12)
  ,@Param_ZuBuchen varchar(1)
  ,@Param_UrsprMenge varchar(12)
  ,@Param_Bemerkung varchar(40)
  ,@Param_VersDate varchar(10)
  ,@Param_Bez1 varchar(42)
  ,@Param_Merkmal varchar(42)
  ,@Param_SollBest varchar(12)
  ,@Param_SollWert varchar(10)
  ,@Param_LastInvBes varchar(12)
  ,@Param_Lagerort varchar(15)
  ,@Param_Einheit varchar(6)
  ,@Param_ManSollBes varchar(1)
  ,@Param_ManEkPreis varchar(1)
  ,@Param_VerbFibu varchar(1)
  ,@Param_Konto varchar(8)
  ,@Param_GKonto varchar(8)
  ,@Param_BuchDatum varchar(10)
  ,@Param_Kladde varchar(20)
  ,@Param_NRHW varchar(15)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Lagerbewegungen] SET
    [ARTNR] = RTRIM(@Param_ARTNR)
   ,[TAG] = RTRIM(@Param_TAG)
   ,[TAGDUMMY] = RTRIM(@Param_TAGDUMMY)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[MONDUMMY] = RTRIM(@Param_MONDUMMY)
   ,[JAHR] = RTRIM(@Param_JAHR)
   ,[KZ_ZA] = RTRIM(@Param_KZ_ZA)
   ,[INFO] = RTRIM(@Param_INFO)
   ,[MENGE] = dbo.itcfunc_float(@Param_MENGE)
   ,[VERBUCHT] = dbo.itcfunc_integer(@Param_VERBUCHT)
   ,[EKPREIS] = dbo.itcfunc_float(@Param_EKPREIS)
   ,[GEBINDE] = dbo.itcfunc_float(@Param_GEBINDE)
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[Wert] = dbo.itcfunc_float(@Param_Wert)
   ,[ZuBuchen] = dbo.itcfunc_integer(@Param_ZuBuchen)
   ,[UrsprMenge] = dbo.itcfunc_float(@Param_UrsprMenge)
   ,[Bemerkung] = RTRIM(@Param_Bemerkung)
   ,[VersDate] = dbo.itcfunc_datetime(@Param_VersDate)
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[SollBest] = dbo.itcfunc_float(@Param_SollBest)
   ,[SollWert] = dbo.itcfunc_float(@Param_SollWert)
   ,[LastInvBes] = dbo.itcfunc_float(@Param_LastInvBes)
   ,[Lagerort] = UPPER(RTRIM(@Param_Lagerort))
   ,[Einheit] = RTRIM(@Param_Einheit)
   ,[ManSollBes] = dbo.itcfunc_integer(@Param_ManSollBes)
   ,[ManEkPreis] = dbo.itcfunc_integer(@Param_ManEkPreis)
   ,[VerbFibu] = dbo.itcfunc_integer(@Param_VerbFibu)
   ,[Konto] = UPPER(RTRIM(@Param_Konto))
   ,[GKonto] = UPPER(RTRIM(@Param_GKonto))
   ,[BuchDatum] = dbo.itcfunc_datetime(@Param_BuchDatum)
   ,[Kladde] = UPPER(RTRIM(@Param_Kladde))
   ,[NRHW] = UPPER(RTRIM(@Param_NRHW))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Lagerbewegungen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Lagerbewegungen')
  DROP PROCEDURE [dbo].[up_TrSel_Lagerbewegungen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Lagerbewegungen]
AS
  SELECT RecId,
    LTRIM(RTRIM([ARTNR]))
   +LTRIM(RTRIM([TAG]))
   +LTRIM(RTRIM([TAGDUMMY]))
   +LTRIM(RTRIM([MONAT]))
   +LTRIM(RTRIM([MONDUMMY]))
   +LTRIM(RTRIM([JAHR]))
   +LTRIM(RTRIM([KZ_ZA]))
   +LTRIM(RTRIM([INFO]))
   +LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([PosRefKey]))
   +LTRIM(RTRIM([Bemerkung]))
   +LTRIM(RTRIM([Bez1]))
   +LTRIM(RTRIM([Merkmal]))
   +LTRIM(RTRIM([Lagerort]))
   +LTRIM(RTRIM([Einheit]))
   +LTRIM(RTRIM([Konto]))
   +LTRIM(RTRIM([GKonto]))
   +LTRIM(RTRIM([Kladde]))
   +LTRIM(RTRIM([NRHW]))
  AS Data FROM [dbo].[ut_Lagerbewegungen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0)
    WHERE (ARTNR > @oldARTNR)
    ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0)
    WHERE (ARTNR < @oldARTNR)
    ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Lagerbewegungen_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY0] @xKey char(250), @xLen integer AS
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,27,2)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY ARTNR,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Lagerbewegungen_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY0] @xKey char(250), @xLen integer AS
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,27,2)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG 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_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY0) WHERE 
      ARTNR = @mARTNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY ARTNR desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1)
    WHERE (JAHR > @oldJAHR)
    ORDER BY JAHR,MONAT,TAG,ARTNR,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_Lagerbewegungen_KEY1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY1] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY1] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1)
    WHERE (JAHR < @oldJAHR)
    ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR 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_Lagerbewegungen_KEY1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY1] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY1] @oldRecId')
GO

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

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

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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR = @mARTNR
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

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

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

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

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

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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR = @mARTNR
      ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

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

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR
      ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

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

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

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY1) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY2')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY2]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY2')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY2] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY2]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY2')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY2]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY2] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY2]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY2')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY2]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY2] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldARTNR char(20)
  DECLARE @oldKZ_ZA char(1)
  SELECT @oldPrjNr = PrjNr, @oldARTNR = ARTNR, @oldKZ_ZA = KZ_ZA FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (RecId > @oldRecId)
    ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR = @oldARTNR) AND (KZ_ZA > @oldKZ_ZA)
    ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR > @oldARTNR)
    ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,ARTNR,KZ_ZA,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_Lagerbewegungen_KEY2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY2] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY2] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY2')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY2]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY2] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldARTNR char(20)
  DECLARE @oldKZ_ZA char(1)
  SELECT @oldPrjNr = PrjNr, @oldARTNR = ARTNR, @oldKZ_ZA = KZ_ZA FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR = @oldARTNR) AND (KZ_ZA < @oldKZ_ZA)
    ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr = @oldPrjNr) AND (ARTNR < @oldARTNR)
    ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,ARTNR desc,KZ_ZA 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_Lagerbewegungen_KEY2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY2] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY2] @oldRecId')
GO

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

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

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

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR AND 
      KZ_ZA >= @mKZ_ZA AND SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY PrjNr,ARTNR,KZ_ZA,RecId
    RETURN
  END

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

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

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA 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_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY2) WHERE 
      PrjNr = @mPrjNr AND 
      ARTNR = @mARTNR AND 
      SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY PrjNr desc,ARTNR desc,KZ_ZA desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY3')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY3] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY3]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY3] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY3]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY3] @oldRecId integer AS
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldARTNR char(20)
  SELECT @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (ARTNR = @oldARTNR) AND (RecId > @oldRecId)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (ARTNR > @oldARTNR)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR > @oldJAHR)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA > @oldKZ_ZA)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,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_Lagerbewegungen_KEY3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY3] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY3] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY3] @oldRecId integer AS
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldARTNR char(20)
  SELECT @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (ARTNR = @oldARTNR) AND (RecId < @oldRecId)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (ARTNR < @oldARTNR)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR < @oldJAHR)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3)
    WHERE (KZ_ZA < @oldKZ_ZA)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR 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_Lagerbewegungen_KEY3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY3] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY3] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY3] @xKey char(250), @xLen integer AS
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,8,2)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,20)

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR = @mARTNR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA >= @mKZ_ZA AND SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen < 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,ARTNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Lagerbewegungen_KEY3')
  DROP PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY3]
GO
CREATE PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY3] @xKey char(250), @xLen integer AS
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,8,2)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,20)

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      ARTNR = @mARTNR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR 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_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY3) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,ARTNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY4')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY4] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY4]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY4] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY4]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY4] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldARTNR char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR = @oldARTNR) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR > @oldARTNR)
    ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,ARTNR,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_Lagerbewegungen_KEY4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY4] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY4] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY4] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldARTNR char(20)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR = @oldARTNR) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR < @oldARTNR)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR 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_Lagerbewegungen_KEY4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY4] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY4] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY4] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR = @mARTNR
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY PrjNr,BelArt,BelNr,ARTNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Lagerbewegungen_KEY4')
  DROP PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY4]
GO
CREATE PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY4] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,20)

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

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR = @mARTNR
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR 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_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY4) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY5')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY5] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY5]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY5] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY5')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY5] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY5]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY5] @oldRecId integer AS
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldLagerort char(15)
  DECLARE @oldARTNR char(20)
  SELECT @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldLagerort = Lagerort, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort = @oldLagerort) AND (ARTNR = @oldARTNR) AND (RecId > @oldRecId)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort = @oldLagerort) AND (ARTNR > @oldARTNR)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort > @oldLagerort)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR > @oldJAHR)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA > @oldKZ_ZA)
    ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,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_Lagerbewegungen_KEY5')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY5] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY5] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY5] @oldRecId integer AS
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldLagerort char(15)
  DECLARE @oldARTNR char(20)
  SELECT @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldLagerort = Lagerort, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort = @oldLagerort) AND (ARTNR = @oldARTNR) AND (RecId < @oldRecId)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort = @oldLagerort) AND (ARTNR < @oldARTNR)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (Lagerort < @oldLagerort)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA = @oldKZ_ZA) AND (JAHR < @oldJAHR)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5)
    WHERE (KZ_ZA < @oldKZ_ZA)
    ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR 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_Lagerbewegungen_KEY5')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY5] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY5] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY5] @xKey char(250), @xLen integer AS
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,8,2)
  DECLARE @mLagerort char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,15)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort AND 
      ARTNR = @mARTNR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen = 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA >= @mKZ_ZA AND SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen < 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort >= @mLagerort AND SUBSTRING(Lagerort,1,LEN(@mLagerort)) = @mLagerort
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY KZ_ZA,JAHR,MONAT,TAG,Lagerort,ARTNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Lagerbewegungen_KEY5')
  DROP PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY5]
GO
CREATE PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY5] @xKey char(250), @xLen integer AS
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,8,2)
  DECLARE @mLagerort char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,15)
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,20)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort AND 
      ARTNR = @mARTNR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR 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_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 7 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(Lagerort,1,LEN(@mLagerort)) = @mLagerort
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY5) WHERE 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      Lagerort = @mLagerort AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,Lagerort desc,ARTNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY6')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY6] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY6]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY6] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY6')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY6] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY6]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY6] @oldRecId integer AS
  DECLARE @oldARTNR char(20)
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldARTNR = ARTNR, @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId > @oldRecId)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR > @oldJAHR)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA > @oldKZ_ZA)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR > @oldARTNR)
    ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY6] @oldRecId integer AS
  DECLARE @oldARTNR char(20)
  DECLARE @oldKZ_ZA char(1)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  SELECT @oldARTNR = ARTNR, @oldKZ_ZA = KZ_ZA, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (RecId < @oldRecId)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA = @oldKZ_ZA) AND (JAHR < @oldJAHR)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR = @oldARTNR) AND (KZ_ZA < @oldKZ_ZA)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6)
    WHERE (ARTNR < @oldARTNR)
    ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_FindF_Lagerbewegungen_KEY6] @xKey char(250), @xLen integer AS
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,28,2)

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA >= @mKZ_ZA AND SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY ARTNR,KZ_ZA,JAHR,MONAT,TAG,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Lagerbewegungen_KEY6')
  DROP PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY6]
GO
CREATE PROCEDURE [dbo].[up_FindL_Lagerbewegungen_KEY6] @xKey char(250), @xLen integer AS
  DECLARE @mARTNR char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mKZ_ZA char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,1)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,22,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,28,2)

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

  IF @xLen = 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG 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_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 21 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      SUBSTRING(KZ_ZA,1,LEN(@mKZ_ZA)) = @mKZ_ZA
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

  IF @xLen < 29 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY6) WHERE 
      ARTNR = @mARTNR AND 
      KZ_ZA = @mKZ_ZA AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY ARTNR desc,KZ_ZA desc,JAHR desc,MONAT desc,TAG desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY7')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY7]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY7] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) ORDER BY BelArt,BelNr,ARTNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY7')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY7] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY7]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY7')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY7]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY7] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY7')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY7] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY7]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY7')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY7]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY7] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldARTNR char(20)
  SELECT @oldBelArt = BelArt, @oldBelNr = BelNr, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR = @oldARTNR) AND (RecId > @oldRecId)
    ORDER BY BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR > @oldARTNR)
    ORDER BY BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY BelArt,BelNr,ARTNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt > @oldBelArt)
    ORDER BY BelArt,BelNr,ARTNR,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_Lagerbewegungen_KEY7')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY7] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY7] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY7')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY7]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY7] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldARTNR char(20)
  SELECT @oldBelArt = BelArt, @oldBelNr = BelNr, @oldARTNR = ARTNR FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR = @oldARTNR) AND (RecId < @oldRecId)
    ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ARTNR < @oldARTNR)
    ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7)
    WHERE (BelArt < @oldBelArt)
    ORDER BY BelArt desc,BelNr desc,ARTNR 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_Lagerbewegungen_KEY7')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY7] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY7] @oldRecId')
GO

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

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

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

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR = @mARTNR
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY BelArt,BelNr,ARTNR,RecId
    RETURN
  END

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

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

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ARTNR = @mARTNR
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY7) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY BelArt desc,BelNr desc,ARTNR desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Lagerbewegungen_KEY8')
  DROP PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY8]
GO
CREATE PROCEDURE [dbo].[up_First_Lagerbewegungen_KEY8] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 0 = VERBUCHT ORDER BY Kladde,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Lagerbewegungen_KEY8')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Lagerbewegungen_KEY8] AS EXECUTE [dbo].[up_First_Lagerbewegungen_KEY8]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Lagerbewegungen_KEY8')
  DROP PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY8]
GO
CREATE PROCEDURE [dbo].[up_Last_Lagerbewegungen_KEY8] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 0 = VERBUCHT ORDER BY Kladde desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Lagerbewegungen_KEY8')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Lagerbewegungen_KEY8] AS EXECUTE [dbo].[up_Last_Lagerbewegungen_KEY8]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Lagerbewegungen_KEY8')
  DROP PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY8]
GO
CREATE PROCEDURE [dbo].[up_Next_Lagerbewegungen_KEY8] @oldRecId integer AS
  DECLARE @oldKladde char(20)
  SELECT @oldKladde = Kladde FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8)
    WHERE ((Kladde = @oldKladde) AND (RecId > @oldRecId)) AND (0 = VERBUCHT)
    ORDER BY Kladde,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8)
    WHERE ((Kladde > @oldKladde)) AND (0 = VERBUCHT)
    ORDER BY Kladde,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_Lagerbewegungen_KEY8')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Lagerbewegungen_KEY8] @oldRecId integer AS EXECUTE [dbo].[up_Next_Lagerbewegungen_KEY8] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Lagerbewegungen_KEY8')
  DROP PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY8]
GO
CREATE PROCEDURE [dbo].[up_Prior_Lagerbewegungen_KEY8] @oldRecId integer AS
  DECLARE @oldKladde char(20)
  SELECT @oldKladde = Kladde FROM [dbo].[ut_Lagerbewegungen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8)
    WHERE ((Kladde = @oldKladde) AND (RecId < @oldRecId)) AND (0 = VERBUCHT)
    ORDER BY Kladde desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8)
    WHERE ((Kladde < @oldKladde)) AND (0 = VERBUCHT)
    ORDER BY Kladde 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_Lagerbewegungen_KEY8')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Lagerbewegungen_KEY8] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Lagerbewegungen_KEY8] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 
      Kladde = @mKladde
      AND (0 = VERBUCHT)
      ORDER BY Kladde,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 
      Kladde >= @mKladde AND SUBSTRING(Kladde,1,LEN(@mKladde)) = @mKladde
      AND (0 = VERBUCHT)
      ORDER BY Kladde,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 
      Kladde = @mKladde
      AND (0 = VERBUCHT)
      ORDER BY Kladde 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_Lagerbewegungen] WITH(INDEX = Ik_KEY8) WHERE 
      SUBSTRING(Kladde,1,LEN(@mKladde)) = @mKladde
      AND (0 = VERBUCHT)
      ORDER BY Kladde desc,RecId desc
    RETURN
  END

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


