-- 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_Tenderer')
BEGIN
CREATE TABLE [dbo].[ut_Tenderer]
(
  [RecId]  integer NOT NULL
 ,[TenderNo] char(20) DEFAULT ''
 ,[Tenderer] integer DEFAULT 0
 ,[TdCode] char(20) DEFAULT ''
 ,[TdName] char(40) DEFAULT ''
  CONSTRAINT [Pk_Tenderer]  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_Tenderer_KeyDel')
  DROP TRIGGER [dbo].[tr_Tenderer_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Tenderer_KeyUpd')
  DROP TRIGGER [dbo].[tr_Tenderer_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Tenderer_Keys')
  DROP TABLE [dbo].[ut_Tenderer_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_Tenderer' and sc.[name] = 'TenderNo')
BEGIN
  ALTER TABLE [dbo].[ut_Tenderer] ADD [TenderNo] char(20) 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_Tenderer' and sc.[name] = 'Tenderer')
BEGIN
  ALTER TABLE [dbo].[ut_Tenderer] ADD [Tenderer] 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_Tenderer' and sc.[name] = 'TdCode')
BEGIN
  ALTER TABLE [dbo].[ut_Tenderer] ADD [TdCode] char(20) 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_Tenderer' and sc.[name] = 'TdName')
BEGIN
  ALTER TABLE [dbo].[ut_Tenderer] ADD [TdName] char(40) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TenderNo'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TenderNo] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TenderNo'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TenderNo] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdCode'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdCode'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TdCode] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdCode'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdCode'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TdCode] varchar(20)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdName'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TdName] char(40)
END
GO
IF (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tenderer' and sc.[name] = 'TdName'))
BEGIN
   ALTER TABLE [dbo].[ut_Tenderer] ALTER COLUMN [TdName] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Tenderer')
  DROP VIEW [dbo].[Tenderer]
GO
CREATE VIEW [dbo].[Tenderer] AS
  SELECT [RecId]
 ,[TenderNo]
 ,[Tenderer]
 ,[TdCode]
 ,[TdName]
  FROM [dbo].[ut_Tenderer]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Tenderer')
  DROP PROCEDURE [dbo].[up_Sel_Tenderer]
GO
CREATE PROCEDURE [dbo].[up_Sel_Tenderer]  @mRI integer
AS
  SELECT
    [TenderNo]
   ,[Tenderer]
   ,[TdCode]
   ,[TdName]
  FROM [dbo].[ut_Tenderer] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Tenderer')
  DROP PROCEDURE [dbo].[up_Append_Tenderer]
GO
CREATE PROCEDURE [dbo].[up_Append_Tenderer]
   @Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_TdCode char(20)
  ,@Param_TdName 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_Tenderer()
      INSERT INTO [dbo].[ut_Tenderer] (RecId
        ,[TenderNo]
        ,[Tenderer]
        ,[TdCode]
        ,[TdName]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_TenderNo))
        ,@Param_Tenderer
        ,UPPER(RTRIM(@Param_TdCode))
        ,RTRIM(@Param_TdName)
          )
      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_Tenderer')
  DROP PROCEDURE [dbo].[up_Add_Tenderer]
GO
CREATE PROCEDURE [dbo].[up_Add_Tenderer]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Tenderer()
  INSERT INTO [dbo].[ut_Tenderer] (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_Tenderer')
  DROP PROCEDURE [dbo].[up_Upd_Tenderer]
GO
CREATE PROCEDURE [dbo].[up_Upd_Tenderer] @mRI integer
  ,@Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_TdCode char(20)
  ,@Param_TdName char(40)
AS
  UPDATE [dbo].[ut_Tenderer] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = @Param_Tenderer
   ,[TdCode] = UPPER(RTRIM(@Param_TdCode))
   ,[TdName] = RTRIM(@Param_TdName)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Tenderer')
  DROP PROCEDURE [dbo].[up_Upd2_Tenderer]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Tenderer] @Param_RecId varchar(20)
  ,@Param_TenderNo varchar(20)
  ,@Param_Tenderer varchar(2)
  ,@Param_TdCode varchar(20)
  ,@Param_TdName varchar(40)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Tenderer] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = dbo.itcfunc_integer(@Param_Tenderer)
   ,[TdCode] = UPPER(RTRIM(@Param_TdCode))
   ,[TdName] = RTRIM(@Param_TdName)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Tenderer] @mRI
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Tenderer_Key00')
  DROP PROCEDURE [dbo].[up_First_Tenderer_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Tenderer_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00) ORDER BY TenderNo,Tenderer,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Tenderer_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Tenderer_Key00] AS EXECUTE [dbo].[up_First_Tenderer_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Tenderer_Key00')
  DROP PROCEDURE [dbo].[up_Next_Tenderer_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Tenderer_Key00] @oldRecId integer AS
  DECLARE @oldTenderNo char(20)
  DECLARE @oldTenderer integer
  SELECT @oldTenderNo = TenderNo, @oldTenderer = Tenderer FROM [dbo].[ut_Tenderer] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (Tenderer = @oldTenderer) AND (RecId > @oldRecId)
    ORDER BY TenderNo,Tenderer,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (Tenderer > @oldTenderer)
    ORDER BY TenderNo,Tenderer,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo > @oldTenderNo)
    ORDER BY TenderNo,Tenderer,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_Tenderer_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Tenderer_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Tenderer_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tenderer_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Tenderer_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Tenderer_Key00] @oldRecId integer AS
  DECLARE @oldTenderNo char(20)
  DECLARE @oldTenderer integer
  SELECT @oldTenderNo = TenderNo, @oldTenderer = Tenderer FROM [dbo].[ut_Tenderer] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (Tenderer = @oldTenderer) AND (RecId < @oldRecId)
    ORDER BY TenderNo desc,Tenderer desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (Tenderer < @oldTenderer)
    ORDER BY TenderNo desc,Tenderer desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo < @oldTenderNo)
    ORDER BY TenderNo desc,Tenderer 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_Tenderer_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Tenderer_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Tenderer_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Tenderer_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Tenderer_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_Tenderer_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mTenderNo char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTenderer integer = dbo.itcfunc_KeyToken_integer(@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_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo AND 
      Tenderer = @mTenderer
      ORDER BY TenderNo,Tenderer,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo
      ORDER BY TenderNo,Tenderer,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo >= @mTenderNo AND SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      ORDER BY TenderNo,Tenderer,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Tenderer_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Tenderer_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_Tenderer_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mTenderNo char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mTenderer integer = dbo.itcfunc_KeyToken_integer(@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_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo AND 
      Tenderer = @mTenderer
      ORDER BY TenderNo desc,Tenderer desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo
      ORDER BY TenderNo desc,Tenderer desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tenderer] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      ORDER BY TenderNo desc,Tenderer desc,RecId desc
    RETURN
  END


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


