-- 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_KundenLizenzen')
BEGIN
CREATE TABLE [dbo].[ut_KundenLizenzen]
(
  [RecId]  integer NOT NULL
 ,[DestNr] char(15) DEFAULT ''
 ,[SyncId] char(40) DEFAULT ''
 ,[SyncState] integer DEFAULT 0
 ,[SyncWeb] integer DEFAULT 0
 ,[AnlageAm] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[BeginnAm] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[EndetAm] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[VertrNr] char(20) DEFAULT ''
 ,[Bemerkung] char(40) DEFAULT ''
 ,[SerienNr] char(50) DEFAULT ''
 ,[Freigabe] char(50) DEFAULT ''
 ,[HWP300] tinyint DEFAULT 0
 ,[HWP500] tinyint DEFAULT 0
 ,[HWP700] tinyint DEFAULT 0
 ,[CountEIN] integer DEFAULT 0
 ,[ZweitLiz] tinyint DEFAULT 0
 ,[GAEB] tinyint DEFAULT 0
 ,[DATEV] tinyint DEFAULT 0
 ,[Wartung] tinyint DEFAULT 0
 ,[MfrPortal] tinyint DEFAULT 0
 ,[WsWgBel] tinyint DEFAULT 0
 ,[Mareon] tinyint DEFAULT 0
 ,[Provider] tinyint DEFAULT 0
 ,[Abzug] float DEFAULT 0
 ,[AbzugGrund] char(40) DEFAULT ''
 ,[GP] float DEFAULT 0
 ,[Status] integer DEFAULT 0
 ,[WebPortal] tinyint DEFAULT 0
 ,[Action] integer DEFAULT 0
 ,[SyncError] integer DEFAULT 0
 ,[WiederVorl] integer DEFAULT 0
 ,[ErsatzFuer] char(40) DEFAULT ''
 ,[ErsetztDur] char(40) DEFAULT ''
 ,[ZUGFeRD] tinyint DEFAULT 0
  CONSTRAINT [Pk_KundenLizenzen]  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_KundenLizenzen_KeyDel')
  DROP TRIGGER [dbo].[tr_KundenLizenzen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KundenLizenzen_KeyUpd')
  DROP TRIGGER [dbo].[tr_KundenLizenzen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_KundenLizenzen_Keys')
  DROP TABLE [dbo].[ut_KundenLizenzen_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_KundenLizenzen' and sc.[name] = 'DestNr')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [DestNr] 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_KundenLizenzen' and sc.[name] = 'SyncId')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [SyncId] 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_KundenLizenzen' and sc.[name] = 'SyncState')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [SyncState] 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_KundenLizenzen' and sc.[name] = 'SyncWeb')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [SyncWeb] 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_KundenLizenzen' and sc.[name] = 'AnlageAm')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [AnlageAm] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'BeginnAm')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [BeginnAm] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'EndetAm')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [EndetAm] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'VertrNr')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [VertrNr] 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_KundenLizenzen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Bemerkung] 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_KundenLizenzen' and sc.[name] = 'SerienNr')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [SerienNr] char(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Freigabe')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Freigabe] char(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'HWP300')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [HWP300] 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_KundenLizenzen' and sc.[name] = 'HWP500')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [HWP500] 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_KundenLizenzen' and sc.[name] = 'HWP700')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [HWP700] 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_KundenLizenzen' and sc.[name] = 'CountEIN')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [CountEIN] 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_KundenLizenzen' and sc.[name] = 'ZweitLiz')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [ZweitLiz] 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_KundenLizenzen' and sc.[name] = 'GAEB')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [GAEB] 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_KundenLizenzen' and sc.[name] = 'DATEV')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [DATEV] 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_KundenLizenzen' and sc.[name] = 'Wartung')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Wartung] 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_KundenLizenzen' and sc.[name] = 'MfrPortal')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [MfrPortal] 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_KundenLizenzen' and sc.[name] = 'WsWgBel')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [WsWgBel] 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_KundenLizenzen' and sc.[name] = 'Mareon')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Mareon] 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_KundenLizenzen' and sc.[name] = 'Provider')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Provider] 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_KundenLizenzen' and sc.[name] = 'Abzug')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Abzug] 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_KundenLizenzen' and sc.[name] = 'AbzugGrund')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [AbzugGrund] 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_KundenLizenzen' and sc.[name] = 'GP')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [GP] 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_KundenLizenzen' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Status] 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_KundenLizenzen' and sc.[name] = 'WebPortal')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [WebPortal] 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_KundenLizenzen' and sc.[name] = 'Action')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [Action] 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_KundenLizenzen' and sc.[name] = 'SyncError')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [SyncError] 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_KundenLizenzen' and sc.[name] = 'WiederVorl')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [WiederVorl] 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_KundenLizenzen' and sc.[name] = 'ErsatzFuer')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [ErsatzFuer] 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_KundenLizenzen' and sc.[name] = 'ErsetztDur')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [ErsetztDur] 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_KundenLizenzen' and sc.[name] = 'ZUGFeRD')
BEGIN
  ALTER TABLE [dbo].[ut_KundenLizenzen] ADD [ZUGFeRD] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'DestNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'DestNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [DestNr] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'DestNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'DestNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [DestNr] 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_KundenLizenzen' and sc.[name] = 'SyncId'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SyncId'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [SyncId] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SyncId'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SyncId'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [SyncId] varchar(40)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'VertrNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'VertrNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [VertrNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'VertrNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'VertrNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [VertrNr] varchar(20)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Bemerkung'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Bemerkung'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [Bemerkung] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Bemerkung'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Bemerkung'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [Bemerkung] 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_KundenLizenzen' and sc.[name] = 'SerienNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SerienNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [SerienNr] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SerienNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'SerienNr'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [SerienNr] 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_KundenLizenzen' and sc.[name] = 'Freigabe'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Freigabe'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [Freigabe] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Freigabe'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'Freigabe'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [Freigabe] 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_KundenLizenzen' and sc.[name] = 'AbzugGrund'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'AbzugGrund'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [AbzugGrund] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'AbzugGrund'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'AbzugGrund'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [AbzugGrund] varchar(40)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsatzFuer'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsatzFuer'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [ErsatzFuer] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsatzFuer'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsatzFuer'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [ErsatzFuer] varchar(40)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsetztDur'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsetztDur'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [ErsetztDur] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsetztDur'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KundenLizenzen' and sc.[name] = 'ErsetztDur'))
BEGIN
   ALTER TABLE [dbo].[ut_KundenLizenzen] ALTER COLUMN [ErsetztDur] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'KundenLizenzen')
  DROP VIEW [dbo].[KundenLizenzen]
GO
CREATE VIEW [dbo].[KundenLizenzen] AS
  SELECT [RecId]
 ,[DestNr]
 ,[SyncId]
 ,[SyncState]
 ,[SyncWeb]
 ,[AnlageAm]
 ,[BeginnAm]
 ,[EndetAm]
 ,[VertrNr]
 ,[Bemerkung]
 ,[SerienNr]
 ,[Freigabe]
 ,[HWP300]
 ,[HWP500]
 ,[HWP700]
 ,[CountEIN]
 ,[ZweitLiz]
 ,[GAEB]
 ,[DATEV]
 ,[Wartung]
 ,[MfrPortal]
 ,[WsWgBel]
 ,[Mareon]
 ,[Provider]
 ,[Abzug]
 ,[AbzugGrund]
 ,[GP]
 ,[Status]
 ,[WebPortal]
 ,[Action]
 ,[SyncError]
 ,[WiederVorl]
 ,[ErsatzFuer]
 ,[ErsetztDur]
 ,[ZUGFeRD]
  FROM [dbo].[ut_KundenLizenzen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_Sel_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_Sel_KundenLizenzen]  @mRI integer
AS
  SELECT
    [DestNr]
   ,[SyncId]
   ,[SyncState]
   ,[SyncWeb]
   ,[AnlageAm]
   ,[BeginnAm]
   ,[EndetAm]
   ,[VertrNr]
   ,[Bemerkung]
   ,[SerienNr]
   ,[Freigabe]
   ,[HWP300]
   ,[HWP500]
   ,[HWP700]
   ,[CountEIN]
   ,[ZweitLiz]
   ,[GAEB]
   ,[DATEV]
   ,[Wartung]
   ,[MfrPortal]
   ,[WsWgBel]
   ,[Mareon]
   ,[Provider]
   ,[Abzug]
   ,[AbzugGrund]
   ,[GP]
   ,[Status]
   ,[WebPortal]
   ,[Action]
   ,[SyncError]
   ,[WiederVorl]
   ,[ErsatzFuer]
   ,[ErsetztDur]
   ,[ZUGFeRD]
  FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_Append_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_Append_KundenLizenzen]
   @Param_DestNr char(15)
  ,@Param_SyncId char(40)
  ,@Param_SyncState integer
  ,@Param_SyncWeb integer
  ,@Param_AnlageAm datetime
  ,@Param_BeginnAm datetime
  ,@Param_EndetAm datetime
  ,@Param_VertrNr char(20)
  ,@Param_Bemerkung char(40)
  ,@Param_SerienNr char(50)
  ,@Param_Freigabe char(50)
  ,@Param_HWP300 tinyint
  ,@Param_HWP500 tinyint
  ,@Param_HWP700 tinyint
  ,@Param_CountEIN integer
  ,@Param_ZweitLiz tinyint
  ,@Param_GAEB tinyint
  ,@Param_DATEV tinyint
  ,@Param_Wartung tinyint
  ,@Param_MfrPortal tinyint
  ,@Param_WsWgBel tinyint
  ,@Param_Mareon tinyint
  ,@Param_Provider tinyint
  ,@Param_Abzug float
  ,@Param_AbzugGrund char(40)
  ,@Param_GP float
  ,@Param_Status integer
  ,@Param_WebPortal tinyint
  ,@Param_Action integer
  ,@Param_SyncError integer
  ,@Param_WiederVorl integer
  ,@Param_ErsatzFuer char(40)
  ,@Param_ErsetztDur char(40)
  ,@Param_ZUGFeRD tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_KundenLizenzen()
      INSERT INTO [dbo].[ut_KundenLizenzen] (RecId
        ,[DestNr]
        ,[SyncId]
        ,[SyncState]
        ,[SyncWeb]
        ,[AnlageAm]
        ,[BeginnAm]
        ,[EndetAm]
        ,[VertrNr]
        ,[Bemerkung]
        ,[SerienNr]
        ,[Freigabe]
        ,[HWP300]
        ,[HWP500]
        ,[HWP700]
        ,[CountEIN]
        ,[ZweitLiz]
        ,[GAEB]
        ,[DATEV]
        ,[Wartung]
        ,[MfrPortal]
        ,[WsWgBel]
        ,[Mareon]
        ,[Provider]
        ,[Abzug]
        ,[AbzugGrund]
        ,[GP]
        ,[Status]
        ,[WebPortal]
        ,[Action]
        ,[SyncError]
        ,[WiederVorl]
        ,[ErsatzFuer]
        ,[ErsetztDur]
        ,[ZUGFeRD]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_DestNr))
        ,UPPER(RTRIM(@Param_SyncId))
        ,@Param_SyncState
        ,@Param_SyncWeb
        ,@Param_AnlageAm
        ,@Param_BeginnAm
        ,@Param_EndetAm
        ,UPPER(RTRIM(@Param_VertrNr))
        ,RTRIM(@Param_Bemerkung)
        ,RTRIM(@Param_SerienNr)
        ,RTRIM(@Param_Freigabe)
        ,@Param_HWP300
        ,@Param_HWP500
        ,@Param_HWP700
        ,@Param_CountEIN
        ,@Param_ZweitLiz
        ,@Param_GAEB
        ,@Param_DATEV
        ,@Param_Wartung
        ,@Param_MfrPortal
        ,@Param_WsWgBel
        ,@Param_Mareon
        ,@Param_Provider
        ,@Param_Abzug
        ,RTRIM(@Param_AbzugGrund)
        ,@Param_GP
        ,@Param_Status
        ,@Param_WebPortal
        ,@Param_Action
        ,@Param_SyncError
        ,@Param_WiederVorl
        ,UPPER(RTRIM(@Param_ErsatzFuer))
        ,UPPER(RTRIM(@Param_ErsetztDur))
        ,@Param_ZUGFeRD
          )
      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_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_Add_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_Add_KundenLizenzen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_KundenLizenzen()
  INSERT INTO [dbo].[ut_KundenLizenzen] (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_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_Upd_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_Upd_KundenLizenzen] @mRI integer
  ,@Param_DestNr char(15)
  ,@Param_SyncId char(40)
  ,@Param_SyncState integer
  ,@Param_SyncWeb integer
  ,@Param_AnlageAm datetime
  ,@Param_BeginnAm datetime
  ,@Param_EndetAm datetime
  ,@Param_VertrNr char(20)
  ,@Param_Bemerkung char(40)
  ,@Param_SerienNr char(50)
  ,@Param_Freigabe char(50)
  ,@Param_HWP300 tinyint
  ,@Param_HWP500 tinyint
  ,@Param_HWP700 tinyint
  ,@Param_CountEIN integer
  ,@Param_ZweitLiz tinyint
  ,@Param_GAEB tinyint
  ,@Param_DATEV tinyint
  ,@Param_Wartung tinyint
  ,@Param_MfrPortal tinyint
  ,@Param_WsWgBel tinyint
  ,@Param_Mareon tinyint
  ,@Param_Provider tinyint
  ,@Param_Abzug float
  ,@Param_AbzugGrund char(40)
  ,@Param_GP float
  ,@Param_Status integer
  ,@Param_WebPortal tinyint
  ,@Param_Action integer
  ,@Param_SyncError integer
  ,@Param_WiederVorl integer
  ,@Param_ErsatzFuer char(40)
  ,@Param_ErsetztDur char(40)
  ,@Param_ZUGFeRD tinyint
AS
  UPDATE [dbo].[ut_KundenLizenzen] SET
    [DestNr] = UPPER(RTRIM(@Param_DestNr))
   ,[SyncId] = UPPER(RTRIM(@Param_SyncId))
   ,[SyncState] = @Param_SyncState
   ,[SyncWeb] = @Param_SyncWeb
   ,[AnlageAm] = @Param_AnlageAm
   ,[BeginnAm] = @Param_BeginnAm
   ,[EndetAm] = @Param_EndetAm
   ,[VertrNr] = UPPER(RTRIM(@Param_VertrNr))
   ,[Bemerkung] = RTRIM(@Param_Bemerkung)
   ,[SerienNr] = RTRIM(@Param_SerienNr)
   ,[Freigabe] = RTRIM(@Param_Freigabe)
   ,[HWP300] = @Param_HWP300
   ,[HWP500] = @Param_HWP500
   ,[HWP700] = @Param_HWP700
   ,[CountEIN] = @Param_CountEIN
   ,[ZweitLiz] = @Param_ZweitLiz
   ,[GAEB] = @Param_GAEB
   ,[DATEV] = @Param_DATEV
   ,[Wartung] = @Param_Wartung
   ,[MfrPortal] = @Param_MfrPortal
   ,[WsWgBel] = @Param_WsWgBel
   ,[Mareon] = @Param_Mareon
   ,[Provider] = @Param_Provider
   ,[Abzug] = @Param_Abzug
   ,[AbzugGrund] = RTRIM(@Param_AbzugGrund)
   ,[GP] = @Param_GP
   ,[Status] = @Param_Status
   ,[WebPortal] = @Param_WebPortal
   ,[Action] = @Param_Action
   ,[SyncError] = @Param_SyncError
   ,[WiederVorl] = @Param_WiederVorl
   ,[ErsatzFuer] = UPPER(RTRIM(@Param_ErsatzFuer))
   ,[ErsetztDur] = UPPER(RTRIM(@Param_ErsetztDur))
   ,[ZUGFeRD] = @Param_ZUGFeRD
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_Upd2_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_KundenLizenzen] @Param_RecId varchar(20)
  ,@Param_DestNr varchar(15)
  ,@Param_SyncId varchar(40)
  ,@Param_SyncState varchar(1)
  ,@Param_SyncWeb varchar(1)
  ,@Param_AnlageAm varchar(10)
  ,@Param_BeginnAm varchar(10)
  ,@Param_EndetAm varchar(10)
  ,@Param_VertrNr varchar(20)
  ,@Param_Bemerkung varchar(40)
  ,@Param_SerienNr varchar(50)
  ,@Param_Freigabe varchar(50)
  ,@Param_HWP300 varchar(1)
  ,@Param_HWP500 varchar(1)
  ,@Param_HWP700 varchar(1)
  ,@Param_CountEIN varchar(2)
  ,@Param_ZweitLiz varchar(1)
  ,@Param_GAEB varchar(1)
  ,@Param_DATEV varchar(1)
  ,@Param_Wartung varchar(1)
  ,@Param_MfrPortal varchar(1)
  ,@Param_WsWgBel varchar(1)
  ,@Param_Mareon varchar(1)
  ,@Param_Provider varchar(1)
  ,@Param_Abzug varchar(10)
  ,@Param_AbzugGrund varchar(40)
  ,@Param_GP varchar(10)
  ,@Param_Status varchar(1)
  ,@Param_WebPortal varchar(1)
  ,@Param_Action varchar(1)
  ,@Param_SyncError varchar(1)
  ,@Param_WiederVorl varchar(1)
  ,@Param_ErsatzFuer varchar(40)
  ,@Param_ErsetztDur varchar(40)
  ,@Param_ZUGFeRD varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_KundenLizenzen] SET
    [DestNr] = UPPER(RTRIM(@Param_DestNr))
   ,[SyncId] = UPPER(RTRIM(@Param_SyncId))
   ,[SyncState] = dbo.itcfunc_integer(@Param_SyncState)
   ,[SyncWeb] = dbo.itcfunc_integer(@Param_SyncWeb)
   ,[AnlageAm] = dbo.itcfunc_datetime(@Param_AnlageAm)
   ,[BeginnAm] = dbo.itcfunc_datetime(@Param_BeginnAm)
   ,[EndetAm] = dbo.itcfunc_datetime(@Param_EndetAm)
   ,[VertrNr] = UPPER(RTRIM(@Param_VertrNr))
   ,[Bemerkung] = RTRIM(@Param_Bemerkung)
   ,[SerienNr] = RTRIM(@Param_SerienNr)
   ,[Freigabe] = RTRIM(@Param_Freigabe)
   ,[HWP300] = dbo.itcfunc_integer(@Param_HWP300)
   ,[HWP500] = dbo.itcfunc_integer(@Param_HWP500)
   ,[HWP700] = dbo.itcfunc_integer(@Param_HWP700)
   ,[CountEIN] = dbo.itcfunc_integer(@Param_CountEIN)
   ,[ZweitLiz] = dbo.itcfunc_integer(@Param_ZweitLiz)
   ,[GAEB] = dbo.itcfunc_integer(@Param_GAEB)
   ,[DATEV] = dbo.itcfunc_integer(@Param_DATEV)
   ,[Wartung] = dbo.itcfunc_integer(@Param_Wartung)
   ,[MfrPortal] = dbo.itcfunc_integer(@Param_MfrPortal)
   ,[WsWgBel] = dbo.itcfunc_integer(@Param_WsWgBel)
   ,[Mareon] = dbo.itcfunc_integer(@Param_Mareon)
   ,[Provider] = dbo.itcfunc_integer(@Param_Provider)
   ,[Abzug] = dbo.itcfunc_float(@Param_Abzug)
   ,[AbzugGrund] = RTRIM(@Param_AbzugGrund)
   ,[GP] = dbo.itcfunc_float(@Param_GP)
   ,[Status] = dbo.itcfunc_integer(@Param_Status)
   ,[WebPortal] = dbo.itcfunc_integer(@Param_WebPortal)
   ,[Action] = dbo.itcfunc_integer(@Param_Action)
   ,[SyncError] = dbo.itcfunc_integer(@Param_SyncError)
   ,[WiederVorl] = dbo.itcfunc_integer(@Param_WiederVorl)
   ,[ErsatzFuer] = UPPER(RTRIM(@Param_ErsatzFuer))
   ,[ErsetztDur] = UPPER(RTRIM(@Param_ErsetztDur))
   ,[ZUGFeRD] = dbo.itcfunc_integer(@Param_ZUGFeRD)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_KundenLizenzen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_KundenLizenzen')
  DROP PROCEDURE [dbo].[up_TrSel_KundenLizenzen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_KundenLizenzen]
AS
  SELECT RecId,
    LTRIM(RTRIM([DestNr]))
   +LTRIM(RTRIM([SyncId]))
   +LTRIM(RTRIM([VertrNr]))
   +LTRIM(RTRIM([Bemerkung]))
   +LTRIM(RTRIM([SerienNr]))
   +LTRIM(RTRIM([Freigabe]))
   +LTRIM(RTRIM([AbzugGrund]))
   +LTRIM(RTRIM([ErsatzFuer]))
   +LTRIM(RTRIM([ErsetztDur]))
  AS Data FROM [dbo].[ut_KundenLizenzen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KundenLizenzen_Key00')
  DROP PROCEDURE [dbo].[up_First_KundenLizenzen_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_KundenLizenzen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) ORDER BY DestNr,SyncId,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_KundenLizenzen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_KundenLizenzen_Key00] AS EXECUTE [dbo].[up_First_KundenLizenzen_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KundenLizenzen_Key00')
  DROP PROCEDURE [dbo].[up_Next_KundenLizenzen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_KundenLizenzen_Key00] @oldRecId integer AS
  DECLARE @oldDestNr char(15)
  DECLARE @oldSyncId char(40)
  SELECT @oldDestNr = DestNr, @oldSyncId = SyncId FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr = @oldDestNr) AND (SyncId = @oldSyncId) AND (RecId > @oldRecId)
    ORDER BY DestNr,SyncId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr = @oldDestNr) AND (SyncId > @oldSyncId)
    ORDER BY DestNr,SyncId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr > @oldDestNr)
    ORDER BY DestNr,SyncId,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_KundenLizenzen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_KundenLizenzen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_KundenLizenzen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KundenLizenzen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key00] @oldRecId integer AS
  DECLARE @oldDestNr char(15)
  DECLARE @oldSyncId char(40)
  SELECT @oldDestNr = DestNr, @oldSyncId = SyncId FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr = @oldDestNr) AND (SyncId = @oldSyncId) AND (RecId < @oldRecId)
    ORDER BY DestNr desc,SyncId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr = @oldDestNr) AND (SyncId < @oldSyncId)
    ORDER BY DestNr desc,SyncId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00)
    WHERE (DestNr < @oldDestNr)
    ORDER BY DestNr desc,SyncId 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_KundenLizenzen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_KundenLizenzen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_KundenLizenzen_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr AND 
      SyncId = @mSyncId
      ORDER BY DestNr,SyncId,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr
      ORDER BY DestNr,SyncId,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr >= @mDestNr AND SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY DestNr,SyncId,RecId
    RETURN
  END

  IF @xLen < 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr AND 
      SyncId >= @mSyncId AND SUBSTRING(SyncId,1,LEN(@mSyncId)) = @mSyncId
      ORDER BY DestNr,SyncId,RecId
    RETURN
  END

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

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

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

  IF @xLen = 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr AND 
      SyncId = @mSyncId
      ORDER BY DestNr desc,SyncId desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr
      ORDER BY DestNr desc,SyncId desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY DestNr desc,SyncId desc,RecId desc
    RETURN
  END

  IF @xLen < 55 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key00) WHERE 
      DestNr = @mDestNr AND 
      SUBSTRING(SyncId,1,LEN(@mSyncId)) = @mSyncId
      ORDER BY DestNr desc,SyncId desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_First_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_KundenLizenzen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) ORDER BY DestNr,AnlageAm,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_KundenLizenzen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_KundenLizenzen_Key01] AS EXECUTE [dbo].[up_First_KundenLizenzen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_Last_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_KundenLizenzen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) ORDER BY DestNr desc,AnlageAm desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_KundenLizenzen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_KundenLizenzen_Key01] AS EXECUTE [dbo].[up_Last_KundenLizenzen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_Next_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_KundenLizenzen_Key01] @oldRecId integer AS
  DECLARE @oldDestNr char(15)
  DECLARE @oldAnlageAm datetime
  SELECT @oldDestNr = DestNr, @oldAnlageAm = AnlageAm FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr = @oldDestNr) AND (AnlageAm = @oldAnlageAm) AND (RecId > @oldRecId)
    ORDER BY DestNr,AnlageAm,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr = @oldDestNr) AND (AnlageAm > @oldAnlageAm)
    ORDER BY DestNr,AnlageAm,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr > @oldDestNr)
    ORDER BY DestNr,AnlageAm,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_KundenLizenzen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_KundenLizenzen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_KundenLizenzen_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key01] @oldRecId integer AS
  DECLARE @oldDestNr char(15)
  DECLARE @oldAnlageAm datetime
  SELECT @oldDestNr = DestNr, @oldAnlageAm = AnlageAm FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr = @oldDestNr) AND (AnlageAm = @oldAnlageAm) AND (RecId < @oldRecId)
    ORDER BY DestNr desc,AnlageAm desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr = @oldDestNr) AND (AnlageAm < @oldAnlageAm)
    ORDER BY DestNr desc,AnlageAm desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01)
    WHERE (DestNr < @oldDestNr)
    ORDER BY DestNr desc,AnlageAm 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_KundenLizenzen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_KundenLizenzen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_KundenLizenzen_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_KundenLizenzen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mDestNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mAnlageAm datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      DestNr = @mDestNr AND 
      AnlageAm = @mAnlageAm
      ORDER BY DestNr,AnlageAm,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      DestNr = @mDestNr
      ORDER BY DestNr,AnlageAm,RecId
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      DestNr >= @mDestNr AND SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY DestNr,AnlageAm,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KundenLizenzen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_KundenLizenzen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_KundenLizenzen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mDestNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,15)
  DECLARE @mAnlageAm datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      DestNr = @mDestNr AND 
      AnlageAm = @mAnlageAm
      ORDER BY DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      DestNr = @mDestNr
      ORDER BY DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END

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

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_First_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_KundenLizenzen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) ORDER BY WiederVorl,DestNr,AnlageAm,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_KundenLizenzen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_KundenLizenzen_Key02] AS EXECUTE [dbo].[up_First_KundenLizenzen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_Last_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_KundenLizenzen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_KundenLizenzen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_KundenLizenzen_Key02] AS EXECUTE [dbo].[up_Last_KundenLizenzen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_Next_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_KundenLizenzen_Key02] @oldRecId integer AS
  DECLARE @oldWiederVorl integer
  DECLARE @oldDestNr char(15)
  DECLARE @oldAnlageAm datetime
  SELECT @oldWiederVorl = WiederVorl, @oldDestNr = DestNr, @oldAnlageAm = AnlageAm FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr = @oldDestNr) AND (AnlageAm = @oldAnlageAm) AND (RecId > @oldRecId)
    ORDER BY WiederVorl,DestNr,AnlageAm,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr = @oldDestNr) AND (AnlageAm > @oldAnlageAm)
    ORDER BY WiederVorl,DestNr,AnlageAm,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr > @oldDestNr)
    ORDER BY WiederVorl,DestNr,AnlageAm,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl > @oldWiederVorl)
    ORDER BY WiederVorl,DestNr,AnlageAm,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_KundenLizenzen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_KundenLizenzen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_KundenLizenzen_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_KundenLizenzen_Key02] @oldRecId integer AS
  DECLARE @oldWiederVorl integer
  DECLARE @oldDestNr char(15)
  DECLARE @oldAnlageAm datetime
  SELECT @oldWiederVorl = WiederVorl, @oldDestNr = DestNr, @oldAnlageAm = AnlageAm FROM [dbo].[ut_KundenLizenzen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr = @oldDestNr) AND (AnlageAm = @oldAnlageAm) AND (RecId < @oldRecId)
    ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr = @oldDestNr) AND (AnlageAm < @oldAnlageAm)
    ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl = @oldWiederVorl) AND (DestNr < @oldDestNr)
    ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02)
    WHERE (WiederVorl < @oldWiederVorl)
    ORDER BY WiederVorl desc,DestNr desc,AnlageAm 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_KundenLizenzen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_KundenLizenzen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_KundenLizenzen_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_KundenLizenzen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mWiederVorl integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDestNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mAnlageAm datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,17,10)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      DestNr = @mDestNr AND 
      AnlageAm = @mAnlageAm
      ORDER BY WiederVorl,DestNr,AnlageAm,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      DestNr = @mDestNr
      ORDER BY WiederVorl,DestNr,AnlageAm,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl
      ORDER BY WiederVorl,DestNr,AnlageAm,RecId
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      DestNr >= @mDestNr AND SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY WiederVorl,DestNr,AnlageAm,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_KundenLizenzen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_KundenLizenzen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_KundenLizenzen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mWiederVorl integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mDestNr char(15) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,15)
  DECLARE @mAnlageAm datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,17,10)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      DestNr = @mDestNr AND 
      AnlageAm = @mAnlageAm
      ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      DestNr = @mDestNr
      ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl
      ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END

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


  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KundenLizenzen] WITH(INDEX = Ik_Key02) WHERE 
      WiederVorl = @mWiederVorl AND 
      SUBSTRING(DestNr,1,LEN(@mDestNr)) = @mDestNr
      ORDER BY WiederVorl desc,DestNr desc,AnlageAm desc,RecId desc
    RETURN
  END


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


