-- 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_Kleinteile')
BEGIN
CREATE TABLE [dbo].[ut_Kleinteile]
(
  [RecId]  integer NOT NULL
 ,[Lkz] char(2) DEFAULT ''
 ,[Artnr] char(18) DEFAULT ''
 ,[LfdNr] char(2) DEFAULT ''
 ,[RefLkz] char(2) DEFAULT ''
 ,[RefArtnr] char(18) DEFAULT ''
 ,[RefMenge] float DEFAULT 0
  CONSTRAINT [Pk_Kleinteile]  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_Kleinteile_KeyDel')
  DROP TRIGGER [dbo].[tr_Kleinteile_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kleinteile_KeyUpd')
  DROP TRIGGER [dbo].[tr_Kleinteile_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Kleinteile_Keys')
  DROP TABLE [dbo].[ut_Kleinteile_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_Kleinteile' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] 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_Kleinteile' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] ADD [Artnr] char(18) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] ADD [LfdNr] 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_Kleinteile' and sc.[name] = 'RefLkz')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] ADD [RefLkz] 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_Kleinteile' and sc.[name] = 'RefArtnr')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] ADD [RefArtnr] char(18) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefMenge')
BEGIN
  ALTER TABLE [dbo].[ut_Kleinteile] ADD [RefMenge] float NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' 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_Kleinteile' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] 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_Kleinteile' 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_Kleinteile' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [Lkz] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'Artnr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [Artnr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'Artnr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [Artnr] varchar(18)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'LfdNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [LfdNr] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'LfdNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [LfdNr] varchar(2)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefLkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [RefLkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefLkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [RefLkz] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefArtnr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefArtnr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [RefArtnr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefArtnr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kleinteile' and sc.[name] = 'RefArtnr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kleinteile] ALTER COLUMN [RefArtnr] varchar(18)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Kleinteile')
  DROP VIEW [dbo].[Kleinteile]
GO
CREATE VIEW [dbo].[Kleinteile] AS
  SELECT [RecId]
 ,[Lkz]
 ,[Artnr]
 ,[LfdNr]
 ,[RefLkz]
 ,[RefArtnr]
 ,[RefMenge]
  FROM [dbo].[ut_Kleinteile]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Kleinteile')
  DROP PROCEDURE [dbo].[up_Sel_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_Sel_Kleinteile]  @mRI integer
AS
  SELECT
    [Lkz]
   ,[Artnr]
   ,[LfdNr]
   ,[RefLkz]
   ,[RefArtnr]
   ,[RefMenge]
  FROM [dbo].[ut_Kleinteile] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Kleinteile')
  DROP PROCEDURE [dbo].[up_Append_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_Append_Kleinteile]
   @Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_LfdNr char(2)
  ,@Param_RefLkz char(2)
  ,@Param_RefArtnr char(18)
  ,@Param_RefMenge float
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Kleinteile()
      INSERT INTO [dbo].[ut_Kleinteile] (RecId
        ,[Lkz]
        ,[Artnr]
        ,[LfdNr]
        ,[RefLkz]
        ,[RefArtnr]
        ,[RefMenge]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_Artnr))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,UPPER(RTRIM(@Param_RefLkz))
        ,UPPER(RTRIM(@Param_RefArtnr))
        ,@Param_RefMenge
          )
      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_Kleinteile')
  DROP PROCEDURE [dbo].[up_Add_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_Add_Kleinteile]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Kleinteile()
  INSERT INTO [dbo].[ut_Kleinteile] (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_Kleinteile')
  DROP PROCEDURE [dbo].[up_Upd_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_Upd_Kleinteile] @mRI integer
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_LfdNr char(2)
  ,@Param_RefLkz char(2)
  ,@Param_RefArtnr char(18)
  ,@Param_RefMenge float
AS
  UPDATE [dbo].[ut_Kleinteile] SET
    [Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[RefLkz] = UPPER(RTRIM(@Param_RefLkz))
   ,[RefArtnr] = UPPER(RTRIM(@Param_RefArtnr))
   ,[RefMenge] = @Param_RefMenge
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Kleinteile')
  DROP PROCEDURE [dbo].[up_Upd2_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Kleinteile] @Param_RecId varchar(20)
  ,@Param_Lkz varchar(2)
  ,@Param_Artnr varchar(18)
  ,@Param_LfdNr varchar(2)
  ,@Param_RefLkz varchar(2)
  ,@Param_RefArtnr varchar(18)
  ,@Param_RefMenge varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Kleinteile] SET
    [Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[RefLkz] = UPPER(RTRIM(@Param_RefLkz))
   ,[RefArtnr] = UPPER(RTRIM(@Param_RefArtnr))
   ,[RefMenge] = dbo.itcfunc_float(@Param_RefMenge)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Kleinteile] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Kleinteile')
  DROP PROCEDURE [dbo].[up_TrSel_Kleinteile]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Kleinteile]
AS
  SELECT RecId,
    LTRIM(RTRIM([Lkz]))
   +LTRIM(RTRIM([Artnr]))
   +LTRIM(RTRIM([LfdNr]))
   +LTRIM(RTRIM([RefLkz]))
   +LTRIM(RTRIM([RefArtnr]))
  AS Data FROM [dbo].[ut_Kleinteile] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_First_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_First_Kleinteile_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) ORDER BY Lkz,Artnr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Kleinteile_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Kleinteile_Key0] AS EXECUTE [dbo].[up_First_Kleinteile_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_Last_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_Last_Kleinteile_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Kleinteile_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Kleinteile_Key0] AS EXECUTE [dbo].[up_Last_Kleinteile_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_Next_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_Next_Kleinteile_Key0] @oldRecId integer AS
  DECLARE @oldLkz char(2)
  DECLARE @oldArtnr char(18)
  DECLARE @oldLfdNr char(2)
  SELECT @oldLkz = Lkz, @oldArtnr = Artnr, @oldLfdNr = LfdNr FROM [dbo].[ut_Kleinteile] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz = @oldLkz) AND (Artnr = @oldArtnr) AND (LfdNr > @oldLfdNr)
    ORDER BY Lkz,Artnr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz = @oldLkz) AND (Artnr > @oldArtnr)
    ORDER BY Lkz,Artnr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz > @oldLkz)
    ORDER BY Lkz,Artnr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_Kleinteile_Key0] @oldRecId integer AS
  DECLARE @oldLkz char(2)
  DECLARE @oldArtnr char(18)
  DECLARE @oldLfdNr char(2)
  SELECT @oldLkz = Lkz, @oldArtnr = Artnr, @oldLfdNr = LfdNr FROM [dbo].[ut_Kleinteile] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz = @oldLkz) AND (Artnr = @oldArtnr) AND (LfdNr < @oldLfdNr)
    ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz = @oldLkz) AND (Artnr < @oldArtnr)
    ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0)
    WHERE (Lkz < @oldLkz)
    ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindF_Kleinteile_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mLkz char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mArtnr char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,18)
  DECLARE @mLfdNr char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,2)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      LfdNr = @mLfdNr
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz >= @mLkz AND SUBSTRING(Lkz,1,LEN(@mLkz)) = @mLkz
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr >= @mArtnr AND SUBSTRING(Artnr,1,LEN(@mArtnr)) = @mArtnr
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Lkz,Artnr,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Kleinteile_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Kleinteile_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_Kleinteile_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mLkz char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mArtnr char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,18)
  DECLARE @mLfdNr char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,2)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      LfdNr = @mLfdNr
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(Lkz,1,LEN(@mLkz)) = @mLkz
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      SUBSTRING(Artnr,1,LEN(@mArtnr)) = @mArtnr
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Lkz desc,Artnr desc,LfdNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kleinteile_Key1')
  DROP PROCEDURE [dbo].[up_First_Kleinteile_Key1]
GO
CREATE PROCEDURE [dbo].[up_First_Kleinteile_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) ORDER BY RefLkz,RefArtnr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Kleinteile_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Kleinteile_Key1] AS EXECUTE [dbo].[up_First_Kleinteile_Key1]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kleinteile_Key1')
  DROP PROCEDURE [dbo].[up_Last_Kleinteile_Key1]
GO
CREATE PROCEDURE [dbo].[up_Last_Kleinteile_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) ORDER BY RefLkz desc,RefArtnr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Kleinteile_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Kleinteile_Key1] AS EXECUTE [dbo].[up_Last_Kleinteile_Key1]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kleinteile_Key1')
  DROP PROCEDURE [dbo].[up_Next_Kleinteile_Key1]
GO
CREATE PROCEDURE [dbo].[up_Next_Kleinteile_Key1] @oldRecId integer AS
  DECLARE @oldRefLkz char(2)
  DECLARE @oldRefArtnr char(18)
  SELECT @oldRefLkz = RefLkz, @oldRefArtnr = RefArtnr FROM [dbo].[ut_Kleinteile] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz = @oldRefLkz) AND (RefArtnr = @oldRefArtnr) AND (RecId > @oldRecId)
    ORDER BY RefLkz,RefArtnr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz = @oldRefLkz) AND (RefArtnr > @oldRefArtnr)
    ORDER BY RefLkz,RefArtnr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz > @oldRefLkz)
    ORDER BY RefLkz,RefArtnr,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_Kleinteile_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Kleinteile_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Next_Kleinteile_Key1] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kleinteile_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Kleinteile_Key1]
GO
CREATE PROCEDURE [dbo].[up_Prior_Kleinteile_Key1] @oldRecId integer AS
  DECLARE @oldRefLkz char(2)
  DECLARE @oldRefArtnr char(18)
  SELECT @oldRefLkz = RefLkz, @oldRefArtnr = RefArtnr FROM [dbo].[ut_Kleinteile] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz = @oldRefLkz) AND (RefArtnr = @oldRefArtnr) AND (RecId < @oldRecId)
    ORDER BY RefLkz desc,RefArtnr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz = @oldRefLkz) AND (RefArtnr < @oldRefArtnr)
    ORDER BY RefLkz desc,RefArtnr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1)
    WHERE (RefLkz < @oldRefLkz)
    ORDER BY RefLkz desc,RefArtnr 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_Kleinteile_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Kleinteile_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Kleinteile_Key1] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz AND 
      RefArtnr = @mRefArtnr
      ORDER BY RefLkz,RefArtnr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz
      ORDER BY RefLkz,RefArtnr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz >= @mRefLkz AND SUBSTRING(RefLkz,1,LEN(@mRefLkz)) = @mRefLkz
      ORDER BY RefLkz,RefArtnr,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz AND 
      RefArtnr >= @mRefArtnr AND SUBSTRING(RefArtnr,1,LEN(@mRefArtnr)) = @mRefArtnr
      ORDER BY RefLkz,RefArtnr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz AND 
      RefArtnr = @mRefArtnr
      ORDER BY RefLkz desc,RefArtnr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz
      ORDER BY RefLkz desc,RefArtnr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      SUBSTRING(RefLkz,1,LEN(@mRefLkz)) = @mRefLkz
      ORDER BY RefLkz desc,RefArtnr desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kleinteile] WITH(INDEX = Ik_Key1) WHERE 
      RefLkz = @mRefLkz AND 
      SUBSTRING(RefArtnr,1,LEN(@mRefArtnr)) = @mRefArtnr
      ORDER BY RefLkz desc,RefArtnr desc,RecId desc
    RETURN
  END

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


