-- 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_ArtikelKladdenKopf')
BEGIN
CREATE TABLE [dbo].[ut_ArtikelKladdenKopf]
(
  [RecId]  integer NOT NULL
 ,[Code] char(20) DEFAULT ''
 ,[Bez] varchar(50) DEFAULT ''
 ,[LogName] char(20) DEFAULT ''
 ,[BuchKladde] char(20) DEFAULT ''
 ,[KZ_ZA] char(1) DEFAULT ''
 ,[Lkz] char(2) DEFAULT ''
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[Layout] integer DEFAULT 0
 ,[CpyDatum] tinyint DEFAULT 0
 ,[CpyKZZA] tinyint DEFAULT 0
 ,[CpyPrjNr] tinyint DEFAULT 0
 ,[CpyExtBelN] tinyint DEFAULT 0
 ,[Annr] char(4) DEFAULT ''
  CONSTRAINT [Pk_ArtikelKladdenKopf]  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_ArtikelKladdenKopf_KeyDel')
  DROP TRIGGER [dbo].[tr_ArtikelKladdenKopf_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ArtikelKladdenKopf_KeyUpd')
  DROP TRIGGER [dbo].[tr_ArtikelKladdenKopf_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ArtikelKladdenKopf_Keys')
  DROP TABLE [dbo].[ut_ArtikelKladdenKopf_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_ArtikelKladdenKopf' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [Code] 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_ArtikelKladdenKopf' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [Bez] 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_ArtikelKladdenKopf' and sc.[name] = 'LogName')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [LogName] 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_ArtikelKladdenKopf' and sc.[name] = 'BuchKladde')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [BuchKladde] 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_ArtikelKladdenKopf' and sc.[name] = 'KZ_ZA')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [KZ_ZA] char(1) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [Lkz] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' and sc.[name] = 'Layout')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [Layout] 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_ArtikelKladdenKopf' and sc.[name] = 'CpyDatum')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [CpyDatum] 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_ArtikelKladdenKopf' and sc.[name] = 'CpyKZZA')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [CpyKZZA] 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_ArtikelKladdenKopf' and sc.[name] = 'CpyPrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [CpyPrjNr] 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_ArtikelKladdenKopf' and sc.[name] = 'CpyExtBelN')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [CpyExtBelN] 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_ArtikelKladdenKopf' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ADD [Annr] char(4) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Code'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Code] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Code'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Code] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Bez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Bez] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Bez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Bez] 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_ArtikelKladdenKopf' and sc.[name] = 'LogName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [LogName] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'LogName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [LogName] 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_ArtikelKladdenKopf' and sc.[name] = 'BuchKladde'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'BuchKladde'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [BuchKladde] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'BuchKladde'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'BuchKladde'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [BuchKladde] varchar(20)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'KZ_ZA'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'KZ_ZA'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [KZ_ZA] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'KZ_ZA'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'KZ_ZA'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [KZ_ZA] varchar(1)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Lkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Lkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Lkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Lkz] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] 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_ArtikelKladdenKopf' 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_ArtikelKladdenKopf' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Annr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Annr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Annr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Annr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelKladdenKopf' and sc.[name] = 'Annr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelKladdenKopf] ALTER COLUMN [Annr] varchar(4)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ArtikelKladdenKopf')
  DROP VIEW [dbo].[ArtikelKladdenKopf]
GO
CREATE VIEW [dbo].[ArtikelKladdenKopf] AS
  SELECT [RecId]
 ,[Code]
 ,[Bez]
 ,[LogName]
 ,[BuchKladde]
 ,[KZ_ZA]
 ,[Lkz]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[Layout]
 ,[CpyDatum]
 ,[CpyKZZA]
 ,[CpyPrjNr]
 ,[CpyExtBelN]
 ,[Annr]
  FROM [dbo].[ut_ArtikelKladdenKopf]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_Sel_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Sel_ArtikelKladdenKopf]  @mRI integer
AS
  SELECT
    [Code]
   ,[Bez]
   ,[LogName]
   ,[BuchKladde]
   ,[KZ_ZA]
   ,[Lkz]
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[Layout]
   ,[CpyDatum]
   ,[CpyKZZA]
   ,[CpyPrjNr]
   ,[CpyExtBelN]
   ,[Annr]
  FROM [dbo].[ut_ArtikelKladdenKopf] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_Append_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Append_ArtikelKladdenKopf]
   @Param_Code char(20)
  ,@Param_Bez varchar(50)
  ,@Param_LogName char(20)
  ,@Param_BuchKladde char(20)
  ,@Param_KZ_ZA char(1)
  ,@Param_Lkz char(2)
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_Layout integer
  ,@Param_CpyDatum tinyint
  ,@Param_CpyKZZA tinyint
  ,@Param_CpyPrjNr tinyint
  ,@Param_CpyExtBelN tinyint
  ,@Param_Annr char(4)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_ArtikelKladdenKopf()
      INSERT INTO [dbo].[ut_ArtikelKladdenKopf] (RecId
        ,[Code]
        ,[Bez]
        ,[LogName]
        ,[BuchKladde]
        ,[KZ_ZA]
        ,[Lkz]
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[Layout]
        ,[CpyDatum]
        ,[CpyKZZA]
        ,[CpyPrjNr]
        ,[CpyExtBelN]
        ,[Annr]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,RTRIM(@Param_Bez)
        ,UPPER(RTRIM(@Param_LogName))
        ,UPPER(RTRIM(@Param_BuchKladde))
        ,RTRIM(@Param_KZ_ZA)
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,@Param_Layout
        ,@Param_CpyDatum
        ,@Param_CpyKZZA
        ,@Param_CpyPrjNr
        ,@Param_CpyExtBelN
        ,UPPER(RTRIM(@Param_Annr))
          )
      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_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_Add_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Add_ArtikelKladdenKopf]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_ArtikelKladdenKopf()
  INSERT INTO [dbo].[ut_ArtikelKladdenKopf] (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_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd_ArtikelKladdenKopf] @mRI integer
  ,@Param_Code char(20)
  ,@Param_Bez varchar(50)
  ,@Param_LogName char(20)
  ,@Param_BuchKladde char(20)
  ,@Param_KZ_ZA char(1)
  ,@Param_Lkz char(2)
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_Layout integer
  ,@Param_CpyDatum tinyint
  ,@Param_CpyKZZA tinyint
  ,@Param_CpyPrjNr tinyint
  ,@Param_CpyExtBelN tinyint
  ,@Param_Annr char(4)
AS
  UPDATE [dbo].[ut_ArtikelKladdenKopf] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Bez] = RTRIM(@Param_Bez)
   ,[LogName] = UPPER(RTRIM(@Param_LogName))
   ,[BuchKladde] = UPPER(RTRIM(@Param_BuchKladde))
   ,[KZ_ZA] = RTRIM(@Param_KZ_ZA)
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[Layout] = @Param_Layout
   ,[CpyDatum] = @Param_CpyDatum
   ,[CpyKZZA] = @Param_CpyKZZA
   ,[CpyPrjNr] = @Param_CpyPrjNr
   ,[CpyExtBelN] = @Param_CpyExtBelN
   ,[Annr] = UPPER(RTRIM(@Param_Annr))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd2_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd2_ArtikelKladdenKopf] @Param_RecId varchar(20)
  ,@Param_Code varchar(20)
  ,@Param_Bez varchar(50)
  ,@Param_LogName varchar(20)
  ,@Param_BuchKladde varchar(20)
  ,@Param_KZ_ZA varchar(1)
  ,@Param_Lkz varchar(2)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_Layout varchar(2)
  ,@Param_CpyDatum varchar(1)
  ,@Param_CpyKZZA varchar(1)
  ,@Param_CpyPrjNr varchar(1)
  ,@Param_CpyExtBelN varchar(1)
  ,@Param_Annr varchar(4)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_ArtikelKladdenKopf] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Bez] = RTRIM(@Param_Bez)
   ,[LogName] = UPPER(RTRIM(@Param_LogName))
   ,[BuchKladde] = UPPER(RTRIM(@Param_BuchKladde))
   ,[KZ_ZA] = RTRIM(@Param_KZ_ZA)
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[Layout] = dbo.itcfunc_integer(@Param_Layout)
   ,[CpyDatum] = dbo.itcfunc_integer(@Param_CpyDatum)
   ,[CpyKZZA] = dbo.itcfunc_integer(@Param_CpyKZZA)
   ,[CpyPrjNr] = dbo.itcfunc_integer(@Param_CpyPrjNr)
   ,[CpyExtBelN] = dbo.itcfunc_integer(@Param_CpyExtBelN)
   ,[Annr] = UPPER(RTRIM(@Param_Annr))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_ArtikelKladdenKopf] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_ArtikelKladdenKopf')
  DROP PROCEDURE [dbo].[up_TrSel_ArtikelKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ArtikelKladdenKopf]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([Bez]))
   +LTRIM(RTRIM([LogName]))
   +LTRIM(RTRIM([BuchKladde]))
   +LTRIM(RTRIM([KZ_ZA]))
   +LTRIM(RTRIM([Lkz]))
   +LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([Annr]))
  AS Data FROM [dbo].[ut_ArtikelKladdenKopf] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00)
    WHERE (Code > @oldCode)
    ORDER BY Code,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_ArtikelKladdenKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_ArtikelKladdenKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_ArtikelKladdenKopf_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00)
    WHERE (Code < @oldCode)
    ORDER BY Code 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_ArtikelKladdenKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_ArtikelKladdenKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_ArtikelKladdenKopf_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ArtikelKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_First_ArtikelKladdenKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_ArtikelKladdenKopf_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) ORDER BY LogName,Code,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_ArtikelKladdenKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_ArtikelKladdenKopf_Key01] AS EXECUTE [dbo].[up_First_ArtikelKladdenKopf_Key01]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ArtikelKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Next_ArtikelKladdenKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_ArtikelKladdenKopf_Key01] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldCode char(20)
  SELECT @oldLogName = LogName, @oldCode = Code FROM [dbo].[ut_ArtikelKladdenKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Code = @oldCode) AND (RecId > @oldRecId)
    ORDER BY LogName,Code,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Code > @oldCode)
    ORDER BY LogName,Code,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName > @oldLogName)
    ORDER BY LogName,Code,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_ArtikelKladdenKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_ArtikelKladdenKopf_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_ArtikelKladdenKopf_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ArtikelKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Prior_ArtikelKladdenKopf_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_ArtikelKladdenKopf_Key01] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldCode char(20)
  SELECT @oldLogName = LogName, @oldCode = Code FROM [dbo].[ut_ArtikelKladdenKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Code = @oldCode) AND (RecId < @oldRecId)
    ORDER BY LogName desc,Code desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Code < @oldCode)
    ORDER BY LogName desc,Code desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01)
    WHERE (LogName < @oldLogName)
    ORDER BY LogName desc,Code 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_ArtikelKladdenKopf_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_ArtikelKladdenKopf_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_ArtikelKladdenKopf_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Code = @mCode
      ORDER BY LogName,Code,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName
      ORDER BY LogName,Code,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName >= @mLogName AND SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName,Code,RecId
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY LogName,Code,RecId
    RETURN
  END

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

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

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

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Code = @mCode
      ORDER BY LogName desc,Code desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName
      ORDER BY LogName desc,Code desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName desc,Code desc,RecId desc
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelKladdenKopf] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY LogName desc,Code desc,RecId desc
    RETURN
  END

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


