-- 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_TenderPrice')
BEGIN
CREATE TABLE [dbo].[ut_TenderPrice]
(
  [RecId]  integer NOT NULL
 ,[TenderNo] char(20) DEFAULT ''
 ,[Tenderer] integer DEFAULT 0
 ,[PosRefKey] char(6) DEFAULT ''
 ,[QtyTBD] tinyint DEFAULT 0
 ,[Qty] float DEFAULT 0
 ,[QU] char(6) DEFAULT ''
 ,[UP] float DEFAULT 0
 ,[DscntPcnt] float DEFAULT 0
 ,[IT] float DEFAULT 0
 ,[QtyPurch] float DEFAULT 0
 ,[Descrip] char(50) DEFAULT ''
 ,[Inquired] tinyint DEFAULT 0
 ,[Vkz] integer DEFAULT 0
  CONSTRAINT [Pk_TenderPrice]  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_TenderPrice_KeyDel')
  DROP TRIGGER [dbo].[tr_TenderPrice_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_TenderPrice_KeyUpd')
  DROP TRIGGER [dbo].[tr_TenderPrice_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_TenderPrice_Keys')
  DROP TABLE [dbo].[ut_TenderPrice_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_TenderPrice' and sc.[name] = 'TenderNo')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] 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_TenderPrice' and sc.[name] = 'Tenderer')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] 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_TenderPrice' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [PosRefKey] char(6) 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_TenderPrice' and sc.[name] = 'QtyTBD')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [QtyTBD] 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_TenderPrice' and sc.[name] = 'Qty')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [Qty] 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_TenderPrice' and sc.[name] = 'QU')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [QU] char(6) 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_TenderPrice' and sc.[name] = 'UP')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [UP] 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_TenderPrice' and sc.[name] = 'DscntPcnt')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [DscntPcnt] 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_TenderPrice' and sc.[name] = 'IT')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [IT] 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_TenderPrice' and sc.[name] = 'QtyPurch')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [QtyPurch] 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_TenderPrice' and sc.[name] = 'Descrip')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [Descrip] char(50) 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_TenderPrice' and sc.[name] = 'Inquired')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [Inquired] 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_TenderPrice' and sc.[name] = 'Vkz')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPrice] ADD [Vkz] integer NOT NULL DEFAULT 0
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_TenderPrice' 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_TenderPrice' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] 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_TenderPrice' 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_TenderPrice' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [TenderNo] varchar(20)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'PosRefKey'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [PosRefKey] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'PosRefKey'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [PosRefKey] varchar(6)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'QU'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'QU'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [QU] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'QU'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'QU'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [QU] varchar(6)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'Descrip'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'Descrip'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [Descrip] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'Descrip'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPrice' and sc.[name] = 'Descrip'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPrice] ALTER COLUMN [Descrip] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'TenderPrice')
  DROP VIEW [dbo].[TenderPrice]
GO
CREATE VIEW [dbo].[TenderPrice] AS
  SELECT [RecId]
 ,[TenderNo]
 ,[Tenderer]
 ,[PosRefKey]
 ,[QtyTBD]
 ,[Qty]
 ,[QU]
 ,[UP]
 ,[DscntPcnt]
 ,[IT]
 ,[QtyPurch]
 ,[Descrip]
 ,[Inquired]
 ,[Vkz]
  FROM [dbo].[ut_TenderPrice]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_TenderPrice')
  DROP PROCEDURE [dbo].[up_Sel_TenderPrice]
GO
CREATE PROCEDURE [dbo].[up_Sel_TenderPrice]  @mRI integer
AS
  SELECT
    [TenderNo]
   ,[Tenderer]
   ,[PosRefKey]
   ,[QtyTBD]
   ,[Qty]
   ,[QU]
   ,[UP]
   ,[DscntPcnt]
   ,[IT]
   ,[QtyPurch]
   ,[Descrip]
   ,[Inquired]
   ,[Vkz]
  FROM [dbo].[ut_TenderPrice] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_TenderPrice')
  DROP PROCEDURE [dbo].[up_Append_TenderPrice]
GO
CREATE PROCEDURE [dbo].[up_Append_TenderPrice]
   @Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_PosRefKey char(6)
  ,@Param_QtyTBD tinyint
  ,@Param_Qty float
  ,@Param_QU char(6)
  ,@Param_UP float
  ,@Param_DscntPcnt float
  ,@Param_IT float
  ,@Param_QtyPurch float
  ,@Param_Descrip char(50)
  ,@Param_Inquired tinyint
  ,@Param_Vkz integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_TenderPrice()
      INSERT INTO [dbo].[ut_TenderPrice] (RecId
        ,[TenderNo]
        ,[Tenderer]
        ,[PosRefKey]
        ,[QtyTBD]
        ,[Qty]
        ,[QU]
        ,[UP]
        ,[DscntPcnt]
        ,[IT]
        ,[QtyPurch]
        ,[Descrip]
        ,[Inquired]
        ,[Vkz]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_TenderNo))
        ,@Param_Tenderer
        ,UPPER(RTRIM(@Param_PosRefKey))
        ,@Param_QtyTBD
        ,@Param_Qty
        ,RTRIM(@Param_QU)
        ,@Param_UP
        ,@Param_DscntPcnt
        ,@Param_IT
        ,@Param_QtyPurch
        ,RTRIM(@Param_Descrip)
        ,@Param_Inquired
        ,@Param_Vkz
          )
      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_TenderPrice')
  DROP PROCEDURE [dbo].[up_Add_TenderPrice]
GO
CREATE PROCEDURE [dbo].[up_Add_TenderPrice]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_TenderPrice()
  INSERT INTO [dbo].[ut_TenderPrice] (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_TenderPrice')
  DROP PROCEDURE [dbo].[up_Upd_TenderPrice]
GO
CREATE PROCEDURE [dbo].[up_Upd_TenderPrice] @mRI integer
  ,@Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_PosRefKey char(6)
  ,@Param_QtyTBD tinyint
  ,@Param_Qty float
  ,@Param_QU char(6)
  ,@Param_UP float
  ,@Param_DscntPcnt float
  ,@Param_IT float
  ,@Param_QtyPurch float
  ,@Param_Descrip char(50)
  ,@Param_Inquired tinyint
  ,@Param_Vkz integer
AS
  UPDATE [dbo].[ut_TenderPrice] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = @Param_Tenderer
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[QtyTBD] = @Param_QtyTBD
   ,[Qty] = @Param_Qty
   ,[QU] = RTRIM(@Param_QU)
   ,[UP] = @Param_UP
   ,[DscntPcnt] = @Param_DscntPcnt
   ,[IT] = @Param_IT
   ,[QtyPurch] = @Param_QtyPurch
   ,[Descrip] = RTRIM(@Param_Descrip)
   ,[Inquired] = @Param_Inquired
   ,[Vkz] = @Param_Vkz
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_TenderPrice')
  DROP PROCEDURE [dbo].[up_Upd2_TenderPrice]
GO
CREATE PROCEDURE [dbo].[up_Upd2_TenderPrice] @Param_RecId varchar(20)
  ,@Param_TenderNo varchar(20)
  ,@Param_Tenderer varchar(2)
  ,@Param_PosRefKey varchar(6)
  ,@Param_QtyTBD varchar(1)
  ,@Param_Qty varchar(12)
  ,@Param_QU varchar(6)
  ,@Param_UP varchar(14)
  ,@Param_DscntPcnt varchar(6)
  ,@Param_IT varchar(14)
  ,@Param_QtyPurch varchar(12)
  ,@Param_Descrip varchar(50)
  ,@Param_Inquired varchar(1)
  ,@Param_Vkz varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_TenderPrice] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = dbo.itcfunc_integer(@Param_Tenderer)
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[QtyTBD] = dbo.itcfunc_integer(@Param_QtyTBD)
   ,[Qty] = dbo.itcfunc_float(@Param_Qty)
   ,[QU] = RTRIM(@Param_QU)
   ,[UP] = dbo.itcfunc_float(@Param_UP)
   ,[DscntPcnt] = dbo.itcfunc_float(@Param_DscntPcnt)
   ,[IT] = dbo.itcfunc_float(@Param_IT)
   ,[QtyPurch] = dbo.itcfunc_float(@Param_QtyPurch)
   ,[Descrip] = RTRIM(@Param_Descrip)
   ,[Inquired] = dbo.itcfunc_integer(@Param_Inquired)
   ,[Vkz] = dbo.itcfunc_integer(@Param_Vkz)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_TenderPrice] @mRI
GO

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

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TenderPrice_Key00')
  DROP PROCEDURE [dbo].[up_FindF_TenderPrice_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_TenderPrice_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)
  DECLARE @mPosRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,23,6)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo AND 
      Tenderer = @mTenderer AND 
      PosRefKey = @mPosRefKey
      ORDER BY TenderNo,Tenderer,PosRefKey,RecId
    RETURN
  END

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

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

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

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


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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TenderPrice_Key00')
  DROP PROCEDURE [dbo].[up_FindL_TenderPrice_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_TenderPrice_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)
  DECLARE @mPosRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,23,6)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo AND 
      Tenderer = @mTenderer AND 
      PosRefKey = @mPosRefKey
      ORDER BY TenderNo desc,Tenderer desc,PosRefKey desc,RecId desc
    RETURN
  END

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key00) WHERE 
      TenderNo = @mTenderNo
      ORDER BY TenderNo desc,Tenderer desc,PosRefKey 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_TenderPrice] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      ORDER BY TenderNo desc,Tenderer desc,PosRefKey desc,RecId desc
    RETURN
  END


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

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


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

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

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

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

  DECLARE @newRecId integer

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01)
    WHERE (TenderNo > @oldTenderNo)
    ORDER BY TenderNo,PosRefKey,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_TenderPrice_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_TenderPrice_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_TenderPrice_Key01] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (RecId < @oldRecId)
    ORDER BY TenderNo desc,PosRefKey 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_TenderPrice] WITH(INDEX = Ik_Key01)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer < @oldTenderer)
    ORDER BY TenderNo desc,PosRefKey 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_TenderPrice] WITH(INDEX = Ik_Key01)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey < @oldPosRefKey)
    ORDER BY TenderNo desc,PosRefKey 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_TenderPrice] WITH(INDEX = Ik_Key01)
    WHERE (TenderNo < @oldTenderNo)
    ORDER BY TenderNo desc,PosRefKey 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_TenderPrice_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_TenderPrice_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_TenderPrice_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TenderPrice_Key01')
  DROP PROCEDURE [dbo].[up_FindF_TenderPrice_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_TenderPrice_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTenderNo char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mPosRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,6)
  DECLARE @mTenderer integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,2)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer
      ORDER BY TenderNo,PosRefKey,Tenderer,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey
      ORDER BY TenderNo,PosRefKey,Tenderer,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo
      ORDER BY TenderNo,PosRefKey,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_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo >= @mTenderNo AND SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      ORDER BY TenderNo,PosRefKey,Tenderer,RecId
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TenderPrice_Key01')
  DROP PROCEDURE [dbo].[up_FindL_TenderPrice_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_TenderPrice_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mTenderNo char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mPosRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,6)
  DECLARE @mTenderer integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,27,2)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo
      ORDER BY TenderNo desc,PosRefKey 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_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,RecId desc
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPrice] WITH(INDEX = Ik_Key01) WHERE 
      TenderNo = @mTenderNo AND 
      SUBSTRING(PosRefKey,1,LEN(@mPosRefKey)) = @mPosRefKey
      ORDER BY TenderNo desc,PosRefKey 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_TenderPrice_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_TenderPrice_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_TenderPrice_Key01] @xKey, @xLen')
GO


