-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, 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_UStSchl')
BEGIN
CREATE TABLE [dbo].[ut_UStSchl]
(
  [RecId]  integer NOT NULL
 ,[SchlDatev] char(10) DEFAULT ''
 ,[SchlKako] char(3) DEFAULT ''
 ,[Art] integer DEFAULT 0
 ,[Satz] float DEFAULT 0
 ,[Descr] char(50) DEFAULT ''
  CONSTRAINT [Pk_UStSchl]  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_UStSchl_KeyDel')
  DROP TRIGGER [dbo].[tr_UStSchl_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UStSchl_KeyUpd')
  DROP TRIGGER [dbo].[tr_UStSchl_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_UStSchl_Keys')
  DROP TABLE [dbo].[ut_UStSchl_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_UStSchl' and sc.[name] = 'SchlDatev')
BEGIN
  ALTER TABLE [dbo].[ut_UStSchl] ADD [SchlDatev] 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_UStSchl' and sc.[name] = 'SchlKako')
BEGIN
  ALTER TABLE [dbo].[ut_UStSchl] ADD [SchlKako] char(3) 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_UStSchl' and sc.[name] = 'Art')
BEGIN
  ALTER TABLE [dbo].[ut_UStSchl] ADD [Art] 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_UStSchl' and sc.[name] = 'Satz')
BEGIN
  ALTER TABLE [dbo].[ut_UStSchl] ADD [Satz] float 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_UStSchl' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_UStSchl] ADD [Descr] char(50) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_UStSchl' and sc.[name] = 'SchlDatev')
BEGIN
   ALTER TABLE [dbo].[ut_UStSchl] ALTER COLUMN [SchlDatev] char(10)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_UStSchl' and sc.[name] = 'SchlKako')
BEGIN
   ALTER TABLE [dbo].[ut_UStSchl] ALTER COLUMN [SchlKako] char(3)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_UStSchl' and sc.[name] = 'Descr')
BEGIN
   ALTER TABLE [dbo].[ut_UStSchl] ALTER COLUMN [Descr] char(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'UStSchl')
  DROP VIEW [dbo].[UStSchl]
GO
CREATE VIEW [dbo].[UStSchl] AS
  SELECT [RecId]
 ,[SchlDatev]
 ,[SchlKako]
 ,[Art]
 ,[Satz]
 ,[Descr]
  FROM [dbo].[ut_UStSchl]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_UStSchl')
  DROP PROCEDURE [dbo].[up_Sel_UStSchl]
GO
CREATE PROCEDURE [dbo].[up_Sel_UStSchl]  @mRI integer
AS
  SELECT
    [SchlDatev]
   ,[SchlKako]
   ,[Art]
   ,[Satz]
   ,[Descr]
  FROM [dbo].[ut_UStSchl] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_UStSchl')
  DROP PROCEDURE [dbo].[up_Append_UStSchl]
GO
CREATE PROCEDURE [dbo].[up_Append_UStSchl]
   @Param_SchlDatev char(10)
  ,@Param_SchlKako char(3)
  ,@Param_Art integer
  ,@Param_Satz float
  ,@Param_Descr char(50)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_UStSchl()
      INSERT INTO [dbo].[ut_UStSchl] (RecId
        ,[SchlDatev]
        ,[SchlKako]
        ,[Art]
        ,[Satz]
        ,[Descr]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_SchlDatev))
        ,UPPER(RTRIM(@Param_SchlKako))
        ,@Param_Art
        ,@Param_Satz
        ,RTRIM(@Param_Descr)
          )
      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_UStSchl')
  DROP PROCEDURE [dbo].[up_Add_UStSchl]
GO
CREATE PROCEDURE [dbo].[up_Add_UStSchl]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_UStSchl()
  INSERT INTO [dbo].[ut_UStSchl] (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_UStSchl')
  DROP PROCEDURE [dbo].[up_Upd_UStSchl]
GO
CREATE PROCEDURE [dbo].[up_Upd_UStSchl] @mRI integer
  ,@Param_SchlDatev char(10)
  ,@Param_SchlKako char(3)
  ,@Param_Art integer
  ,@Param_Satz float
  ,@Param_Descr char(50)
AS
  UPDATE [dbo].[ut_UStSchl] SET
    [SchlDatev] = UPPER(RTRIM(@Param_SchlDatev))
   ,[SchlKako] = UPPER(RTRIM(@Param_SchlKako))
   ,[Art] = @Param_Art
   ,[Satz] = @Param_Satz
   ,[Descr] = RTRIM(@Param_Descr)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_UStSchl')
  DROP PROCEDURE [dbo].[up_Upd2_UStSchl]
GO
CREATE PROCEDURE [dbo].[up_Upd2_UStSchl] @Param_RecId varchar(20)
  ,@Param_SchlDatev varchar(10)
  ,@Param_SchlKako varchar(3)
  ,@Param_Art varchar(1)
  ,@Param_Satz varchar(7)
  ,@Param_Descr varchar(50)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_UStSchl] SET
    [SchlDatev] = UPPER(RTRIM(@Param_SchlDatev))
   ,[SchlKako] = UPPER(RTRIM(@Param_SchlKako))
   ,[Art] = dbo.itcfunc_integer(@Param_Art)
   ,[Satz] = dbo.itcfunc_float(@Param_Satz)
   ,[Descr] = RTRIM(@Param_Descr)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_UStSchl] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UStSchl_Key00')
  DROP PROCEDURE [dbo].[up_Next_UStSchl_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_UStSchl_Key00] @oldRecId integer AS
  DECLARE @oldSchlKako char(3)
  SELECT @oldSchlKako = SchlKako FROM [dbo].[ut_UStSchl] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00)
    WHERE (SchlKako > @oldSchlKako)
    ORDER BY SchlKako,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_UStSchl_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_UStSchl_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_UStSchl_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UStSchl_Key00')
  DROP PROCEDURE [dbo].[up_Prior_UStSchl_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_UStSchl_Key00] @oldRecId integer AS
  DECLARE @oldSchlKako char(3)
  SELECT @oldSchlKako = SchlKako FROM [dbo].[ut_UStSchl] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00)
    WHERE (SchlKako < @oldSchlKako)
    ORDER BY SchlKako 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_UStSchl_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_UStSchl_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_UStSchl_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00) WHERE 
      SchlKako = @mSchlKako
      ORDER BY SchlKako,RecId
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00) WHERE 
      SchlKako >= @mSchlKako AND SUBSTRING(SchlKako,1,LEN(@mSchlKako)) = @mSchlKako
      ORDER BY SchlKako,RecId
    RETURN
  END

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

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

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

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00) WHERE 
      SchlKako = @mSchlKako
      ORDER BY SchlKako desc,RecId desc
    RETURN
  END

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

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(SchlKako,1,LEN(@mSchlKako)) = @mSchlKako
      ORDER BY SchlKako desc,RecId desc
    RETURN
  END

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


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UStSchl_Key01')
  DROP PROCEDURE [dbo].[up_Next_UStSchl_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_UStSchl_Key01] @oldRecId integer AS
  DECLARE @oldSchlDatev char(10)
  SELECT @oldSchlDatev = SchlDatev FROM [dbo].[ut_UStSchl] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01)
    WHERE (SchlDatev > @oldSchlDatev)
    ORDER BY SchlDatev,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_UStSchl_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_UStSchl_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_UStSchl_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UStSchl_Key01')
  DROP PROCEDURE [dbo].[up_Prior_UStSchl_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_UStSchl_Key01] @oldRecId integer AS
  DECLARE @oldSchlDatev char(10)
  SELECT @oldSchlDatev = SchlDatev FROM [dbo].[ut_UStSchl] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01)
    WHERE (SchlDatev < @oldSchlDatev)
    ORDER BY SchlDatev 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_UStSchl_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_UStSchl_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_UStSchl_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01) WHERE 
      SchlDatev = @mSchlDatev
      ORDER BY SchlDatev,RecId
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01) WHERE 
      SchlDatev >= @mSchlDatev AND SUBSTRING(SchlDatev,1,LEN(@mSchlDatev)) = @mSchlDatev
      ORDER BY SchlDatev,RecId
    RETURN
  END

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01) WHERE 
      SchlDatev = @mSchlDatev
      ORDER BY SchlDatev desc,RecId desc
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStSchl] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(SchlDatev,1,LEN(@mSchlDatev)) = @mSchlDatev
      ORDER BY SchlDatev desc,RecId desc
    RETURN
  END

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


