-- 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_TableSnipped')
BEGIN
CREATE TABLE [dbo].[ut_TableSnipped]
(
  [RecId]  integer NOT NULL
 ,[SnippedId] varchar(40) DEFAULT ''
 ,[TableName] varchar(30) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
 ,[CurrTime] varchar(16) DEFAULT ''
 ,[DataLen] integer DEFAULT 0
  CONSTRAINT [Pk_TableSnipped]  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_TableSnipped_KeyDel')
  DROP TRIGGER [dbo].[tr_TableSnipped_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_TableSnipped_KeyUpd')
  DROP TRIGGER [dbo].[tr_TableSnipped_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_TableSnipped_Keys')
  DROP TABLE [dbo].[ut_TableSnipped_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_TableSnipped' and sc.[name] = 'SnippedId')
BEGIN
  ALTER TABLE [dbo].[ut_TableSnipped] ADD [SnippedId] varchar(40) 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_TableSnipped' and sc.[name] = 'TableName')
BEGIN
  ALTER TABLE [dbo].[ut_TableSnipped] ADD [TableName] varchar(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_TableSnipped' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_TableSnipped] 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_TableSnipped' and sc.[name] = 'CurrTime')
BEGIN
  ALTER TABLE [dbo].[ut_TableSnipped] ADD [CurrTime] varchar(16) 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_TableSnipped' and sc.[name] = 'DataLen')
BEGIN
  ALTER TABLE [dbo].[ut_TableSnipped] ADD [DataLen] integer NOT NULL DEFAULT 0
END
GO

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

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'TableSnipped')
  DROP VIEW [dbo].[TableSnipped]
GO
CREATE VIEW [dbo].[TableSnipped] AS
  SELECT [RecId]
 ,[SnippedId]
 ,[TableName]
 ,[LfdNr]
 ,[CurrTime]
 ,[DataLen]
  FROM [dbo].[ut_TableSnipped]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_TableSnipped')
  DROP PROCEDURE [dbo].[up_Sel_TableSnipped]
GO
CREATE PROCEDURE [dbo].[up_Sel_TableSnipped]  @mRI integer
AS
  SELECT
    [SnippedId]
   ,[TableName]
   ,[LfdNr]
   ,[CurrTime]
   ,[DataLen]
  FROM [dbo].[ut_TableSnipped] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_TableSnipped')
  DROP PROCEDURE [dbo].[up_Append_TableSnipped]
GO
CREATE PROCEDURE [dbo].[up_Append_TableSnipped]
   @Param_SnippedId varchar(40)
  ,@Param_TableName varchar(30)
  ,@Param_LfdNr integer
  ,@Param_CurrTime varchar(16)
  ,@Param_DataLen integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_TableSnipped()
      INSERT INTO [dbo].[ut_TableSnipped] (RecId
        ,[SnippedId]
        ,[TableName]
        ,[LfdNr]
        ,[CurrTime]
        ,[DataLen]
          ) VALUES (@NeueId
        ,RTRIM(@Param_SnippedId)
        ,RTRIM(@Param_TableName)
        ,@Param_LfdNr
        ,RTRIM(@Param_CurrTime)
        ,@Param_DataLen
          )
      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_TableSnipped')
  DROP PROCEDURE [dbo].[up_Add_TableSnipped]
GO
CREATE PROCEDURE [dbo].[up_Add_TableSnipped]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_TableSnipped()
  INSERT INTO [dbo].[ut_TableSnipped] (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_TableSnipped')
  DROP PROCEDURE [dbo].[up_Upd_TableSnipped]
GO
CREATE PROCEDURE [dbo].[up_Upd_TableSnipped] @mRI integer
  ,@Param_SnippedId varchar(40)
  ,@Param_TableName varchar(30)
  ,@Param_LfdNr integer
  ,@Param_CurrTime varchar(16)
  ,@Param_DataLen integer
AS
  UPDATE [dbo].[ut_TableSnipped] SET
    [SnippedId] = RTRIM(@Param_SnippedId)
   ,[TableName] = RTRIM(@Param_TableName)
   ,[LfdNr] = @Param_LfdNr
   ,[CurrTime] = RTRIM(@Param_CurrTime)
   ,[DataLen] = @Param_DataLen
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_TableSnipped')
  DROP PROCEDURE [dbo].[up_Upd2_TableSnipped]
GO
CREATE PROCEDURE [dbo].[up_Upd2_TableSnipped] @Param_RecId varchar(20)
  ,@Param_SnippedId varchar(40)
  ,@Param_TableName varchar(30)
  ,@Param_LfdNr varchar(5)
  ,@Param_CurrTime varchar(16)
  ,@Param_DataLen varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_TableSnipped] SET
    [SnippedId] = RTRIM(@Param_SnippedId)
   ,[TableName] = RTRIM(@Param_TableName)
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[CurrTime] = RTRIM(@Param_CurrTime)
   ,[DataLen] = dbo.itcfunc_integer(@Param_DataLen)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_TableSnipped] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_TableSnipped_Key00')
  DROP PROCEDURE [dbo].[up_Next_TableSnipped_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_TableSnipped_Key00] @oldRecId integer AS
  DECLARE @oldSnippedId varchar(40)
  DECLARE @oldTableName varchar(30)
  SELECT @oldSnippedId = SnippedId, @oldTableName = TableName FROM [dbo].[ut_TableSnipped] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00)
    WHERE (SnippedId = @oldSnippedId) AND (TableName > @oldTableName)
    ORDER BY SnippedId,TableName,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00)
    WHERE (SnippedId > @oldSnippedId)
    ORDER BY SnippedId,TableName,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_TableSnipped_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_TableSnipped_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_TableSnipped_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_TableSnipped_Key00')
  DROP PROCEDURE [dbo].[up_Prior_TableSnipped_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_TableSnipped_Key00] @oldRecId integer AS
  DECLARE @oldSnippedId varchar(40)
  DECLARE @oldTableName varchar(30)
  SELECT @oldSnippedId = SnippedId, @oldTableName = TableName FROM [dbo].[ut_TableSnipped] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00)
    WHERE (SnippedId = @oldSnippedId) AND (TableName < @oldTableName)
    ORDER BY SnippedId desc,TableName desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00)
    WHERE (SnippedId < @oldSnippedId)
    ORDER BY SnippedId desc,TableName 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_TableSnipped_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_TableSnipped_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_TableSnipped_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId AND 
      TableName = @mTableName
      ORDER BY SnippedId,TableName,RecId
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId
      ORDER BY SnippedId,TableName,RecId
    RETURN
  END

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

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId >= @mSnippedId AND SUBSTRING(SnippedId,1,LEN(@mSnippedId)) = @mSnippedId
      ORDER BY SnippedId,TableName,RecId
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId AND 
      TableName >= @mTableName AND SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY SnippedId,TableName,RecId
    RETURN
  END

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

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

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

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId AND 
      TableName = @mTableName
      ORDER BY SnippedId desc,TableName desc,RecId desc
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId
      ORDER BY SnippedId desc,TableName desc,RecId desc
    RETURN
  END

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

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(SnippedId,1,LEN(@mSnippedId)) = @mSnippedId
      ORDER BY SnippedId desc,TableName desc,RecId desc
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key00) WHERE 
      SnippedId = @mSnippedId AND 
      SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY SnippedId desc,TableName desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_First_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_TableSnipped_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) ORDER BY SnippedId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_TableSnipped_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_TableSnipped_Key01] AS EXECUTE [dbo].[up_First_TableSnipped_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_Last_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_TableSnipped_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) ORDER BY SnippedId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_TableSnipped_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_TableSnipped_Key01] AS EXECUTE [dbo].[up_Last_TableSnipped_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_Next_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_TableSnipped_Key01] @oldRecId integer AS
  DECLARE @oldSnippedId varchar(40)
  DECLARE @oldLfdNr integer
  SELECT @oldSnippedId = SnippedId, @oldLfdNr = LfdNr FROM [dbo].[ut_TableSnipped] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId = @oldSnippedId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY SnippedId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId = @oldSnippedId) AND (LfdNr > @oldLfdNr)
    ORDER BY SnippedId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId > @oldSnippedId)
    ORDER BY SnippedId,LfdNr,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_TableSnipped_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_TableSnipped_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_TableSnipped_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_Prior_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_TableSnipped_Key01] @oldRecId integer AS
  DECLARE @oldSnippedId varchar(40)
  DECLARE @oldLfdNr integer
  SELECT @oldSnippedId = SnippedId, @oldLfdNr = LfdNr FROM [dbo].[ut_TableSnipped] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId = @oldSnippedId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY SnippedId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId = @oldSnippedId) AND (LfdNr < @oldLfdNr)
    ORDER BY SnippedId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01)
    WHERE (SnippedId < @oldSnippedId)
    ORDER BY SnippedId desc,LfdNr 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_TableSnipped_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_TableSnipped_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_TableSnipped_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_FindF_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_TableSnipped_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mSnippedId varchar(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,40)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,41,5)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SnippedId = @mSnippedId AND 
      LfdNr = @mLfdNr
      ORDER BY SnippedId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SnippedId = @mSnippedId
      ORDER BY SnippedId,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SnippedId >= @mSnippedId AND SUBSTRING(SnippedId,1,LEN(@mSnippedId)) = @mSnippedId
      ORDER BY SnippedId,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_TableSnipped_Key01')
  DROP PROCEDURE [dbo].[up_FindL_TableSnipped_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_TableSnipped_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mSnippedId varchar(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,40)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,41,5)

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

  IF @xLen = 45 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SnippedId = @mSnippedId AND 
      LfdNr = @mLfdNr
      ORDER BY SnippedId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SnippedId = @mSnippedId
      ORDER BY SnippedId desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_TableSnipped] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(SnippedId,1,LEN(@mSnippedId)) = @mSnippedId
      ORDER BY SnippedId desc,LfdNr desc,RecId desc
    RETURN
  END


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


