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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelPos')
BEGIN
CREATE TABLE [dbo].[ut_BelPos]
(
  [RecId]  integer NOT NULL
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[LfdNr] char(6) DEFAULT ''
 ,[Ebene] integer DEFAULT 0
 ,[RefKey] char(6) DEFAULT ''
 ,[SatzArt] integer DEFAULT 0
 ,[Nachtrag] integer DEFAULT 0
 ,[Bedarf] tinyint DEFAULT 0
 ,[Alternativ] tinyint DEFAULT 0
 ,[Entfaellt] tinyint DEFAULT 0
 ,[Fixed] tinyint DEFAULT 0
 ,[Marked] tinyint DEFAULT 0
 ,[Fremd] tinyint DEFAULT 0
 ,[Vk2JN] tinyint DEFAULT 0
 ,[LohnJN] tinyint DEFAULT 0
 ,[MatJN] tinyint DEFAULT 0
 ,[PosNr] char(20) DEFAULT ''
 ,[ArtNr] char(20) DEFAULT ''
 ,[Ktxt1] varchar(50) DEFAULT ''
 ,[Menge] float DEFAULT 0
 ,[VkMat] float DEFAULT 0
 ,[VkLohn] float DEFAULT 0
 ,[VkFremd] float DEFAULT 0
 ,[EkMat] float DEFAULT 0
 ,[EkLohn] float DEFAULT 0
 ,[EkFremd] float DEFAULT 0
 ,[RWegZube] integer DEFAULT 0
 ,[RWegNe] integer DEFAULT 0
 ,[NeFlag] tinyint DEFAULT 0
 ,[EkZube] float DEFAULT 0
 ,[MittelLohn] float DEFAULT 0
 ,[RabattDM] float DEFAULT 0
 ,[RabattVH] float DEFAULT 0
 ,[Zeit] float DEFAULT 0
 ,[Aufwand] float DEFAULT 0
 ,[Me] char(6) DEFAULT ''
 ,[Pe] char(2) DEFAULT ''
 ,[Gebinde] float DEFAULT 0
 ,[AufLohn] float DEFAULT 0
 ,[AufMat] float DEFAULT 0
 ,[Ktxt2] varchar(50) DEFAULT ''
 ,[Ktxt3] varchar(50) DEFAULT ''
 ,[KzDrk1] integer DEFAULT 0
 ,[KzDrk2] integer DEFAULT 0
 ,[TxtNr] char(20) DEFAULT ''
 ,[DrkIndi] tinyint DEFAULT 0
 ,[DrkMenge] tinyint DEFAULT 0
 ,[DrkEP] tinyint DEFAULT 0
 ,[DrkGP] tinyint DEFAULT 0
 ,[DrkLM] tinyint DEFAULT 0
 ,[DrkUpos] tinyint DEFAULT 0
 ,[DrkUposPr] tinyint DEFAULT 0
 ,[DrkUposAbs] tinyint DEFAULT 0
 ,[Vk2Lohn] float DEFAULT 0
 ,[Vk2Mat] float DEFAULT 0
 ,[Brutto] float DEFAULT 0
 ,[Rab1] float DEFAULT 0
 ,[Rab2] float DEFAULT 0
 ,[EkNe] float DEFAULT 0
 ,[OldMLohn] tinyint DEFAULT 0
 ,[Faktor1] float DEFAULT 0
 ,[Faktor2] float DEFAULT 0
 ,[Faktor3] float DEFAULT 0
 ,[ZSAbStart] tinyint DEFAULT 0
 ,[ZSNeutral] tinyint DEFAULT 0
 ,[ZSWieVor] tinyint DEFAULT 0
 ,[DrkFett] tinyint DEFAULT 0
 ,[DrkFF] tinyint DEFAULT 0
 ,[DrkLF] tinyint DEFAULT 0
 ,[KzMwst] integer DEFAULT 0
 ,[MengeVA] float DEFAULT 0
 ,[Kst] varchar(20) DEFAULT ''
 ,[Ktr] varchar(20) DEFAULT ''
 ,[EGruMat] char(3) DEFAULT ''
 ,[EGruLohn] char(3) DEFAULT ''
 ,[SuSp] char(20) DEFAULT ''
 ,[SuSpTyp] integer DEFAULT 0
 ,[AbsMenge] float DEFAULT 0
 ,[EPMat] float DEFAULT 0
 ,[EPLohn] float DEFAULT 0
 ,[EPPos] float DEFAULT 0
 ,[GPPos] float DEFAULT 0
 ,[AbsGP] float DEFAULT 0
 ,[ParentRef] char(6) DEFAULT ''
 ,[Expanded] tinyint DEFAULT 0
 ,[Visible] tinyint DEFAULT 0
 ,[SuSpValid] tinyint DEFAULT 0
 ,[Dropped] tinyint DEFAULT 0
 ,[Saved] tinyint DEFAULT 0
 ,[EkSonstig] float DEFAULT 0
 ,[VkBedarf] float DEFAULT 0
 ,[CuGewicht] float DEFAULT 0
 ,[KGruppe] integer DEFAULT 0
 ,[FloskelJN] tinyint DEFAULT 0
 ,[GAEBxOZ] varchar(20) DEFAULT ''
 ,[GAEBxTyp] integer DEFAULT 0
 ,[GAEBxFrM] tinyint DEFAULT 0
 ,[AlGewicht] float DEFAULT 0
 ,[SumBrutto] float DEFAULT 0
 ,[ZZGrpNr] integer DEFAULT 0
 ,[ZZLfdNr] integer DEFAULT 0
 ,[EPAufgl] tinyint DEFAULT 0
 ,[EpAnteile] varchar(18) DEFAULT ''
 ,[VkAnt1] float DEFAULT 0
 ,[VkAnt2] float DEFAULT 0
 ,[VkAnt3] float DEFAULT 0
 ,[VkAnt4] float DEFAULT 0
 ,[Herkunft] integer DEFAULT 0
 ,[BuchGrp] char(10) DEFAULT ''
 ,[BezugAuf] char(6) DEFAULT ''
 ,[BezugBis] char(6) DEFAULT ''
 ,[Flags] integer DEFAULT 0
 ,[VkAnt5] float DEFAULT 0
 ,[VkAnt6] float DEFAULT 0
 ,[CVkAnt1] float DEFAULT 0
 ,[CVkAnt2] float DEFAULT 0
 ,[CVkAnt3] float DEFAULT 0
 ,[CVkAnt4] float DEFAULT 0
 ,[CVkAnt5] float DEFAULT 0
 ,[CVkAnt6] float DEFAULT 0
 ,[EkAnteile] varchar(168) DEFAULT ''
  CONSTRAINT [Pk_BelPos]  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_BelPos_KeyDel')
  DROP TRIGGER [dbo].[tr_BelPos_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelPos_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelPos_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelPos_Keys')
  DROP TABLE [dbo].[ut_BelPos_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_BelPos' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [LfdNr] char(6) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ebene')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Ebene] 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_BelPos' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [RefKey] 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_BelPos' and sc.[name] = 'SatzArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [SatzArt] 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_BelPos' and sc.[name] = 'Nachtrag')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Nachtrag] 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_BelPos' and sc.[name] = 'Bedarf')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Bedarf] 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_BelPos' and sc.[name] = 'Alternativ')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Alternativ] 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_BelPos' and sc.[name] = 'Entfaellt')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Entfaellt] 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_BelPos' and sc.[name] = 'Fixed')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Fixed] 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_BelPos' and sc.[name] = 'Marked')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Marked] 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_BelPos' and sc.[name] = 'Fremd')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Fremd] 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_BelPos' and sc.[name] = 'Vk2JN')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Vk2JN] 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_BelPos' and sc.[name] = 'LohnJN')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [LohnJN] 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_BelPos' and sc.[name] = 'MatJN')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [MatJN] 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_BelPos' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [PosNr] 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_BelPos' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'Ktxt1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Ktxt1] varchar(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'VkMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkMat] 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_BelPos' and sc.[name] = 'VkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkLohn] 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_BelPos' and sc.[name] = 'VkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkFremd] 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_BelPos' and sc.[name] = 'EkMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkMat] 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_BelPos' and sc.[name] = 'EkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkLohn] 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_BelPos' and sc.[name] = 'EkFremd')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkFremd] 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_BelPos' and sc.[name] = 'RWegZube')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [RWegZube] 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_BelPos' and sc.[name] = 'RWegNe')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [RWegNe] 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_BelPos' and sc.[name] = 'NeFlag')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [NeFlag] 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_BelPos' and sc.[name] = 'EkZube')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkZube] 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_BelPos' and sc.[name] = 'MittelLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [MittelLohn] 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_BelPos' and sc.[name] = 'RabattDM')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [RabattDM] 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_BelPos' and sc.[name] = 'RabattVH')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [RabattVH] 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_BelPos' and sc.[name] = 'Zeit')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Zeit] 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_BelPos' and sc.[name] = 'Aufwand')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Aufwand] 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_BelPos' and sc.[name] = 'Me')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Me] 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_BelPos' and sc.[name] = 'Pe')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Pe] 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_BelPos' and sc.[name] = 'Gebinde')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'AufLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [AufLohn] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'AufMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [AufMat] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Ktxt2] varchar(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt3')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Ktxt3] varchar(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'KzDrk1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [KzDrk1] 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_BelPos' and sc.[name] = 'KzDrk2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [KzDrk2] 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_BelPos' and sc.[name] = 'TxtNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [TxtNr] 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_BelPos' and sc.[name] = 'DrkIndi')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkIndi] 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_BelPos' and sc.[name] = 'DrkMenge')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkMenge] 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_BelPos' and sc.[name] = 'DrkEP')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkEP] 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_BelPos' and sc.[name] = 'DrkGP')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkGP] 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_BelPos' and sc.[name] = 'DrkLM')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkLM] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'DrkUpos')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkUpos] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'DrkUposPr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkUposPr] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'DrkUposAbs')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkUposAbs] 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_BelPos' and sc.[name] = 'Vk2Lohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Vk2Lohn] 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_BelPos' and sc.[name] = 'Vk2Mat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Vk2Mat] 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_BelPos' and sc.[name] = 'Brutto')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Brutto] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Rab1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Rab1] 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_BelPos' and sc.[name] = 'Rab2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Rab2] 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_BelPos' and sc.[name] = 'EkNe')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkNe] 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_BelPos' and sc.[name] = 'OldMLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [OldMLohn] 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_BelPos' and sc.[name] = 'Faktor1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Faktor1] 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_BelPos' and sc.[name] = 'Faktor2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Faktor2] 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_BelPos' and sc.[name] = 'Faktor3')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Faktor3] 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_BelPos' and sc.[name] = 'ZSAbStart')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ZSAbStart] 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_BelPos' and sc.[name] = 'ZSNeutral')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ZSNeutral] 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_BelPos' and sc.[name] = 'ZSWieVor')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ZSWieVor] 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_BelPos' and sc.[name] = 'DrkFett')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkFett] 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_BelPos' and sc.[name] = 'DrkFF')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkFF] 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_BelPos' and sc.[name] = 'DrkLF')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [DrkLF] 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_BelPos' and sc.[name] = 'KzMwst')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [KzMwst] 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_BelPos' and sc.[name] = 'MengeVA')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [MengeVA] 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_BelPos' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Kst] 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_BelPos' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Ktr] 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_BelPos' and sc.[name] = 'EGruMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EGruMat] 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_BelPos' and sc.[name] = 'EGruLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EGruLohn] 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_BelPos' and sc.[name] = 'SuSp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [SuSp] 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_BelPos' and sc.[name] = 'SuSpTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [SuSpTyp] 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_BelPos' and sc.[name] = 'AbsMenge')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [AbsMenge] 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_BelPos' and sc.[name] = 'EPMat')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EPMat] 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_BelPos' and sc.[name] = 'EPLohn')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EPLohn] 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_BelPos' and sc.[name] = 'EPPos')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EPPos] 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_BelPos' and sc.[name] = 'GPPos')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [GPPos] 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_BelPos' and sc.[name] = 'AbsGP')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [AbsGP] 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_BelPos' and sc.[name] = 'ParentRef')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ParentRef] 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_BelPos' and sc.[name] = 'Expanded')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Expanded] 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_BelPos' and sc.[name] = 'Visible')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Visible] 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_BelPos' and sc.[name] = 'SuSpValid')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [SuSpValid] 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_BelPos' and sc.[name] = 'Dropped')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Dropped] 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_BelPos' and sc.[name] = 'Saved')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Saved] 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_BelPos' and sc.[name] = 'EkSonstig')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkSonstig] 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_BelPos' and sc.[name] = 'VkBedarf')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkBedarf] 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_BelPos' and sc.[name] = 'CuGewicht')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CuGewicht] 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_BelPos' and sc.[name] = 'KGruppe')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [KGruppe] 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_BelPos' and sc.[name] = 'FloskelJN')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [FloskelJN] 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_BelPos' and sc.[name] = 'GAEBxOZ')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [GAEBxOZ] 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_BelPos' and sc.[name] = 'GAEBxTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [GAEBxTyp] 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_BelPos' and sc.[name] = 'GAEBxFrM')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [GAEBxFrM] 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_BelPos' and sc.[name] = 'AlGewicht')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [AlGewicht] 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_BelPos' and sc.[name] = 'SumBrutto')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [SumBrutto] 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_BelPos' and sc.[name] = 'ZZGrpNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ZZGrpNr] 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_BelPos' and sc.[name] = 'ZZLfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [ZZLfdNr] 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_BelPos' and sc.[name] = 'EPAufgl')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EPAufgl] 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_BelPos' and sc.[name] = 'EpAnteile')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EpAnteile] varchar(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_BelPos' and sc.[name] = 'VkAnt1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt1] 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_BelPos' and sc.[name] = 'VkAnt2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt2] 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_BelPos' and sc.[name] = 'VkAnt3')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt3] 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_BelPos' and sc.[name] = 'VkAnt4')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt4] 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_BelPos' and sc.[name] = 'Herkunft')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Herkunft] 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_BelPos' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [BuchGrp] 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_BelPos' and sc.[name] = 'BezugAuf')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [BezugAuf] 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_BelPos' and sc.[name] = 'BezugBis')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [BezugBis] 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_BelPos' and sc.[name] = 'Flags')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [Flags] 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_BelPos' and sc.[name] = 'VkAnt5')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt5] 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_BelPos' and sc.[name] = 'VkAnt6')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [VkAnt6] 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_BelPos' and sc.[name] = 'CVkAnt1')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt1] 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_BelPos' and sc.[name] = 'CVkAnt2')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt2] 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_BelPos' and sc.[name] = 'CVkAnt3')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt3] 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_BelPos' and sc.[name] = 'CVkAnt4')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt4] 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_BelPos' and sc.[name] = 'CVkAnt5')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt5] 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_BelPos' and sc.[name] = 'CVkAnt6')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [CVkAnt6] 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_BelPos' and sc.[name] = 'EkAnteile')
BEGIN
  ALTER TABLE [dbo].[ut_BelPos] ADD [EkAnteile] varchar(168) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' 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_BelPos' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' and sc.[name] = 'LfdNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [LfdNr] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'LfdNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [LfdNr] varchar(6)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'RefKey'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'RefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [RefKey] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'RefKey'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'RefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [RefKey] varchar(6)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'PosNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'PosNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [PosNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'PosNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'PosNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [PosNr] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' 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_BelPos' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] 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_BelPos' 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_BelPos' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [ArtNr] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt1] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt1] varchar(50)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Me'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Me'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Me] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Me'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Me'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Me] varchar(6)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Pe'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Pe'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Pe] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Pe'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Pe'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Pe] varchar(2)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt2] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt2] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt3'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt3'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt3] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt3'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktxt3'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktxt3] varchar(50)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'TxtNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'TxtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [TxtNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'TxtNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'TxtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [TxtNr] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' 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_BelPos' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Kst] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' 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_BelPos' and sc.[name] = 'Kst'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Kst] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'Ktr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [Ktr] varchar(20)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruMat'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruMat'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EGruMat] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruMat'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruMat'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EGruMat] varchar(3)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruLohn'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruLohn'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EGruLohn] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruLohn'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EGruLohn'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EGruLohn] varchar(3)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'SuSp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'SuSp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [SuSp] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'SuSp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'SuSp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [SuSp] varchar(20)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'ParentRef'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'ParentRef'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [ParentRef] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'ParentRef'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'ParentRef'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [ParentRef] varchar(6)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'GAEBxOZ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'GAEBxOZ'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [GAEBxOZ] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'GAEBxOZ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'GAEBxOZ'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [GAEBxOZ] varchar(20)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EpAnteile'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EpAnteile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EpAnteile] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EpAnteile'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EpAnteile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EpAnteile] varchar(18)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BuchGrp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BuchGrp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BuchGrp] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BuchGrp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BuchGrp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BuchGrp] 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_BelPos' and sc.[name] = 'BezugAuf'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugAuf'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BezugAuf] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugAuf'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugAuf'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BezugAuf] varchar(6)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugBis'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugBis'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BezugBis] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugBis'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'BezugBis'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [BezugBis] varchar(6)
END
GO
IF (168 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EkAnteile'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EkAnteile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EkAnteile] char(168)
END
GO
IF (168 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EkAnteile'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelPos' and sc.[name] = 'EkAnteile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelPos] ALTER COLUMN [EkAnteile] varchar(168)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelPos')
  DROP VIEW [dbo].[BelPos]
GO
CREATE VIEW [dbo].[BelPos] AS
  SELECT [RecId]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[LfdNr]
 ,[Ebene]
 ,[RefKey]
 ,[SatzArt]
 ,[Nachtrag]
 ,[Bedarf]
 ,[Alternativ]
 ,[Entfaellt]
 ,[Fixed]
 ,[Marked]
 ,[Fremd]
 ,[Vk2JN]
 ,[LohnJN]
 ,[MatJN]
 ,[PosNr]
 ,[ArtNr]
 ,[Ktxt1]
 ,[Menge]
 ,[VkMat]
 ,[VkLohn]
 ,[VkFremd]
 ,[EkMat]
 ,[EkLohn]
 ,[EkFremd]
 ,[RWegZube]
 ,[RWegNe]
 ,[NeFlag]
 ,[EkZube]
 ,[MittelLohn]
 ,[RabattDM]
 ,[RabattVH]
 ,[Zeit]
 ,[Aufwand]
 ,[Me]
 ,[Pe]
 ,[Gebinde]
 ,[AufLohn]
 ,[AufMat]
 ,[Ktxt2]
 ,[Ktxt3]
 ,[KzDrk1]
 ,[KzDrk2]
 ,[TxtNr]
 ,[DrkIndi]
 ,[DrkMenge]
 ,[DrkEP]
 ,[DrkGP]
 ,[DrkLM]
 ,[DrkUpos]
 ,[DrkUposPr]
 ,[DrkUposAbs]
 ,[Vk2Lohn]
 ,[Vk2Mat]
 ,[Brutto]
 ,[Rab1]
 ,[Rab2]
 ,[EkNe]
 ,[OldMLohn]
 ,[Faktor1]
 ,[Faktor2]
 ,[Faktor3]
 ,[ZSAbStart]
 ,[ZSNeutral]
 ,[ZSWieVor]
 ,[DrkFett]
 ,[DrkFF]
 ,[DrkLF]
 ,[KzMwst]
 ,[MengeVA]
 ,[Kst]
 ,[Ktr]
 ,[EGruMat]
 ,[EGruLohn]
 ,[SuSp]
 ,[SuSpTyp]
 ,[AbsMenge]
 ,[EPMat]
 ,[EPLohn]
 ,[EPPos]
 ,[GPPos]
 ,[AbsGP]
 ,[ParentRef]
 ,[Expanded]
 ,[Visible]
 ,[SuSpValid]
 ,[Dropped]
 ,[Saved]
 ,[EkSonstig]
 ,[VkBedarf]
 ,[CuGewicht]
 ,[KGruppe]
 ,[FloskelJN]
 ,[GAEBxOZ]
 ,[GAEBxTyp]
 ,[GAEBxFrM]
 ,[AlGewicht]
 ,[SumBrutto]
 ,[ZZGrpNr]
 ,[ZZLfdNr]
 ,[EPAufgl]
 ,[EpAnteile]
 ,[VkAnt1]
 ,[VkAnt2]
 ,[VkAnt3]
 ,[VkAnt4]
 ,[Herkunft]
 ,[BuchGrp]
 ,[BezugAuf]
 ,[BezugBis]
 ,[Flags]
 ,[VkAnt5]
 ,[VkAnt6]
 ,[CVkAnt1]
 ,[CVkAnt2]
 ,[CVkAnt3]
 ,[CVkAnt4]
 ,[CVkAnt5]
 ,[CVkAnt6]
 ,[EkAnteile]
  FROM [dbo].[ut_BelPos]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelPos')
  DROP PROCEDURE [dbo].[up_Sel_BelPos]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelPos]  @mRI integer
AS
  SELECT
    [PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
   ,[Ebene]
   ,[RefKey]
   ,[SatzArt]
   ,[Nachtrag]
   ,[Bedarf]
   ,[Alternativ]
   ,[Entfaellt]
   ,[Fixed]
   ,[Marked]
   ,[Fremd]
   ,[Vk2JN]
   ,[LohnJN]
   ,[MatJN]
   ,[PosNr]
   ,[ArtNr]
   ,[Ktxt1]
   ,[Menge]
   ,[VkMat]
   ,[VkLohn]
   ,[VkFremd]
   ,[EkMat]
   ,[EkLohn]
   ,[EkFremd]
   ,[RWegZube]
   ,[RWegNe]
   ,[NeFlag]
   ,[EkZube]
   ,[MittelLohn]
   ,[RabattDM]
   ,[RabattVH]
   ,[Zeit]
   ,[Aufwand]
   ,[Me]
   ,[Pe]
   ,[Gebinde]
   ,[AufLohn]
   ,[AufMat]
   ,[Ktxt2]
   ,[Ktxt3]
   ,[KzDrk1]
   ,[KzDrk2]
   ,[TxtNr]
   ,[DrkIndi]
   ,[DrkMenge]
   ,[DrkEP]
   ,[DrkGP]
   ,[DrkLM]
   ,[DrkUpos]
   ,[DrkUposPr]
   ,[DrkUposAbs]
   ,[Vk2Lohn]
   ,[Vk2Mat]
   ,[Brutto]
   ,[Rab1]
   ,[Rab2]
   ,[EkNe]
   ,[OldMLohn]
   ,[Faktor1]
   ,[Faktor2]
   ,[Faktor3]
   ,[ZSAbStart]
   ,[ZSNeutral]
   ,[ZSWieVor]
   ,[DrkFett]
   ,[DrkFF]
   ,[DrkLF]
   ,[KzMwst]
   ,[MengeVA]
   ,[Kst]
   ,[Ktr]
   ,[EGruMat]
   ,[EGruLohn]
   ,[SuSp]
   ,[SuSpTyp]
   ,[AbsMenge]
   ,[EPMat]
   ,[EPLohn]
   ,[EPPos]
   ,[GPPos]
   ,[AbsGP]
   ,[ParentRef]
   ,[Expanded]
   ,[Visible]
   ,[SuSpValid]
   ,[Dropped]
   ,[Saved]
   ,[EkSonstig]
   ,[VkBedarf]
   ,[CuGewicht]
   ,[KGruppe]
   ,[FloskelJN]
   ,[GAEBxOZ]
   ,[GAEBxTyp]
   ,[GAEBxFrM]
   ,[AlGewicht]
   ,[SumBrutto]
   ,[ZZGrpNr]
   ,[ZZLfdNr]
   ,[EPAufgl]
   ,[EpAnteile]
   ,[VkAnt1]
   ,[VkAnt2]
   ,[VkAnt3]
   ,[VkAnt4]
   ,[Herkunft]
   ,[BuchGrp]
   ,[BezugAuf]
   ,[BezugBis]
   ,[Flags]
   ,[VkAnt5]
   ,[VkAnt6]
   ,[CVkAnt1]
   ,[CVkAnt2]
   ,[CVkAnt3]
   ,[CVkAnt4]
   ,[CVkAnt5]
   ,[CVkAnt6]
   ,[EkAnteile]
  FROM [dbo].[ut_BelPos] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelPos')
  DROP PROCEDURE [dbo].[up_Append_BelPos]
GO
CREATE PROCEDURE [dbo].[up_Append_BelPos]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr char(6)
  ,@Param_Ebene integer
  ,@Param_RefKey char(6)
  ,@Param_SatzArt integer
  ,@Param_Nachtrag integer
  ,@Param_Bedarf tinyint
  ,@Param_Alternativ tinyint
  ,@Param_Entfaellt tinyint
  ,@Param_Fixed tinyint
  ,@Param_Marked tinyint
  ,@Param_Fremd tinyint
  ,@Param_Vk2JN tinyint
  ,@Param_LohnJN tinyint
  ,@Param_MatJN tinyint
  ,@Param_PosNr char(20)
  ,@Param_ArtNr char(20)
  ,@Param_Ktxt1 varchar(50)
  ,@Param_Menge float
  ,@Param_VkMat float
  ,@Param_VkLohn float
  ,@Param_VkFremd float
  ,@Param_EkMat float
  ,@Param_EkLohn float
  ,@Param_EkFremd float
  ,@Param_RWegZube integer
  ,@Param_RWegNe integer
  ,@Param_NeFlag tinyint
  ,@Param_EkZube float
  ,@Param_MittelLohn float
  ,@Param_RabattDM float
  ,@Param_RabattVH float
  ,@Param_Zeit float
  ,@Param_Aufwand float
  ,@Param_Me char(6)
  ,@Param_Pe char(2)
  ,@Param_Gebinde float
  ,@Param_AufLohn float
  ,@Param_AufMat float
  ,@Param_Ktxt2 varchar(50)
  ,@Param_Ktxt3 varchar(50)
  ,@Param_KzDrk1 integer
  ,@Param_KzDrk2 integer
  ,@Param_TxtNr char(20)
  ,@Param_DrkIndi tinyint
  ,@Param_DrkMenge tinyint
  ,@Param_DrkEP tinyint
  ,@Param_DrkGP tinyint
  ,@Param_DrkLM tinyint
  ,@Param_DrkUpos tinyint
  ,@Param_DrkUposPr tinyint
  ,@Param_DrkUposAbs tinyint
  ,@Param_Vk2Lohn float
  ,@Param_Vk2Mat float
  ,@Param_Brutto float
  ,@Param_Rab1 float
  ,@Param_Rab2 float
  ,@Param_EkNe float
  ,@Param_OldMLohn tinyint
  ,@Param_Faktor1 float
  ,@Param_Faktor2 float
  ,@Param_Faktor3 float
  ,@Param_ZSAbStart tinyint
  ,@Param_ZSNeutral tinyint
  ,@Param_ZSWieVor tinyint
  ,@Param_DrkFett tinyint
  ,@Param_DrkFF tinyint
  ,@Param_DrkLF tinyint
  ,@Param_KzMwst integer
  ,@Param_MengeVA float
  ,@Param_Kst varchar(20)
  ,@Param_Ktr varchar(20)
  ,@Param_EGruMat char(3)
  ,@Param_EGruLohn char(3)
  ,@Param_SuSp char(20)
  ,@Param_SuSpTyp integer
  ,@Param_AbsMenge float
  ,@Param_EPMat float
  ,@Param_EPLohn float
  ,@Param_EPPos float
  ,@Param_GPPos float
  ,@Param_AbsGP float
  ,@Param_ParentRef char(6)
  ,@Param_Expanded tinyint
  ,@Param_Visible tinyint
  ,@Param_SuSpValid tinyint
  ,@Param_Dropped tinyint
  ,@Param_Saved tinyint
  ,@Param_EkSonstig float
  ,@Param_VkBedarf float
  ,@Param_CuGewicht float
  ,@Param_KGruppe integer
  ,@Param_FloskelJN tinyint
  ,@Param_GAEBxOZ varchar(20)
  ,@Param_GAEBxTyp integer
  ,@Param_GAEBxFrM tinyint
  ,@Param_AlGewicht float
  ,@Param_SumBrutto float
  ,@Param_ZZGrpNr integer
  ,@Param_ZZLfdNr integer
  ,@Param_EPAufgl tinyint
  ,@Param_EpAnteile varchar(18)
  ,@Param_VkAnt1 float
  ,@Param_VkAnt2 float
  ,@Param_VkAnt3 float
  ,@Param_VkAnt4 float
  ,@Param_Herkunft integer
  ,@Param_BuchGrp char(10)
  ,@Param_BezugAuf char(6)
  ,@Param_BezugBis char(6)
  ,@Param_Flags integer
  ,@Param_VkAnt5 float
  ,@Param_VkAnt6 float
  ,@Param_CVkAnt1 float
  ,@Param_CVkAnt2 float
  ,@Param_CVkAnt3 float
  ,@Param_CVkAnt4 float
  ,@Param_CVkAnt5 float
  ,@Param_CVkAnt6 float
  ,@Param_EkAnteile varchar(168)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelPos()
      INSERT INTO [dbo].[ut_BelPos] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[LfdNr]
        ,[Ebene]
        ,[RefKey]
        ,[SatzArt]
        ,[Nachtrag]
        ,[Bedarf]
        ,[Alternativ]
        ,[Entfaellt]
        ,[Fixed]
        ,[Marked]
        ,[Fremd]
        ,[Vk2JN]
        ,[LohnJN]
        ,[MatJN]
        ,[PosNr]
        ,[ArtNr]
        ,[Ktxt1]
        ,[Menge]
        ,[VkMat]
        ,[VkLohn]
        ,[VkFremd]
        ,[EkMat]
        ,[EkLohn]
        ,[EkFremd]
        ,[RWegZube]
        ,[RWegNe]
        ,[NeFlag]
        ,[EkZube]
        ,[MittelLohn]
        ,[RabattDM]
        ,[RabattVH]
        ,[Zeit]
        ,[Aufwand]
        ,[Me]
        ,[Pe]
        ,[Gebinde]
        ,[AufLohn]
        ,[AufMat]
        ,[Ktxt2]
        ,[Ktxt3]
        ,[KzDrk1]
        ,[KzDrk2]
        ,[TxtNr]
        ,[DrkIndi]
        ,[DrkMenge]
        ,[DrkEP]
        ,[DrkGP]
        ,[DrkLM]
        ,[DrkUpos]
        ,[DrkUposPr]
        ,[DrkUposAbs]
        ,[Vk2Lohn]
        ,[Vk2Mat]
        ,[Brutto]
        ,[Rab1]
        ,[Rab2]
        ,[EkNe]
        ,[OldMLohn]
        ,[Faktor1]
        ,[Faktor2]
        ,[Faktor3]
        ,[ZSAbStart]
        ,[ZSNeutral]
        ,[ZSWieVor]
        ,[DrkFett]
        ,[DrkFF]
        ,[DrkLF]
        ,[KzMwst]
        ,[MengeVA]
        ,[Kst]
        ,[Ktr]
        ,[EGruMat]
        ,[EGruLohn]
        ,[SuSp]
        ,[SuSpTyp]
        ,[AbsMenge]
        ,[EPMat]
        ,[EPLohn]
        ,[EPPos]
        ,[GPPos]
        ,[AbsGP]
        ,[ParentRef]
        ,[Expanded]
        ,[Visible]
        ,[SuSpValid]
        ,[Dropped]
        ,[Saved]
        ,[EkSonstig]
        ,[VkBedarf]
        ,[CuGewicht]
        ,[KGruppe]
        ,[FloskelJN]
        ,[GAEBxOZ]
        ,[GAEBxTyp]
        ,[GAEBxFrM]
        ,[AlGewicht]
        ,[SumBrutto]
        ,[ZZGrpNr]
        ,[ZZLfdNr]
        ,[EPAufgl]
        ,[EpAnteile]
        ,[VkAnt1]
        ,[VkAnt2]
        ,[VkAnt3]
        ,[VkAnt4]
        ,[Herkunft]
        ,[BuchGrp]
        ,[BezugAuf]
        ,[BezugBis]
        ,[Flags]
        ,[VkAnt5]
        ,[VkAnt6]
        ,[CVkAnt1]
        ,[CVkAnt2]
        ,[CVkAnt3]
        ,[CVkAnt4]
        ,[CVkAnt5]
        ,[CVkAnt6]
        ,[EkAnteile]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,@Param_Ebene
        ,UPPER(RTRIM(@Param_RefKey))
        ,@Param_SatzArt
        ,@Param_Nachtrag
        ,@Param_Bedarf
        ,@Param_Alternativ
        ,@Param_Entfaellt
        ,@Param_Fixed
        ,@Param_Marked
        ,@Param_Fremd
        ,@Param_Vk2JN
        ,@Param_LohnJN
        ,@Param_MatJN
        ,UPPER(RTRIM(@Param_PosNr))
        ,UPPER(RTRIM(@Param_ArtNr))
        ,RTRIM(@Param_Ktxt1)
        ,@Param_Menge
        ,@Param_VkMat
        ,@Param_VkLohn
        ,@Param_VkFremd
        ,@Param_EkMat
        ,@Param_EkLohn
        ,@Param_EkFremd
        ,@Param_RWegZube
        ,@Param_RWegNe
        ,@Param_NeFlag
        ,@Param_EkZube
        ,@Param_MittelLohn
        ,@Param_RabattDM
        ,@Param_RabattVH
        ,@Param_Zeit
        ,@Param_Aufwand
        ,RTRIM(@Param_Me)
        ,UPPER(RTRIM(@Param_Pe))
        ,@Param_Gebinde
        ,@Param_AufLohn
        ,@Param_AufMat
        ,RTRIM(@Param_Ktxt2)
        ,RTRIM(@Param_Ktxt3)
        ,@Param_KzDrk1
        ,@Param_KzDrk2
        ,UPPER(RTRIM(@Param_TxtNr))
        ,@Param_DrkIndi
        ,@Param_DrkMenge
        ,@Param_DrkEP
        ,@Param_DrkGP
        ,@Param_DrkLM
        ,@Param_DrkUpos
        ,@Param_DrkUposPr
        ,@Param_DrkUposAbs
        ,@Param_Vk2Lohn
        ,@Param_Vk2Mat
        ,@Param_Brutto
        ,@Param_Rab1
        ,@Param_Rab2
        ,@Param_EkNe
        ,@Param_OldMLohn
        ,@Param_Faktor1
        ,@Param_Faktor2
        ,@Param_Faktor3
        ,@Param_ZSAbStart
        ,@Param_ZSNeutral
        ,@Param_ZSWieVor
        ,@Param_DrkFett
        ,@Param_DrkFF
        ,@Param_DrkLF
        ,@Param_KzMwst
        ,@Param_MengeVA
        ,RTRIM(@Param_Kst)
        ,RTRIM(@Param_Ktr)
        ,UPPER(RTRIM(@Param_EGruMat))
        ,UPPER(RTRIM(@Param_EGruLohn))
        ,UPPER(RTRIM(@Param_SuSp))
        ,@Param_SuSpTyp
        ,@Param_AbsMenge
        ,@Param_EPMat
        ,@Param_EPLohn
        ,@Param_EPPos
        ,@Param_GPPos
        ,@Param_AbsGP
        ,UPPER(RTRIM(@Param_ParentRef))
        ,@Param_Expanded
        ,@Param_Visible
        ,@Param_SuSpValid
        ,@Param_Dropped
        ,@Param_Saved
        ,@Param_EkSonstig
        ,@Param_VkBedarf
        ,@Param_CuGewicht
        ,@Param_KGruppe
        ,@Param_FloskelJN
        ,RTRIM(@Param_GAEBxOZ)
        ,@Param_GAEBxTyp
        ,@Param_GAEBxFrM
        ,@Param_AlGewicht
        ,@Param_SumBrutto
        ,@Param_ZZGrpNr
        ,@Param_ZZLfdNr
        ,@Param_EPAufgl
        ,RTRIM(@Param_EpAnteile)
        ,@Param_VkAnt1
        ,@Param_VkAnt2
        ,@Param_VkAnt3
        ,@Param_VkAnt4
        ,@Param_Herkunft
        ,UPPER(RTRIM(@Param_BuchGrp))
        ,UPPER(RTRIM(@Param_BezugAuf))
        ,UPPER(RTRIM(@Param_BezugBis))
        ,@Param_Flags
        ,@Param_VkAnt5
        ,@Param_VkAnt6
        ,@Param_CVkAnt1
        ,@Param_CVkAnt2
        ,@Param_CVkAnt3
        ,@Param_CVkAnt4
        ,@Param_CVkAnt5
        ,@Param_CVkAnt6
        ,RTRIM(@Param_EkAnteile)
          )
      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_BelPos')
  DROP PROCEDURE [dbo].[up_Add_BelPos]
GO
CREATE PROCEDURE [dbo].[up_Add_BelPos]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BelPos()
  INSERT INTO [dbo].[ut_BelPos] (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_BelPos')
  DROP PROCEDURE [dbo].[up_Upd_BelPos]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelPos] @mRI integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr char(6)
  ,@Param_Ebene integer
  ,@Param_RefKey char(6)
  ,@Param_SatzArt integer
  ,@Param_Nachtrag integer
  ,@Param_Bedarf tinyint
  ,@Param_Alternativ tinyint
  ,@Param_Entfaellt tinyint
  ,@Param_Fixed tinyint
  ,@Param_Marked tinyint
  ,@Param_Fremd tinyint
  ,@Param_Vk2JN tinyint
  ,@Param_LohnJN tinyint
  ,@Param_MatJN tinyint
  ,@Param_PosNr char(20)
  ,@Param_ArtNr char(20)
  ,@Param_Ktxt1 varchar(50)
  ,@Param_Menge float
  ,@Param_VkMat float
  ,@Param_VkLohn float
  ,@Param_VkFremd float
  ,@Param_EkMat float
  ,@Param_EkLohn float
  ,@Param_EkFremd float
  ,@Param_RWegZube integer
  ,@Param_RWegNe integer
  ,@Param_NeFlag tinyint
  ,@Param_EkZube float
  ,@Param_MittelLohn float
  ,@Param_RabattDM float
  ,@Param_RabattVH float
  ,@Param_Zeit float
  ,@Param_Aufwand float
  ,@Param_Me char(6)
  ,@Param_Pe char(2)
  ,@Param_Gebinde float
  ,@Param_AufLohn float
  ,@Param_AufMat float
  ,@Param_Ktxt2 varchar(50)
  ,@Param_Ktxt3 varchar(50)
  ,@Param_KzDrk1 integer
  ,@Param_KzDrk2 integer
  ,@Param_TxtNr char(20)
  ,@Param_DrkIndi tinyint
  ,@Param_DrkMenge tinyint
  ,@Param_DrkEP tinyint
  ,@Param_DrkGP tinyint
  ,@Param_DrkLM tinyint
  ,@Param_DrkUpos tinyint
  ,@Param_DrkUposPr tinyint
  ,@Param_DrkUposAbs tinyint
  ,@Param_Vk2Lohn float
  ,@Param_Vk2Mat float
  ,@Param_Brutto float
  ,@Param_Rab1 float
  ,@Param_Rab2 float
  ,@Param_EkNe float
  ,@Param_OldMLohn tinyint
  ,@Param_Faktor1 float
  ,@Param_Faktor2 float
  ,@Param_Faktor3 float
  ,@Param_ZSAbStart tinyint
  ,@Param_ZSNeutral tinyint
  ,@Param_ZSWieVor tinyint
  ,@Param_DrkFett tinyint
  ,@Param_DrkFF tinyint
  ,@Param_DrkLF tinyint
  ,@Param_KzMwst integer
  ,@Param_MengeVA float
  ,@Param_Kst varchar(20)
  ,@Param_Ktr varchar(20)
  ,@Param_EGruMat char(3)
  ,@Param_EGruLohn char(3)
  ,@Param_SuSp char(20)
  ,@Param_SuSpTyp integer
  ,@Param_AbsMenge float
  ,@Param_EPMat float
  ,@Param_EPLohn float
  ,@Param_EPPos float
  ,@Param_GPPos float
  ,@Param_AbsGP float
  ,@Param_ParentRef char(6)
  ,@Param_Expanded tinyint
  ,@Param_Visible tinyint
  ,@Param_SuSpValid tinyint
  ,@Param_Dropped tinyint
  ,@Param_Saved tinyint
  ,@Param_EkSonstig float
  ,@Param_VkBedarf float
  ,@Param_CuGewicht float
  ,@Param_KGruppe integer
  ,@Param_FloskelJN tinyint
  ,@Param_GAEBxOZ varchar(20)
  ,@Param_GAEBxTyp integer
  ,@Param_GAEBxFrM tinyint
  ,@Param_AlGewicht float
  ,@Param_SumBrutto float
  ,@Param_ZZGrpNr integer
  ,@Param_ZZLfdNr integer
  ,@Param_EPAufgl tinyint
  ,@Param_EpAnteile varchar(18)
  ,@Param_VkAnt1 float
  ,@Param_VkAnt2 float
  ,@Param_VkAnt3 float
  ,@Param_VkAnt4 float
  ,@Param_Herkunft integer
  ,@Param_BuchGrp char(10)
  ,@Param_BezugAuf char(6)
  ,@Param_BezugBis char(6)
  ,@Param_Flags integer
  ,@Param_VkAnt5 float
  ,@Param_VkAnt6 float
  ,@Param_CVkAnt1 float
  ,@Param_CVkAnt2 float
  ,@Param_CVkAnt3 float
  ,@Param_CVkAnt4 float
  ,@Param_CVkAnt5 float
  ,@Param_CVkAnt6 float
  ,@Param_EkAnteile varchar(168)
AS
  UPDATE [dbo].[ut_BelPos] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Ebene] = @Param_Ebene
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[SatzArt] = @Param_SatzArt
   ,[Nachtrag] = @Param_Nachtrag
   ,[Bedarf] = @Param_Bedarf
   ,[Alternativ] = @Param_Alternativ
   ,[Entfaellt] = @Param_Entfaellt
   ,[Fixed] = @Param_Fixed
   ,[Marked] = @Param_Marked
   ,[Fremd] = @Param_Fremd
   ,[Vk2JN] = @Param_Vk2JN
   ,[LohnJN] = @Param_LohnJN
   ,[MatJN] = @Param_MatJN
   ,[PosNr] = UPPER(RTRIM(@Param_PosNr))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[Ktxt1] = RTRIM(@Param_Ktxt1)
   ,[Menge] = @Param_Menge
   ,[VkMat] = @Param_VkMat
   ,[VkLohn] = @Param_VkLohn
   ,[VkFremd] = @Param_VkFremd
   ,[EkMat] = @Param_EkMat
   ,[EkLohn] = @Param_EkLohn
   ,[EkFremd] = @Param_EkFremd
   ,[RWegZube] = @Param_RWegZube
   ,[RWegNe] = @Param_RWegNe
   ,[NeFlag] = @Param_NeFlag
   ,[EkZube] = @Param_EkZube
   ,[MittelLohn] = @Param_MittelLohn
   ,[RabattDM] = @Param_RabattDM
   ,[RabattVH] = @Param_RabattVH
   ,[Zeit] = @Param_Zeit
   ,[Aufwand] = @Param_Aufwand
   ,[Me] = RTRIM(@Param_Me)
   ,[Pe] = UPPER(RTRIM(@Param_Pe))
   ,[Gebinde] = @Param_Gebinde
   ,[AufLohn] = @Param_AufLohn
   ,[AufMat] = @Param_AufMat
   ,[Ktxt2] = RTRIM(@Param_Ktxt2)
   ,[Ktxt3] = RTRIM(@Param_Ktxt3)
   ,[KzDrk1] = @Param_KzDrk1
   ,[KzDrk2] = @Param_KzDrk2
   ,[TxtNr] = UPPER(RTRIM(@Param_TxtNr))
   ,[DrkIndi] = @Param_DrkIndi
   ,[DrkMenge] = @Param_DrkMenge
   ,[DrkEP] = @Param_DrkEP
   ,[DrkGP] = @Param_DrkGP
   ,[DrkLM] = @Param_DrkLM
   ,[DrkUpos] = @Param_DrkUpos
   ,[DrkUposPr] = @Param_DrkUposPr
   ,[DrkUposAbs] = @Param_DrkUposAbs
   ,[Vk2Lohn] = @Param_Vk2Lohn
   ,[Vk2Mat] = @Param_Vk2Mat
   ,[Brutto] = @Param_Brutto
   ,[Rab1] = @Param_Rab1
   ,[Rab2] = @Param_Rab2
   ,[EkNe] = @Param_EkNe
   ,[OldMLohn] = @Param_OldMLohn
   ,[Faktor1] = @Param_Faktor1
   ,[Faktor2] = @Param_Faktor2
   ,[Faktor3] = @Param_Faktor3
   ,[ZSAbStart] = @Param_ZSAbStart
   ,[ZSNeutral] = @Param_ZSNeutral
   ,[ZSWieVor] = @Param_ZSWieVor
   ,[DrkFett] = @Param_DrkFett
   ,[DrkFF] = @Param_DrkFF
   ,[DrkLF] = @Param_DrkLF
   ,[KzMwst] = @Param_KzMwst
   ,[MengeVA] = @Param_MengeVA
   ,[Kst] = RTRIM(@Param_Kst)
   ,[Ktr] = RTRIM(@Param_Ktr)
   ,[EGruMat] = UPPER(RTRIM(@Param_EGruMat))
   ,[EGruLohn] = UPPER(RTRIM(@Param_EGruLohn))
   ,[SuSp] = UPPER(RTRIM(@Param_SuSp))
   ,[SuSpTyp] = @Param_SuSpTyp
   ,[AbsMenge] = @Param_AbsMenge
   ,[EPMat] = @Param_EPMat
   ,[EPLohn] = @Param_EPLohn
   ,[EPPos] = @Param_EPPos
   ,[GPPos] = @Param_GPPos
   ,[AbsGP] = @Param_AbsGP
   ,[ParentRef] = UPPER(RTRIM(@Param_ParentRef))
   ,[Expanded] = @Param_Expanded
   ,[Visible] = @Param_Visible
   ,[SuSpValid] = @Param_SuSpValid
   ,[Dropped] = @Param_Dropped
   ,[Saved] = @Param_Saved
   ,[EkSonstig] = @Param_EkSonstig
   ,[VkBedarf] = @Param_VkBedarf
   ,[CuGewicht] = @Param_CuGewicht
   ,[KGruppe] = @Param_KGruppe
   ,[FloskelJN] = @Param_FloskelJN
   ,[GAEBxOZ] = RTRIM(@Param_GAEBxOZ)
   ,[GAEBxTyp] = @Param_GAEBxTyp
   ,[GAEBxFrM] = @Param_GAEBxFrM
   ,[AlGewicht] = @Param_AlGewicht
   ,[SumBrutto] = @Param_SumBrutto
   ,[ZZGrpNr] = @Param_ZZGrpNr
   ,[ZZLfdNr] = @Param_ZZLfdNr
   ,[EPAufgl] = @Param_EPAufgl
   ,[EpAnteile] = RTRIM(@Param_EpAnteile)
   ,[VkAnt1] = @Param_VkAnt1
   ,[VkAnt2] = @Param_VkAnt2
   ,[VkAnt3] = @Param_VkAnt3
   ,[VkAnt4] = @Param_VkAnt4
   ,[Herkunft] = @Param_Herkunft
   ,[BuchGrp] = UPPER(RTRIM(@Param_BuchGrp))
   ,[BezugAuf] = UPPER(RTRIM(@Param_BezugAuf))
   ,[BezugBis] = UPPER(RTRIM(@Param_BezugBis))
   ,[Flags] = @Param_Flags
   ,[VkAnt5] = @Param_VkAnt5
   ,[VkAnt6] = @Param_VkAnt6
   ,[CVkAnt1] = @Param_CVkAnt1
   ,[CVkAnt2] = @Param_CVkAnt2
   ,[CVkAnt3] = @Param_CVkAnt3
   ,[CVkAnt4] = @Param_CVkAnt4
   ,[CVkAnt5] = @Param_CVkAnt5
   ,[CVkAnt6] = @Param_CVkAnt6
   ,[EkAnteile] = RTRIM(@Param_EkAnteile)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelPos')
  DROP PROCEDURE [dbo].[up_Upd2_BelPos]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelPos] @Param_RecId varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_LfdNr varchar(6)
  ,@Param_Ebene varchar(1)
  ,@Param_RefKey varchar(6)
  ,@Param_SatzArt varchar(2)
  ,@Param_Nachtrag varchar(1)
  ,@Param_Bedarf varchar(1)
  ,@Param_Alternativ varchar(1)
  ,@Param_Entfaellt varchar(1)
  ,@Param_Fixed varchar(1)
  ,@Param_Marked varchar(1)
  ,@Param_Fremd varchar(1)
  ,@Param_Vk2JN varchar(1)
  ,@Param_LohnJN varchar(1)
  ,@Param_MatJN varchar(1)
  ,@Param_PosNr varchar(20)
  ,@Param_ArtNr varchar(20)
  ,@Param_Ktxt1 varchar(50)
  ,@Param_Menge varchar(12)
  ,@Param_VkMat varchar(14)
  ,@Param_VkLohn varchar(14)
  ,@Param_VkFremd varchar(14)
  ,@Param_EkMat varchar(14)
  ,@Param_EkLohn varchar(14)
  ,@Param_EkFremd varchar(14)
  ,@Param_RWegZube varchar(1)
  ,@Param_RWegNe varchar(1)
  ,@Param_NeFlag varchar(1)
  ,@Param_EkZube varchar(14)
  ,@Param_MittelLohn varchar(6)
  ,@Param_RabattDM varchar(14)
  ,@Param_RabattVH varchar(6)
  ,@Param_Zeit varchar(10)
  ,@Param_Aufwand varchar(10)
  ,@Param_Me varchar(6)
  ,@Param_Pe varchar(2)
  ,@Param_Gebinde varchar(12)
  ,@Param_AufLohn varchar(8)
  ,@Param_AufMat varchar(8)
  ,@Param_Ktxt2 varchar(50)
  ,@Param_Ktxt3 varchar(50)
  ,@Param_KzDrk1 varchar(1)
  ,@Param_KzDrk2 varchar(1)
  ,@Param_TxtNr varchar(20)
  ,@Param_DrkIndi varchar(1)
  ,@Param_DrkMenge varchar(1)
  ,@Param_DrkEP varchar(1)
  ,@Param_DrkGP varchar(1)
  ,@Param_DrkLM varchar(1)
  ,@Param_DrkUpos varchar(1)
  ,@Param_DrkUposPr varchar(1)
  ,@Param_DrkUposAbs varchar(1)
  ,@Param_Vk2Lohn varchar(14)
  ,@Param_Vk2Mat varchar(14)
  ,@Param_Brutto varchar(14)
  ,@Param_Rab1 varchar(6)
  ,@Param_Rab2 varchar(6)
  ,@Param_EkNe varchar(14)
  ,@Param_OldMLohn varchar(1)
  ,@Param_Faktor1 varchar(4)
  ,@Param_Faktor2 varchar(5)
  ,@Param_Faktor3 varchar(5)
  ,@Param_ZSAbStart varchar(1)
  ,@Param_ZSNeutral varchar(1)
  ,@Param_ZSWieVor varchar(1)
  ,@Param_DrkFett varchar(1)
  ,@Param_DrkFF varchar(1)
  ,@Param_DrkLF varchar(1)
  ,@Param_KzMwst varchar(1)
  ,@Param_MengeVA varchar(12)
  ,@Param_Kst varchar(20)
  ,@Param_Ktr varchar(20)
  ,@Param_EGruMat varchar(3)
  ,@Param_EGruLohn varchar(3)
  ,@Param_SuSp varchar(20)
  ,@Param_SuSpTyp varchar(2)
  ,@Param_AbsMenge varchar(12)
  ,@Param_EPMat varchar(14)
  ,@Param_EPLohn varchar(14)
  ,@Param_EPPos varchar(14)
  ,@Param_GPPos varchar(14)
  ,@Param_AbsGP varchar(14)
  ,@Param_ParentRef varchar(6)
  ,@Param_Expanded varchar(1)
  ,@Param_Visible varchar(1)
  ,@Param_SuSpValid varchar(1)
  ,@Param_Dropped varchar(1)
  ,@Param_Saved varchar(1)
  ,@Param_EkSonstig varchar(14)
  ,@Param_VkBedarf varchar(14)
  ,@Param_CuGewicht varchar(12)
  ,@Param_KGruppe varchar(2)
  ,@Param_FloskelJN varchar(1)
  ,@Param_GAEBxOZ varchar(20)
  ,@Param_GAEBxTyp varchar(1)
  ,@Param_GAEBxFrM varchar(1)
  ,@Param_AlGewicht varchar(12)
  ,@Param_SumBrutto varchar(14)
  ,@Param_ZZGrpNr varchar(3)
  ,@Param_ZZLfdNr varchar(1)
  ,@Param_EPAufgl varchar(1)
  ,@Param_EpAnteile varchar(18)
  ,@Param_VkAnt1 varchar(14)
  ,@Param_VkAnt2 varchar(14)
  ,@Param_VkAnt3 varchar(14)
  ,@Param_VkAnt4 varchar(14)
  ,@Param_Herkunft varchar(2)
  ,@Param_BuchGrp varchar(10)
  ,@Param_BezugAuf varchar(6)
  ,@Param_BezugBis varchar(6)
  ,@Param_Flags varchar(8)
  ,@Param_VkAnt5 varchar(14)
  ,@Param_VkAnt6 varchar(14)
  ,@Param_CVkAnt1 varchar(14)
  ,@Param_CVkAnt2 varchar(14)
  ,@Param_CVkAnt3 varchar(14)
  ,@Param_CVkAnt4 varchar(14)
  ,@Param_CVkAnt5 varchar(14)
  ,@Param_CVkAnt6 varchar(14)
  ,@Param_EkAnteile varchar(168)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelPos] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Ebene] = dbo.itcfunc_integer(@Param_Ebene)
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[SatzArt] = dbo.itcfunc_integer(@Param_SatzArt)
   ,[Nachtrag] = dbo.itcfunc_integer(@Param_Nachtrag)
   ,[Bedarf] = dbo.itcfunc_integer(@Param_Bedarf)
   ,[Alternativ] = dbo.itcfunc_integer(@Param_Alternativ)
   ,[Entfaellt] = dbo.itcfunc_integer(@Param_Entfaellt)
   ,[Fixed] = dbo.itcfunc_integer(@Param_Fixed)
   ,[Marked] = dbo.itcfunc_integer(@Param_Marked)
   ,[Fremd] = dbo.itcfunc_integer(@Param_Fremd)
   ,[Vk2JN] = dbo.itcfunc_integer(@Param_Vk2JN)
   ,[LohnJN] = dbo.itcfunc_integer(@Param_LohnJN)
   ,[MatJN] = dbo.itcfunc_integer(@Param_MatJN)
   ,[PosNr] = UPPER(RTRIM(@Param_PosNr))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[Ktxt1] = RTRIM(@Param_Ktxt1)
   ,[Menge] = dbo.itcfunc_float(@Param_Menge)
   ,[VkMat] = dbo.itcfunc_float(@Param_VkMat)
   ,[VkLohn] = dbo.itcfunc_float(@Param_VkLohn)
   ,[VkFremd] = dbo.itcfunc_float(@Param_VkFremd)
   ,[EkMat] = dbo.itcfunc_float(@Param_EkMat)
   ,[EkLohn] = dbo.itcfunc_float(@Param_EkLohn)
   ,[EkFremd] = dbo.itcfunc_float(@Param_EkFremd)
   ,[RWegZube] = dbo.itcfunc_integer(@Param_RWegZube)
   ,[RWegNe] = dbo.itcfunc_integer(@Param_RWegNe)
   ,[NeFlag] = dbo.itcfunc_integer(@Param_NeFlag)
   ,[EkZube] = dbo.itcfunc_float(@Param_EkZube)
   ,[MittelLohn] = dbo.itcfunc_float(@Param_MittelLohn)
   ,[RabattDM] = dbo.itcfunc_float(@Param_RabattDM)
   ,[RabattVH] = dbo.itcfunc_float(@Param_RabattVH)
   ,[Zeit] = dbo.itcfunc_float(@Param_Zeit)
   ,[Aufwand] = dbo.itcfunc_float(@Param_Aufwand)
   ,[Me] = RTRIM(@Param_Me)
   ,[Pe] = UPPER(RTRIM(@Param_Pe))
   ,[Gebinde] = dbo.itcfunc_float(@Param_Gebinde)
   ,[AufLohn] = dbo.itcfunc_float(@Param_AufLohn)
   ,[AufMat] = dbo.itcfunc_float(@Param_AufMat)
   ,[Ktxt2] = RTRIM(@Param_Ktxt2)
   ,[Ktxt3] = RTRIM(@Param_Ktxt3)
   ,[KzDrk1] = dbo.itcfunc_integer(@Param_KzDrk1)
   ,[KzDrk2] = dbo.itcfunc_integer(@Param_KzDrk2)
   ,[TxtNr] = UPPER(RTRIM(@Param_TxtNr))
   ,[DrkIndi] = dbo.itcfunc_integer(@Param_DrkIndi)
   ,[DrkMenge] = dbo.itcfunc_integer(@Param_DrkMenge)
   ,[DrkEP] = dbo.itcfunc_integer(@Param_DrkEP)
   ,[DrkGP] = dbo.itcfunc_integer(@Param_DrkGP)
   ,[DrkLM] = dbo.itcfunc_integer(@Param_DrkLM)
   ,[DrkUpos] = dbo.itcfunc_integer(@Param_DrkUpos)
   ,[DrkUposPr] = dbo.itcfunc_integer(@Param_DrkUposPr)
   ,[DrkUposAbs] = dbo.itcfunc_integer(@Param_DrkUposAbs)
   ,[Vk2Lohn] = dbo.itcfunc_float(@Param_Vk2Lohn)
   ,[Vk2Mat] = dbo.itcfunc_float(@Param_Vk2Mat)
   ,[Brutto] = dbo.itcfunc_float(@Param_Brutto)
   ,[Rab1] = dbo.itcfunc_float(@Param_Rab1)
   ,[Rab2] = dbo.itcfunc_float(@Param_Rab2)
   ,[EkNe] = dbo.itcfunc_float(@Param_EkNe)
   ,[OldMLohn] = dbo.itcfunc_integer(@Param_OldMLohn)
   ,[Faktor1] = dbo.itcfunc_float(@Param_Faktor1)
   ,[Faktor2] = dbo.itcfunc_float(@Param_Faktor2)
   ,[Faktor3] = dbo.itcfunc_float(@Param_Faktor3)
   ,[ZSAbStart] = dbo.itcfunc_integer(@Param_ZSAbStart)
   ,[ZSNeutral] = dbo.itcfunc_integer(@Param_ZSNeutral)
   ,[ZSWieVor] = dbo.itcfunc_integer(@Param_ZSWieVor)
   ,[DrkFett] = dbo.itcfunc_integer(@Param_DrkFett)
   ,[DrkFF] = dbo.itcfunc_integer(@Param_DrkFF)
   ,[DrkLF] = dbo.itcfunc_integer(@Param_DrkLF)
   ,[KzMwst] = dbo.itcfunc_integer(@Param_KzMwst)
   ,[MengeVA] = dbo.itcfunc_float(@Param_MengeVA)
   ,[Kst] = RTRIM(@Param_Kst)
   ,[Ktr] = RTRIM(@Param_Ktr)
   ,[EGruMat] = UPPER(RTRIM(@Param_EGruMat))
   ,[EGruLohn] = UPPER(RTRIM(@Param_EGruLohn))
   ,[SuSp] = UPPER(RTRIM(@Param_SuSp))
   ,[SuSpTyp] = dbo.itcfunc_integer(@Param_SuSpTyp)
   ,[AbsMenge] = dbo.itcfunc_float(@Param_AbsMenge)
   ,[EPMat] = dbo.itcfunc_float(@Param_EPMat)
   ,[EPLohn] = dbo.itcfunc_float(@Param_EPLohn)
   ,[EPPos] = dbo.itcfunc_float(@Param_EPPos)
   ,[GPPos] = dbo.itcfunc_float(@Param_GPPos)
   ,[AbsGP] = dbo.itcfunc_float(@Param_AbsGP)
   ,[ParentRef] = UPPER(RTRIM(@Param_ParentRef))
   ,[Expanded] = dbo.itcfunc_integer(@Param_Expanded)
   ,[Visible] = dbo.itcfunc_integer(@Param_Visible)
   ,[SuSpValid] = dbo.itcfunc_integer(@Param_SuSpValid)
   ,[Dropped] = dbo.itcfunc_integer(@Param_Dropped)
   ,[Saved] = dbo.itcfunc_integer(@Param_Saved)
   ,[EkSonstig] = dbo.itcfunc_float(@Param_EkSonstig)
   ,[VkBedarf] = dbo.itcfunc_float(@Param_VkBedarf)
   ,[CuGewicht] = dbo.itcfunc_float(@Param_CuGewicht)
   ,[KGruppe] = dbo.itcfunc_integer(@Param_KGruppe)
   ,[FloskelJN] = dbo.itcfunc_integer(@Param_FloskelJN)
   ,[GAEBxOZ] = RTRIM(@Param_GAEBxOZ)
   ,[GAEBxTyp] = dbo.itcfunc_integer(@Param_GAEBxTyp)
   ,[GAEBxFrM] = dbo.itcfunc_integer(@Param_GAEBxFrM)
   ,[AlGewicht] = dbo.itcfunc_float(@Param_AlGewicht)
   ,[SumBrutto] = dbo.itcfunc_float(@Param_SumBrutto)
   ,[ZZGrpNr] = dbo.itcfunc_integer(@Param_ZZGrpNr)
   ,[ZZLfdNr] = dbo.itcfunc_integer(@Param_ZZLfdNr)
   ,[EPAufgl] = dbo.itcfunc_integer(@Param_EPAufgl)
   ,[EpAnteile] = RTRIM(@Param_EpAnteile)
   ,[VkAnt1] = dbo.itcfunc_float(@Param_VkAnt1)
   ,[VkAnt2] = dbo.itcfunc_float(@Param_VkAnt2)
   ,[VkAnt3] = dbo.itcfunc_float(@Param_VkAnt3)
   ,[VkAnt4] = dbo.itcfunc_float(@Param_VkAnt4)
   ,[Herkunft] = dbo.itcfunc_integer(@Param_Herkunft)
   ,[BuchGrp] = UPPER(RTRIM(@Param_BuchGrp))
   ,[BezugAuf] = UPPER(RTRIM(@Param_BezugAuf))
   ,[BezugBis] = UPPER(RTRIM(@Param_BezugBis))
   ,[Flags] = dbo.itcfunc_integer(@Param_Flags)
   ,[VkAnt5] = dbo.itcfunc_float(@Param_VkAnt5)
   ,[VkAnt6] = dbo.itcfunc_float(@Param_VkAnt6)
   ,[CVkAnt1] = dbo.itcfunc_float(@Param_CVkAnt1)
   ,[CVkAnt2] = dbo.itcfunc_float(@Param_CVkAnt2)
   ,[CVkAnt3] = dbo.itcfunc_float(@Param_CVkAnt3)
   ,[CVkAnt4] = dbo.itcfunc_float(@Param_CVkAnt4)
   ,[CVkAnt5] = dbo.itcfunc_float(@Param_CVkAnt5)
   ,[CVkAnt6] = dbo.itcfunc_float(@Param_CVkAnt6)
   ,[EkAnteile] = RTRIM(@Param_EkAnteile)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelPos] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BelPos')
  DROP PROCEDURE [dbo].[up_TrSel_BelPos]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelPos]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([LfdNr]))
   +LTRIM(RTRIM([RefKey]))
   +LTRIM(RTRIM([PosNr]))
   +LTRIM(RTRIM([ArtNr]))
   +LTRIM(RTRIM([Ktxt1]))
   +LTRIM(RTRIM([Me]))
   +LTRIM(RTRIM([Pe]))
   +LTRIM(RTRIM([Ktxt2]))
   +LTRIM(RTRIM([Ktxt3]))
   +LTRIM(RTRIM([TxtNr]))
   +LTRIM(RTRIM([Kst]))
   +LTRIM(RTRIM([Ktr]))
   +LTRIM(RTRIM([EGruMat]))
   +LTRIM(RTRIM([EGruLohn]))
   +LTRIM(RTRIM([SuSp]))
   +LTRIM(RTRIM([ParentRef]))
   +LTRIM(RTRIM([GAEBxOZ]))
   +LTRIM(RTRIM([EpAnteile]))
   +LTRIM(RTRIM([BuchGrp]))
   +LTRIM(RTRIM([BezugAuf]))
   +LTRIM(RTRIM([BezugBis]))
   +LTRIM(RTRIM([EkAnteile]))
  AS Data FROM [dbo].[ut_BelPos] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key00')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 0 = Dropped ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key00] AS EXECUTE [dbo].[up_First_BelPos_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 0 = Dropped ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key00] AS EXECUTE [dbo].[up_Last_BelPos_Key00]')
GO

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00)
    WHERE ((PrjNr > @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,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_BelPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00)
    WHERE ((PrjNr < @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelPos_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key00] @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 @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

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

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

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key00] @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 @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key01')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 0 = Dropped ORDER BY PrjNr,BelArt,BelNr,PosNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key01] AS EXECUTE [dbo].[up_First_BelPos_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 0 = Dropped ORDER BY PrjNr desc,BelArt desc,BelNr desc,PosNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key01] AS EXECUTE [dbo].[up_Last_BelPos_Key01]')
GO

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr > @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,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_BelPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key01] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (PosNr = @oldPosNr) AND (RecId < @oldRecId)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt 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_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (PosNr < @oldPosNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt 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_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt 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_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt 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_BelPos] WITH(INDEX = Ik_Key01)
    WHERE ((PrjNr < @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt 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_BelPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key01] @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 @mPosNr 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_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,PosNr,RecId
    RETURN
  END

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,PosNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,PosNr,RecId
    RETURN
  END

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

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

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

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

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      PosNr >= @mPosNr AND SUBSTRING(PosNr,1,LEN(@mPosNr)) = @mPosNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,PosNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key01] @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 @mPosNr 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_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      PosNr = @mPosNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,PosNr 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_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,PosNr desc,RecId desc
    RETURN
  END

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

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

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key01) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(PosNr,1,LEN(@mPosNr)) = @mPosNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt 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_BelPos_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPos_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPos_Key01] @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_BelPos' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  DROP INDEX [Ik_Key02] ON [dbo].[ut_BelPos]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPos' and so.[id] = si.[id] and si.[name] = 'Ik_Key02')
  CREATE INDEX [Ik_Key02] ON [dbo].[ut_BelPos] (PrjNr,BelArt,BelNr,ArtNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02) WHERE 0 = Dropped ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key02] AS EXECUTE [dbo].[up_First_BelPos_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02) WHERE 0 = Dropped 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_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key02] AS EXECUTE [dbo].[up_Last_BelPos_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key02] @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_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (RecId > @oldRecId)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr > @oldArtNr)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr > @oldPrjNr)) AND (0 = Dropped)
    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_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key02] @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_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (RecId < @oldRecId)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr < @oldArtNr)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND (0 = Dropped)
    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_BelPos] WITH(INDEX = Ik_Key02)
    WHERE ((PrjNr < @oldPrjNr)) AND (0 = Dropped)
    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_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key02] @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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

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

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

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND (0 = Dropped)
      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_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPos_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPos_Key02] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key02')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key02] @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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND (0 = Dropped)
      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_BelPos] WITH(INDEX = Ik_Key02) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND (0 = Dropped)
      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_BelPos_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPos_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPos_Key02] @xKey, @xLen')
GO


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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,RefKey,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_BelPos_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key03] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey 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_BelPos_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key03')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key03] @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 @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

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

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

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

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

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

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

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,RefKey,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key03] @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 @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,RecId desc
    RETURN
  END

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

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

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

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

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key03) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RefKey desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key04')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key04]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE (0 = Dropped) AND (1 = Visible) ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key04] AS EXECUTE [dbo].[up_First_BelPos_Key04]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key04')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key04]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key04] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE (0 = Dropped) AND (1 = Visible) ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key04] AS EXECUTE [dbo].[up_Last_BelPos_Key04]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND ((0 = Dropped) AND (1 = Visible))
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04)
    WHERE ((PrjNr > @oldPrjNr)) AND ((0 = Dropped) AND (1 = Visible))
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,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_BelPos_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key04] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND ((0 = Dropped) AND (1 = Visible))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04)
    WHERE ((PrjNr < @oldPrjNr)) AND ((0 = Dropped) AND (1 = Visible))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelPos_Key04')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key04] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key04] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key04')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key04] @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 @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key04')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key04]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key04] @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 @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)

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

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key04) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((0 = Dropped) AND (1 = Visible))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 0 = Dropped ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key05] AS EXECUTE [dbo].[up_First_BelPos_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key05] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 0 = Dropped ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key05] AS EXECUTE [dbo].[up_Last_BelPos_Key05]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key05] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldParentRef char(6)
  DECLARE @oldLfdNr char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldParentRef = ParentRef, @oldLfdNr = LfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ParentRef = @oldParentRef) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ParentRef = @oldParentRef) AND (LfdNr > @oldLfdNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr > @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,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_BelPos_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key05] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key05] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldParentRef char(6)
  DECLARE @oldLfdNr char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldParentRef = ParentRef, @oldLfdNr = LfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ParentRef = @oldParentRef) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ParentRef = @oldParentRef) AND (LfdNr < @oldLfdNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05)
    WHERE ((PrjNr < @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr 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_BelPos_Key05')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key05] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key05] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key05] @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 @mParentRef char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,6)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef AND 
      LfdNr = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef >= @mParentRef AND SUBSTRING(ParentRef,1,LEN(@mParentRef)) = @mParentRef
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ParentRef,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key05')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key05]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key05] @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 @mParentRef char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,6)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,25,6)

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

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef AND 
      LfdNr = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr 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_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ParentRef,1,LEN(@mParentRef)) = @mParentRef
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key05) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ParentRef = @mParentRef AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ParentRef desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key06')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key06]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key06) WHERE 0 = Saved ORDER BY PrjNr,BelArt,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key06] AS EXECUTE [dbo].[up_First_BelPos_Key06]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key06')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key06]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key06] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key06) WHERE 0 = Saved ORDER BY PrjNr desc,BelArt desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key06] AS EXECUTE [dbo].[up_Last_BelPos_Key06]')
GO

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

  DECLARE @newRecId integer

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key06')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key06] @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)

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Saved)
      ORDER BY PrjNr,BelArt,BelNr,RecId
    RETURN
  END

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key06')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key06]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key06] @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)

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key06) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Saved)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key06) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Saved)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

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

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

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_BelPos_Key06')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPos_Key06] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPos_Key06] @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_BelPos' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  DROP INDEX [Ik_Key07] ON [dbo].[ut_BelPos]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelPos' and so.[id] = si.[id] and si.[name] = 'Ik_Key07')
  CREATE INDEX [Ik_Key07] ON [dbo].[ut_BelPos] (PrjNr,BelArt,BelNr,ArtNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE (0 = Dropped) AND (SatzArt = 17) ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key07] AS EXECUTE [dbo].[up_First_BelPos_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key07] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE (0 = Dropped) AND (SatzArt = 17) 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_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key07] AS EXECUTE [dbo].[up_Last_BelPos_Key07]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key07] @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_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (RecId > @oldRecId)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr > @oldArtNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr > @oldPrjNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key07] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key07] @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_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (RecId < @oldRecId)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr < @oldArtNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos] WITH(INDEX = Ik_Key07)
    WHERE ((PrjNr < @oldPrjNr)) AND ((0 = Dropped) AND (SatzArt = 17))
    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_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key07] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key07] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key07] @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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelPos_Key07] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelPos_Key07] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key07')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key07]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key07] @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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos] WITH(INDEX = Ik_Key07) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND (SatzArt = 17))
      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_BelPos_Key07')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelPos_Key07] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelPos_Key07] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key08')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key08]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) ORDER BY PrjNr,RefKey,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key08] AS EXECUTE [dbo].[up_First_BelPos_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key08')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key08]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key08] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) ORDER BY PrjNr desc,RefKey desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key08] AS EXECUTE [dbo].[up_Last_BelPos_Key08]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key08')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key08]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key08] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldRefKey char(6)
  SELECT @oldPrjNr = PrjNr, @oldRefKey = RefKey FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr = @oldPrjNr) AND (RefKey = @oldRefKey) AND (RecId > @oldRecId)
    ORDER BY PrjNr,RefKey,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr = @oldPrjNr) AND (RefKey > @oldRefKey)
    ORDER BY PrjNr,RefKey,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,RefKey,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_BelPos_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key08] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key08')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key08]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key08] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldRefKey char(6)
  SELECT @oldPrjNr = PrjNr, @oldRefKey = RefKey FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr = @oldPrjNr) AND (RefKey = @oldRefKey) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,RefKey desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr = @oldPrjNr) AND (RefKey < @oldRefKey)
    ORDER BY PrjNr desc,RefKey desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,RefKey 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_BelPos_Key08')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key08] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key08] @oldRecId')
GO

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

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

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

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

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) WHERE 
      PrjNr = @mPrjNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,RefKey,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,RefKey,RecId
    RETURN
  END

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

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

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) WHERE 
      PrjNr = @mPrjNr AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,RefKey,RecId
    RETURN
  END

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

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

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

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) WHERE 
      PrjNr = @mPrjNr AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,RefKey desc,RecId desc
    RETURN
  END

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

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key08) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,RefKey desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key09')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key09]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE (BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)) ORDER BY ArtNr,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key09] AS EXECUTE [dbo].[up_First_BelPos_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key09')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key09]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key09] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE (BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)) ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key09] AS EXECUTE [dbo].[up_Last_BelPos_Key09]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key09')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key09]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key09] @oldRecId integer AS
  DECLARE @oldArtNr char(20)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr char(6)
  SELECT @oldArtNr = ArtNr, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (LfdNr > @oldLfdNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr > @oldBelNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr > @oldArtNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr,BelNr,LfdNr,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_BelPos_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key09] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key09')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key09]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key09] @oldRecId integer AS
  DECLARE @oldArtNr char(20)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr char(6)
  SELECT @oldArtNr = ArtNr, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (LfdNr < @oldLfdNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr < @oldBelNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09)
    WHERE ((ArtNr < @oldArtNr)) AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY ArtNr desc,BelNr desc,LfdNr 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_BelPos_Key09')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key09] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key09] @oldRecId')
GO

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

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr,BelNr,LfdNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr 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_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key09) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((BelArt >= ' 1') AND (BelArt <= ' 4') AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY ArtNr desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE (0 = Dropped) AND (SatzArt = 7) ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key10] AS EXECUTE [dbo].[up_First_BelPos_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE (0 = Dropped) AND (SatzArt = 7) ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key10] AS EXECUTE [dbo].[up_Last_BelPos_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key10] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldKGruppe integer
  DECLARE @oldParentRef char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldKGruppe = KGruppe, @oldParentRef = ParentRef FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe = @oldKGruppe) AND (ParentRef = @oldParentRef) AND (RecId > @oldRecId)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe = @oldKGruppe) AND (ParentRef > @oldParentRef)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe > @oldKGruppe)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr > @oldPrjNr)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,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_BelPos_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key10] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldKGruppe integer
  DECLARE @oldParentRef char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldKGruppe = KGruppe, @oldParentRef = ParentRef FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe = @oldKGruppe) AND (ParentRef = @oldParentRef) AND (RecId < @oldRecId)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe = @oldKGruppe) AND (ParentRef < @oldParentRef)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (KGruppe < @oldKGruppe)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10)
    WHERE ((PrjNr < @oldPrjNr)) AND ((0 = Dropped) AND (SatzArt = 7))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef 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_BelPos_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key10] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key10] @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 @mKGruppe integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,2)
  DECLARE @mParentRef char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,6)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe AND 
      ParentRef = @mParentRef
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END


  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe AND 
      ParentRef >= @mParentRef AND SUBSTRING(ParentRef,1,LEN(@mParentRef)) = @mParentRef
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr,BelArt,BelNr,KGruppe,ParentRef,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key10')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key10] @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 @mKGruppe integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,2)
  DECLARE @mParentRef char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,6)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe AND 
      ParentRef = @mParentRef
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef 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_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END


  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key10) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      KGruppe = @mKGruppe AND 
      SUBSTRING(ParentRef,1,LEN(@mParentRef)) = @mParentRef
      AND ((0 = Dropped) AND (SatzArt = 7))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,KGruppe desc,ParentRef desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key11')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE (0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4') ORDER BY ArtNr,BelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key11] AS EXECUTE [dbo].[up_First_BelPos_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key11')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE (0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4') ORDER BY ArtNr desc,BelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key11] AS EXECUTE [dbo].[up_Last_BelPos_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key11')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key11]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key11] @oldRecId integer AS
  DECLARE @oldArtNr char(20)
  DECLARE @oldBelNr char(8)
  SELECT @oldArtNr = ArtNr, @oldBelNr = BelNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (RecId > @oldRecId)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr > @oldBelNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr > @oldArtNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key11')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key11]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key11] @oldRecId integer AS
  DECLARE @oldArtNr char(20)
  DECLARE @oldBelNr char(8)
  SELECT @oldArtNr = ArtNr, @oldBelNr = BelNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr = @oldBelNr) AND (RecId < @oldRecId)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr = @oldArtNr) AND (BelNr < @oldBelNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11)
    WHERE ((ArtNr < @oldArtNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
    ORDER BY ArtNr desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr,BelNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr,BelNr,RecId
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr,BelNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key11) WHERE 
      ArtNr = @mArtNr AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) AND (BelArt >= ' 1') AND (BelArt <= ' 4'))
      ORDER BY ArtNr desc,BelNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key12')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE (0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key12] AS EXECUTE [dbo].[up_First_BelPos_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key12')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE (0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)) ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key12] AS EXECUTE [dbo].[up_Last_BelPos_Key12]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (LfdNr > @oldLfdNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr > @oldArtNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr > @oldPrjNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,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_BelPos_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key12] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (LfdNr < @oldLfdNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr < @oldArtNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12)
    WHERE ((PrjNr < @oldPrjNr)) AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr 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_BelPos_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key12] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key12')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key12] @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)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,6)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      LfdNr = @mLfdNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key12')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key12] @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)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,39,6)

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

  IF @xLen = 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      LfdNr = @mLfdNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr 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_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 44 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key12) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      AND ((0 = Dropped) AND ((SatzArt = 7) OR (SatzArt = 8)))
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_First_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_BelPos_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 0 = Dropped ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelPos_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelPos_Key13] AS EXECUTE [dbo].[up_First_BelPos_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_Last_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_BelPos_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 0 = Dropped ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelPos_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelPos_Key13] AS EXECUTE [dbo].[up_Last_BelPos_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_Next_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_BelPos_Key13] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldArtNr char(20)
  DECLARE @oldZZGrpNr integer
  DECLARE @oldZZLfdNr integer
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldArtNr = ArtNr, @oldZZGrpNr = ZZGrpNr, @oldZZLfdNr = ZZLfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr = @oldZZGrpNr) AND (ZZLfdNr = @oldZZLfdNr) AND (RecId > @oldRecId)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr = @oldZZGrpNr) AND (ZZLfdNr > @oldZZLfdNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr > @oldZZGrpNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr > @oldArtNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr > @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,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_BelPos_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelPos_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelPos_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_Prior_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelPos_Key13] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldArtNr char(20)
  DECLARE @oldZZGrpNr integer
  DECLARE @oldZZLfdNr integer
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldArtNr = ArtNr, @oldZZGrpNr = ZZGrpNr, @oldZZLfdNr = ZZLfdNr FROM [dbo].[ut_BelPos] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr = @oldZZGrpNr) AND (ZZLfdNr = @oldZZLfdNr) AND (RecId < @oldRecId)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr = @oldZZGrpNr) AND (ZZLfdNr < @oldZZLfdNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr = @oldArtNr) AND (ZZGrpNr < @oldZZGrpNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (ArtNr < @oldArtNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13)
    WHERE ((PrjNr < @oldPrjNr)) AND (0 = Dropped)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr 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_BelPos_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelPos_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelPos_Key13] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_FindF_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelPos_Key13] @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)
  DECLARE @mZZGrpNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,39,3)
  DECLARE @mZZLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,42,1)

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

  IF @xLen = 42 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      ZZGrpNr = @mZZGrpNr AND 
      ZZLfdNr = @mZZLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      ZZGrpNr = @mZZGrpNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND (0 = Dropped)
      ORDER BY PrjNr,BelArt,BelNr,ArtNr,ZZGrpNr,ZZLfdNr,RecId
    RETURN
  END



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelPos_Key13')
  DROP PROCEDURE [dbo].[up_FindL_BelPos_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelPos_Key13] @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)
  DECLARE @mZZGrpNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,39,3)
  DECLARE @mZZLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,42,1)

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

  IF @xLen = 42 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      ZZGrpNr = @mZZGrpNr AND 
      ZZLfdNr = @mZZLfdNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 41 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr AND 
      ZZGrpNr = @mZZGrpNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      ArtNr = @mArtNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr 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_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 38 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelPos] WITH(INDEX = Ik_Key13) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      AND (0 = Dropped)
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,ArtNr desc,ZZGrpNr desc,ZZLfdNr desc,RecId desc
    RETURN
  END



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


