-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, 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_WartungsTermine')
BEGIN
CREATE TABLE [dbo].[ut_WartungsTermine]
(
  [RecId]  integer NOT NULL
 ,[Type] integer DEFAULT 0
 ,[RefIdent] integer DEFAULT 0
 ,[RefDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[RefLfdNr] integer DEFAULT 0
 ,[TerminId] char(8) DEFAULT ''
  CONSTRAINT [Pk_WartungsTermine]  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_WartungsTermine_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsTermine_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTermine_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsTermine_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsTermine_Keys')
  DROP TABLE [dbo].[ut_WartungsTermine_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_WartungsTermine' and sc.[name] = 'Type')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTermine] ADD [Type] 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_WartungsTermine' and sc.[name] = 'RefIdent')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTermine] ADD [RefIdent] 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_WartungsTermine' and sc.[name] = 'RefDatum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTermine] ADD [RefDatum] 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_WartungsTermine' and sc.[name] = 'RefLfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTermine] ADD [RefLfdNr] 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_WartungsTermine' and sc.[name] = 'TerminId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTermine] ADD [TerminId] char(8) NOT NULL DEFAULT ''
END
GO

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

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsTermine')
  DROP VIEW [dbo].[WartungsTermine]
GO
CREATE VIEW [dbo].[WartungsTermine] AS
  SELECT [RecId]
 ,[Type]
 ,[RefIdent]
 ,[RefDatum]
 ,[RefLfdNr]
 ,[TerminId]
  FROM [dbo].[ut_WartungsTermine]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsTermine')
  DROP PROCEDURE [dbo].[up_Sel_WartungsTermine]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsTermine]  @mRI integer
AS
  SELECT
    [Type]
   ,[RefIdent]
   ,[RefDatum]
   ,[RefLfdNr]
   ,[TerminId]
  FROM [dbo].[ut_WartungsTermine] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsTermine')
  DROP PROCEDURE [dbo].[up_Append_WartungsTermine]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsTermine]
   @Param_Type integer
  ,@Param_RefIdent integer
  ,@Param_RefDatum datetime
  ,@Param_RefLfdNr integer
  ,@Param_TerminId char(8)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsTermine()
      INSERT INTO [dbo].[ut_WartungsTermine] (RecId
        ,[Type]
        ,[RefIdent]
        ,[RefDatum]
        ,[RefLfdNr]
        ,[TerminId]
          ) VALUES (@NeueId
        ,@Param_Type
        ,@Param_RefIdent
        ,@Param_RefDatum
        ,@Param_RefLfdNr
        ,UPPER(RTRIM(@Param_TerminId))
          )
      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_WartungsTermine')
  DROP PROCEDURE [dbo].[up_Add_WartungsTermine]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsTermine]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsTermine()
  INSERT INTO [dbo].[ut_WartungsTermine] (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_WartungsTermine')
  DROP PROCEDURE [dbo].[up_Upd_WartungsTermine]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsTermine] @mRI integer
  ,@Param_Type integer
  ,@Param_RefIdent integer
  ,@Param_RefDatum datetime
  ,@Param_RefLfdNr integer
  ,@Param_TerminId char(8)
AS
  UPDATE [dbo].[ut_WartungsTermine] SET
    [Type] = @Param_Type
   ,[RefIdent] = @Param_RefIdent
   ,[RefDatum] = @Param_RefDatum
   ,[RefLfdNr] = @Param_RefLfdNr
   ,[TerminId] = UPPER(RTRIM(@Param_TerminId))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsTermine')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsTermine]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsTermine] @Param_RecId varchar(20)
  ,@Param_Type varchar(1)
  ,@Param_RefIdent varchar(8)
  ,@Param_RefDatum varchar(10)
  ,@Param_RefLfdNr varchar(8)
  ,@Param_TerminId varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsTermine] SET
    [Type] = dbo.itcfunc_integer(@Param_Type)
   ,[RefIdent] = dbo.itcfunc_integer(@Param_RefIdent)
   ,[RefDatum] = dbo.itcfunc_datetime(@Param_RefDatum)
   ,[RefLfdNr] = dbo.itcfunc_integer(@Param_RefLfdNr)
   ,[TerminId] = UPPER(RTRIM(@Param_TerminId))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsTermine] @mRI
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsTermine_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsTermine_Key00] AS EXECUTE [dbo].[up_First_WartungsTermine_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsTermine_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsTermine_Key00] AS EXECUTE [dbo].[up_Last_WartungsTermine_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsTermine_Key00] @oldRecId integer AS
  DECLARE @oldType integer
  DECLARE @oldRefIdent integer
  DECLARE @oldRefDatum datetime
  DECLARE @oldRefLfdNr integer
  SELECT @oldType = Type, @oldRefIdent = RefIdent, @oldRefDatum = RefDatum, @oldRefLfdNr = RefLfdNr FROM [dbo].[ut_WartungsTermine] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent = @oldRefIdent) AND (RefDatum = @oldRefDatum) AND (RefLfdNr > @oldRefLfdNr)
    ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent = @oldRefIdent) AND (RefDatum > @oldRefDatum)
    ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent > @oldRefIdent)
    ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type > @oldType)
    ORDER BY Type,RefIdent,RefDatum,RefLfdNr,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_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsTermine_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsTermine_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsTermine_Key00] @oldRecId integer AS
  DECLARE @oldType integer
  DECLARE @oldRefIdent integer
  DECLARE @oldRefDatum datetime
  DECLARE @oldRefLfdNr integer
  SELECT @oldType = Type, @oldRefIdent = RefIdent, @oldRefDatum = RefDatum, @oldRefLfdNr = RefLfdNr FROM [dbo].[ut_WartungsTermine] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent = @oldRefIdent) AND (RefDatum = @oldRefDatum) AND (RefLfdNr < @oldRefLfdNr)
    ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent = @oldRefIdent) AND (RefDatum < @oldRefDatum)
    ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type = @oldType) AND (RefIdent < @oldRefIdent)
    ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00)
    WHERE (Type < @oldType)
    ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr 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_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsTermine_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsTermine_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsTermine_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mType integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mRefIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mRefDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mRefLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent AND 
      RefDatum = @mRefDatum AND 
      RefLfdNr = @mRefLfdNr
      ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent AND 
      RefDatum = @mRefDatum
      ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent
      ORDER BY Type,RefIdent,RefDatum,RefLfdNr,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType
      ORDER BY Type,RefIdent,RefDatum,RefLfdNr,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_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_WartungsTermine_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_WartungsTermine_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsTermine_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsTermine_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsTermine_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mType integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mRefIdent integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,8)
  DECLARE @mRefDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,10,10)
  DECLARE @mRefLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,20,8)

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

  IF @xLen = 27 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent AND 
      RefDatum = @mRefDatum AND 
      RefLfdNr = @mRefLfdNr
      ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent AND 
      RefDatum = @mRefDatum
      ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType AND 
      RefIdent = @mRefIdent
      ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key00) WHERE 
      Type = @mType
      ORDER BY Type desc,RefIdent desc,RefDatum desc,RefLfdNr 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_WartungsTermine_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_WartungsTermine_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_WartungsTermine_Key00] @xKey, @xLen')
GO


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsTermine_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsTermine_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsTermine_Key01] @oldRecId integer AS
  DECLARE @oldTerminId char(8)
  SELECT @oldTerminId = TerminId FROM [dbo].[ut_WartungsTermine] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01)
    WHERE (TerminId > @oldTerminId)
    ORDER BY TerminId,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_WartungsTermine_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsTermine_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsTermine_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsTermine_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsTermine_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsTermine_Key01] @oldRecId integer AS
  DECLARE @oldTerminId char(8)
  SELECT @oldTerminId = TerminId FROM [dbo].[ut_WartungsTermine] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01)
    WHERE (TerminId < @oldTerminId)
    ORDER BY TerminId 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_WartungsTermine_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsTermine_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsTermine_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01) WHERE 
      TerminId = @mTerminId
      ORDER BY TerminId,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01) WHERE 
      TerminId >= @mTerminId AND SUBSTRING(TerminId,1,LEN(@mTerminId)) = @mTerminId
      ORDER BY TerminId,RecId
    RETURN
  END

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01) WHERE 
      TerminId = @mTerminId
      ORDER BY TerminId desc,RecId desc
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsTermine] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(TerminId,1,LEN(@mTerminId)) = @mTerminId
      ORDER BY TerminId desc,RecId desc
    RETURN
  END

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


