-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_SearchHint')
BEGIN
CREATE TABLE [dbo].[ut_SearchHint]
(
  [RecId]  integer NOT NULL
 ,[Token] char(30) DEFAULT ''
 ,[UsageCount] integer DEFAULT 0
 ,[Weight] integer DEFAULT 0
 ,[Exclude] tinyint DEFAULT 0
  CONSTRAINT [Pk_SearchHint]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_SearchHint_KeyDel')
  DROP TRIGGER [dbo].[tr_SearchHint_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_SearchHint_KeyUpd')
  DROP TRIGGER [dbo].[tr_SearchHint_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_SearchHint_Keys')
  DROP TABLE [dbo].[ut_SearchHint_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SearchHint' and sc.[name] = 'Token')
BEGIN
  ALTER TABLE [dbo].[ut_SearchHint] ADD [Token] 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_SearchHint' and sc.[name] = 'UsageCount')
BEGIN
  ALTER TABLE [dbo].[ut_SearchHint] ADD [UsageCount] 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_SearchHint' and sc.[name] = 'Weight')
BEGIN
  ALTER TABLE [dbo].[ut_SearchHint] ADD [Weight] 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_SearchHint' and sc.[name] = 'Exclude')
BEGIN
  ALTER TABLE [dbo].[ut_SearchHint] ADD [Exclude] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SearchHint' and sc.[name] = 'Token'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SearchHint' and sc.[name] = 'Token'))
BEGIN
   ALTER TABLE [dbo].[ut_SearchHint] ALTER COLUMN [Token] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SearchHint' and sc.[name] = 'Token'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SearchHint' and sc.[name] = 'Token'))
BEGIN
   ALTER TABLE [dbo].[ut_SearchHint] ALTER COLUMN [Token] varchar(30)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'SearchHint')
  DROP VIEW [dbo].[SearchHint]
GO
CREATE VIEW [dbo].[SearchHint] AS
  SELECT [RecId]
 ,[Token]
 ,[UsageCount]
 ,[Weight]
 ,[Exclude]
  FROM [dbo].[ut_SearchHint]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_SearchHint', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_SearchHint
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_SearchHint() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_SearchHint] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_SearchHint')
  DROP PROCEDURE [dbo].[up_Sel_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Sel_SearchHint]  @mRI integer
AS
  SELECT
    [Token]
   ,[UsageCount]
   ,[Weight]
   ,[Exclude]
  FROM [dbo].[ut_SearchHint] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_SearchHint')
  DROP PROCEDURE [dbo].[up_Sel2_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Sel2_SearchHint]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[SearchHint] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_SearchHint')
  DROP PROCEDURE [dbo].[up_Append_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Append_SearchHint]
   @Param_Token char(30)
  ,@Param_UsageCount integer
  ,@Param_Weight integer
  ,@Param_Exclude tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_SearchHint()
      INSERT INTO [dbo].[ut_SearchHint] (RecId
        ,[Token]
        ,[UsageCount]
        ,[Weight]
        ,[Exclude]
          ) VALUES (@NeueId
        ,RTRIM(@Param_Token)
        ,@Param_UsageCount
        ,@Param_Weight
        ,@Param_Exclude
          )
      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 -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_SearchHint')
  DROP PROCEDURE [dbo].[up_Add_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Add_SearchHint]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_SearchHint()
  INSERT INTO [dbo].[ut_SearchHint] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_SearchHint')
  DROP PROCEDURE [dbo].[up_Upd_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Upd_SearchHint] @mRI integer
  ,@Param_Token char(30)
  ,@Param_UsageCount integer
  ,@Param_Weight integer
  ,@Param_Exclude tinyint
AS
  UPDATE [dbo].[ut_SearchHint] SET
    [Token] = RTRIM(@Param_Token)
   ,[UsageCount] = @Param_UsageCount
   ,[Weight] = @Param_Weight
   ,[Exclude] = @Param_Exclude
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_SearchHint')
  DROP PROCEDURE [dbo].[up_Upd2_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Upd2_SearchHint] @Param_RecId varchar(20)
  ,@Param_Token varchar(30)
  ,@Param_UsageCount varchar(8)
  ,@Param_Weight varchar(2)
  ,@Param_Exclude varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_SearchHint] SET
    [Token] = RTRIM(@Param_Token)
   ,[UsageCount] = dbo.itcfunc_integer(@Param_UsageCount)
   ,[Weight] = dbo.itcfunc_integer(@Param_Weight)
   ,[Exclude] = dbo.itcfunc_integer(@Param_Exclude)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_SearchHint] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_SearchHint')
  DROP PROCEDURE [dbo].[up_Del_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Del_SearchHint] @mRI integer
AS
  DELETE FROM [dbo].[ut_SearchHint] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_SearchHint')
  DROP PROCEDURE [dbo].[up_TrSel_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_TrSel_SearchHint]
AS
  SELECT RecId,
    LTRIM(RTRIM([Token]))
  AS Data FROM [dbo].[ut_SearchHint] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_SearchHint')
  DROP PROCEDURE [dbo].[up_Reorg_SearchHint]
GO
CREATE PROCEDURE [dbo].[up_Reorg_SearchHint] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_SearchHint] 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_SearchHint] 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_SearchHint] )
  END
GO

-- logical key  ORDER BY Token,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_SearchHint' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_SearchHint]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_SearchHint' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_SearchHint] (Token,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_First_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_SearchHint_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) ORDER BY Token,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_SearchHint_Key00] AS EXECUTE [dbo].[up_First_SearchHint_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_Last_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_SearchHint_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) ORDER BY Token desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_SearchHint_Key00] AS EXECUTE [dbo].[up_Last_SearchHint_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_Next_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_SearchHint_Key00] @oldRecId integer AS
  DECLARE @oldToken char(30)
  SELECT @oldToken = Token FROM [dbo].[ut_SearchHint] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00)
    WHERE (Token = @oldToken) AND (RecId > @oldRecId)
    ORDER BY Token,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00)
    WHERE (Token > @oldToken)
    ORDER BY Token,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_SearchHint_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_SearchHint_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_Prior_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_SearchHint_Key00] @oldRecId integer AS
  DECLARE @oldToken char(30)
  SELECT @oldToken = Token FROM [dbo].[ut_SearchHint] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00)
    WHERE (Token = @oldToken) AND (RecId < @oldRecId)
    ORDER BY Token desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00)
    WHERE (Token < @oldToken)
    ORDER BY Token desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_SearchHint_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_SearchHint_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_Next50_SearchHint_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_SearchHint_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_SearchHint_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_SearchHint_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_SearchHint_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_SearchHint_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_FindF_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_SearchHint_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mToken char(30) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,30)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) WHERE 
      Token = @mToken
      ORDER BY Token,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) WHERE 
      Token >= @mToken AND SUBSTRING(Token,1,LEN(@mToken)) = @mToken
      ORDER BY Token,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_SearchHint_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_SearchHint_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_SearchHint_Key00')
  DROP PROCEDURE [dbo].[up_FindL_SearchHint_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_SearchHint_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mToken char(30) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,30)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) WHERE 
      Token = @mToken
      ORDER BY Token desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Token,1,LEN(@mToken)) = @mToken
      ORDER BY Token desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_SearchHint_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_SearchHint_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_SearchHint_Key00] @xKey, @xLen')
GO


-- logical key  ORDER BY Weight,UsageCount,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_SearchHint' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_SearchHint]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_SearchHint' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_SearchHint] (Weight,UsageCount,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_First_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_SearchHint_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 0 =Exclude ORDER BY Weight,UsageCount,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_SearchHint_Key01] AS EXECUTE [dbo].[up_First_SearchHint_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_Last_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_SearchHint_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 0 =Exclude ORDER BY Weight desc,UsageCount desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_SearchHint_Key01] AS EXECUTE [dbo].[up_Last_SearchHint_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_Next_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_SearchHint_Key01] @oldRecId integer AS
  DECLARE @oldWeight integer
  DECLARE @oldUsageCount integer
  SELECT @oldWeight = Weight, @oldUsageCount = UsageCount FROM [dbo].[ut_SearchHint] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight = @oldWeight) AND (UsageCount = @oldUsageCount) AND (RecId > @oldRecId)) AND (0 =Exclude)
    ORDER BY Weight,UsageCount,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight = @oldWeight) AND (UsageCount > @oldUsageCount)) AND (0 =Exclude)
    ORDER BY Weight,UsageCount,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight > @oldWeight)) AND (0 =Exclude)
    ORDER BY Weight,UsageCount,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_SearchHint_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_SearchHint_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_Prior_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_SearchHint_Key01] @oldRecId integer AS
  DECLARE @oldWeight integer
  DECLARE @oldUsageCount integer
  SELECT @oldWeight = Weight, @oldUsageCount = UsageCount FROM [dbo].[ut_SearchHint] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight = @oldWeight) AND (UsageCount = @oldUsageCount) AND (RecId < @oldRecId)) AND (0 =Exclude)
    ORDER BY Weight desc,UsageCount desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight = @oldWeight) AND (UsageCount < @oldUsageCount)) AND (0 =Exclude)
    ORDER BY Weight desc,UsageCount desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01)
    WHERE ((Weight < @oldWeight)) AND (0 =Exclude)
    ORDER BY Weight desc,UsageCount desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_SearchHint_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_SearchHint_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_Next50_SearchHint_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_SearchHint_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_SearchHint_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_SearchHint_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_SearchHint_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_SearchHint_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_FindF_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_SearchHint_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mWeight integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mUsageCount integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 
      Weight = @mWeight AND 
      UsageCount = @mUsageCount
      AND (0 =Exclude)
      ORDER BY Weight,UsageCount,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 
      Weight = @mWeight
      AND (0 =Exclude)
      ORDER BY Weight,UsageCount,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)



  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_SearchHint_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_SearchHint_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_SearchHint_Key01')
  DROP PROCEDURE [dbo].[up_FindL_SearchHint_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_SearchHint_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mWeight integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mUsageCount integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 
      Weight = @mWeight AND 
      UsageCount = @mUsageCount
      AND (0 =Exclude)
      ORDER BY Weight desc,UsageCount desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SearchHint] WITH(INDEX = Ik_Key01) WHERE 
      Weight = @mWeight
      AND (0 =Exclude)
      ORDER BY Weight desc,UsageCount desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)



  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_SearchHint_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_SearchHint_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_SearchHint_Key01] @xKey, @xLen')
GO


