-- 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_Tender')
BEGIN
CREATE TABLE [dbo].[ut_Tender]
(
  [RecId]  integer NOT NULL
 ,[No] char(20) DEFAULT ''
 ,[BshPrjNr] char(8) DEFAULT ''
 ,[BshBelArt] char(2) DEFAULT ''
 ,[BshBelNr] char(8) DEFAULT ''
  CONSTRAINT [Pk_Tender]  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_Tender_KeyDel')
  DROP TRIGGER [dbo].[tr_Tender_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Tender_KeyUpd')
  DROP TRIGGER [dbo].[tr_Tender_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Tender_Keys')
  DROP TABLE [dbo].[ut_Tender_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_Tender' and sc.[name] = 'No')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [No] 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_Tender' and sc.[name] = 'BshPrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [BshPrjNr] char(8) 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_Tender' and sc.[name] = 'BshBelArt')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [BshBelArt] 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_Tender' and sc.[name] = 'BshBelNr')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [BshBelNr] char(8) 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_Tender' and sc.[name] = 'No'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'No'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [No] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'No'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'No'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [No] varchar(20)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshPrjNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshPrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshPrjNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshPrjNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshPrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshPrjNr] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelArt] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelArt] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Tender' and sc.[name] = 'BshBelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelNr] varchar(8)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Tender')
  DROP VIEW [dbo].[Tender]
GO
CREATE VIEW [dbo].[Tender] AS
  SELECT [RecId]
 ,[No]
 ,[BshPrjNr]
 ,[BshBelArt]
 ,[BshBelNr]
  FROM [dbo].[ut_Tender]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Tender')
  DROP PROCEDURE [dbo].[up_Sel_Tender]
GO
CREATE PROCEDURE [dbo].[up_Sel_Tender]  @mRI integer
AS
  SELECT
    [No]
   ,[BshPrjNr]
   ,[BshBelArt]
   ,[BshBelNr]
  FROM [dbo].[ut_Tender] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Tender')
  DROP PROCEDURE [dbo].[up_Append_Tender]
GO
CREATE PROCEDURE [dbo].[up_Append_Tender]
   @Param_No char(20)
  ,@Param_BshPrjNr char(8)
  ,@Param_BshBelArt char(2)
  ,@Param_BshBelNr char(8)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Tender()
      INSERT INTO [dbo].[ut_Tender] (RecId
        ,[No]
        ,[BshPrjNr]
        ,[BshBelArt]
        ,[BshBelNr]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_No))
        ,UPPER(RTRIM(@Param_BshPrjNr))
        ,UPPER(RTRIM(@Param_BshBelArt))
        ,UPPER(RTRIM(@Param_BshBelNr))
          )
      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_Tender')
  DROP PROCEDURE [dbo].[up_Add_Tender]
GO
CREATE PROCEDURE [dbo].[up_Add_Tender]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Tender()
  INSERT INTO [dbo].[ut_Tender] (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_Tender')
  DROP PROCEDURE [dbo].[up_Upd_Tender]
GO
CREATE PROCEDURE [dbo].[up_Upd_Tender] @mRI integer
  ,@Param_No char(20)
  ,@Param_BshPrjNr char(8)
  ,@Param_BshBelArt char(2)
  ,@Param_BshBelNr char(8)
AS
  UPDATE [dbo].[ut_Tender] SET
    [No] = UPPER(RTRIM(@Param_No))
   ,[BshPrjNr] = UPPER(RTRIM(@Param_BshPrjNr))
   ,[BshBelArt] = UPPER(RTRIM(@Param_BshBelArt))
   ,[BshBelNr] = UPPER(RTRIM(@Param_BshBelNr))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Tender')
  DROP PROCEDURE [dbo].[up_Upd2_Tender]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Tender] @Param_RecId varchar(20)
  ,@Param_No varchar(20)
  ,@Param_BshPrjNr varchar(8)
  ,@Param_BshBelArt varchar(2)
  ,@Param_BshBelNr varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Tender] SET
    [No] = UPPER(RTRIM(@Param_No))
   ,[BshPrjNr] = UPPER(RTRIM(@Param_BshPrjNr))
   ,[BshBelArt] = UPPER(RTRIM(@Param_BshBelArt))
   ,[BshBelNr] = UPPER(RTRIM(@Param_BshBelNr))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Tender] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Tender_Key00')
  DROP PROCEDURE [dbo].[up_Next_Tender_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Tender_Key00] @oldRecId integer AS
  DECLARE @oldNo char(20)
  SELECT @oldNo = No FROM [dbo].[ut_Tender] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key00)
    WHERE (No > @oldNo)
    ORDER BY No,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_Tender_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Tender_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Tender_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tender_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Tender_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Tender_Key00] @oldRecId integer AS
  DECLARE @oldNo char(20)
  SELECT @oldNo = No FROM [dbo].[ut_Tender] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key00)
    WHERE (No < @oldNo)
    ORDER BY No 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_Tender_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Tender_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Tender_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key00) WHERE 
      No = @mNo
      ORDER BY No,RecId
    RETURN
  END

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

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key00) WHERE 
      No = @mNo
      ORDER BY No 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_Tender] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(No,1,LEN(@mNo)) = @mNo
      ORDER BY No desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Tender_Key01')
  DROP PROCEDURE [dbo].[up_First_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_Tender_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Tender_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Tender_Key01] AS EXECUTE [dbo].[up_First_Tender_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Tender_Key01')
  DROP PROCEDURE [dbo].[up_Last_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_Tender_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Tender_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Tender_Key01] AS EXECUTE [dbo].[up_Last_Tender_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Tender_Key01')
  DROP PROCEDURE [dbo].[up_Next_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Tender_Key01] @oldRecId integer AS
  DECLARE @oldBshPrjNr char(8)
  DECLARE @oldBshBelArt char(2)
  DECLARE @oldBshBelNr char(8)
  SELECT @oldBshPrjNr = BshPrjNr, @oldBshBelArt = BshBelArt, @oldBshBelNr = BshBelNr FROM [dbo].[ut_Tender] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt = @oldBshBelArt) AND (BshBelNr = @oldBshBelNr) AND (RecId > @oldRecId)
    ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt = @oldBshBelArt) AND (BshBelNr > @oldBshBelNr)
    ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt > @oldBshBelArt)
    ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr > @oldBshPrjNr)
    ORDER BY BshPrjNr,BshBelArt,BshBelNr,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_Tender_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Tender_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Tender_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tender_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Tender_Key01] @oldRecId integer AS
  DECLARE @oldBshPrjNr char(8)
  DECLARE @oldBshBelArt char(2)
  DECLARE @oldBshBelNr char(8)
  SELECT @oldBshPrjNr = BshPrjNr, @oldBshBelArt = BshBelArt, @oldBshBelNr = BshBelNr FROM [dbo].[ut_Tender] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt = @oldBshBelArt) AND (BshBelNr = @oldBshBelNr) AND (RecId < @oldRecId)
    ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt = @oldBshBelArt) AND (BshBelNr < @oldBshBelNr)
    ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr = @oldBshPrjNr) AND (BshBelArt < @oldBshBelArt)
    ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01)
    WHERE (BshPrjNr < @oldBshPrjNr)
    ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr 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_Tender_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Tender_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Tender_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Tender_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_Tender_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mBshPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBshBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBshBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt AND 
      BshBelNr = @mBshBelNr
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr >= @mBshPrjNr AND SUBSTRING(BshPrjNr,1,LEN(@mBshPrjNr)) = @mBshPrjNr
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt >= @mBshBelArt AND SUBSTRING(BshBelArt,1,LEN(@mBshBelArt)) = @mBshBelArt
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt AND 
      BshBelNr >= @mBshBelNr AND SUBSTRING(BshBelNr,1,LEN(@mBshBelNr)) = @mBshBelNr
      ORDER BY BshPrjNr,BshBelArt,BshBelNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Tender_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Tender_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_Tender_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mBshPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBshBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBshBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt AND 
      BshBelNr = @mBshBelNr
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(BshPrjNr,1,LEN(@mBshPrjNr)) = @mBshPrjNr
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      SUBSTRING(BshBelArt,1,LEN(@mBshBelArt)) = @mBshBelArt
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Tender] WITH(INDEX = Ik_Key01) WHERE 
      BshPrjNr = @mBshPrjNr AND 
      BshBelArt = @mBshBelArt AND 
      SUBSTRING(BshBelNr,1,LEN(@mBshBelNr)) = @mBshBelNr
      ORDER BY BshPrjNr desc,BshBelArt desc,BshBelNr desc,RecId desc
    RETURN
  END

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


