------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonAttachment')
BEGIN
CREATE TABLE [dbo].[ut_MareonAttachment]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Type] integer DEFAULT 0
  ,[NrBsh] char(20) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[Dropped] tinyint DEFAULT 0
  ,[ReadOnly] tinyint DEFAULT 0
  ,[MareonId] char(20) DEFAULT ' '
  ,[BlobId] integer DEFAULT 0
  ,[Version] char(30) DEFAULT ' '
  ,[FileName] char(250) DEFAULT ' '
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Created] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Modify] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Download] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Upload] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  CONSTRAINT [Pk_MareonAttachment]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonAttachment_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonAttachment_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonAttachment_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonAttachment_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonAttachment_Keys')
  DROP TABLE [dbo].[ut_MareonAttachment_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'Type')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Type] 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_MareonAttachment' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [NrBsh] 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_MareonAttachment' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] 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_MareonAttachment' and sc.[name] = 'Dropped')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Dropped] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'ReadOnly')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [ReadOnly] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'MareonId')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [MareonId] 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_MareonAttachment' and sc.[name] = 'BlobId')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [BlobId] 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_MareonAttachment' and sc.[name] = 'Version')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Version] char(30) 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_MareonAttachment' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [FileName] char(250) 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_MareonAttachment' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] 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_MareonAttachment' and sc.[name] = 'Created')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Created] 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_MareonAttachment' and sc.[name] = 'Modify')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Modify] 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_MareonAttachment' and sc.[name] = 'Download')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Download] 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_MareonAttachment' and sc.[name] = 'Upload')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ADD [Upload] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ALTER COLUMN [NrBsh] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'MareonId')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ALTER COLUMN [MareonId] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'Version')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ALTER COLUMN [Version] char(30)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonAttachment' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_MareonAttachment] ALTER COLUMN [FileName] char(250)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonAttachment')
  DROP VIEW [dbo].[MareonAttachment]
GO
CREATE VIEW [dbo].[MareonAttachment] AS
  SELECT * FROM [dbo].[ut_MareonAttachment]
GO
CREATE TABLE [dbo].[ut_MareonAttachment_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(35) NOT NULL
  ,[Key01_Value] CHAR(51) NOT NULL
  CONSTRAINT [Pk_ut_MareonAttachment_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonAttachment_KeyDel] ON [dbo].[ut_MareonAttachment]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonAttachment_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_MareonAttachment_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([Type],1,0)+convert(char(20),[NrBsh])+STR([LfdNr],4,0)+STR(RecId,10))
     ,UPPER(STR([Type],1,0)+convert(char(20),[NrBsh])+convert(char(20),[MareonId])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_MareonAttachment_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([Type],1,0)+convert(char(20),[NrBsh])+STR([LfdNr],4,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(STR([Type],1,0)+convert(char(20),[NrBsh])+convert(char(20),[MareonId])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Add_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonAttachment] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonAttachment])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonAttachment] (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_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Upd_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonAttachment]
  @RI integer
 ,@Parm_Type integer
 ,@Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_Dropped tinyint
 ,@Parm_ReadOnly tinyint
 ,@Parm_MareonId char(20)
 ,@Parm_BlobId integer
 ,@Parm_Version char(30)
 ,@Parm_FileName char(250)
 ,@Parm_HostDate datetime
 ,@Parm_Created datetime
 ,@Parm_Modify datetime
 ,@Parm_Download datetime
 ,@Parm_Upload datetime
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonAttachment] SET
     [Type] = @Parm_Type
    ,[NrBsh] = RTRIM(@Parm_NrBsh)
    ,[LfdNr] = @Parm_LfdNr
    ,[Dropped] = @Parm_Dropped
    ,[ReadOnly] = @Parm_ReadOnly
    ,[MareonId] = RTRIM(@Parm_MareonId)
    ,[BlobId] = @Parm_BlobId
    ,[Version] = RTRIM(@Parm_Version)
    ,[FileName] = RTRIM(@Parm_FileName)
    ,[HostDate] = @Parm_HostDate
    ,[Created] = @Parm_Created
    ,[Modify] = @Parm_Modify
    ,[Download] = @Parm_Download
    ,[Upload] = @Parm_Upload
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Sel_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonAttachment]
  @RI integer
AS
SELECT
     [Type]
    ,[NrBsh]
    ,[LfdNr]
    ,[Dropped]
    ,[ReadOnly]
    ,[MareonId]
    ,[BlobId]
    ,[Version]
    ,[FileName]
    ,[HostDate]
    ,[Created]
    ,[Modify]
    ,[Download]
    ,[Upload]
  FROM [dbo].[ut_MareonAttachment]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Del_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonAttachment]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonAttachment] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Reorg_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonAttachment]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonAttachment] 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_MareonAttachment] 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_MareonAttachment] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonAttachment')
  DROP PROCEDURE [dbo].[up_Append_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonAttachment]
  @Parm_Type integer
 ,@Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_Dropped tinyint
 ,@Parm_ReadOnly tinyint
 ,@Parm_MareonId char(20)
 ,@Parm_BlobId integer
 ,@Parm_Version char(30)
 ,@Parm_FileName char(250)
 ,@Parm_HostDate datetime
 ,@Parm_Created datetime
 ,@Parm_Modify datetime
 ,@Parm_Download datetime
 ,@Parm_Upload datetime
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonAttachment])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonAttachment] (
    RecId
   ,[Type]
   ,[NrBsh]
   ,[LfdNr]
   ,[Dropped]
   ,[ReadOnly]
   ,[MareonId]
   ,[BlobId]
   ,[Version]
   ,[FileName]
   ,[HostDate]
   ,[Created]
   ,[Modify]
   ,[Download]
   ,[Upload]
     ) VALUES (
    @NeueId
   ,@Parm_Type
   ,RTRIM(@Parm_NrBsh)
   ,@Parm_LfdNr
   ,@Parm_Dropped
   ,@Parm_ReadOnly
   ,RTRIM(@Parm_MareonId)
   ,@Parm_BlobId
   ,RTRIM(@Parm_Version)
   ,RTRIM(@Parm_FileName)
   ,@Parm_HostDate
   ,@Parm_Created
   ,@Parm_Modify
   ,@Parm_Download
   ,@Parm_Upload
       )
  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_MareonAttachment')
  DROP PROCEDURE [dbo].[up_TrSel_MareonAttachment]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonAttachment]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NrBsh]))
      +LTRIM(RTRIM([MareonId]))
      +LTRIM(RTRIM([Version]))
      +LTRIM(RTRIM([FileName]))
    AS Data
  FROM     [dbo].[ut_MareonAttachment]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonAttachment_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonAttachment_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonAttachment_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonAttachment_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_MareonAttachment_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_First_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_Last_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_Prior_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_Next_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_Next50_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_FindL_MareonAttachment_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonAttachment_Key01')
  DROP PROCEDURE [dbo].[up_FindF_MareonAttachment_Key01]
GO

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

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

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

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

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

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

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

