------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonFPA')
BEGIN
CREATE TABLE [dbo].[ut_MareonFPA]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[UNR] char(20) DEFAULT ' '
  ,[FPA] char(20) DEFAULT ' '
  ,[OffPlus] char(1) DEFAULT ' '
  ,[Limit] float DEFAULT 0
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Percent] float DEFAULT 0
  ,[Amount] float DEFAULT 0
  ,[SubUNR] char(20) DEFAULT ' '
  CONSTRAINT [Pk_MareonFPA]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonFPA_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonFPA_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonFPA_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonFPA_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonFPA_Keys')
  DROP TABLE [dbo].[ut_MareonFPA_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonFPA' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [UNR] 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_MareonFPA' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [FPA] 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_MareonFPA' and sc.[name] = 'OffPlus')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [OffPlus] char(1) 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_MareonFPA' and sc.[name] = 'Limit')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [Limit] 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_MareonFPA' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [HostDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonFPA' and sc.[name] = 'Percent')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [Percent] 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_MareonFPA' and sc.[name] = 'Amount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [Amount] 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_MareonFPA' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ADD [SubUNR] char(20) 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_MareonFPA' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ALTER COLUMN [UNR] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonFPA' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ALTER COLUMN [FPA] char(20)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonFPA' and sc.[name] = 'OffPlus')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ALTER COLUMN [OffPlus] char(1)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonFPA' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonFPA] ALTER COLUMN [SubUNR] char(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonFPA')
  DROP VIEW [dbo].[MareonFPA]
GO
CREATE VIEW [dbo].[MareonFPA] AS
  SELECT * FROM [dbo].[ut_MareonFPA]
GO
CREATE TABLE [dbo].[ut_MareonFPA_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(81) NOT NULL
  ,[Key01_Value] CHAR(81) NOT NULL
  CONSTRAINT [Pk_ut_MareonFPA_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonFPA_KeyDel] ON [dbo].[ut_MareonFPA]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonFPA_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_MareonFPA_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[FPA])+convert(char(1),[OffPlus])+STR([Limit],10,2)+STR(RecId,10))
     ,UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[FPA])+STR([Limit],10,2)+convert(char(1),[OffPlus])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_MareonFPA_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[FPA])+convert(char(1),[OffPlus])+STR([Limit],10,2)+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+convert(char(20),[FPA])+STR([Limit],10,2)+convert(char(1),[OffPlus])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonFPA')
  DROP PROCEDURE [dbo].[up_Add_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonFPA] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonFPA])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonFPA] (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_MareonFPA')
  DROP PROCEDURE [dbo].[up_Upd_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonFPA]
  @RI integer
 ,@Parm_UNR char(20)
 ,@Parm_FPA char(20)
 ,@Parm_OffPlus char(1)
 ,@Parm_Limit float
 ,@Parm_HostDate datetime
 ,@Parm_Percent float
 ,@Parm_Amount float
 ,@Parm_SubUNR char(20)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonFPA] SET
     [UNR] = RTRIM(@Parm_UNR)
    ,[FPA] = RTRIM(@Parm_FPA)
    ,[OffPlus] = RTRIM(@Parm_OffPlus)
    ,[Limit] = @Parm_Limit
    ,[HostDate] = @Parm_HostDate
    ,[Percent] = @Parm_Percent
    ,[Amount] = @Parm_Amount
    ,[SubUNR] = RTRIM(@Parm_SubUNR)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonFPA')
  DROP PROCEDURE [dbo].[up_Sel_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonFPA]
  @RI integer
AS
SELECT
     [UNR]
    ,[FPA]
    ,[OffPlus]
    ,[Limit]
    ,[HostDate]
    ,[Percent]
    ,[Amount]
    ,[SubUNR]
  FROM [dbo].[ut_MareonFPA]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonFPA')
  DROP PROCEDURE [dbo].[up_Del_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonFPA]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonFPA] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonFPA')
  DROP PROCEDURE [dbo].[up_Reorg_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonFPA]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonFPA] 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_MareonFPA] 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_MareonFPA] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonFPA')
  DROP PROCEDURE [dbo].[up_Append_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonFPA]
  @Parm_UNR char(20)
 ,@Parm_FPA char(20)
 ,@Parm_OffPlus char(1)
 ,@Parm_Limit float
 ,@Parm_HostDate datetime
 ,@Parm_Percent float
 ,@Parm_Amount float
 ,@Parm_SubUNR char(20)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonFPA])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonFPA] (
    RecId
   ,[UNR]
   ,[FPA]
   ,[OffPlus]
   ,[Limit]
   ,[HostDate]
   ,[Percent]
   ,[Amount]
   ,[SubUNR]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_UNR)
   ,RTRIM(@Parm_FPA)
   ,RTRIM(@Parm_OffPlus)
   ,@Parm_Limit
   ,@Parm_HostDate
   ,@Parm_Percent
   ,@Parm_Amount
   ,RTRIM(@Parm_SubUNR)
       )
  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_MareonFPA')
  DROP PROCEDURE [dbo].[up_TrSel_MareonFPA]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonFPA]
AS
  SELECT   RecId,
      LTRIM(RTRIM([UNR]))
      +LTRIM(RTRIM([FPA]))
      +LTRIM(RTRIM([OffPlus]))
      +LTRIM(RTRIM([SubUNR]))
    AS Data
  FROM     [dbo].[ut_MareonFPA]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonFPA_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonFPA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonFPA_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonFPA_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_MareonFPA_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonFPA_Keys] ORDER BY [Key00_Value]
GO

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_MareonFPA_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_MareonFPA_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_MareonFPA_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonFPA_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

CREATE INDEX [Ik_Key01] ON [dbo].[ut_MareonFPA_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_First_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_Last_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_Prior_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_Next_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_Next50_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_FindL_MareonFPA_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonFPA_Key01')
  DROP PROCEDURE [dbo].[up_FindF_MareonFPA_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_MareonFPA_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonFPA_Keys] ORDER BY [Key01_Value]
GO

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_MareonFPA_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_MareonFPA_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_MareonFPA_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonFPA_Keys]
  WHERE [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

