------------------------------------------------------------
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
  ,[MandId] integer DEFAULT 1
  ,[No] char(20) DEFAULT ' '
  ,[NavJobNo] char(20) DEFAULT ' '
  ,[NavDocNo] char(20) DEFAULT ' '
  ,[BshPrjNr] char(8) DEFAULT ' '
  ,[BshBelArt] char(2) DEFAULT ' '
  ,[BshBelNr] char(8) DEFAULT ' '
  CONSTRAINT [Pk_Tender]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
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

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] = 'NavJobNo')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [NavJobNo] 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] = 'NavDocNo')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ADD [NavDocNo] 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
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')
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] = 'NavJobNo')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [NavJobNo] 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] = 'NavDocNo')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [NavDocNo] char(20)
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')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshPrjNr] char(8)
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')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelArt] char(2)
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')
BEGIN
  ALTER TABLE [dbo].[ut_Tender] ALTER COLUMN [BshBelNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Tender')
  DROP VIEW [dbo].[Tender]
GO
CREATE VIEW [dbo].[Tender] AS
  SELECT * FROM [dbo].[ut_Tender]
GO
CREATE TABLE [dbo].[ut_Tender_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(30) NOT NULL
  ,[Key01_Value] CHAR(50) NOT NULL
  ,[Key02_Value] CHAR(28) NOT NULL
  CONSTRAINT [Pk_ut_Tender_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Tender_KeyDel] ON [dbo].[ut_Tender]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Tender_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_Tender_KeyUpd] ON [dbo].[ut_Tender]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_Tender_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_Tender_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[No])+STR(RecId,10))
     ,UPPER(convert(char(20),[NavJobNo])+convert(char(20),[NavDocNo])+STR(RecId,10))
     ,UPPER(convert(char(8),[BshPrjNr])+convert(char(2),[BshBelArt])+convert(char(8),[BshBelNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Tender_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[No])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(20),[NavJobNo])+convert(char(20),[NavDocNo])+STR([RecId],10))
   ,[Key02_Value] = UPPER(convert(char(8),[BshPrjNr])+convert(char(2),[BshBelArt])+convert(char(8),[BshBelNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_Tender] SET RecId = RecId
GO


------------------------------------------------------------
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
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Tender])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Tender] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
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]
  @RI integer
 ,@Parm_No char(20)
 ,@Parm_NavJobNo char(20)
 ,@Parm_NavDocNo char(20)
 ,@Parm_BshPrjNr char(8)
 ,@Parm_BshBelArt char(2)
 ,@Parm_BshBelNr char(8)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Tender] SET
     [No] = RTRIM(@Parm_No)
    ,[NavJobNo] = RTRIM(@Parm_NavJobNo)
    ,[NavDocNo] = RTRIM(@Parm_NavDocNo)
    ,[BshPrjNr] = RTRIM(@Parm_BshPrjNr)
    ,[BshBelArt] = RTRIM(@Parm_BshBelArt)
    ,[BshBelNr] = RTRIM(@Parm_BshBelNr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
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]
  @RI integer
AS
SELECT
     [No]
    ,[NavJobNo]
    ,[NavDocNo]
    ,[BshPrjNr]
    ,[BshBelArt]
    ,[BshBelNr]
  FROM [dbo].[ut_Tender]
  WHERE RecId = @RI
GO
------------------------------------------------------------
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]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Tender] WHERE RecId = @RI
GO
------------------------------------------------------------
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

------------------------------------------------------------
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]
  @Parm_No char(20)
 ,@Parm_NavJobNo char(20)
 ,@Parm_NavDocNo char(20)
 ,@Parm_BshPrjNr char(8)
 ,@Parm_BshBelArt char(2)
 ,@Parm_BshBelNr char(8)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Tender])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Tender] (
    RecId
   ,[No]
   ,[NavJobNo]
   ,[NavDocNo]
   ,[BshPrjNr]
   ,[BshBelArt]
   ,[BshBelNr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_No)
   ,RTRIM(@Parm_NavJobNo)
   ,RTRIM(@Parm_NavDocNo)
   ,RTRIM(@Parm_BshPrjNr)
   ,RTRIM(@Parm_BshBelArt)
   ,RTRIM(@Parm_BshBelNr)
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
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([NavJobNo]))
      +LTRIM(RTRIM([NavDocNo]))
      +LTRIM(RTRIM([BshPrjNr]))
      +LTRIM(RTRIM([BshBelArt]))
      +LTRIM(RTRIM([BshBelNr]))
    AS Data
  FROM     [dbo].[ut_Tender]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_Tender_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Tender_Key00')
  DROP PROCEDURE [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
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tender_Key00')
  DROP PROCEDURE [dbo].[up_Prior_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
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] = 'up_Next50_Tender_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Tender_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Tender_Key00')
  DROP PROCEDURE [dbo].[up_FindL_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_First_Tender_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Tender_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Tender_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_Tender_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_Tender_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_Tender_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_Tender_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_Tender_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_Tender_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

CREATE INDEX [Ik_Key01] ON [dbo].[ut_Tender_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Tender_Key01')
  DROP PROCEDURE [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
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tender_Key01')
  DROP PROCEDURE [dbo].[up_Prior_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
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] = 'up_Next50_Tender_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Tender_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Tender_Key01')
  DROP PROCEDURE [dbo].[up_FindL_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_First_Tender_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Tender_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Tender_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_Tender_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_Tender_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_Tender_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_Tender_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_Tender_Keys]
      WHERE [Key01_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key01_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_Tender_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

CREATE INDEX [Ik_Key02] ON [dbo].[ut_Tender_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Tender_Key02')
  DROP PROCEDURE [dbo].[up_First_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Tender_Key02')
  DROP PROCEDURE [dbo].[up_Last_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Tender_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Tender_Key02')
  DROP PROCEDURE [dbo].[up_Next_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Tender_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Tender_Key02')
  DROP PROCEDURE [dbo].[up_Next50_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Tender_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Tender_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Tender_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Tender_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_Tender_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Tender_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys] ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Tender_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_Tender_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_Tender_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_Tender_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_Tender_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_Tender_Key02] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key02_Value] AS Key_Value FROM [dbo].[ut_Tender_Keys]
      WHERE [Key02_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key02_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_Tender_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Tender_Keys]
  WHERE [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

