------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_DnoFileRefs')
BEGIN
CREATE TABLE [dbo].[ut_DnoFileRefs]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[VendorId] char(10) DEFAULT ' '
  ,[FileKey] char(50) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[Name] char(100) DEFAULT ' '
  ,[Format] char(30) DEFAULT ' '
  ,[URL] char(250) DEFAULT ' '
  ,[FileName] char(50) DEFAULT ' '
  ,[FileDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[FileSize] integer DEFAULT 0
  ,[HttpAuth] tinyint DEFAULT 0
  ,[Domain] char(250) DEFAULT ' '
  ,[Cookie] char(250) DEFAULT ' '
  ,[SaveName] char(250) DEFAULT ' '
  ,[IsPacked] tinyint DEFAULT 0
  ,[DtFileRef] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[DtDownLoad] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  CONSTRAINT [Pk_DnoFileRefs]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_DnoFileRefs_KeyDel')
  DROP TRIGGER [dbo].[tr_DnoFileRefs_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_DnoFileRefs_KeyUpd')
  DROP TRIGGER [dbo].[tr_DnoFileRefs_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_DnoFileRefs_Keys')
  DROP TABLE [dbo].[ut_DnoFileRefs_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'VendorId')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [VendorId] char(10) 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_DnoFileRefs' and sc.[name] = 'FileKey')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [FileKey] char(50) 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_DnoFileRefs' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] 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_DnoFileRefs' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [Name] char(100) 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_DnoFileRefs' and sc.[name] = 'Format')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [Format] 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_DnoFileRefs' and sc.[name] = 'URL')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [URL] 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_DnoFileRefs' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [FileName] char(50) 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_DnoFileRefs' and sc.[name] = 'FileDate')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [FileDate] 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_DnoFileRefs' and sc.[name] = 'FileSize')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [FileSize] 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_DnoFileRefs' and sc.[name] = 'HttpAuth')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [HttpAuth] 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_DnoFileRefs' and sc.[name] = 'Domain')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [Domain] 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_DnoFileRefs' and sc.[name] = 'Cookie')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [Cookie] 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_DnoFileRefs' and sc.[name] = 'SaveName')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [SaveName] 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_DnoFileRefs' and sc.[name] = 'IsPacked')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [IsPacked] 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_DnoFileRefs' and sc.[name] = 'DtFileRef')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [DtFileRef] 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_DnoFileRefs' and sc.[name] = 'DtDownLoad')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ADD [DtDownLoad] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'VendorId')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [VendorId] char(10)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'FileKey')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [FileKey] char(50)
END
GO
IF 100 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [Name] char(100)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'Format')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [Format] char(30)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'URL')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [URL] char(250)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'FileName')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [FileName] char(50)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'Domain')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [Domain] char(250)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'Cookie')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [Cookie] char(250)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_DnoFileRefs' and sc.[name] = 'SaveName')
BEGIN
  ALTER TABLE [dbo].[ut_DnoFileRefs] ALTER COLUMN [SaveName] char(250)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'DnoFileRefs')
  DROP VIEW [dbo].[DnoFileRefs]
GO
CREATE VIEW [dbo].[DnoFileRefs] AS
  SELECT * FROM [dbo].[ut_DnoFileRefs]
GO
CREATE TABLE [dbo].[ut_DnoFileRefs_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(74) NOT NULL
  CONSTRAINT [Pk_ut_DnoFileRefs_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_DnoFileRefs_KeyDel] ON [dbo].[ut_DnoFileRefs]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_DnoFileRefs_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_DnoFileRefs_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(10),[VendorId])+convert(char(50),[FileKey])+STR([LfdNr],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_DnoFileRefs_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(10),[VendorId])+convert(char(50),[FileKey])+STR([LfdNr],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Add_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Add_DnoFileRefs] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_DnoFileRefs])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_DnoFileRefs] (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_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Upd_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Upd_DnoFileRefs]
  @RI integer
 ,@Parm_VendorId char(10)
 ,@Parm_FileKey char(50)
 ,@Parm_LfdNr integer
 ,@Parm_Name char(100)
 ,@Parm_Format char(30)
 ,@Parm_URL char(250)
 ,@Parm_FileName char(50)
 ,@Parm_FileDate datetime
 ,@Parm_FileSize integer
 ,@Parm_HttpAuth tinyint
 ,@Parm_Domain char(250)
 ,@Parm_Cookie char(250)
 ,@Parm_SaveName char(250)
 ,@Parm_IsPacked tinyint
 ,@Parm_DtFileRef datetime
 ,@Parm_DtDownLoad datetime
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_DnoFileRefs] SET
     [VendorId] = RTRIM(@Parm_VendorId)
    ,[FileKey] = RTRIM(@Parm_FileKey)
    ,[LfdNr] = @Parm_LfdNr
    ,[Name] = RTRIM(@Parm_Name)
    ,[Format] = RTRIM(@Parm_Format)
    ,[URL] = RTRIM(@Parm_URL)
    ,[FileName] = RTRIM(@Parm_FileName)
    ,[FileDate] = @Parm_FileDate
    ,[FileSize] = @Parm_FileSize
    ,[HttpAuth] = @Parm_HttpAuth
    ,[Domain] = RTRIM(@Parm_Domain)
    ,[Cookie] = RTRIM(@Parm_Cookie)
    ,[SaveName] = RTRIM(@Parm_SaveName)
    ,[IsPacked] = @Parm_IsPacked
    ,[DtFileRef] = @Parm_DtFileRef
    ,[DtDownLoad] = @Parm_DtDownLoad
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Sel_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Sel_DnoFileRefs]
  @RI integer
AS
SELECT
     [VendorId]
    ,[FileKey]
    ,[LfdNr]
    ,[Name]
    ,[Format]
    ,[URL]
    ,[FileName]
    ,[FileDate]
    ,[FileSize]
    ,[HttpAuth]
    ,[Domain]
    ,[Cookie]
    ,[SaveName]
    ,[IsPacked]
    ,[DtFileRef]
    ,[DtDownLoad]
  FROM [dbo].[ut_DnoFileRefs]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Del_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Del_DnoFileRefs]
  @RI integer
AS
  DELETE FROM [dbo].[ut_DnoFileRefs] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Reorg_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Reorg_DnoFileRefs]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_DnoFileRefs] 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_DnoFileRefs] 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_DnoFileRefs] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_Append_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_Append_DnoFileRefs]
  @Parm_VendorId char(10)
 ,@Parm_FileKey char(50)
 ,@Parm_LfdNr integer
 ,@Parm_Name char(100)
 ,@Parm_Format char(30)
 ,@Parm_URL char(250)
 ,@Parm_FileName char(50)
 ,@Parm_FileDate datetime
 ,@Parm_FileSize integer
 ,@Parm_HttpAuth tinyint
 ,@Parm_Domain char(250)
 ,@Parm_Cookie char(250)
 ,@Parm_SaveName char(250)
 ,@Parm_IsPacked tinyint
 ,@Parm_DtFileRef datetime
 ,@Parm_DtDownLoad datetime
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_DnoFileRefs])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_DnoFileRefs] (
    RecId
   ,[VendorId]
   ,[FileKey]
   ,[LfdNr]
   ,[Name]
   ,[Format]
   ,[URL]
   ,[FileName]
   ,[FileDate]
   ,[FileSize]
   ,[HttpAuth]
   ,[Domain]
   ,[Cookie]
   ,[SaveName]
   ,[IsPacked]
   ,[DtFileRef]
   ,[DtDownLoad]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_VendorId)
   ,RTRIM(@Parm_FileKey)
   ,@Parm_LfdNr
   ,RTRIM(@Parm_Name)
   ,RTRIM(@Parm_Format)
   ,RTRIM(@Parm_URL)
   ,RTRIM(@Parm_FileName)
   ,@Parm_FileDate
   ,@Parm_FileSize
   ,@Parm_HttpAuth
   ,RTRIM(@Parm_Domain)
   ,RTRIM(@Parm_Cookie)
   ,RTRIM(@Parm_SaveName)
   ,@Parm_IsPacked
   ,@Parm_DtFileRef
   ,@Parm_DtDownLoad
       )
  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_DnoFileRefs')
  DROP PROCEDURE [dbo].[up_TrSel_DnoFileRefs]
GO
CREATE PROCEDURE [dbo].[up_TrSel_DnoFileRefs]
AS
  SELECT   RecId,
      LTRIM(RTRIM([VendorId]))
      +LTRIM(RTRIM([FileKey]))
      +LTRIM(RTRIM([Name]))
      +LTRIM(RTRIM([Format]))
      +LTRIM(RTRIM([URL]))
      +LTRIM(RTRIM([FileName]))
      +LTRIM(RTRIM([Domain]))
      +LTRIM(RTRIM([Cookie]))
      +LTRIM(RTRIM([SaveName]))
    AS Data
  FROM     [dbo].[ut_DnoFileRefs]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_DnoFileRefs_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_First_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_Last_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_Prior_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_Next_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_Next50_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_FindL_DnoFileRefs_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_DnoFileRefs_Key00')
  DROP PROCEDURE [dbo].[up_FindF_DnoFileRefs_Key00]
GO

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

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

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

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

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

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

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

