------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_OCVendorFile')
BEGIN
CREATE TABLE [dbo].[ut_OCVendorFile]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[VendorId] char(10) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[LfdNrSub] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[DatenDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Groesse] integer DEFAULT 0
  ,[Auth] char(10) DEFAULT ' '
  ,[Beschr] char(120) DEFAULT ' '
  ,[Info] char(120) DEFAULT ' '
  ,[Url] char(250) DEFAULT ' '
  ,[DateiType] char(20) DEFAULT ' '
  ,[DateiName] char(128) DEFAULT ' '
  ,[FileStamp] char(32) DEFAULT ' '
  CONSTRAINT [Pk_OCVendorFile]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_OCVendorFile_KeyDel')
  DROP TRIGGER [dbo].[tr_OCVendorFile_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_OCVendorFile_KeyUpd')
  DROP TRIGGER [dbo].[tr_OCVendorFile_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_OCVendorFile_Keys')
  DROP TABLE [dbo].[ut_OCVendorFile_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'VendorId')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] 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_OCVendorFile' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] 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_OCVendorFile' and sc.[name] = 'LfdNrSub')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [LfdNrSub] 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_OCVendorFile' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [Datum] 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_OCVendorFile' and sc.[name] = 'DatenDatum')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [DatenDatum] 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_OCVendorFile' and sc.[name] = 'Groesse')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [Groesse] 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_OCVendorFile' and sc.[name] = 'Auth')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [Auth] 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_OCVendorFile' and sc.[name] = 'Beschr')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [Beschr] char(120) 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_OCVendorFile' and sc.[name] = 'Info')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [Info] char(120) 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_OCVendorFile' and sc.[name] = 'Url')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] 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_OCVendorFile' and sc.[name] = 'DateiType')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [DateiType] 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_OCVendorFile' and sc.[name] = 'DateiName')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [DateiName] 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_OCVendorFile' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ADD [FileStamp] char(32) NOT NULL DEFAULT ' '
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'VendorId')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [VendorId] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'Auth')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [Auth] char(10)
END
GO
IF 120 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'Beschr')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [Beschr] char(120)
END
GO
IF 120 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'Info')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [Info] char(120)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'Url')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [Url] char(250)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'DateiType')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [DateiType] char(20)
END
GO
IF 128 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'DateiName')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [DateiName] char(128)
END
GO
IF 32 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_OCVendorFile' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_OCVendorFile] ALTER COLUMN [FileStamp] char(32)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'OCVendorFile')
  DROP VIEW [dbo].[OCVendorFile]
GO
CREATE VIEW [dbo].[OCVendorFile] AS
  SELECT * FROM [dbo].[ut_OCVendorFile]
GO
CREATE TABLE [dbo].[ut_OCVendorFile_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(36) NOT NULL
  CONSTRAINT [Pk_ut_OCVendorFile_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_OCVendorFile_KeyDel] ON [dbo].[ut_OCVendorFile]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_OCVendorFile_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_OCVendorFile_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(10),[VendorId])+STR([LfdNr],8,0)+STR([LfdNrSub],8,0)+STR(RecId,10))

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Add_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Add_OCVendorFile] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_OCVendorFile])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_OCVendorFile] (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_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Upd_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Upd_OCVendorFile]
  @RI integer
 ,@Parm_VendorId char(10)
 ,@Parm_LfdNr integer
 ,@Parm_LfdNrSub integer
 ,@Parm_Datum datetime
 ,@Parm_DatenDatum datetime
 ,@Parm_Groesse integer
 ,@Parm_Auth char(10)
 ,@Parm_Beschr char(120)
 ,@Parm_Info char(120)
 ,@Parm_Url char(250)
 ,@Parm_DateiType char(20)
 ,@Parm_DateiName char(128)
 ,@Parm_FileStamp char(32)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_OCVendorFile] SET
     [VendorId] = RTRIM(@Parm_VendorId)
    ,[LfdNr] = @Parm_LfdNr
    ,[LfdNrSub] = @Parm_LfdNrSub
    ,[Datum] = @Parm_Datum
    ,[DatenDatum] = @Parm_DatenDatum
    ,[Groesse] = @Parm_Groesse
    ,[Auth] = RTRIM(@Parm_Auth)
    ,[Beschr] = RTRIM(@Parm_Beschr)
    ,[Info] = RTRIM(@Parm_Info)
    ,[Url] = RTRIM(@Parm_Url)
    ,[DateiType] = RTRIM(@Parm_DateiType)
    ,[DateiName] = RTRIM(@Parm_DateiName)
    ,[FileStamp] = RTRIM(@Parm_FileStamp)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Sel_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Sel_OCVendorFile]
  @RI integer
AS
SELECT
     [VendorId]
    ,[LfdNr]
    ,[LfdNrSub]
    ,[Datum]
    ,[DatenDatum]
    ,[Groesse]
    ,[Auth]
    ,[Beschr]
    ,[Info]
    ,[Url]
    ,[DateiType]
    ,[DateiName]
    ,[FileStamp]
  FROM [dbo].[ut_OCVendorFile]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Del_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Del_OCVendorFile]
  @RI integer
AS
  DELETE FROM [dbo].[ut_OCVendorFile] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Reorg_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Reorg_OCVendorFile]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_OCVendorFile] 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_OCVendorFile] 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_OCVendorFile] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_OCVendorFile')
  DROP PROCEDURE [dbo].[up_Append_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_Append_OCVendorFile]
  @Parm_VendorId char(10)
 ,@Parm_LfdNr integer
 ,@Parm_LfdNrSub integer
 ,@Parm_Datum datetime
 ,@Parm_DatenDatum datetime
 ,@Parm_Groesse integer
 ,@Parm_Auth char(10)
 ,@Parm_Beschr char(120)
 ,@Parm_Info char(120)
 ,@Parm_Url char(250)
 ,@Parm_DateiType char(20)
 ,@Parm_DateiName char(128)
 ,@Parm_FileStamp char(32)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_OCVendorFile])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_OCVendorFile] (
    RecId
   ,[VendorId]
   ,[LfdNr]
   ,[LfdNrSub]
   ,[Datum]
   ,[DatenDatum]
   ,[Groesse]
   ,[Auth]
   ,[Beschr]
   ,[Info]
   ,[Url]
   ,[DateiType]
   ,[DateiName]
   ,[FileStamp]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_VendorId)
   ,@Parm_LfdNr
   ,@Parm_LfdNrSub
   ,@Parm_Datum
   ,@Parm_DatenDatum
   ,@Parm_Groesse
   ,RTRIM(@Parm_Auth)
   ,RTRIM(@Parm_Beschr)
   ,RTRIM(@Parm_Info)
   ,RTRIM(@Parm_Url)
   ,RTRIM(@Parm_DateiType)
   ,RTRIM(@Parm_DateiName)
   ,RTRIM(@Parm_FileStamp)
       )
  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_OCVendorFile')
  DROP PROCEDURE [dbo].[up_TrSel_OCVendorFile]
GO
CREATE PROCEDURE [dbo].[up_TrSel_OCVendorFile]
AS
  SELECT   RecId,
      LTRIM(RTRIM([VendorId]))
      +LTRIM(RTRIM([Auth]))
      +LTRIM(RTRIM([Beschr]))
      +LTRIM(RTRIM([Info]))
      +LTRIM(RTRIM([Url]))
      +LTRIM(RTRIM([DateiType]))
      +LTRIM(RTRIM([DateiName]))
      +LTRIM(RTRIM([FileStamp]))
    AS Data
  FROM     [dbo].[ut_OCVendorFile]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_OCVendorFile_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_First_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_Last_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_Prior_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_Next_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_Next50_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_FindL_OCVendorFile_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_OCVendorFile_Key00')
  DROP PROCEDURE [dbo].[up_FindF_OCVendorFile_Key00]
GO

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

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

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

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

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

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

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

