-- 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_NeZuschlaege')
BEGIN
CREATE TABLE [dbo].[ut_NeZuschlaege]
(
  [RecId]  integer NOT NULL
 ,[Lkz] char(2) DEFAULT ''
 ,[Artnr] char(18) DEFAULT ''
 ,[NeKz] char(2) DEFAULT ''
 ,[Gewicht] float DEFAULT 0
 ,[Gebinde] float DEFAULT 0
 ,[BasisPreis] float DEFAULT 0
 ,[KgJeBasis] float DEFAULT 0
  CONSTRAINT [Pk_NeZuschlaege]  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_NeZuschlaege_KeyDel')
  DROP TRIGGER [dbo].[tr_NeZuschlaege_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_NeZuschlaege_KeyUpd')
  DROP TRIGGER [dbo].[tr_NeZuschlaege_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_NeZuschlaege_Keys')
  DROP TABLE [dbo].[ut_NeZuschlaege_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_NeZuschlaege' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' and sc.[name] = 'NeKz')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] ADD [NeKz] 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_NeZuschlaege' and sc.[name] = 'Gewicht')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] ADD [Gewicht] 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_NeZuschlaege' and sc.[name] = 'Gebinde')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] ADD [Gebinde] 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_NeZuschlaege' and sc.[name] = 'BasisPreis')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] ADD [BasisPreis] 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_NeZuschlaege' and sc.[name] = 'KgJeBasis')
BEGIN
  ALTER TABLE [dbo].[ut_NeZuschlaege] ADD [KgJeBasis] 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_NeZuschlaege' 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_NeZuschlaege' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' 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_NeZuschlaege' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' 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_NeZuschlaege' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' 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_NeZuschlaege' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] 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_NeZuschlaege' and sc.[name] = 'NeKz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_NeZuschlaege' and sc.[name] = 'NeKz'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] ALTER COLUMN [NeKz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_NeZuschlaege' and sc.[name] = 'NeKz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_NeZuschlaege' and sc.[name] = 'NeKz'))
BEGIN
   ALTER TABLE [dbo].[ut_NeZuschlaege] ALTER COLUMN [NeKz] varchar(2)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'NeZuschlaege')
  DROP VIEW [dbo].[NeZuschlaege]
GO
CREATE VIEW [dbo].[NeZuschlaege] AS
  SELECT [RecId]
 ,[Lkz]
 ,[Artnr]
 ,[NeKz]
 ,[Gewicht]
 ,[Gebinde]
 ,[BasisPreis]
 ,[KgJeBasis]
  FROM [dbo].[ut_NeZuschlaege]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_NeZuschlaege')
  DROP PROCEDURE [dbo].[up_Sel_NeZuschlaege]
GO
CREATE PROCEDURE [dbo].[up_Sel_NeZuschlaege]  @mRI integer
AS
  SELECT
    [Lkz]
   ,[Artnr]
   ,[NeKz]
   ,[Gewicht]
   ,[Gebinde]
   ,[BasisPreis]
   ,[KgJeBasis]
  FROM [dbo].[ut_NeZuschlaege] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_NeZuschlaege')
  DROP PROCEDURE [dbo].[up_Append_NeZuschlaege]
GO
CREATE PROCEDURE [dbo].[up_Append_NeZuschlaege]
   @Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_NeKz char(2)
  ,@Param_Gewicht float
  ,@Param_Gebinde float
  ,@Param_BasisPreis float
  ,@Param_KgJeBasis float
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_NeZuschlaege()
      INSERT INTO [dbo].[ut_NeZuschlaege] (RecId
        ,[Lkz]
        ,[Artnr]
        ,[NeKz]
        ,[Gewicht]
        ,[Gebinde]
        ,[BasisPreis]
        ,[KgJeBasis]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_Artnr))
        ,UPPER(RTRIM(@Param_NeKz))
        ,@Param_Gewicht
        ,@Param_Gebinde
        ,@Param_BasisPreis
        ,@Param_KgJeBasis
          )
      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_NeZuschlaege')
  DROP PROCEDURE [dbo].[up_Add_NeZuschlaege]
GO
CREATE PROCEDURE [dbo].[up_Add_NeZuschlaege]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_NeZuschlaege()
  INSERT INTO [dbo].[ut_NeZuschlaege] (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_NeZuschlaege')
  DROP PROCEDURE [dbo].[up_Upd_NeZuschlaege]
GO
CREATE PROCEDURE [dbo].[up_Upd_NeZuschlaege] @mRI integer
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_NeKz char(2)
  ,@Param_Gewicht float
  ,@Param_Gebinde float
  ,@Param_BasisPreis float
  ,@Param_KgJeBasis float
AS
  UPDATE [dbo].[ut_NeZuschlaege] SET
    [Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[NeKz] = UPPER(RTRIM(@Param_NeKz))
   ,[Gewicht] = @Param_Gewicht
   ,[Gebinde] = @Param_Gebinde
   ,[BasisPreis] = @Param_BasisPreis
   ,[KgJeBasis] = @Param_KgJeBasis
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_NeZuschlaege')
  DROP PROCEDURE [dbo].[up_Upd2_NeZuschlaege]
GO
CREATE PROCEDURE [dbo].[up_Upd2_NeZuschlaege] @Param_RecId varchar(20)
  ,@Param_Lkz varchar(2)
  ,@Param_Artnr varchar(18)
  ,@Param_NeKz varchar(2)
  ,@Param_Gewicht varchar(8)
  ,@Param_Gebinde varchar(12)
  ,@Param_BasisPreis varchar(8)
  ,@Param_KgJeBasis varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_NeZuschlaege] SET
    [Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[NeKz] = UPPER(RTRIM(@Param_NeKz))
   ,[Gewicht] = dbo.itcfunc_float(@Param_Gewicht)
   ,[Gebinde] = dbo.itcfunc_float(@Param_Gebinde)
   ,[BasisPreis] = dbo.itcfunc_float(@Param_BasisPreis)
   ,[KgJeBasis] = dbo.itcfunc_float(@Param_KgJeBasis)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_NeZuschlaege] @mRI
GO

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

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_NeZuschlaege] WITH(INDEX = Ik_Key0)
    WHERE (Lkz > @oldLkz)
    ORDER BY Lkz,Artnr,NeKz,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_NeZuschlaege_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_NeZuschlaege_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_NeZuschlaege_Key0] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_NeZuschlaege] WITH(INDEX = Ik_Key0)
    WHERE (Lkz < @oldLkz)
    ORDER BY Lkz desc,Artnr desc,NeKz 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_NeZuschlaege_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_NeZuschlaege_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_NeZuschlaege_Key0] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_NeZuschlaege_Key0')
  DROP PROCEDURE [dbo].[up_FindF_NeZuschlaege_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindF_NeZuschlaege_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 @mNeKz 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_NeZuschlaege] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      NeKz = @mNeKz
      ORDER BY Lkz,Artnr,NeKz,RecId
    RETURN
  END

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

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_NeZuschlaege_Key0')
  DROP PROCEDURE [dbo].[up_FindL_NeZuschlaege_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_NeZuschlaege_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 @mNeKz 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_NeZuschlaege] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz AND 
      Artnr = @mArtnr AND 
      NeKz = @mNeKz
      ORDER BY Lkz desc,Artnr desc,NeKz desc,RecId desc
    RETURN
  END

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_NeZuschlaege] WITH(INDEX = Ik_Key0) WHERE 
      Lkz = @mLkz
      ORDER BY Lkz desc,Artnr desc,NeKz 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_NeZuschlaege] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(Lkz,1,LEN(@mLkz)) = @mLkz
      ORDER BY Lkz desc,Artnr desc,NeKz desc,RecId desc
    RETURN
  END

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

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

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


