-- 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_ArtikelDimensionen')
BEGIN
CREATE TABLE [dbo].[ut_ArtikelDimensionen]
(
  [RecId]  integer NOT NULL
 ,[Nr] char(5) DEFAULT ''
 ,[LfdNr] char(3) DEFAULT ''
 ,[Bez] char(40) DEFAULT ''
  CONSTRAINT [Pk_ArtikelDimensionen]  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_ArtikelDimensionen_KeyDel')
  DROP TRIGGER [dbo].[tr_ArtikelDimensionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ArtikelDimensionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_ArtikelDimensionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ArtikelDimensionen_Keys')
  DROP TABLE [dbo].[ut_ArtikelDimensionen_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_ArtikelDimensionen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelDimensionen] ADD [Nr] char(5) 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_ArtikelDimensionen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelDimensionen] 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_ArtikelDimensionen' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_ArtikelDimensionen] ADD [Bez] char(40) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (5 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Nr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] ALTER COLUMN [Nr] char(5)
END
GO
IF (5 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Nr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] ALTER COLUMN [Nr] varchar(5)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' 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_ArtikelDimensionen' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] 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_ArtikelDimensionen' 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_ArtikelDimensionen' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] ALTER COLUMN [LfdNr] varchar(3)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Bez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] ALTER COLUMN [Bez] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Bez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_ArtikelDimensionen' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_ArtikelDimensionen] ALTER COLUMN [Bez] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ArtikelDimensionen')
  DROP VIEW [dbo].[ArtikelDimensionen]
GO
CREATE VIEW [dbo].[ArtikelDimensionen] AS
  SELECT [RecId]
 ,[Nr]
 ,[LfdNr]
 ,[Bez]
  FROM [dbo].[ut_ArtikelDimensionen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ArtikelDimensionen')
  DROP PROCEDURE [dbo].[up_Sel_ArtikelDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_ArtikelDimensionen]  @mRI integer
AS
  SELECT
    [Nr]
   ,[LfdNr]
   ,[Bez]
  FROM [dbo].[ut_ArtikelDimensionen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ArtikelDimensionen')
  DROP PROCEDURE [dbo].[up_Append_ArtikelDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Append_ArtikelDimensionen]
   @Param_Nr char(5)
  ,@Param_LfdNr char(3)
  ,@Param_Bez char(40)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_ArtikelDimensionen()
      INSERT INTO [dbo].[ut_ArtikelDimensionen] (RecId
        ,[Nr]
        ,[LfdNr]
        ,[Bez]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Nr))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,RTRIM(@Param_Bez)
          )
      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_ArtikelDimensionen')
  DROP PROCEDURE [dbo].[up_Add_ArtikelDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Add_ArtikelDimensionen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_ArtikelDimensionen()
  INSERT INTO [dbo].[ut_ArtikelDimensionen] (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_ArtikelDimensionen')
  DROP PROCEDURE [dbo].[up_Upd_ArtikelDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_ArtikelDimensionen] @mRI integer
  ,@Param_Nr char(5)
  ,@Param_LfdNr char(3)
  ,@Param_Bez char(40)
AS
  UPDATE [dbo].[ut_ArtikelDimensionen] SET
    [Nr] = UPPER(RTRIM(@Param_Nr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Bez] = RTRIM(@Param_Bez)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_ArtikelDimensionen')
  DROP PROCEDURE [dbo].[up_Upd2_ArtikelDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_ArtikelDimensionen] @Param_RecId varchar(20)
  ,@Param_Nr varchar(5)
  ,@Param_LfdNr varchar(3)
  ,@Param_Bez varchar(40)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_ArtikelDimensionen] SET
    [Nr] = UPPER(RTRIM(@Param_Nr))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Bez] = RTRIM(@Param_Bez)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_ArtikelDimensionen] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ArtikelDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Next_ArtikelDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_ArtikelDimensionen_Key00] @oldRecId integer AS
  DECLARE @oldNr char(5)
  DECLARE @oldLfdNr char(3)
  SELECT @oldNr = Nr, @oldLfdNr = LfdNr FROM [dbo].[ut_ArtikelDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_ArtikelDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_ArtikelDimensionen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_ArtikelDimensionen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ArtikelDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_ArtikelDimensionen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_ArtikelDimensionen_Key00] @oldRecId integer AS
  DECLARE @oldNr char(5)
  DECLARE @oldLfdNr char(3)
  SELECT @oldNr = Nr, @oldLfdNr = LfdNr FROM [dbo].[ut_ArtikelDimensionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (Nr = @oldNr) AND (LfdNr < @oldLfdNr)
    ORDER BY Nr 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_ArtikelDimensionen] WITH(INDEX = Ik_Key00)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_ArtikelDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_ArtikelDimensionen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_ArtikelDimensionen_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr AND 
      LfdNr = @mLfdNr
      ORDER BY Nr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Nr,LfdNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr AND 
      LfdNr = @mLfdNr
      ORDER BY Nr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ArtikelDimensionen] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY Nr 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_ArtikelDimensionen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_ArtikelDimensionen_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_ArtikelDimensionen_Key00] @xKey, @xLen')
GO


