-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, 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_LagerEinUst')
BEGIN
CREATE TABLE [dbo].[ut_LagerEinUst]
(
  [RecId]  integer NOT NULL
 ,[BelNr] integer DEFAULT 0
 ,[UstSchl] char(3) DEFAULT ''
 ,[Netto] float DEFAULT 0
 ,[UstVH] float DEFAULT 0
 ,[Ust] float DEFAULT 0
 ,[Brutto] float DEFAULT 0
 ,[UstKorrekt] float DEFAULT 0
 ,[BelTyp] char(1) DEFAULT ''
  CONSTRAINT [Pk_LagerEinUst]  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_LagerEinUst_KeyDel')
  DROP TRIGGER [dbo].[tr_LagerEinUst_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_LagerEinUst_KeyUpd')
  DROP TRIGGER [dbo].[tr_LagerEinUst_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_LagerEinUst_Keys')
  DROP TABLE [dbo].[ut_LagerEinUst_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_LagerEinUst' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [BelNr] 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_LagerEinUst' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [UstSchl] 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_LagerEinUst' and sc.[name] = 'Netto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [Netto] 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_LagerEinUst' and sc.[name] = 'UstVH')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [UstVH] 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_LagerEinUst' and sc.[name] = 'Ust')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [Ust] 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_LagerEinUst' and sc.[name] = 'Brutto')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [Brutto] 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_LagerEinUst' and sc.[name] = 'UstKorrekt')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [UstKorrekt] 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_LagerEinUst' and sc.[name] = 'BelTyp')
BEGIN
  ALTER TABLE [dbo].[ut_LagerEinUst] ADD [BelTyp] char(1) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinUst' and sc.[name] = 'UstSchl')
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinUst] ALTER COLUMN [UstSchl] char(3)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_LagerEinUst' and sc.[name] = 'BelTyp')
BEGIN
   ALTER TABLE [dbo].[ut_LagerEinUst] ALTER COLUMN [BelTyp] char(1)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'LagerEinUst')
  DROP VIEW [dbo].[LagerEinUst]
GO
CREATE VIEW [dbo].[LagerEinUst] AS
  SELECT [RecId]
 ,[BelNr]
 ,[UstSchl]
 ,[Netto]
 ,[UstVH]
 ,[Ust]
 ,[Brutto]
 ,[UstKorrekt]
 ,[BelTyp]
  FROM [dbo].[ut_LagerEinUst]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_LagerEinUst')
  DROP PROCEDURE [dbo].[up_Sel_LagerEinUst]
GO
CREATE PROCEDURE [dbo].[up_Sel_LagerEinUst]  @mRI integer
AS
  SELECT
    [BelNr]
   ,[UstSchl]
   ,[Netto]
   ,[UstVH]
   ,[Ust]
   ,[Brutto]
   ,[UstKorrekt]
   ,[BelTyp]
  FROM [dbo].[ut_LagerEinUst] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_LagerEinUst')
  DROP PROCEDURE [dbo].[up_Append_LagerEinUst]
GO
CREATE PROCEDURE [dbo].[up_Append_LagerEinUst]
   @Param_BelNr integer
  ,@Param_UstSchl char(3)
  ,@Param_Netto float
  ,@Param_UstVH float
  ,@Param_Ust float
  ,@Param_Brutto float
  ,@Param_UstKorrekt float
  ,@Param_BelTyp char(1)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_LagerEinUst()
      INSERT INTO [dbo].[ut_LagerEinUst] (RecId
        ,[BelNr]
        ,[UstSchl]
        ,[Netto]
        ,[UstVH]
        ,[Ust]
        ,[Brutto]
        ,[UstKorrekt]
        ,[BelTyp]
          ) VALUES (@NeueId
        ,@Param_BelNr
        ,UPPER(RTRIM(@Param_UstSchl))
        ,@Param_Netto
        ,@Param_UstVH
        ,@Param_Ust
        ,@Param_Brutto
        ,@Param_UstKorrekt
        ,UPPER(RTRIM(@Param_BelTyp))
          )
      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_LagerEinUst')
  DROP PROCEDURE [dbo].[up_Add_LagerEinUst]
GO
CREATE PROCEDURE [dbo].[up_Add_LagerEinUst]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_LagerEinUst()
  INSERT INTO [dbo].[ut_LagerEinUst] (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_LagerEinUst')
  DROP PROCEDURE [dbo].[up_Upd_LagerEinUst]
GO
CREATE PROCEDURE [dbo].[up_Upd_LagerEinUst] @mRI integer
  ,@Param_BelNr integer
  ,@Param_UstSchl char(3)
  ,@Param_Netto float
  ,@Param_UstVH float
  ,@Param_Ust float
  ,@Param_Brutto float
  ,@Param_UstKorrekt float
  ,@Param_BelTyp char(1)
AS
  UPDATE [dbo].[ut_LagerEinUst] SET
    [BelNr] = @Param_BelNr
   ,[UstSchl] = UPPER(RTRIM(@Param_UstSchl))
   ,[Netto] = @Param_Netto
   ,[UstVH] = @Param_UstVH
   ,[Ust] = @Param_Ust
   ,[Brutto] = @Param_Brutto
   ,[UstKorrekt] = @Param_UstKorrekt
   ,[BelTyp] = UPPER(RTRIM(@Param_BelTyp))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_LagerEinUst')
  DROP PROCEDURE [dbo].[up_Upd2_LagerEinUst]
GO
CREATE PROCEDURE [dbo].[up_Upd2_LagerEinUst] @Param_RecId varchar(20)
  ,@Param_BelNr varchar(8)
  ,@Param_UstSchl varchar(3)
  ,@Param_Netto varchar(12)
  ,@Param_UstVH varchar(5)
  ,@Param_Ust varchar(12)
  ,@Param_Brutto varchar(12)
  ,@Param_UstKorrekt varchar(5)
  ,@Param_BelTyp varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_LagerEinUst] SET
    [BelNr] = dbo.itcfunc_integer(@Param_BelNr)
   ,[UstSchl] = UPPER(RTRIM(@Param_UstSchl))
   ,[Netto] = dbo.itcfunc_float(@Param_Netto)
   ,[UstVH] = dbo.itcfunc_float(@Param_UstVH)
   ,[Ust] = dbo.itcfunc_float(@Param_Ust)
   ,[Brutto] = dbo.itcfunc_float(@Param_Brutto)
   ,[UstKorrekt] = dbo.itcfunc_float(@Param_UstKorrekt)
   ,[BelTyp] = UPPER(RTRIM(@Param_BelTyp))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_LagerEinUst] @mRI
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_LagerEinUst_Key00')
  DROP PROCEDURE [dbo].[up_First_LagerEinUst_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_LagerEinUst_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) ORDER BY BelTyp,BelNr,UstSchl,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_LagerEinUst_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_LagerEinUst_Key00] AS EXECUTE [dbo].[up_First_LagerEinUst_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_LagerEinUst_Key00')
  DROP PROCEDURE [dbo].[up_Next_LagerEinUst_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_LagerEinUst_Key00] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldUstSchl char(3)
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldUstSchl = UstSchl FROM [dbo].[ut_LagerEinUst] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UstSchl > @oldUstSchl)
    ORDER BY BelTyp,BelNr,UstSchl,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr > @oldBelNr)
    ORDER BY BelTyp,BelNr,UstSchl,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp > @oldBelTyp)
    ORDER BY BelTyp,BelNr,UstSchl,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_LagerEinUst_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_LagerEinUst_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_LagerEinUst_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_LagerEinUst_Key00')
  DROP PROCEDURE [dbo].[up_Prior_LagerEinUst_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_LagerEinUst_Key00] @oldRecId integer AS
  DECLARE @oldBelTyp char(1)
  DECLARE @oldBelNr integer
  DECLARE @oldUstSchl char(3)
  SELECT @oldBelTyp = BelTyp, @oldBelNr = BelNr, @oldUstSchl = UstSchl FROM [dbo].[ut_LagerEinUst] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr = @oldBelNr) AND (UstSchl < @oldUstSchl)
    ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp = @oldBelTyp) AND (BelNr < @oldBelNr)
    ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00)
    WHERE (BelTyp < @oldBelTyp)
    ORDER BY BelTyp desc,BelNr desc,UstSchl 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_LagerEinUst_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_LagerEinUst_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_LagerEinUst_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_LagerEinUst_Key00')
  DROP PROCEDURE [dbo].[up_FindF_LagerEinUst_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_LagerEinUst_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mBelTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mUstSchl char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,3)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UstSchl = @mUstSchl
      ORDER BY BelTyp,BelNr,UstSchl,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp,BelNr,UstSchl,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp,BelNr,UstSchl,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp >= @mBelTyp AND SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp,BelNr,UstSchl,RecId
    RETURN
  END


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UstSchl >= @mUstSchl AND SUBSTRING(UstSchl,1,LEN(@mUstSchl)) = @mUstSchl
      ORDER BY BelTyp,BelNr,UstSchl,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_LagerEinUst_Key00')
  DROP PROCEDURE [dbo].[up_FindL_LagerEinUst_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_LagerEinUst_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mBelTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mBelNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mUstSchl char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,10,3)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      UstSchl = @mUstSchl
      ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr
      ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(BelTyp,1,LEN(@mBelTyp)) = @mBelTyp
      ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
    RETURN
  END


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_LagerEinUst] WITH(INDEX = Ik_Key00) WHERE 
      BelTyp = @mBelTyp AND 
      BelNr = @mBelNr AND 
      SUBSTRING(UstSchl,1,LEN(@mUstSchl)) = @mUstSchl
      ORDER BY BelTyp desc,BelNr desc,UstSchl desc,RecId desc
    RETURN
  END

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


