-- 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_TenderPriceDetail')
BEGIN
CREATE TABLE [dbo].[ut_TenderPriceDetail]
(
  [RecId]  integer NOT NULL
 ,[TenderNo] char(20) DEFAULT ''
 ,[Tenderer] integer DEFAULT 0
 ,[PosRefKey] char(6) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
 ,[Lkz] char(2) DEFAULT ''
 ,[Artnr] char(18) DEFAULT ''
 ,[Bez1] varchar(42) DEFAULT ''
 ,[Bez2] varchar(42) DEFAULT ''
 ,[Qty] float DEFAULT 0
 ,[QtyUnit] char(6) DEFAULT ''
 ,[PriceUnit] float DEFAULT 0
 ,[Price] float DEFAULT 0
 ,[RequQty] float DEFAULT 0
 ,[RequAmount] float DEFAULT 0
 ,[Vkz] integer DEFAULT 0
  CONSTRAINT [Pk_TenderPriceDetail]  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_TenderPriceDetail_KeyDel')
  DROP TRIGGER [dbo].[tr_TenderPriceDetail_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_TenderPriceDetail_KeyUpd')
  DROP TRIGGER [dbo].[tr_TenderPriceDetail_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_TenderPriceDetail_Keys')
  DROP TABLE [dbo].[ut_TenderPriceDetail_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_TenderPriceDetail' and sc.[name] = 'TenderNo')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' and sc.[name] = 'Tenderer')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' and sc.[name] = 'PosRefKey')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [LfdNr] 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_TenderPriceDetail' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [Lkz] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [Artnr] char(18) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [Bez1] varchar(42) 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_TenderPriceDetail' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [Bez2] varchar(42) 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_TenderPriceDetail' and sc.[name] = 'Qty')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' and sc.[name] = 'QtyUnit')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [QtyUnit] 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_TenderPriceDetail' and sc.[name] = 'PriceUnit')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [PriceUnit] 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_TenderPriceDetail' and sc.[name] = 'Price')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [Price] 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_TenderPriceDetail' and sc.[name] = 'RequQty')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [RequQty] 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_TenderPriceDetail' and sc.[name] = 'RequAmount')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] ADD [RequAmount] 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_TenderPriceDetail' and sc.[name] = 'Vkz')
BEGIN
  ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' 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_TenderPriceDetail' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' 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_TenderPriceDetail' and sc.[name] = 'TenderNo'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' 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_TenderPriceDetail' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] 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_TenderPriceDetail' 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_TenderPriceDetail' and sc.[name] = 'PosRefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [PosRefKey] varchar(6)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Lkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Lkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Lkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Lkz] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Artnr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Artnr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Artnr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Artnr'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Artnr] varchar(18)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Bez1] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Bez1] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Bez2] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [Bez2] varchar(42)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'QtyUnit'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'QtyUnit'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [QtyUnit] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'QtyUnit'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_TenderPriceDetail' and sc.[name] = 'QtyUnit'))
BEGIN
   ALTER TABLE [dbo].[ut_TenderPriceDetail] ALTER COLUMN [QtyUnit] varchar(6)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'TenderPriceDetail')
  DROP VIEW [dbo].[TenderPriceDetail]
GO
CREATE VIEW [dbo].[TenderPriceDetail] AS
  SELECT [RecId]
 ,[TenderNo]
 ,[Tenderer]
 ,[PosRefKey]
 ,[LfdNr]
 ,[Lkz]
 ,[Artnr]
 ,[Bez1]
 ,[Bez2]
 ,[Qty]
 ,[QtyUnit]
 ,[PriceUnit]
 ,[Price]
 ,[RequQty]
 ,[RequAmount]
 ,[Vkz]
  FROM [dbo].[ut_TenderPriceDetail]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_TenderPriceDetail')
  DROP PROCEDURE [dbo].[up_Sel_TenderPriceDetail]
GO
CREATE PROCEDURE [dbo].[up_Sel_TenderPriceDetail]  @mRI integer
AS
  SELECT
    [TenderNo]
   ,[Tenderer]
   ,[PosRefKey]
   ,[LfdNr]
   ,[Lkz]
   ,[Artnr]
   ,[Bez1]
   ,[Bez2]
   ,[Qty]
   ,[QtyUnit]
   ,[PriceUnit]
   ,[Price]
   ,[RequQty]
   ,[RequAmount]
   ,[Vkz]
  FROM [dbo].[ut_TenderPriceDetail] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_TenderPriceDetail')
  DROP PROCEDURE [dbo].[up_Append_TenderPriceDetail]
GO
CREATE PROCEDURE [dbo].[up_Append_TenderPriceDetail]
   @Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_PosRefKey char(6)
  ,@Param_LfdNr integer
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_Bez1 varchar(42)
  ,@Param_Bez2 varchar(42)
  ,@Param_Qty float
  ,@Param_QtyUnit char(6)
  ,@Param_PriceUnit float
  ,@Param_Price float
  ,@Param_RequQty float
  ,@Param_RequAmount float
  ,@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_TenderPriceDetail()
      INSERT INTO [dbo].[ut_TenderPriceDetail] (RecId
        ,[TenderNo]
        ,[Tenderer]
        ,[PosRefKey]
        ,[LfdNr]
        ,[Lkz]
        ,[Artnr]
        ,[Bez1]
        ,[Bez2]
        ,[Qty]
        ,[QtyUnit]
        ,[PriceUnit]
        ,[Price]
        ,[RequQty]
        ,[RequAmount]
        ,[Vkz]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_TenderNo))
        ,@Param_Tenderer
        ,UPPER(RTRIM(@Param_PosRefKey))
        ,@Param_LfdNr
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_Artnr))
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Bez2)
        ,@Param_Qty
        ,RTRIM(@Param_QtyUnit)
        ,@Param_PriceUnit
        ,@Param_Price
        ,@Param_RequQty
        ,@Param_RequAmount
        ,@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_TenderPriceDetail')
  DROP PROCEDURE [dbo].[up_Add_TenderPriceDetail]
GO
CREATE PROCEDURE [dbo].[up_Add_TenderPriceDetail]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_TenderPriceDetail()
  INSERT INTO [dbo].[ut_TenderPriceDetail] (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_TenderPriceDetail')
  DROP PROCEDURE [dbo].[up_Upd_TenderPriceDetail]
GO
CREATE PROCEDURE [dbo].[up_Upd_TenderPriceDetail] @mRI integer
  ,@Param_TenderNo char(20)
  ,@Param_Tenderer integer
  ,@Param_PosRefKey char(6)
  ,@Param_LfdNr integer
  ,@Param_Lkz char(2)
  ,@Param_Artnr char(18)
  ,@Param_Bez1 varchar(42)
  ,@Param_Bez2 varchar(42)
  ,@Param_Qty float
  ,@Param_QtyUnit char(6)
  ,@Param_PriceUnit float
  ,@Param_Price float
  ,@Param_RequQty float
  ,@Param_RequAmount float
  ,@Param_Vkz integer
AS
  UPDATE [dbo].[ut_TenderPriceDetail] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = @Param_Tenderer
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[LfdNr] = @Param_LfdNr
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Qty] = @Param_Qty
   ,[QtyUnit] = RTRIM(@Param_QtyUnit)
   ,[PriceUnit] = @Param_PriceUnit
   ,[Price] = @Param_Price
   ,[RequQty] = @Param_RequQty
   ,[RequAmount] = @Param_RequAmount
   ,[Vkz] = @Param_Vkz
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_TenderPriceDetail')
  DROP PROCEDURE [dbo].[up_Upd2_TenderPriceDetail]
GO
CREATE PROCEDURE [dbo].[up_Upd2_TenderPriceDetail] @Param_RecId varchar(20)
  ,@Param_TenderNo varchar(20)
  ,@Param_Tenderer varchar(2)
  ,@Param_PosRefKey varchar(6)
  ,@Param_LfdNr varchar(4)
  ,@Param_Lkz varchar(2)
  ,@Param_Artnr varchar(18)
  ,@Param_Bez1 varchar(42)
  ,@Param_Bez2 varchar(42)
  ,@Param_Qty varchar(12)
  ,@Param_QtyUnit varchar(6)
  ,@Param_PriceUnit varchar(12)
  ,@Param_Price varchar(12)
  ,@Param_RequQty varchar(12)
  ,@Param_RequAmount varchar(12)
  ,@Param_Vkz varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_TenderPriceDetail] SET
    [TenderNo] = UPPER(RTRIM(@Param_TenderNo))
   ,[Tenderer] = dbo.itcfunc_integer(@Param_Tenderer)
   ,[PosRefKey] = UPPER(RTRIM(@Param_PosRefKey))
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Qty] = dbo.itcfunc_float(@Param_Qty)
   ,[QtyUnit] = RTRIM(@Param_QtyUnit)
   ,[PriceUnit] = dbo.itcfunc_float(@Param_PriceUnit)
   ,[Price] = dbo.itcfunc_float(@Param_Price)
   ,[RequQty] = dbo.itcfunc_float(@Param_RequQty)
   ,[RequAmount] = dbo.itcfunc_float(@Param_RequAmount)
   ,[Vkz] = dbo.itcfunc_integer(@Param_Vkz)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_TenderPriceDetail] @mRI
GO

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

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

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

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

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

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPriceDetail] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr < @oldLfdNr)
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer < @oldTenderer)
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo = @oldTenderNo) AND (PosRefKey < @oldPosRefKey)
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] WITH(INDEX = Ik_Key00)
    WHERE (TenderNo < @oldTenderNo)
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_TenderPriceDetail_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_TenderPriceDetail_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TenderPriceDetail_Key00')
  DROP PROCEDURE [dbo].[up_FindF_TenderPriceDetail_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_TenderPriceDetail_Key00] @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)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,4)

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

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

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

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

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

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

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

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



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TenderPriceDetail_Key00')
  DROP PROCEDURE [dbo].[up_FindL_TenderPriceDetail_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_TenderPriceDetail_Key00] @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)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,4)

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

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

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

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

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

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_TenderPriceDetail_Key01')
  DROP PROCEDURE [dbo].[up_First_TenderPriceDetail_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_TenderPriceDetail_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE (Vkz < 3) ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_TenderPriceDetail_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_TenderPriceDetail_Key01] AS EXECUTE [dbo].[up_First_TenderPriceDetail_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_TenderPriceDetail_Key01')
  DROP PROCEDURE [dbo].[up_Last_TenderPriceDetail_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_TenderPriceDetail_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE (Vkz < 3) ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_TenderPriceDetail_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_TenderPriceDetail_Key01] AS EXECUTE [dbo].[up_Last_TenderPriceDetail_Key01]')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND ((Vkz < 3))
    ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr > @oldLfdNr)) AND ((Vkz < 3))
    ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPriceDetail] 
    WHERE ((TenderNo > @oldTenderNo)) AND ((Vkz < 3))
    ORDER BY TenderNo,PosRefKey,Tenderer,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_TenderPriceDetail_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_TenderPriceDetail_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_TenderPriceDetail_Key01] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND ((Vkz < 3))
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer = @oldTenderer) AND (LfdNr < @oldLfdNr)) AND ((Vkz < 3))
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey = @oldPosRefKey) AND (Tenderer < @oldTenderer)) AND ((Vkz < 3))
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] 
    WHERE ((TenderNo = @oldTenderNo) AND (PosRefKey < @oldPosRefKey)) AND ((Vkz < 3))
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail] 
    WHERE ((TenderNo < @oldTenderNo)) AND ((Vkz < 3))
    ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_TenderPriceDetail_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_TenderPriceDetail_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TenderPriceDetail_Key01')
  DROP PROCEDURE [dbo].[up_FindF_TenderPriceDetail_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_TenderPriceDetail_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)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,4)

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

  IF @xLen = 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer AND 
      LfdNr = @mLfdNr
      AND ((Vkz < 3))
      ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer
      AND ((Vkz < 3))
      ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey
      AND ((Vkz < 3))
      ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo
      AND ((Vkz < 3))
      ORDER BY TenderNo,PosRefKey,Tenderer,LfdNr,RecId
    RETURN
  END

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

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

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



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TenderPriceDetail_Key01')
  DROP PROCEDURE [dbo].[up_FindL_TenderPriceDetail_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_TenderPriceDetail_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)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,29,4)

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

  IF @xLen = 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer AND 
      LfdNr = @mLfdNr
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey AND 
      Tenderer = @mTenderer
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      PosRefKey = @mPosRefKey
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr 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_TenderPriceDetail]  WHERE 
      SUBSTRING(TenderNo,1,LEN(@mTenderNo)) = @mTenderNo
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TenderPriceDetail]  WHERE 
      TenderNo = @mTenderNo AND 
      SUBSTRING(PosRefKey,1,LEN(@mPosRefKey)) = @mPosRefKey
      AND ((Vkz < 3))
      ORDER BY TenderNo desc,PosRefKey desc,Tenderer 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_TenderPriceDetail_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_TenderPriceDetail_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_TenderPriceDetail_Key01] @xKey, @xLen')
GO


