-- 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_LagerEinPos')
BEGIN
CREATE TABLE [dbo].[ut_LagerEinPos]
(
  [RecId]  integer NOT NULL
 ,[BelNr] integer DEFAULT 0
 ,[PosNr] integer DEFAULT 0
 ,[UPosNr] integer DEFAULT 0
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelegNr] char(8) DEFAULT ''
 ,[Lkz] char(2) DEFAULT ''
 ,[Artnr] char(18) DEFAULT ''
 ,[Bez1] char(42) DEFAULT ''
 ,[Bez2] char(42) DEFAULT ''
 ,[Merkmal] char(42) DEFAULT ''
 ,[LgKto] char(8) DEFAULT ''
 ,[ZuAb] char(1) DEFAULT ''
 ,[Menge] float DEFAULT 0
 ,[PreisJe] float DEFAULT 0
 ,[BruttoEk] float DEFAULT 0
 ,[Rabatt1] float DEFAULT 0
 ,[Rabatt2] float DEFAULT 0
 ,[NettoEk] float DEFAULT 0
 ,[NeJe100] float DEFAULT 0
 ,[NettoGes] float DEFAULT 0
 ,[UstSchl] char(3) DEFAULT ''
 ,[VerbMenge] float DEFAULT 0
 ,[VerbNetto] float DEFAULT 0
 ,[Kst] char(8) DEFAULT ''
 ,[NRHW] char(15) DEFAULT ''
 ,[POSNRHW] char(10) DEFAULT ''
 ,[Einheit] char(6) DEFAULT ''
 ,[VerbrKto] char(8) DEFAULT ''
 ,[BelTyp] char(1) DEFAULT ''
 ,[VkPreis] float DEFAULT 0
 ,[VkGesamt] float DEFAULT 0
 ,[NonStock] tinyint DEFAULT 0
  CONSTRAINT [Pk_LagerEinPos]  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_LagerEinPos_KeyDel')
  DROP TRIGGER [dbo].[tr_LagerEinPos_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_LagerEinPos_KeyUpd')
  DROP TRIGGER [dbo].[tr_LagerEinPos_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_LagerEinPos_Keys')
  DROP TABLE [dbo].[ut_LagerEinPos_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_LagerEinPos' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [BelNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [PosNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'UPosNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [UPosNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [BelegNr] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Lkz] 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_LagerEinPos' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Artnr] char(18) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Bez1] char(42) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Bez2] char(42) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Merkmal] char(42) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'LgKto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [LgKto] 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_LagerEinPos' and sc.[name] = 'ZuAb')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [ZuAb] 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_LagerEinPos' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'PreisJe')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [PreisJe] 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_LagerEinPos' and sc.[name] = 'BruttoEk')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [BruttoEk] 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_LagerEinPos' and sc.[name] = 'Rabatt1')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Rabatt1] 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_LagerEinPos' and sc.[name] = 'Rabatt2')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Rabatt2] 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_LagerEinPos' and sc.[name] = 'NettoEk')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [NettoEk] 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_LagerEinPos' and sc.[name] = 'NeJe100')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [NeJe100] 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_LagerEinPos' and sc.[name] = 'NettoGes')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [NettoGes] 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_LagerEinPos' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [UstSchl] char(3) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'VerbMenge')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [VerbMenge] 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_LagerEinPos' and sc.[name] = 'VerbNetto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [VerbNetto] 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_LagerEinPos' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Kst] 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_LagerEinPos' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [NRHW] 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_LagerEinPos' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [POSNRHW] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [Einheit] 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_LagerEinPos' and sc.[name] = 'VerbrKto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [VerbrKto] 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_LagerEinPos' and sc.[name] = 'BelTyp')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [BelTyp] 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_LagerEinPos' and sc.[name] = 'VkPreis')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [VkPreis] 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_LagerEinPos' and sc.[name] = 'VkGesamt')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [VkGesamt] 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_LagerEinPos' and sc.[name] = 'NonStock')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinPos] ADD [NonStock] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'BelegNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelegNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [BelegNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelegNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelegNr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [BelegNr] 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_LagerEinPos' and sc.[name] = 'Lkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Lkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Lkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Lkz] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Artnr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Artnr] varchar(18)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'Bez2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Bez2] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Bez2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Bez2] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Merkmal] varchar(42)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'LgKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'LgKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [LgKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'LgKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'LgKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [LgKto] varchar(8)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'ZuAb'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'ZuAb'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [ZuAb] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'ZuAb'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'ZuAb'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [ZuAb] varchar(1)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'UstSchl'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'UstSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [UstSchl] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'UstSchl'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'UstSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [UstSchl] varchar(3)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Kst'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Kst] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Kst'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [Kst] varchar(8)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'NRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'NRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [NRHW] varchar(15)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'POSNRHW'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'POSNRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [POSNRHW] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'POSNRHW'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'POSNRHW'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [POSNRHW] varchar(10)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Einheit'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' 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_LagerEinPos' and sc.[name] = 'Einheit'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] 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_LagerEinPos' and sc.[name] = 'VerbrKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'VerbrKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [VerbrKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'VerbrKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'VerbrKto'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [VerbrKto] varchar(8)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelTyp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [BelTyp] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelTyp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinPos' and sc.[name] = 'BelTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinPos] ALTER COLUMN [BelTyp] varchar(1)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'LagerEinPos')
  DROP VIEW [dbo].[LagerEinPos]
GO
CREATE VIEW [dbo].[LagerEinPos] AS
  SELECT [RecId]
 ,[BelNr]
 ,[PosNr]
 ,[UPosNr]
 ,[PrjNr]
 ,[BelArt]
 ,[BelegNr]
 ,[Lkz]
 ,[Artnr]
 ,[Bez1]
 ,[Bez2]
 ,[Merkmal]
 ,[LgKto]
 ,[ZuAb]
 ,[Menge]
 ,[PreisJe]
 ,[BruttoEk]
 ,[Rabatt1]
 ,[Rabatt2]
 ,[NettoEk]
 ,[NeJe100]
 ,[NettoGes]
 ,[UstSchl]
 ,[VerbMenge]
 ,[VerbNetto]
 ,[Kst]
 ,[NRHW]
 ,[POSNRHW]
 ,[Einheit]
 ,[VerbrKto]
 ,[BelTyp]
 ,[VkPreis]
 ,[VkGesamt]
 ,[NonStock]
  FROM [dbo].[ut_LagerEinPos]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_LagerEinPos')
  DROP PROCEDURE [dbo].[up_Sel_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_Sel_LagerEinPos]  @mRI integer
AS
  SELECT
    [BelNr]
   ,[PosNr]
   ,[UPosNr]
   ,[PrjNr]
   ,[BelArt]
   ,[BelegNr]
   ,[Lkz]
   ,[Artnr]
   ,[Bez1]
   ,[Bez2]
   ,[Merkmal]
   ,[LgKto]
   ,[ZuAb]
   ,[Menge]
   ,[PreisJe]
   ,[BruttoEk]
   ,[Rabatt1]
   ,[Rabatt2]
   ,[NettoEk]
   ,[NeJe100]
   ,[NettoGes]
   ,[UstSchl]
   ,[VerbMenge]
   ,[VerbNetto]
   ,[Kst]
   ,[NRHW]
   ,[POSNRHW]
   ,[Einheit]
   ,[VerbrKto]
   ,[BelTyp]
   ,[VkPreis]
   ,[VkGesamt]
   ,[NonStock]
  FROM [dbo].[ut_LagerEinPos] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_LagerEinPos')
  DROP PROCEDURE [dbo].[up_Append_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_Append_LagerEinPos]
   @Param_BelNr integer
  ,@Param_PosNr integer
  ,@Param_UPosNr integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelegNr char(8)
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_Bez1 char(42)
  ,@Param_Bez2 char(42)
  ,@Param_Merkmal char(42)
  ,@Param_LgKto char(8)
  ,@Param_ZuAb char(1)
  ,@Param_Menge float
  ,@Param_PreisJe float
  ,@Param_BruttoEk float
  ,@Param_Rabatt1 float
  ,@Param_Rabatt2 float
  ,@Param_NettoEk float
  ,@Param_NeJe100 float
  ,@Param_NettoGes float
  ,@Param_UstSchl char(3)
  ,@Param_VerbMenge float
  ,@Param_VerbNetto float
  ,@Param_Kst char(8)
  ,@Param_NRHW char(15)
  ,@Param_POSNRHW char(10)
  ,@Param_Einheit char(6)
  ,@Param_VerbrKto char(8)
  ,@Param_BelTyp char(1)
  ,@Param_VkPreis float
  ,@Param_VkGesamt float
  ,@Param_NonStock tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_LagerEinPos()
      INSERT INTO [dbo].[ut_LagerEinPos] (RecId
        ,[BelNr]
        ,[PosNr]
        ,[UPosNr]
        ,[PrjNr]
        ,[BelArt]
        ,[BelegNr]
        ,[Lkz]
        ,[Artnr]
        ,[Bez1]
        ,[Bez2]
        ,[Merkmal]
        ,[LgKto]
        ,[ZuAb]
        ,[Menge]
        ,[PreisJe]
        ,[BruttoEk]
        ,[Rabatt1]
        ,[Rabatt2]
        ,[NettoEk]
        ,[NeJe100]
        ,[NettoGes]
        ,[UstSchl]
        ,[VerbMenge]
        ,[VerbNetto]
        ,[Kst]
        ,[NRHW]
        ,[POSNRHW]
        ,[Einheit]
        ,[VerbrKto]
        ,[BelTyp]
        ,[VkPreis]
        ,[VkGesamt]
        ,[NonStock]
          ) VALUES (@NeueId
        ,@Param_BelNr
        ,@Param_PosNr
        ,@Param_UPosNr
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelegNr))
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_Artnr))
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Bez2)
        ,RTRIM(@Param_Merkmal)
        ,UPPER(RTRIM(@Param_LgKto))
        ,UPPER(RTRIM(@Param_ZuAb))
        ,@Param_Menge
        ,@Param_PreisJe
        ,@Param_BruttoEk
        ,@Param_Rabatt1
        ,@Param_Rabatt2
        ,@Param_NettoEk
        ,@Param_NeJe100
        ,@Param_NettoGes
        ,UPPER(RTRIM(@Param_UstSchl))
        ,@Param_VerbMenge
        ,@Param_VerbNetto
        ,UPPER(RTRIM(@Param_Kst))
        ,UPPER(RTRIM(@Param_NRHW))
        ,UPPER(RTRIM(@Param_POSNRHW))
        ,RTRIM(@Param_Einheit)
        ,UPPER(RTRIM(@Param_VerbrKto))
        ,UPPER(RTRIM(@Param_BelTyp))
        ,@Param_VkPreis
        ,@Param_VkGesamt
        ,@Param_NonStock
          )
      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_LagerEinPos')
  DROP PROCEDURE [dbo].[up_Add_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_Add_LagerEinPos]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_LagerEinPos()
  INSERT INTO [dbo].[ut_LagerEinPos] (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_LagerEinPos')
  DROP PROCEDURE [dbo].[up_Upd_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_Upd_LagerEinPos] @mRI integer
  ,@Param_BelNr integer
  ,@Param_PosNr integer
  ,@Param_UPosNr integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelegNr char(8)
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_Bez1 char(42)
  ,@Param_Bez2 char(42)
  ,@Param_Merkmal char(42)
  ,@Param_LgKto char(8)
  ,@Param_ZuAb char(1)
  ,@Param_Menge float
  ,@Param_PreisJe float
  ,@Param_BruttoEk float
  ,@Param_Rabatt1 float
  ,@Param_Rabatt2 float
  ,@Param_NettoEk float
  ,@Param_NeJe100 float
  ,@Param_NettoGes float
  ,@Param_UstSchl char(3)
  ,@Param_VerbMenge float
  ,@Param_VerbNetto float
  ,@Param_Kst char(8)
  ,@Param_NRHW char(15)
  ,@Param_POSNRHW char(10)
  ,@Param_Einheit char(6)
  ,@Param_VerbrKto char(8)
  ,@Param_BelTyp char(1)
  ,@Param_VkPreis float
  ,@Param_VkGesamt float
  ,@Param_NonStock tinyint
AS
  UPDATE [dbo].[ut_LagerEinPos] SET
    [BelNr] = @Param_BelNr
   ,[PosNr] = @Param_PosNr
   ,[UPosNr] = @Param_UPosNr
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelegNr] = UPPER(RTRIM(@Param_BelegNr))
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[LgKto] = UPPER(RTRIM(@Param_LgKto))
   ,[ZuAb] = UPPER(RTRIM(@Param_ZuAb))
   ,[Menge] = @Param_Menge
   ,[PreisJe] = @Param_PreisJe
   ,[BruttoEk] = @Param_BruttoEk
   ,[Rabatt1] = @Param_Rabatt1
   ,[Rabatt2] = @Param_Rabatt2
   ,[NettoEk] = @Param_NettoEk
   ,[NeJe100] = @Param_NeJe100
   ,[NettoGes] = @Param_NettoGes
   ,[UstSchl] = UPPER(RTRIM(@Param_UstSchl))
   ,[VerbMenge] = @Param_VerbMenge
   ,[VerbNetto] = @Param_VerbNetto
   ,[Kst] = UPPER(RTRIM(@Param_Kst))
   ,[NRHW] = UPPER(RTRIM(@Param_NRHW))
   ,[POSNRHW] = UPPER(RTRIM(@Param_POSNRHW))
   ,[Einheit] = RTRIM(@Param_Einheit)
   ,[VerbrKto] = UPPER(RTRIM(@Param_VerbrKto))
   ,[BelTyp] = UPPER(RTRIM(@Param_BelTyp))
   ,[VkPreis] = @Param_VkPreis
   ,[VkGesamt] = @Param_VkGesamt
   ,[NonStock] = @Param_NonStock
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_LagerEinPos')
  DROP PROCEDURE [dbo].[up_Upd2_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_Upd2_LagerEinPos] @Param_RecId varchar(20)
  ,@Param_BelNr varchar(8)
  ,@Param_PosNr varchar(4)
  ,@Param_UPosNr varchar(2)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelegNr varchar(8)
  ,@Param_Lkz varchar(2)
  ,@Param_Artnr varchar(18)
  ,@Param_Bez1 varchar(42)
  ,@Param_Bez2 varchar(42)
  ,@Param_Merkmal varchar(42)
  ,@Param_LgKto varchar(8)
  ,@Param_ZuAb varchar(1)
  ,@Param_Menge varchar(12)
  ,@Param_PreisJe varchar(12)
  ,@Param_BruttoEk varchar(10)
  ,@Param_Rabatt1 varchar(5)
  ,@Param_Rabatt2 varchar(5)
  ,@Param_NettoEk varchar(10)
  ,@Param_NeJe100 varchar(10)
  ,@Param_NettoGes varchar(10)
  ,@Param_UstSchl varchar(3)
  ,@Param_VerbMenge varchar(12)
  ,@Param_VerbNetto varchar(10)
  ,@Param_Kst varchar(8)
  ,@Param_NRHW varchar(15)
  ,@Param_POSNRHW varchar(10)
  ,@Param_Einheit varchar(6)
  ,@Param_VerbrKto varchar(8)
  ,@Param_BelTyp varchar(1)
  ,@Param_VkPreis varchar(14)
  ,@Param_VkGesamt varchar(14)
  ,@Param_NonStock varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_LagerEinPos] SET
    [BelNr] = dbo.itcfunc_integer(@Param_BelNr)
   ,[PosNr] = dbo.itcfunc_integer(@Param_PosNr)
   ,[UPosNr] = dbo.itcfunc_integer(@Param_UPosNr)
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelegNr] = UPPER(RTRIM(@Param_BelegNr))
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[LgKto] = UPPER(RTRIM(@Param_LgKto))
   ,[ZuAb] = UPPER(RTRIM(@Param_ZuAb))
   ,[Menge] = dbo.itcfunc_float(@Param_Menge)
   ,[PreisJe] = dbo.itcfunc_float(@Param_PreisJe)
   ,[BruttoEk] = dbo.itcfunc_float(@Param_BruttoEk)
   ,[Rabatt1] = dbo.itcfunc_float(@Param_Rabatt1)
   ,[Rabatt2] = dbo.itcfunc_float(@Param_Rabatt2)
   ,[NettoEk] = dbo.itcfunc_float(@Param_NettoEk)
   ,[NeJe100] = dbo.itcfunc_float(@Param_NeJe100)
   ,[NettoGes] = dbo.itcfunc_float(@Param_NettoGes)
   ,[UstSchl] = UPPER(RTRIM(@Param_UstSchl))
   ,[VerbMenge] = dbo.itcfunc_float(@Param_VerbMenge)
   ,[VerbNetto] = dbo.itcfunc_float(@Param_VerbNetto)
   ,[Kst] = UPPER(RTRIM(@Param_Kst))
   ,[NRHW] = UPPER(RTRIM(@Param_NRHW))
   ,[POSNRHW] = UPPER(RTRIM(@Param_POSNRHW))
   ,[Einheit] = RTRIM(@Param_Einheit)
   ,[VerbrKto] = UPPER(RTRIM(@Param_VerbrKto))
   ,[BelTyp] = UPPER(RTRIM(@Param_BelTyp))
   ,[VkPreis] = dbo.itcfunc_float(@Param_VkPreis)
   ,[VkGesamt] = dbo.itcfunc_float(@Param_VkGesamt)
   ,[NonStock] = dbo.itcfunc_integer(@Param_NonStock)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_LagerEinPos] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_LagerEinPos')
  DROP PROCEDURE [dbo].[up_TrSel_LagerEinPos]
GO
CREATE PROCEDURE [dbo].[up_TrSel_LagerEinPos]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelegNr]))
   +LTRIM(RTRIM([Lkz]))
   +LTRIM(RTRIM([Artnr]))
   +LTRIM(RTRIM([Bez1]))
   +LTRIM(RTRIM([Bez2]))
   +LTRIM(RTRIM([Merkmal]))
   +LTRIM(RTRIM([LgKto]))
   +LTRIM(RTRIM([ZuAb]))
   +LTRIM(RTRIM([UstSchl]))
   +LTRIM(RTRIM([Kst]))
   +LTRIM(RTRIM([NRHW]))
   +LTRIM(RTRIM([POSNRHW]))
   +LTRIM(RTRIM([Einheit]))
   +LTRIM(RTRIM([VerbrKto]))
   +LTRIM(RTRIM([BelTyp]))
  AS Data FROM [dbo].[ut_LagerEinPos] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinPos_Key00')
  DROP PROCEDURE [dbo].[up_Next_LagerEinPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinPos_Key00] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldPosNr integer
  DECLARE @oldUPosNr integer
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldPosNr = PosNr, @oldUPosNr = UPosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (PosNr = @oldPosNr) AND (UPosNr > @oldUPosNr)
    ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (PosNr > @oldPosNr)
    ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp > @oldBelTyp)
    ORDER BY BelTyp,BelNr,PosNr,UPosNr,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_LagerEinPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinPos_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinPos_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinPos_Key00')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinPos_Key00] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldPosNr integer
  DECLARE @oldUPosNr integer
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldPosNr = PosNr, @oldUPosNr = UPosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (PosNr = @oldPosNr) AND (UPosNr < @oldUPosNr)
    ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (PosNr < @oldPosNr)
    ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp < @oldBelTyp)
    ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr 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_LagerEinPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinPos_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinPos_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr AND 
      UPosNr = @mUPosNr
      ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp >= @mBelTyp AND SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp,BelNr,PosNr,UPosNr,RecId
    RETURN
  END




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

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

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr AND 
      UPosNr = @mUPosNr
      ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr 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_LagerEinPos] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,PosNr desc,UPosNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinPos_Key01')
  DROP PROCEDURE [dbo].[up_First_LagerEinPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinPos_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinPos_Key01] AS EXECUTE [dbo].[up_First_LagerEinPos_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinPos_Key01')
  DROP PROCEDURE [dbo].[up_Last_LagerEinPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinPos_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinPos_Key01] AS EXECUTE [dbo].[up_Last_LagerEinPos_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinPos_Key01')
  DROP PROCEDURE [dbo].[up_Next_LagerEinPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinPos_Key01] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldUPosNr integer
  DECLARE @oldPosNr integer
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldUPosNr = UPosNr, @oldPosNr = PosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr = @oldUPosNr) AND (PosNr = @oldPosNr) AND (RecId > @oldRecId)
    ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr = @oldUPosNr) AND (PosNr > @oldPosNr)
    ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr > @oldUPosNr)
    ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr > @oldBelNr)
    ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp > @oldBelTyp)
    ORDER BY BelTyp,BelNr,UPosNr,PosNr,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_LagerEinPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinPos_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinPos_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinPos_Key01')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinPos_Key01] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldUPosNr integer
  DECLARE @oldPosNr integer
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldUPosNr = UPosNr, @oldPosNr = PosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr = @oldUPosNr) AND (PosNr = @oldPosNr) AND (RecId < @oldRecId)
    ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr = @oldUPosNr) AND (PosNr < @oldPosNr)
    ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UPosNr < @oldUPosNr)
    ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr < @oldBelNr)
    ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01)
    WHERE (BelTyp < @oldBelTyp)
    ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr 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_LagerEinPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinPos_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinPos_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UPosNr = @mUPosNr AND 
      PosNr = @mPosNr
      ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UPosNr = @mUPosNr
      ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp >= @mBelTyp AND SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp,BelNr,UPosNr,PosNr,RecId
    RETURN
  END




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

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

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

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UPosNr = @mUPosNr AND 
      PosNr = @mPosNr
      ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 11 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UPosNr = @mUPosNr
      ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr 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_LagerEinPos] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,UPosNr desc,PosNr desc,RecId desc
    RETURN
  END




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_First_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinPos_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinPos_Key02] AS EXECUTE [dbo].[up_First_LagerEinPos_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_Last_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_LagerEinPos_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_LagerEinPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_LagerEinPos_Key02] AS EXECUTE [dbo].[up_Last_LagerEinPos_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_Next_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinPos_Key02] @oldRecId integer AS
  DECLARE @oldNRHW char(15)
  DECLARE @oldPOSNRHW char(10)
  DECLARE @oldBelTyp char(1)
  DECLARE @oldUPosNr integer
  DECLARE @oldBelNr integer
  DECLARE @oldPosNr integer
  SELECT @oldNRHW = NRHW, @oldPOSNRHW = POSNRHW, @oldBelTyp = BelTyp, @oldUPosNr = UPosNr, @oldBelNr = BelNr, @oldPosNr = PosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr = @oldBelNr) AND (PosNr = @oldPosNr) AND (RecId > @oldRecId)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr = @oldBelNr) AND (PosNr > @oldPosNr)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr > @oldBelNr)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr > @oldUPosNr)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp > @oldBelTyp)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW > @oldPOSNRHW)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW > @oldNRHW)
    ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,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_LagerEinPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinPos_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinPos_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinPos_Key02] @oldRecId integer AS
  DECLARE @oldNRHW char(15)
  DECLARE @oldPOSNRHW char(10)
  DECLARE @oldBelTyp char(1)
  DECLARE @oldUPosNr integer
  DECLARE @oldBelNr integer
  DECLARE @oldPosNr integer
  SELECT @oldNRHW = NRHW, @oldPOSNRHW = POSNRHW, @oldBelTyp = BelTyp, @oldUPosNr = UPosNr, @oldBelNr = BelNr, @oldPosNr = PosNr FROM [dbo].[ut_LagerEinPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr = @oldBelNr) AND (PosNr = @oldPosNr) AND (RecId < @oldRecId)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr = @oldBelNr) AND (PosNr < @oldPosNr)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr = @oldUPosNr) AND (BelNr < @oldBelNr)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp = @oldBelTyp) AND (UPosNr < @oldUPosNr)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW = @oldPOSNRHW) AND (BelTyp < @oldBelTyp)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW = @oldNRHW) AND (POSNRHW < @oldPOSNRHW)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02)
    WHERE (NRHW < @oldNRHW)
    ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr 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_LagerEinPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinPos_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinPos_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinPos_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mNRHW char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mPOSNRHW char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,10)
  DECLARE @mBelTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,1)
  DECLARE @mUPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,2)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,37,4)

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

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr AND 
      BelNr = @mBelNr
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW >= @mNRHW AND SUBSTRING(NRHW,1,LEN(@mNRHW)) = @mNRHW
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW >= @mPOSNRHW AND SUBSTRING(POSNRHW,1,LEN(@mPOSNRHW)) = @mPOSNRHW
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp >= @mBelTyp AND SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY NRHW,POSNRHW,BelTyp,UPosNr,BelNr,PosNr,RecId
    RETURN
  END




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinPos_Key02')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinPos_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mNRHW char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mPOSNRHW char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,16,10)
  DECLARE @mBelTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,1)
  DECLARE @mUPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,2)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,8)
  DECLARE @mPosNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,37,4)

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

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 36 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr AND 
      BelNr = @mBelNr
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp AND 
      UPosNr = @mUPosNr
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      BelTyp = @mBelTyp
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(NRHW,1,LEN(@mNRHW)) = @mNRHW
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      SUBSTRING(POSNRHW,1,LEN(@mPOSNRHW)) = @mPOSNRHW
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinPos] WITH(INDEX = Ik_Key02) WHERE 
      NRHW = @mNRHW AND 
      POSNRHW = @mPOSNRHW AND 
      SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY NRHW desc,POSNRHW desc,BelTyp desc,UPosNr desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END




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


