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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelAdresse')
BEGIN
CREATE TABLE [dbo].[ut_BelAdresse]
(
  [RecId]  integer NOT NULL
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[LfdNr] char(1) DEFAULT ''
 ,[DestDebNr] char(15) DEFAULT ''
 ,[DestTyp] integer DEFAULT 0
 ,[Suchen] char(40) DEFAULT ''
 ,[Anrede] varchar(50) DEFAULT ''
 ,[Vorname] varchar(50) DEFAULT ''
 ,[Name1] varchar(50) DEFAULT ''
 ,[Name2] varchar(50) DEFAULT ''
 ,[Branche] varchar(50) DEFAULT ''
 ,[Strasse] varchar(50) DEFAULT ''
 ,[Ort] varchar(50) DEFAULT ''
 ,[Plz] char(8) DEFAULT ''
 ,[PlzSub] char(2) DEFAULT ''
 ,[zuHaenden] varchar(50) DEFAULT ''
 ,[BrAnrede] varchar(50) DEFAULT ''
 ,[BrGruss] varchar(50) DEFAULT ''
 ,[Telefon1] varchar(20) DEFAULT ''
 ,[Telefon2] varchar(20) DEFAULT ''
 ,[FaxNummer] varchar(20) DEFAULT ''
 ,[EMail] varchar(80) DEFAULT ''
 ,[FibuKonto] char(15) DEFAULT ''
 ,[UstId1] char(15) DEFAULT ''
 ,[Land] varchar(10) DEFAULT ''
 ,[LandName] varchar(50) DEFAULT ''
 ,[PeppolAddr] varchar(40) DEFAULT ''
  CONSTRAINT [Pk_BelAdresse]  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_BelAdresse_KeyDel')
  DROP TRIGGER [dbo].[tr_BelAdresse_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelAdresse_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelAdresse_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelAdresse_Keys')
  DROP TABLE [dbo].[ut_BelAdresse_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_BelAdresse' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [LfdNr] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestDebNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [DestDebNr] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [DestTyp] 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_BelAdresse' and sc.[name] = 'Suchen')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Suchen] char(40) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Anrede')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Anrede] 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_BelAdresse' and sc.[name] = 'Vorname')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Vorname] 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_BelAdresse' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Name1] 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_BelAdresse' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Name2] 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_BelAdresse' and sc.[name] = 'Branche')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Branche] 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_BelAdresse' and sc.[name] = 'Strasse')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Strasse] 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_BelAdresse' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Ort] 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_BelAdresse' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Plz] 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_BelAdresse' and sc.[name] = 'PlzSub')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [PlzSub] 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_BelAdresse' and sc.[name] = 'zuHaenden')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [zuHaenden] 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_BelAdresse' and sc.[name] = 'BrAnrede')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [BrAnrede] 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_BelAdresse' and sc.[name] = 'BrGruss')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [BrGruss] 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_BelAdresse' and sc.[name] = 'Telefon1')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Telefon1] 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_BelAdresse' and sc.[name] = 'Telefon2')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Telefon2] 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_BelAdresse' and sc.[name] = 'FaxNummer')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [FaxNummer] 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_BelAdresse' and sc.[name] = 'EMail')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [EMail] varchar(80) 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_BelAdresse' and sc.[name] = 'FibuKonto')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [FibuKonto] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'UstId1')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [UstId1] char(15) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Land')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [Land] varchar(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_BelAdresse' and sc.[name] = 'LandName')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [LandName] 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_BelAdresse' and sc.[name] = 'PeppolAddr')
BEGIN
  ALTER TABLE [dbo].[ut_BelAdresse] ADD [PeppolAddr] varchar(40) 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] 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_BelAdresse' 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_BelAdresse' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' 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_BelAdresse' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [LfdNr] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' 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_BelAdresse' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [LfdNr] varchar(1)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestDebNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestDebNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [DestDebNr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestDebNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'DestDebNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [DestDebNr] varchar(15)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Suchen'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Suchen] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Suchen'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Suchen] varchar(40)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Anrede'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Anrede'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Anrede] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Anrede'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Anrede'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Anrede] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Vorname'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Vorname'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Vorname] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Vorname'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Vorname'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Vorname] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Name1] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Name1] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Name2] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Name2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Name2] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Branche'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Branche'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Branche] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Branche'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Branche'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Branche] 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_BelAdresse' and sc.[name] = 'Strasse'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Strasse'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Strasse] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Strasse'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Strasse'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Strasse] 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_BelAdresse' and sc.[name] = 'Ort'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Ort'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Ort] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Ort'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Ort'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Ort] varchar(50)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Plz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Plz'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Plz] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Plz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Plz'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Plz] 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_BelAdresse' and sc.[name] = 'PlzSub'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PlzSub'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [PlzSub] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PlzSub'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PlzSub'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [PlzSub] 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_BelAdresse' and sc.[name] = 'zuHaenden'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'zuHaenden'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [zuHaenden] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'zuHaenden'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'zuHaenden'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [zuHaenden] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrAnrede'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrAnrede'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [BrAnrede] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrAnrede'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrAnrede'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [BrAnrede] 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_BelAdresse' and sc.[name] = 'BrGruss'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrGruss'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [BrGruss] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrGruss'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'BrGruss'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [BrGruss] 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_BelAdresse' and sc.[name] = 'Telefon1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Telefon1] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Telefon1] 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_BelAdresse' and sc.[name] = 'Telefon2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Telefon2] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Telefon2'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Telefon2] 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_BelAdresse' and sc.[name] = 'FaxNummer'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FaxNummer'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [FaxNummer] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FaxNummer'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FaxNummer'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [FaxNummer] varchar(20)
END
GO
IF (80 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'EMail'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'EMail'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [EMail] char(80)
END
GO
IF (80 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'EMail'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'EMail'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [EMail] varchar(80)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FibuKonto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FibuKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [FibuKonto] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FibuKonto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'FibuKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [FibuKonto] varchar(15)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'UstId1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'UstId1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [UstId1] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'UstId1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'UstId1'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [UstId1] varchar(15)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Land'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Land'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Land] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Land'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'Land'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [Land] varchar(10)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'LandName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'LandName'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [LandName] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'LandName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'LandName'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [LandName] varchar(50)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PeppolAddr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PeppolAddr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [PeppolAddr] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PeppolAddr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelAdresse' and sc.[name] = 'PeppolAddr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelAdresse] ALTER COLUMN [PeppolAddr] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelAdresse')
  DROP VIEW [dbo].[BelAdresse]
GO
CREATE VIEW [dbo].[BelAdresse] AS
  SELECT [RecId]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[LfdNr]
 ,[DestDebNr]
 ,[DestTyp]
 ,[Suchen]
 ,[Anrede]
 ,[Vorname]
 ,[Name1]
 ,[Name2]
 ,[Branche]
 ,[Strasse]
 ,[Ort]
 ,[Plz]
 ,[PlzSub]
 ,[zuHaenden]
 ,[BrAnrede]
 ,[BrGruss]
 ,[Telefon1]
 ,[Telefon2]
 ,[FaxNummer]
 ,[EMail]
 ,[FibuKonto]
 ,[UstId1]
 ,[Land]
 ,[LandName]
 ,[PeppolAddr]
  FROM [dbo].[ut_BelAdresse]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelAdresse')
  DROP PROCEDURE [dbo].[up_Sel_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelAdresse]  @mRI integer
AS
  SELECT
    [PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
   ,[DestDebNr]
   ,[DestTyp]
   ,[Suchen]
   ,[Anrede]
   ,[Vorname]
   ,[Name1]
   ,[Name2]
   ,[Branche]
   ,[Strasse]
   ,[Ort]
   ,[Plz]
   ,[PlzSub]
   ,[zuHaenden]
   ,[BrAnrede]
   ,[BrGruss]
   ,[Telefon1]
   ,[Telefon2]
   ,[FaxNummer]
   ,[EMail]
   ,[FibuKonto]
   ,[UstId1]
   ,[Land]
   ,[LandName]
   ,[PeppolAddr]
  FROM [dbo].[ut_BelAdresse] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelAdresse')
  DROP PROCEDURE [dbo].[up_Append_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Append_BelAdresse]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr char(1)
  ,@Param_DestDebNr char(15)
  ,@Param_DestTyp integer
  ,@Param_Suchen char(40)
  ,@Param_Anrede varchar(50)
  ,@Param_Vorname varchar(50)
  ,@Param_Name1 varchar(50)
  ,@Param_Name2 varchar(50)
  ,@Param_Branche varchar(50)
  ,@Param_Strasse varchar(50)
  ,@Param_Ort varchar(50)
  ,@Param_Plz char(8)
  ,@Param_PlzSub char(2)
  ,@Param_zuHaenden varchar(50)
  ,@Param_BrAnrede varchar(50)
  ,@Param_BrGruss varchar(50)
  ,@Param_Telefon1 varchar(20)
  ,@Param_Telefon2 varchar(20)
  ,@Param_FaxNummer varchar(20)
  ,@Param_EMail varchar(80)
  ,@Param_FibuKonto char(15)
  ,@Param_UstId1 char(15)
  ,@Param_Land varchar(10)
  ,@Param_LandName varchar(50)
  ,@Param_PeppolAddr varchar(40)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelAdresse()
      INSERT INTO [dbo].[ut_BelAdresse] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[LfdNr]
        ,[DestDebNr]
        ,[DestTyp]
        ,[Suchen]
        ,[Anrede]
        ,[Vorname]
        ,[Name1]
        ,[Name2]
        ,[Branche]
        ,[Strasse]
        ,[Ort]
        ,[Plz]
        ,[PlzSub]
        ,[zuHaenden]
        ,[BrAnrede]
        ,[BrGruss]
        ,[Telefon1]
        ,[Telefon2]
        ,[FaxNummer]
        ,[EMail]
        ,[FibuKonto]
        ,[UstId1]
        ,[Land]
        ,[LandName]
        ,[PeppolAddr]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,UPPER(RTRIM(@Param_DestDebNr))
        ,@Param_DestTyp
        ,UPPER(RTRIM(@Param_Suchen))
        ,RTRIM(@Param_Anrede)
        ,RTRIM(@Param_Vorname)
        ,RTRIM(@Param_Name1)
        ,RTRIM(@Param_Name2)
        ,RTRIM(@Param_Branche)
        ,RTRIM(@Param_Strasse)
        ,RTRIM(@Param_Ort)
        ,UPPER(RTRIM(@Param_Plz))
        ,UPPER(RTRIM(@Param_PlzSub))
        ,RTRIM(@Param_zuHaenden)
        ,RTRIM(@Param_BrAnrede)
        ,RTRIM(@Param_BrGruss)
        ,RTRIM(@Param_Telefon1)
        ,RTRIM(@Param_Telefon2)
        ,RTRIM(@Param_FaxNummer)
        ,RTRIM(@Param_EMail)
        ,UPPER(RTRIM(@Param_FibuKonto))
        ,UPPER(RTRIM(@Param_UstId1))
        ,RTRIM(@Param_Land)
        ,RTRIM(@Param_LandName)
        ,RTRIM(@Param_PeppolAddr)
          )
      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_BelAdresse')
  DROP PROCEDURE [dbo].[up_Add_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Add_BelAdresse]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BelAdresse()
  INSERT INTO [dbo].[ut_BelAdresse] (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_BelAdresse')
  DROP PROCEDURE [dbo].[up_Upd_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelAdresse] @mRI integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr char(1)
  ,@Param_DestDebNr char(15)
  ,@Param_DestTyp integer
  ,@Param_Suchen char(40)
  ,@Param_Anrede varchar(50)
  ,@Param_Vorname varchar(50)
  ,@Param_Name1 varchar(50)
  ,@Param_Name2 varchar(50)
  ,@Param_Branche varchar(50)
  ,@Param_Strasse varchar(50)
  ,@Param_Ort varchar(50)
  ,@Param_Plz char(8)
  ,@Param_PlzSub char(2)
  ,@Param_zuHaenden varchar(50)
  ,@Param_BrAnrede varchar(50)
  ,@Param_BrGruss varchar(50)
  ,@Param_Telefon1 varchar(20)
  ,@Param_Telefon2 varchar(20)
  ,@Param_FaxNummer varchar(20)
  ,@Param_EMail varchar(80)
  ,@Param_FibuKonto char(15)
  ,@Param_UstId1 char(15)
  ,@Param_Land varchar(10)
  ,@Param_LandName varchar(50)
  ,@Param_PeppolAddr varchar(40)
AS
  UPDATE [dbo].[ut_BelAdresse] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[DestDebNr] = UPPER(RTRIM(@Param_DestDebNr))
   ,[DestTyp] = @Param_DestTyp
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
   ,[Anrede] = RTRIM(@Param_Anrede)
   ,[Vorname] = RTRIM(@Param_Vorname)
   ,[Name1] = RTRIM(@Param_Name1)
   ,[Name2] = RTRIM(@Param_Name2)
   ,[Branche] = RTRIM(@Param_Branche)
   ,[Strasse] = RTRIM(@Param_Strasse)
   ,[Ort] = RTRIM(@Param_Ort)
   ,[Plz] = UPPER(RTRIM(@Param_Plz))
   ,[PlzSub] = UPPER(RTRIM(@Param_PlzSub))
   ,[zuHaenden] = RTRIM(@Param_zuHaenden)
   ,[BrAnrede] = RTRIM(@Param_BrAnrede)
   ,[BrGruss] = RTRIM(@Param_BrGruss)
   ,[Telefon1] = RTRIM(@Param_Telefon1)
   ,[Telefon2] = RTRIM(@Param_Telefon2)
   ,[FaxNummer] = RTRIM(@Param_FaxNummer)
   ,[EMail] = RTRIM(@Param_EMail)
   ,[FibuKonto] = UPPER(RTRIM(@Param_FibuKonto))
   ,[UstId1] = UPPER(RTRIM(@Param_UstId1))
   ,[Land] = RTRIM(@Param_Land)
   ,[LandName] = RTRIM(@Param_LandName)
   ,[PeppolAddr] = RTRIM(@Param_PeppolAddr)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelAdresse')
  DROP PROCEDURE [dbo].[up_Upd2_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelAdresse] @Param_RecId varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_LfdNr varchar(1)
  ,@Param_DestDebNr varchar(15)
  ,@Param_DestTyp varchar(2)
  ,@Param_Suchen varchar(40)
  ,@Param_Anrede varchar(50)
  ,@Param_Vorname varchar(50)
  ,@Param_Name1 varchar(50)
  ,@Param_Name2 varchar(50)
  ,@Param_Branche varchar(50)
  ,@Param_Strasse varchar(50)
  ,@Param_Ort varchar(50)
  ,@Param_Plz varchar(8)
  ,@Param_PlzSub varchar(2)
  ,@Param_zuHaenden varchar(50)
  ,@Param_BrAnrede varchar(50)
  ,@Param_BrGruss varchar(50)
  ,@Param_Telefon1 varchar(20)
  ,@Param_Telefon2 varchar(20)
  ,@Param_FaxNummer varchar(20)
  ,@Param_EMail varchar(80)
  ,@Param_FibuKonto varchar(15)
  ,@Param_UstId1 varchar(15)
  ,@Param_Land varchar(10)
  ,@Param_LandName varchar(50)
  ,@Param_PeppolAddr varchar(40)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelAdresse] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[DestDebNr] = UPPER(RTRIM(@Param_DestDebNr))
   ,[DestTyp] = dbo.itcfunc_integer(@Param_DestTyp)
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
   ,[Anrede] = RTRIM(@Param_Anrede)
   ,[Vorname] = RTRIM(@Param_Vorname)
   ,[Name1] = RTRIM(@Param_Name1)
   ,[Name2] = RTRIM(@Param_Name2)
   ,[Branche] = RTRIM(@Param_Branche)
   ,[Strasse] = RTRIM(@Param_Strasse)
   ,[Ort] = RTRIM(@Param_Ort)
   ,[Plz] = UPPER(RTRIM(@Param_Plz))
   ,[PlzSub] = UPPER(RTRIM(@Param_PlzSub))
   ,[zuHaenden] = RTRIM(@Param_zuHaenden)
   ,[BrAnrede] = RTRIM(@Param_BrAnrede)
   ,[BrGruss] = RTRIM(@Param_BrGruss)
   ,[Telefon1] = RTRIM(@Param_Telefon1)
   ,[Telefon2] = RTRIM(@Param_Telefon2)
   ,[FaxNummer] = RTRIM(@Param_FaxNummer)
   ,[EMail] = RTRIM(@Param_EMail)
   ,[FibuKonto] = UPPER(RTRIM(@Param_FibuKonto))
   ,[UstId1] = UPPER(RTRIM(@Param_UstId1))
   ,[Land] = RTRIM(@Param_Land)
   ,[LandName] = RTRIM(@Param_LandName)
   ,[PeppolAddr] = RTRIM(@Param_PeppolAddr)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelAdresse] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BelAdresse')
  DROP PROCEDURE [dbo].[up_TrSel_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelAdresse]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([LfdNr]))
   +LTRIM(RTRIM([DestDebNr]))
   +LTRIM(RTRIM([Suchen]))
   +LTRIM(RTRIM([Anrede]))
   +LTRIM(RTRIM([Vorname]))
   +LTRIM(RTRIM([Name1]))
   +LTRIM(RTRIM([Name2]))
   +LTRIM(RTRIM([Branche]))
   +LTRIM(RTRIM([Strasse]))
   +LTRIM(RTRIM([Ort]))
   +LTRIM(RTRIM([Plz]))
   +LTRIM(RTRIM([PlzSub]))
   +LTRIM(RTRIM([zuHaenden]))
   +LTRIM(RTRIM([BrAnrede]))
   +LTRIM(RTRIM([BrGruss]))
   +LTRIM(RTRIM([Telefon1]))
   +LTRIM(RTRIM([Telefon2]))
   +LTRIM(RTRIM([FaxNummer]))
   +LTRIM(RTRIM([EMail]))
   +LTRIM(RTRIM([FibuKonto]))
   +LTRIM(RTRIM([UstId1]))
   +LTRIM(RTRIM([Land]))
   +LTRIM(RTRIM([LandName]))
   +LTRIM(RTRIM([PeppolAddr]))
  AS Data FROM [dbo].[ut_BelAdresse] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelAdresse')
  DROP PROCEDURE [dbo].[up_Reorg_BelAdresse]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelAdresse] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelAdresse] 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_BelAdresse] 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_BelAdresse] )
  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_BelAdresse' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  DROP INDEX [Ik_Key0] ON [dbo].[ut_BelAdresse]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelAdresse' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelAdresse] (PrjNr,BelArt,BelNr,LfdNr,RecId)
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelAdresse_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelAdresse_Key0]
GO
CREATE PROCEDURE [dbo].[up_Last_BelAdresse_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0) 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_BelAdresse_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelAdresse_Key0] AS EXECUTE [dbo].[up_Last_BelAdresse_Key0]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr > @oldLfdNr)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr > @oldPrjNr)
    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_BelAdresse_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelAdresse_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelAdresse_Key0] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr < @oldLfdNr)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    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_BelAdresse] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr < @oldPrjNr)
    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_BelAdresse_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelAdresse_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelAdresse_Key0] @oldRecId')
GO

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

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

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

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

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

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

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

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

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

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

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      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_BelAdresse_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelAdresse_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelAdresse_Key0] @xKey, @xLen')
GO

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

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

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      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_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelAdresse] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      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_BelAdresse_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelAdresse_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelAdresse_Key0] @xKey, @xLen')
GO


