------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_FileRef')
BEGIN
CREATE TABLE [dbo].[ut_FileRef]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[FileStamp] char(32) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[BlobId] integer DEFAULT 0
  ,[DefVersion] tinyint DEFAULT 0
  ,[Invisible] tinyint DEFAULT 0
  ,[Computer] char(128) DEFAULT ' '
  ,[Location] char(250) DEFAULT ' '
  ,[BaseName] char(200) DEFAULT ' '
  ,[Extension] char(20) DEFAULT ' '
  CONSTRAINT [Pk_FileRef]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_FileRef_KeyDel')
  DROP TRIGGER [dbo].[tr_FileRef_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_FileRef_KeyUpd')
  DROP TRIGGER [dbo].[tr_FileRef_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_FileRef_Keys')
  DROP TABLE [dbo].[ut_FileRef_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [FileStamp] char(32) 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_FileRef' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] 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_FileRef' and sc.[name] = 'BlobId')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] 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_FileRef' and sc.[name] = 'DefVersion')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [DefVersion] 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_FileRef' and sc.[name] = 'Invisible')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [Invisible] 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_FileRef' and sc.[name] = 'Computer')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [Computer] char(128) 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_FileRef' and sc.[name] = 'Location')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [Location] 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_FileRef' and sc.[name] = 'BaseName')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [BaseName] char(200) 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_FileRef' and sc.[name] = 'Extension')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ADD [Extension] char(20) NOT NULL DEFAULT ' '
END
GO
IF 32 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ALTER COLUMN [FileStamp] char(32)
END
GO
IF 128 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'Computer')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ALTER COLUMN [Computer] char(128)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'Location')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ALTER COLUMN [Location] char(250)
END
GO
IF 200 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'BaseName')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ALTER COLUMN [BaseName] char(200)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_FileRef' and sc.[name] = 'Extension')
BEGIN
  ALTER TABLE [dbo].[ut_FileRef] ALTER COLUMN [Extension] char(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'FileRef')
  DROP VIEW [dbo].[FileRef]
GO
CREATE VIEW [dbo].[FileRef] AS
  SELECT RecId
    ,[FileStamp]
    ,[LfdNr]
    ,[BlobId]
    ,[DefVersion]
    ,[Invisible]
    ,[Computer]
    ,[Location]
    ,[BaseName]
    ,[Extension]
  FROM [dbo].[ut_FileRef]
GO
CREATE TABLE [dbo].[ut_FileRef_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(46) NOT NULL
  ,[Key01_Filter] TINYINT NOT NULL
  ,[Key01_Value] CHAR(50) NOT NULL
  ,[Key02_Filter] TINYINT NOT NULL
  ,[Key02_Value] CHAR(42) NOT NULL
  ,[Key03_Filter] TINYINT NOT NULL
  ,[Key03_Value] CHAR(42) NOT NULL
  CONSTRAINT [Pk_ut_FileRef_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_FileRef_KeyDel] ON [dbo].[ut_FileRef]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_FileRef_Keys] /*WITH (SERIALIZABLE)*/
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_FileRef_KeyUpd] ON [dbo].[ut_FileRef]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_FileRef_Keys] /*WITH (SERIALIZABLE)*/
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_FileRef_Keys] /*WITH (SERIALIZABLE)*/
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Filter],[Key01_Value],[Key02_Filter],[Key02_Value],[Key03_Filter],[Key03_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(32),[FileStamp])+STR([LfdNr],4,0)+STR(RecId,10))
     ,CASE WHEN BlobId <> 0 THEN 1 ELSE 0 END
     ,UPPER(convert(char(32),[FileStamp])+STR([BlobId],8,0)+STR(RecId,10))
     ,CASE WHEN 1 =  DefVersion THEN 1 ELSE 0 END
     ,UPPER(convert(char(32),[FileStamp])+STR(RecId,10))
     ,CASE WHEN 0 =  Invisible THEN 1 ELSE 0 END
     ,UPPER(convert(char(32),[FileStamp])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_FileRef_Keys] /*WITH (SERIALIZABLE)*/ SET
    [Key00_Value] = UPPER(convert(char(32),[FileStamp])+STR([LfdNr],4,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(32),[FileStamp])+STR([BlobId],8,0)+STR([RecId],10))
   ,[Key01_Filter] = CASE WHEN BlobId <> 0 THEN 1 ELSE 0 END
   ,[Key02_Value] = UPPER(convert(char(32),[FileStamp])+STR([RecId],10))
   ,[Key02_Filter] = CASE WHEN 1 =  DefVersion THEN 1 ELSE 0 END
   ,[Key03_Value] = UPPER(convert(char(32),[FileStamp])+STR([RecId],10))
   ,[Key03_Filter] = CASE WHEN 0 =  Invisible THEN 1 ELSE 0 END
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_FileRef', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_FileRef
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_FileRef() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_FileRef] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_FileRef')
  DROP PROCEDURE [dbo].[up_Sel_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Sel_FileRef]
  @RI integer
AS
SELECT
     [FileStamp]
    ,[LfdNr]
    ,[BlobId]
    ,[DefVersion]
    ,[Invisible]
    ,[Computer]
    ,[Location]
    ,[BaseName]
    ,[Extension]
  FROM [dbo].[ut_FileRef]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_FileRef')
  DROP PROCEDURE [dbo].[up_Sel2_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Sel2_FileRef]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[FileRef]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_FileRef')
  DROP PROCEDURE [dbo].[up_Append_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Append_FileRef]
  @Parm_FileStamp char(32)
 ,@Parm_LfdNr integer
 ,@Parm_BlobId integer
 ,@Parm_DefVersion tinyint
 ,@Parm_Invisible tinyint
 ,@Parm_Computer char(128)
 ,@Parm_Location char(250)
 ,@Parm_BaseName char(200)
 ,@Parm_Extension char(20)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_FileRef()
      INSERT INTO [dbo].[ut_FileRef] (RecId
       ,[FileStamp]
       ,[LfdNr]
       ,[BlobId]
       ,[DefVersion]
       ,[Invisible]
       ,[Computer]
       ,[Location]
       ,[BaseName]
       ,[Extension]
         ) VALUES (@NeueId
       ,RTRIM(@Parm_FileStamp)
       ,@Parm_LfdNr
       ,@Parm_BlobId
       ,@Parm_DefVersion
       ,@Parm_Invisible
       ,RTRIM(@Parm_Computer)
       ,RTRIM(@Parm_Location)
       ,RTRIM(@Parm_BaseName)
       ,RTRIM(@Parm_Extension)
           )
      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
  SELECT @NeueId AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_FileRef')
  DROP PROCEDURE [dbo].[up_Upd_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Upd_FileRef]
  @RI integer
 ,@Parm_FileStamp char(32)
 ,@Parm_LfdNr integer
 ,@Parm_BlobId integer
 ,@Parm_DefVersion tinyint
 ,@Parm_Invisible tinyint
 ,@Parm_Computer char(128)
 ,@Parm_Location char(250)
 ,@Parm_BaseName char(200)
 ,@Parm_Extension char(20)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_FileRef] SET
     [FileStamp] = RTRIM(@Parm_FileStamp)
    ,[LfdNr] = @Parm_LfdNr
    ,[BlobId] = @Parm_BlobId
    ,[DefVersion] = @Parm_DefVersion
    ,[Invisible] = @Parm_Invisible
    ,[Computer] = RTRIM(@Parm_Computer)
    ,[Location] = RTRIM(@Parm_Location)
    ,[BaseName] = RTRIM(@Parm_BaseName)
    ,[Extension] = RTRIM(@Parm_Extension)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_FileRef')
  DROP PROCEDURE [dbo].[up_Upd2_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Upd2_FileRef]
  @Param_RecId varchar(20)
 ,@Param_FileStamp varchar(32)
 ,@Param_LfdNr varchar(20)
 ,@Param_BlobId varchar(20)
 ,@Param_DefVersion varchar(10)
 ,@Param_Invisible varchar(10)
 ,@Param_Computer varchar(128)
 ,@Param_Location varchar(250)
 ,@Param_BaseName varchar(200)
 ,@Param_Extension varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_FileRef] SET
     [FileStamp] = UPPER(RTRIM(@Param_FileStamp))
    ,[LfdNr] = dbo.itcfunc_float(@Param_LfdNr)
    ,[BlobId] = dbo.itcfunc_float(@Param_BlobId)
    ,[DefVersion] = dbo.itcfunc_integer(@Param_DefVersion)
    ,[Invisible] = dbo.itcfunc_integer(@Param_Invisible)
    ,[Computer] = RTRIM(@Param_Computer)
    ,[Location] = RTRIM(@Param_Location)
    ,[BaseName] = RTRIM(@Param_BaseName)
    ,[Extension] = RTRIM(@Param_Extension)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_FileRef] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_FileRef')
  DROP PROCEDURE [dbo].[up_TrSel_FileRef]
GO
CREATE PROCEDURE [dbo].[up_TrSel_FileRef]
AS
  SELECT   RecId,
      LTRIM(RTRIM([FileStamp]))
      +LTRIM(RTRIM([Computer]))
      +LTRIM(RTRIM([Location]))
      +LTRIM(RTRIM([BaseName]))
      +LTRIM(RTRIM([Extension]))
    AS Data
  FROM     [dbo].[ut_FileRef]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_FileRef')
  DROP PROCEDURE [dbo].[up_Add_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Add_FileRef] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_FileRef()
  INSERT INTO [dbo].[ut_FileRef] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_FileRef')
  DROP PROCEDURE [dbo].[up_Del_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Del_FileRef]
  @RI integer
AS
  DELETE FROM [dbo].[ut_FileRef] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_FileRef')
  DROP PROCEDURE [dbo].[up_Reorg_FileRef]
GO
CREATE PROCEDURE [dbo].[up_Reorg_FileRef]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_FileRef] 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_FileRef] 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_FileRef] )
  END
GO


CREATE INDEX [Ik_Key00] ON [dbo].[ut_FileRef_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_FileRef'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_FileRef]
(FileStamp,LfdNr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_First_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_Last_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_Prior_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_Next_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_Next50_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_FindL_FileRef_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_FileRef_Key00')
  DROP PROCEDURE [dbo].[up_FindF_FileRef_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_FileRef_Keys]
( [Key01_Filter],[Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_First_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_Last_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_Prior_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_Next_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_Next50_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_FindL_FileRef_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_FileRef_Key01')
  DROP PROCEDURE [dbo].[up_FindF_FileRef_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_FileRef_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_FileRef_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_FileRef_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_FileRef_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_FileRef_Keys]
( [Key02_Filter],[Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_First_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_Last_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_Prior_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_Next_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_Next50_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_FindL_FileRef_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_FileRef_Key02')
  DROP PROCEDURE [dbo].[up_FindF_FileRef_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_FileRef_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_FileRef_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_FileRef_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_FileRef_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_FileRef_Key02] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key02_Value] AS Key_Value FROM [dbo].[ut_FileRef_Keys]
      WHERE [Key02_Filter] = 1 AND [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_FileRef_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

CREATE INDEX [Ik_Key03] ON [dbo].[ut_FileRef_Keys]
( [Key03_Filter],[Key03_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_First_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_Last_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_Prior_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_Next_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_Next50_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_FindL_FileRef_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_FileRef_Key03')
  DROP PROCEDURE [dbo].[up_FindF_FileRef_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_FileRef_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_FileRef_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_FileRef_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_FileRef_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_FileRef_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_FileRef_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] < (SELECT [Key03_Value] FROM [dbo].[ut_FileRef_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value] DESC
GO

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

CREATE PROCEDURE [dbo].[up_FindL_FileRef_Key03] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_FileRef_Keys]
  WHERE [Key03_Filter] = 1 AND [Key03_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key03_Value] DESC
GO

