-- 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_StammPakete')
BEGIN
CREATE TABLE [dbo].[ut_StammPakete]
(
  [RecId]  integer NOT NULL
 ,[Gruppe] char(2) DEFAULT ''
 ,[PktNr] char(17) DEFAULT ''
 ,[LfdNr] char(3) DEFAULT ''
 ,[Suchen] char(15) DEFAULT ''
 ,[Txt1] char(42) DEFAULT ''
 ,[Txt2] char(42) DEFAULT ''
 ,[Txt3] char(42) DEFAULT ''
 ,[ArtLkz] char(2) DEFAULT ''
 ,[ArtNr] char(18) DEFAULT ''
 ,[TxtLkz] char(2) DEFAULT ''
 ,[TxtNr] char(18) DEFAULT ''
 ,[KzJumbo] tinyint DEFAULT 0
 ,[Aufwand] float DEFAULT 0
 ,[KzDrk1] integer DEFAULT 0
 ,[KzAktiv] tinyint DEFAULT 0
 ,[Einheit] integer DEFAULT 0
  CONSTRAINT [Pk_StammPakete]  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_StammPakete_KeyDel')
  DROP TRIGGER [dbo].[tr_StammPakete_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_StammPakete_KeyUpd')
  DROP TRIGGER [dbo].[tr_StammPakete_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_StammPakete_Keys')
  DROP TABLE [dbo].[ut_StammPakete_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_StammPakete' and sc.[name] = 'Gruppe')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Gruppe] 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_StammPakete' and sc.[name] = 'PktNr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [PktNr] char(17) 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_StammPakete' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [LfdNr] char(3) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Suchen')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Suchen] 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_StammPakete' and sc.[name] = 'Txt1')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Txt1] char(42) 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_StammPakete' and sc.[name] = 'Txt2')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Txt2] char(42) 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_StammPakete' and sc.[name] = 'Txt3')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Txt3] char(42) 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_StammPakete' and sc.[name] = 'ArtLkz')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [ArtLkz] 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_StammPakete' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] 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_StammPakete' and sc.[name] = 'TxtLkz')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [TxtLkz] 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_StammPakete' and sc.[name] = 'TxtNr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [TxtNr] 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_StammPakete' and sc.[name] = 'KzJumbo')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [KzJumbo] 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_StammPakete' and sc.[name] = 'Aufwand')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Aufwand] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'KzDrk1')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [KzDrk1] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'KzAktiv')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [KzAktiv] 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_StammPakete' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_StammPakete] ADD [Einheit] integer 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_StammPakete' and sc.[name] = 'Gruppe'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Gruppe'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Gruppe] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Gruppe'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Gruppe'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Gruppe] varchar(2)
END
GO
IF (17 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'PktNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'PktNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [PktNr] char(17)
END
GO
IF (17 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'PktNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'PktNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [PktNr] varchar(17)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' 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_StammPakete' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [LfdNr] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' 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_StammPakete' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [LfdNr] varchar(3)
END
GO
IF (15 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Suchen'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Suchen] char(15)
END
GO
IF (15 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Suchen'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Suchen] varchar(15)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt1'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt1] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt1'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt1] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt2'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt2] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt2'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt2] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt3'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt3'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt3] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt3'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'Txt3'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [Txt3] varchar(42)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'ArtLkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'ArtLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [ArtLkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'ArtLkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'ArtLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [ArtLkz] 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_StammPakete' 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_StammPakete' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] 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_StammPakete' 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_StammPakete' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] 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_StammPakete' and sc.[name] = 'TxtLkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [TxtLkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtLkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtLkz'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [TxtLkz] 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_StammPakete' and sc.[name] = 'TxtNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [TxtNr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_StammPakete' and sc.[name] = 'TxtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_StammPakete] ALTER COLUMN [TxtNr] varchar(18)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'StammPakete')
  DROP VIEW [dbo].[StammPakete]
GO
CREATE VIEW [dbo].[StammPakete] AS
  SELECT [RecId]
 ,[Gruppe]
 ,[PktNr]
 ,[LfdNr]
 ,[Suchen]
 ,[Txt1]
 ,[Txt2]
 ,[Txt3]
 ,[ArtLkz]
 ,[ArtNr]
 ,[TxtLkz]
 ,[TxtNr]
 ,[KzJumbo]
 ,[Aufwand]
 ,[KzDrk1]
 ,[KzAktiv]
 ,[Einheit]
  FROM [dbo].[ut_StammPakete]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_StammPakete')
  DROP PROCEDURE [dbo].[up_Sel_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_Sel_StammPakete]  @mRI integer
AS
  SELECT
    [Gruppe]
   ,[PktNr]
   ,[LfdNr]
   ,[Suchen]
   ,[Txt1]
   ,[Txt2]
   ,[Txt3]
   ,[ArtLkz]
   ,[ArtNr]
   ,[TxtLkz]
   ,[TxtNr]
   ,[KzJumbo]
   ,[Aufwand]
   ,[KzDrk1]
   ,[KzAktiv]
   ,[Einheit]
  FROM [dbo].[ut_StammPakete] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_StammPakete')
  DROP PROCEDURE [dbo].[up_Append_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_Append_StammPakete]
   @Param_Gruppe char(2)
  ,@Param_PktNr char(17)
  ,@Param_LfdNr char(3)
  ,@Param_Suchen char(15)
  ,@Param_Txt1 char(42)
  ,@Param_Txt2 char(42)
  ,@Param_Txt3 char(42)
  ,@Param_ArtLkz char(2)
  ,@Param_ArtNr char(18)
  ,@Param_TxtLkz char(2)
  ,@Param_TxtNr char(18)
  ,@Param_KzJumbo tinyint
  ,@Param_Aufwand float
  ,@Param_KzDrk1 integer
  ,@Param_KzAktiv tinyint
  ,@Param_Einheit integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_StammPakete()
      INSERT INTO [dbo].[ut_StammPakete] (RecId
        ,[Gruppe]
        ,[PktNr]
        ,[LfdNr]
        ,[Suchen]
        ,[Txt1]
        ,[Txt2]
        ,[Txt3]
        ,[ArtLkz]
        ,[ArtNr]
        ,[TxtLkz]
        ,[TxtNr]
        ,[KzJumbo]
        ,[Aufwand]
        ,[KzDrk1]
        ,[KzAktiv]
        ,[Einheit]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Gruppe))
        ,UPPER(RTRIM(@Param_PktNr))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,UPPER(RTRIM(@Param_Suchen))
        ,RTRIM(@Param_Txt1)
        ,RTRIM(@Param_Txt2)
        ,RTRIM(@Param_Txt3)
        ,UPPER(RTRIM(@Param_ArtLkz))
        ,UPPER(RTRIM(@Param_ArtNr))
        ,UPPER(RTRIM(@Param_TxtLkz))
        ,UPPER(RTRIM(@Param_TxtNr))
        ,@Param_KzJumbo
        ,@Param_Aufwand
        ,@Param_KzDrk1
        ,@Param_KzAktiv
        ,@Param_Einheit
          )
      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_StammPakete')
  DROP PROCEDURE [dbo].[up_Add_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_Add_StammPakete]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_StammPakete()
  INSERT INTO [dbo].[ut_StammPakete] (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_StammPakete')
  DROP PROCEDURE [dbo].[up_Upd_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_Upd_StammPakete] @mRI integer
  ,@Param_Gruppe char(2)
  ,@Param_PktNr char(17)
  ,@Param_LfdNr char(3)
  ,@Param_Suchen char(15)
  ,@Param_Txt1 char(42)
  ,@Param_Txt2 char(42)
  ,@Param_Txt3 char(42)
  ,@Param_ArtLkz char(2)
  ,@Param_ArtNr char(18)
  ,@Param_TxtLkz char(2)
  ,@Param_TxtNr char(18)
  ,@Param_KzJumbo tinyint
  ,@Param_Aufwand float
  ,@Param_KzDrk1 integer
  ,@Param_KzAktiv tinyint
  ,@Param_Einheit integer
AS
  UPDATE [dbo].[ut_StammPakete] SET
    [Gruppe] = UPPER(RTRIM(@Param_Gruppe))
   ,[PktNr] = UPPER(RTRIM(@Param_PktNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
   ,[Txt1] = RTRIM(@Param_Txt1)
   ,[Txt2] = RTRIM(@Param_Txt2)
   ,[Txt3] = RTRIM(@Param_Txt3)
   ,[ArtLkz] = UPPER(RTRIM(@Param_ArtLkz))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[TxtLkz] = UPPER(RTRIM(@Param_TxtLkz))
   ,[TxtNr] = UPPER(RTRIM(@Param_TxtNr))
   ,[KzJumbo] = @Param_KzJumbo
   ,[Aufwand] = @Param_Aufwand
   ,[KzDrk1] = @Param_KzDrk1
   ,[KzAktiv] = @Param_KzAktiv
   ,[Einheit] = @Param_Einheit
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_StammPakete')
  DROP PROCEDURE [dbo].[up_Upd2_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_Upd2_StammPakete] @Param_RecId varchar(20)
  ,@Param_Gruppe varchar(2)
  ,@Param_PktNr varchar(17)
  ,@Param_LfdNr varchar(3)
  ,@Param_Suchen varchar(15)
  ,@Param_Txt1 varchar(42)
  ,@Param_Txt2 varchar(42)
  ,@Param_Txt3 varchar(42)
  ,@Param_ArtLkz varchar(2)
  ,@Param_ArtNr varchar(18)
  ,@Param_TxtLkz varchar(2)
  ,@Param_TxtNr varchar(18)
  ,@Param_KzJumbo varchar(1)
  ,@Param_Aufwand varchar(10)
  ,@Param_KzDrk1 varchar(1)
  ,@Param_KzAktiv varchar(1)
  ,@Param_Einheit varchar(4)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_StammPakete] SET
    [Gruppe] = UPPER(RTRIM(@Param_Gruppe))
   ,[PktNr] = UPPER(RTRIM(@Param_PktNr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
   ,[Txt1] = RTRIM(@Param_Txt1)
   ,[Txt2] = RTRIM(@Param_Txt2)
   ,[Txt3] = RTRIM(@Param_Txt3)
   ,[ArtLkz] = UPPER(RTRIM(@Param_ArtLkz))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[TxtLkz] = UPPER(RTRIM(@Param_TxtLkz))
   ,[TxtNr] = UPPER(RTRIM(@Param_TxtNr))
   ,[KzJumbo] = dbo.itcfunc_integer(@Param_KzJumbo)
   ,[Aufwand] = dbo.itcfunc_float(@Param_Aufwand)
   ,[KzDrk1] = dbo.itcfunc_integer(@Param_KzDrk1)
   ,[KzAktiv] = dbo.itcfunc_integer(@Param_KzAktiv)
   ,[Einheit] = dbo.itcfunc_integer(@Param_Einheit)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_StammPakete] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_StammPakete')
  DROP PROCEDURE [dbo].[up_TrSel_StammPakete]
GO
CREATE PROCEDURE [dbo].[up_TrSel_StammPakete]
AS
  SELECT RecId,
    LTRIM(RTRIM([Gruppe]))
   +LTRIM(RTRIM([PktNr]))
   +LTRIM(RTRIM([LfdNr]))
   +LTRIM(RTRIM([Suchen]))
   +LTRIM(RTRIM([Txt1]))
   +LTRIM(RTRIM([Txt2]))
   +LTRIM(RTRIM([Txt3]))
   +LTRIM(RTRIM([ArtLkz]))
   +LTRIM(RTRIM([ArtNr]))
   +LTRIM(RTRIM([TxtLkz]))
   +LTRIM(RTRIM([TxtNr]))
  AS Data FROM [dbo].[ut_StammPakete] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPakete_Key0')
  DROP PROCEDURE [dbo].[up_Next_StammPakete_Key0]
GO
CREATE PROCEDURE [dbo].[up_Next_StammPakete_Key0] @oldRecId integer AS
  DECLARE @oldLfdNr char(3)
  DECLARE @oldPktNr char(17)
  SELECT @oldLfdNr = LfdNr, @oldPktNr = PktNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0)
    WHERE (LfdNr = @oldLfdNr) AND (PktNr = @oldPktNr) AND (RecId > @oldRecId)
    ORDER BY LfdNr,PktNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0)
    WHERE (LfdNr > @oldLfdNr)
    ORDER BY LfdNr,PktNr,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_StammPakete_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_StammPakete_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_StammPakete_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPakete_Key0')
  DROP PROCEDURE [dbo].[up_Prior_StammPakete_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_StammPakete_Key0] @oldRecId integer AS
  DECLARE @oldLfdNr char(3)
  DECLARE @oldPktNr char(17)
  SELECT @oldLfdNr = LfdNr, @oldPktNr = PktNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0)
    WHERE (LfdNr = @oldLfdNr) AND (PktNr = @oldPktNr) AND (RecId < @oldRecId)
    ORDER BY LfdNr desc,PktNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0)
    WHERE (LfdNr < @oldLfdNr)
    ORDER BY LfdNr desc,PktNr 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_StammPakete_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_StammPakete_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_StammPakete_Key0] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr AND 
      PktNr = @mPktNr
      ORDER BY LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr
      ORDER BY LfdNr,PktNr,RecId
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr AND 
      PktNr >= @mPktNr AND SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY LfdNr,PktNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr AND 
      PktNr = @mPktNr
      ORDER BY LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr
      ORDER BY LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key0) WHERE 
      LfdNr = @mLfdNr AND 
      SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_StammPakete_Key1')
  DROP PROCEDURE [dbo].[up_First_StammPakete_Key1]
GO
CREATE PROCEDURE [dbo].[up_First_StammPakete_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) ORDER BY LfdNr,Suchen,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_StammPakete_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_StammPakete_Key1] AS EXECUTE [dbo].[up_First_StammPakete_Key1]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPakete_Key1')
  DROP PROCEDURE [dbo].[up_Next_StammPakete_Key1]
GO
CREATE PROCEDURE [dbo].[up_Next_StammPakete_Key1] @oldRecId integer AS
  DECLARE @oldLfdNr char(3)
  DECLARE @oldSuchen char(15)
  SELECT @oldLfdNr = LfdNr, @oldSuchen = Suchen FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr = @oldLfdNr) AND (Suchen = @oldSuchen) AND (RecId > @oldRecId)
    ORDER BY LfdNr,Suchen,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr = @oldLfdNr) AND (Suchen > @oldSuchen)
    ORDER BY LfdNr,Suchen,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr > @oldLfdNr)
    ORDER BY LfdNr,Suchen,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_StammPakete_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_StammPakete_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Next_StammPakete_Key1] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPakete_Key1')
  DROP PROCEDURE [dbo].[up_Prior_StammPakete_Key1]
GO
CREATE PROCEDURE [dbo].[up_Prior_StammPakete_Key1] @oldRecId integer AS
  DECLARE @oldLfdNr char(3)
  DECLARE @oldSuchen char(15)
  SELECT @oldLfdNr = LfdNr, @oldSuchen = Suchen FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr = @oldLfdNr) AND (Suchen = @oldSuchen) AND (RecId < @oldRecId)
    ORDER BY LfdNr desc,Suchen desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr = @oldLfdNr) AND (Suchen < @oldSuchen)
    ORDER BY LfdNr desc,Suchen desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1)
    WHERE (LfdNr < @oldLfdNr)
    ORDER BY LfdNr desc,Suchen 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_StammPakete_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_StammPakete_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Prior_StammPakete_Key1] @oldRecId')
GO

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

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

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

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr AND 
      Suchen = @mSuchen
      ORDER BY LfdNr,Suchen,RecId
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr
      ORDER BY LfdNr,Suchen,RecId
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY LfdNr,Suchen,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr AND 
      Suchen >= @mSuchen AND SUBSTRING(Suchen,1,LEN(@mSuchen)) = @mSuchen
      ORDER BY LfdNr,Suchen,RecId
    RETURN
  END

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

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

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr AND 
      Suchen = @mSuchen
      ORDER BY LfdNr desc,Suchen desc,RecId desc
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr
      ORDER BY LfdNr desc,Suchen desc,RecId desc
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY LfdNr desc,Suchen desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key1) WHERE 
      LfdNr = @mLfdNr AND 
      SUBSTRING(Suchen,1,LEN(@mSuchen)) = @mSuchen
      ORDER BY LfdNr desc,Suchen desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_StammPakete_Key2')
  DROP PROCEDURE [dbo].[up_First_StammPakete_Key2]
GO
CREATE PROCEDURE [dbo].[up_First_StammPakete_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) ORDER BY PktNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_StammPakete_Key2')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_StammPakete_Key2] AS EXECUTE [dbo].[up_First_StammPakete_Key2]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_StammPakete_Key2')
  DROP PROCEDURE [dbo].[up_Last_StammPakete_Key2]
GO
CREATE PROCEDURE [dbo].[up_Last_StammPakete_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) ORDER BY PktNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_StammPakete_Key2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_StammPakete_Key2] AS EXECUTE [dbo].[up_Last_StammPakete_Key2]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPakete_Key2')
  DROP PROCEDURE [dbo].[up_Next_StammPakete_Key2]
GO
CREATE PROCEDURE [dbo].[up_Next_StammPakete_Key2] @oldRecId integer AS
  DECLARE @oldPktNr char(17)
  DECLARE @oldLfdNr char(3)
  SELECT @oldPktNr = PktNr, @oldLfdNr = LfdNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2)
    WHERE (PktNr = @oldPktNr) AND (LfdNr > @oldLfdNr)
    ORDER BY PktNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2)
    WHERE (PktNr > @oldPktNr)
    ORDER BY PktNr,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_StammPakete_Key2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_StammPakete_Key2] @oldRecId integer AS EXECUTE [dbo].[up_Next_StammPakete_Key2] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPakete_Key2')
  DROP PROCEDURE [dbo].[up_Prior_StammPakete_Key2]
GO
CREATE PROCEDURE [dbo].[up_Prior_StammPakete_Key2] @oldRecId integer AS
  DECLARE @oldPktNr char(17)
  DECLARE @oldLfdNr char(3)
  SELECT @oldPktNr = PktNr, @oldLfdNr = LfdNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2)
    WHERE (PktNr = @oldPktNr) AND (LfdNr < @oldLfdNr)
    ORDER BY PktNr 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_StammPakete] WITH(INDEX = Ik_Key2)
    WHERE (PktNr < @oldPktNr)
    ORDER BY PktNr 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_StammPakete_Key2')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_StammPakete_Key2] @oldRecId integer AS EXECUTE [dbo].[up_Prior_StammPakete_Key2] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr AND 
      LfdNr = @mLfdNr
      ORDER BY PktNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr
      ORDER BY PktNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr >= @mPktNr AND SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY PktNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY PktNr,LfdNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr AND 
      LfdNr = @mLfdNr
      ORDER BY PktNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr
      ORDER BY PktNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 17 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY PktNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key2) WHERE 
      PktNr = @mPktNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY PktNr 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_StammPakete_Key2')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_StammPakete_Key2] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_StammPakete_Key2] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_First_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_First_StammPakete_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) ORDER BY ArtLkz,ArtNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_StammPakete_Key3')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_StammPakete_Key3] AS EXECUTE [dbo].[up_First_StammPakete_Key3]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_Last_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_Last_StammPakete_Key3] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) ORDER BY ArtLkz desc,ArtNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_StammPakete_Key3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_StammPakete_Key3] AS EXECUTE [dbo].[up_Last_StammPakete_Key3]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_Next_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_Next_StammPakete_Key3] @oldRecId integer AS
  DECLARE @oldArtLkz char(2)
  DECLARE @oldArtNr char(18)
  DECLARE @oldLfdNr char(3)
  SELECT @oldArtLkz = ArtLkz, @oldArtNr = ArtNr, @oldLfdNr = LfdNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr = @oldArtNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY ArtLkz,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr = @oldArtNr) AND (LfdNr > @oldLfdNr)
    ORDER BY ArtLkz,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr > @oldArtNr)
    ORDER BY ArtLkz,ArtNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz > @oldArtLkz)
    ORDER BY ArtLkz,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_StammPakete_Key3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_StammPakete_Key3] @oldRecId integer AS EXECUTE [dbo].[up_Next_StammPakete_Key3] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_Prior_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_Prior_StammPakete_Key3] @oldRecId integer AS
  DECLARE @oldArtLkz char(2)
  DECLARE @oldArtNr char(18)
  DECLARE @oldLfdNr char(3)
  SELECT @oldArtLkz = ArtLkz, @oldArtNr = ArtNr, @oldLfdNr = LfdNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr = @oldArtNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY ArtLkz 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_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr = @oldArtNr) AND (LfdNr < @oldLfdNr)
    ORDER BY ArtLkz 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_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz = @oldArtLkz) AND (ArtNr < @oldArtNr)
    ORDER BY ArtLkz 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_StammPakete] WITH(INDEX = Ik_Key3)
    WHERE (ArtLkz < @oldArtLkz)
    ORDER BY ArtLkz 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_StammPakete_Key3')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_StammPakete_Key3] @oldRecId integer AS EXECUTE [dbo].[up_Prior_StammPakete_Key3] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_FindF_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_FindF_StammPakete_Key3] @xKey char(250), @xLen integer AS
  DECLARE @mArtLkz 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(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,3)

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

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr AND 
      LfdNr = @mLfdNr
      ORDER BY ArtLkz,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr
      ORDER BY ArtLkz,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz
      ORDER BY ArtLkz,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_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz >= @mArtLkz AND SUBSTRING(ArtLkz,1,LEN(@mArtLkz)) = @mArtLkz
      ORDER BY ArtLkz,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr >= @mArtNr AND SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      ORDER BY ArtLkz,ArtNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY ArtLkz,ArtNr,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_StammPakete_Key3')
  DROP PROCEDURE [dbo].[up_FindL_StammPakete_Key3]
GO
CREATE PROCEDURE [dbo].[up_FindL_StammPakete_Key3] @xKey char(250), @xLen integer AS
  DECLARE @mArtLkz 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(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,3)

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

  IF @xLen = 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr AND 
      LfdNr = @mLfdNr
      ORDER BY ArtLkz desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr
      ORDER BY ArtLkz desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz
      ORDER BY ArtLkz 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_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      SUBSTRING(ArtLkz,1,LEN(@mArtLkz)) = @mArtLkz
      ORDER BY ArtLkz desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      SUBSTRING(ArtNr,1,LEN(@mArtNr)) = @mArtNr
      ORDER BY ArtLkz desc,ArtNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 23 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key3) WHERE 
      ArtLkz = @mArtLkz AND 
      ArtNr = @mArtNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY ArtLkz 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_StammPakete_Key3')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_StammPakete_Key3] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_StammPakete_Key3] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_First_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_First_StammPakete_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) ORDER BY Gruppe,LfdNr,PktNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_StammPakete_Key4')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_StammPakete_Key4] AS EXECUTE [dbo].[up_First_StammPakete_Key4]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_Last_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_Last_StammPakete_Key4] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_StammPakete_Key4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_StammPakete_Key4] AS EXECUTE [dbo].[up_Last_StammPakete_Key4]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_Next_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_Next_StammPakete_Key4] @oldRecId integer AS
  DECLARE @oldGruppe char(2)
  DECLARE @oldLfdNr char(3)
  DECLARE @oldPktNr char(17)
  SELECT @oldGruppe = Gruppe, @oldLfdNr = LfdNr, @oldPktNr = PktNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr = @oldLfdNr) AND (PktNr = @oldPktNr) AND (RecId > @oldRecId)
    ORDER BY Gruppe,LfdNr,PktNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr = @oldLfdNr) AND (PktNr > @oldPktNr)
    ORDER BY Gruppe,LfdNr,PktNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr > @oldLfdNr)
    ORDER BY Gruppe,LfdNr,PktNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe > @oldGruppe)
    ORDER BY Gruppe,LfdNr,PktNr,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_StammPakete_Key4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_StammPakete_Key4] @oldRecId integer AS EXECUTE [dbo].[up_Next_StammPakete_Key4] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_Prior_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_Prior_StammPakete_Key4] @oldRecId integer AS
  DECLARE @oldGruppe char(2)
  DECLARE @oldLfdNr char(3)
  DECLARE @oldPktNr char(17)
  SELECT @oldGruppe = Gruppe, @oldLfdNr = LfdNr, @oldPktNr = PktNr FROM [dbo].[ut_StammPakete] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr = @oldLfdNr) AND (PktNr = @oldPktNr) AND (RecId < @oldRecId)
    ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr = @oldLfdNr) AND (PktNr < @oldPktNr)
    ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe = @oldGruppe) AND (LfdNr < @oldLfdNr)
    ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4)
    WHERE (Gruppe < @oldGruppe)
    ORDER BY Gruppe desc,LfdNr desc,PktNr 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_StammPakete_Key4')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_StammPakete_Key4] @oldRecId integer AS EXECUTE [dbo].[up_Prior_StammPakete_Key4] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_FindF_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_FindF_StammPakete_Key4] @xKey char(250), @xLen integer AS
  DECLARE @mGruppe char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mLfdNr char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,3)
  DECLARE @mPktNr char(17) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,17)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr AND 
      PktNr = @mPktNr
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe >= @mGruppe AND SUBSTRING(Gruppe,1,LEN(@mGruppe)) = @mGruppe
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr AND 
      PktNr >= @mPktNr AND SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY Gruppe,LfdNr,PktNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_StammPakete_Key4')
  DROP PROCEDURE [dbo].[up_FindL_StammPakete_Key4]
GO
CREATE PROCEDURE [dbo].[up_FindL_StammPakete_Key4] @xKey char(250), @xLen integer AS
  DECLARE @mGruppe char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mLfdNr char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,3)
  DECLARE @mPktNr char(17) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,17)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr AND 
      PktNr = @mPktNr
      ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr
      ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe
      ORDER BY Gruppe desc,LfdNr desc,PktNr 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_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      SUBSTRING(Gruppe,1,LEN(@mGruppe)) = @mGruppe
      ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_StammPakete] WITH(INDEX = Ik_Key4) WHERE 
      Gruppe = @mGruppe AND 
      LfdNr = @mLfdNr AND 
      SUBSTRING(PktNr,1,LEN(@mPktNr)) = @mPktNr
      ORDER BY Gruppe desc,LfdNr desc,PktNr desc,RecId desc
    RETURN
  END

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


