------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_ZeitKladdenKopf')
BEGIN
CREATE TABLE [dbo].[ut_ZeitKladdenKopf]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Code] char(20) DEFAULT ' '
  ,[Bez] char(50) DEFAULT ' '
  ,[Annr] char(4) DEFAULT ' '
  ,[CpyAnnr] tinyint DEFAULT 0
  ,[EnAnnr] tinyint DEFAULT 0
  ,[EnStunden] tinyint DEFAULT 0
  ,[EnBetrag] tinyint DEFAULT 0
  ,[DupBuchDat] tinyint DEFAULT 0
  ,[DupErfDat] tinyint DEFAULT 0
  ,[CpyBuchDat] tinyint DEFAULT 0
  ,[CpyErfDat] tinyint DEFAULT 0
  ,[CpyLoa] tinyint DEFAULT 0
  ,[Layout] integer DEFAULT 0
  ,[ShowAll] tinyint DEFAULT 0
  ,[CpyBelNr] tinyint DEFAULT 0
  ,[CpyKst] tinyint DEFAULT 0
  ,[CpyKtr] tinyint DEFAULT 0
  ,[CpyExtBelN] tinyint DEFAULT 0
  ,[CpyVorgang] tinyint DEFAULT 0
  ,[LogName] char(20) DEFAULT ' '
  CONSTRAINT [Pk_ZeitKladdenKopf]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ZeitKladdenKopf_KeyDel')
  DROP TRIGGER [dbo].[tr_ZeitKladdenKopf_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ZeitKladdenKopf_KeyUpd')
  DROP TRIGGER [dbo].[tr_ZeitKladdenKopf_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ZeitKladdenKopf_Keys')
  DROP TABLE [dbo].[ut_ZeitKladdenKopf_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [Code] char(20) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [Bez] char(50) 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_ZeitKladdenKopf' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [Annr] char(4) 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_ZeitKladdenKopf' and sc.[name] = 'CpyAnnr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyAnnr] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'EnAnnr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [EnAnnr] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'EnStunden')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [EnStunden] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'EnBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [EnBetrag] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'DupBuchDat')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [DupBuchDat] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'DupErfDat')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [DupErfDat] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyBuchDat')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyBuchDat] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyErfDat')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyErfDat] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyLoa')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyLoa] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'Layout')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [Layout] 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_ZeitKladdenKopf' and sc.[name] = 'ShowAll')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [ShowAll] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyBelNr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyBelNr] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyKst')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyKst] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyKtr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyKtr] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyExtBelN')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyExtBelN] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'CpyVorgang')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [CpyVorgang] tinyint 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_ZeitKladdenKopf' and sc.[name] = 'LogName')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ADD [LogName] char(20) NOT NULL DEFAULT ' '
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ALTER COLUMN [Code] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ALTER COLUMN [Bez] char(50)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ALTER COLUMN [Annr] char(4)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ZeitKladdenKopf' and sc.[name] = 'LogName')
BEGIN
  ALTER TABLE [dbo].[ut_ZeitKladdenKopf] ALTER COLUMN [LogName] char(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ZeitKladdenKopf')
  DROP VIEW [dbo].[ZeitKladdenKopf]
GO
CREATE VIEW [dbo].[ZeitKladdenKopf] AS
  SELECT RecId
    ,[Code]
    ,[Bez]
    ,[Annr]
    ,[CpyAnnr]
    ,[EnAnnr]
    ,[EnStunden]
    ,[EnBetrag]
    ,[DupBuchDat]
    ,[DupErfDat]
    ,[CpyBuchDat]
    ,[CpyErfDat]
    ,[CpyLoa]
    ,[Layout]
    ,[ShowAll]
    ,[CpyBelNr]
    ,[CpyKst]
    ,[CpyKtr]
    ,[CpyExtBelN]
    ,[CpyVorgang]
    ,[LogName]
  FROM [dbo].[ut_ZeitKladdenKopf]
GO

------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_ZeitKladdenKopf', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_ZeitKladdenKopf
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_ZeitKladdenKopf() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_ZeitKladdenKopf] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Sel_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Sel_ZeitKladdenKopf]
  @RI integer
AS
SELECT
     [Code]
    ,[Bez]
    ,[Annr]
    ,[CpyAnnr]
    ,[EnAnnr]
    ,[EnStunden]
    ,[EnBetrag]
    ,[DupBuchDat]
    ,[DupErfDat]
    ,[CpyBuchDat]
    ,[CpyErfDat]
    ,[CpyLoa]
    ,[Layout]
    ,[ShowAll]
    ,[CpyBelNr]
    ,[CpyKst]
    ,[CpyKtr]
    ,[CpyExtBelN]
    ,[CpyVorgang]
    ,[LogName]
  FROM [dbo].[ut_ZeitKladdenKopf]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Sel2_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Sel2_ZeitKladdenKopf]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[ZeitKladdenKopf]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Append_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Append_ZeitKladdenKopf]
  @Parm_Code char(20)
 ,@Parm_Bez char(50)
 ,@Parm_Annr char(4)
 ,@Parm_CpyAnnr tinyint
 ,@Parm_EnAnnr tinyint
 ,@Parm_EnStunden tinyint
 ,@Parm_EnBetrag tinyint
 ,@Parm_DupBuchDat tinyint
 ,@Parm_DupErfDat tinyint
 ,@Parm_CpyBuchDat tinyint
 ,@Parm_CpyErfDat tinyint
 ,@Parm_CpyLoa tinyint
 ,@Parm_Layout integer
 ,@Parm_ShowAll tinyint
 ,@Parm_CpyBelNr tinyint
 ,@Parm_CpyKst tinyint
 ,@Parm_CpyKtr tinyint
 ,@Parm_CpyExtBelN tinyint
 ,@Parm_CpyVorgang tinyint
 ,@Parm_LogName char(20)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_ZeitKladdenKopf()
      INSERT INTO [dbo].[ut_ZeitKladdenKopf] (RecId
       ,[Code]
       ,[Bez]
       ,[Annr]
       ,[CpyAnnr]
       ,[EnAnnr]
       ,[EnStunden]
       ,[EnBetrag]
       ,[DupBuchDat]
       ,[DupErfDat]
       ,[CpyBuchDat]
       ,[CpyErfDat]
       ,[CpyLoa]
       ,[Layout]
       ,[ShowAll]
       ,[CpyBelNr]
       ,[CpyKst]
       ,[CpyKtr]
       ,[CpyExtBelN]
       ,[CpyVorgang]
       ,[LogName]
         ) VALUES (@NeueId
       ,RTRIM(@Parm_Code)
       ,RTRIM(@Parm_Bez)
       ,RTRIM(@Parm_Annr)
       ,@Parm_CpyAnnr
       ,@Parm_EnAnnr
       ,@Parm_EnStunden
       ,@Parm_EnBetrag
       ,@Parm_DupBuchDat
       ,@Parm_DupErfDat
       ,@Parm_CpyBuchDat
       ,@Parm_CpyErfDat
       ,@Parm_CpyLoa
       ,@Parm_Layout
       ,@Parm_ShowAll
       ,@Parm_CpyBelNr
       ,@Parm_CpyKst
       ,@Parm_CpyKtr
       ,@Parm_CpyExtBelN
       ,@Parm_CpyVorgang
       ,RTRIM(@Parm_LogName)
           )
      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
  SELECT @NeueId AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd_ZeitKladdenKopf]
  @RI integer
 ,@Parm_Code char(20)
 ,@Parm_Bez char(50)
 ,@Parm_Annr char(4)
 ,@Parm_CpyAnnr tinyint
 ,@Parm_EnAnnr tinyint
 ,@Parm_EnStunden tinyint
 ,@Parm_EnBetrag tinyint
 ,@Parm_DupBuchDat tinyint
 ,@Parm_DupErfDat tinyint
 ,@Parm_CpyBuchDat tinyint
 ,@Parm_CpyErfDat tinyint
 ,@Parm_CpyLoa tinyint
 ,@Parm_Layout integer
 ,@Parm_ShowAll tinyint
 ,@Parm_CpyBelNr tinyint
 ,@Parm_CpyKst tinyint
 ,@Parm_CpyKtr tinyint
 ,@Parm_CpyExtBelN tinyint
 ,@Parm_CpyVorgang tinyint
 ,@Parm_LogName char(20)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_ZeitKladdenKopf] SET
     [Code] = RTRIM(@Parm_Code)
    ,[Bez] = RTRIM(@Parm_Bez)
    ,[Annr] = RTRIM(@Parm_Annr)
    ,[CpyAnnr] = @Parm_CpyAnnr
    ,[EnAnnr] = @Parm_EnAnnr
    ,[EnStunden] = @Parm_EnStunden
    ,[EnBetrag] = @Parm_EnBetrag
    ,[DupBuchDat] = @Parm_DupBuchDat
    ,[DupErfDat] = @Parm_DupErfDat
    ,[CpyBuchDat] = @Parm_CpyBuchDat
    ,[CpyErfDat] = @Parm_CpyErfDat
    ,[CpyLoa] = @Parm_CpyLoa
    ,[Layout] = @Parm_Layout
    ,[ShowAll] = @Parm_ShowAll
    ,[CpyBelNr] = @Parm_CpyBelNr
    ,[CpyKst] = @Parm_CpyKst
    ,[CpyKtr] = @Parm_CpyKtr
    ,[CpyExtBelN] = @Parm_CpyExtBelN
    ,[CpyVorgang] = @Parm_CpyVorgang
    ,[LogName] = RTRIM(@Parm_LogName)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd2_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd2_ZeitKladdenKopf]
  @Param_RecId varchar(20)
 ,@Param_Code varchar(20)
 ,@Param_Bez varchar(50)
 ,@Param_Annr varchar(4)
 ,@Param_CpyAnnr varchar(10)
 ,@Param_EnAnnr varchar(10)
 ,@Param_EnStunden varchar(10)
 ,@Param_EnBetrag varchar(10)
 ,@Param_DupBuchDat varchar(10)
 ,@Param_DupErfDat varchar(10)
 ,@Param_CpyBuchDat varchar(10)
 ,@Param_CpyErfDat varchar(10)
 ,@Param_CpyLoa varchar(10)
 ,@Param_Layout varchar(20)
 ,@Param_ShowAll varchar(10)
 ,@Param_CpyBelNr varchar(10)
 ,@Param_CpyKst varchar(10)
 ,@Param_CpyKtr varchar(10)
 ,@Param_CpyExtBelN varchar(10)
 ,@Param_CpyVorgang varchar(10)
 ,@Param_LogName varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_ZeitKladdenKopf] SET
     [Code] = UPPER(RTRIM(@Param_Code))
    ,[Bez] = RTRIM(@Param_Bez)
    ,[Annr] = UPPER(RTRIM(@Param_Annr))
    ,[CpyAnnr] = dbo.itcfunc_integer(@Param_CpyAnnr)
    ,[EnAnnr] = dbo.itcfunc_integer(@Param_EnAnnr)
    ,[EnStunden] = dbo.itcfunc_integer(@Param_EnStunden)
    ,[EnBetrag] = dbo.itcfunc_integer(@Param_EnBetrag)
    ,[DupBuchDat] = dbo.itcfunc_integer(@Param_DupBuchDat)
    ,[DupErfDat] = dbo.itcfunc_integer(@Param_DupErfDat)
    ,[CpyBuchDat] = dbo.itcfunc_integer(@Param_CpyBuchDat)
    ,[CpyErfDat] = dbo.itcfunc_integer(@Param_CpyErfDat)
    ,[CpyLoa] = dbo.itcfunc_integer(@Param_CpyLoa)
    ,[Layout] = dbo.itcfunc_float(@Param_Layout)
    ,[ShowAll] = dbo.itcfunc_integer(@Param_ShowAll)
    ,[CpyBelNr] = dbo.itcfunc_integer(@Param_CpyBelNr)
    ,[CpyKst] = dbo.itcfunc_integer(@Param_CpyKst)
    ,[CpyKtr] = dbo.itcfunc_integer(@Param_CpyKtr)
    ,[CpyExtBelN] = dbo.itcfunc_integer(@Param_CpyExtBelN)
    ,[CpyVorgang] = dbo.itcfunc_integer(@Param_CpyVorgang)
    ,[LogName] = UPPER(RTRIM(@Param_LogName))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_ZeitKladdenKopf] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_TrSel_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ZeitKladdenKopf]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Code]))
      +LTRIM(RTRIM([Bez]))
      +LTRIM(RTRIM([Annr]))
      +LTRIM(RTRIM([LogName]))
    AS Data
  FROM     [dbo].[ut_ZeitKladdenKopf]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Add_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Add_ZeitKladdenKopf] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_ZeitKladdenKopf()
  INSERT INTO [dbo].[ut_ZeitKladdenKopf] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Del_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Del_ZeitKladdenKopf]
  @RI integer
AS
  DELETE FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_ZeitKladdenKopf')
  DROP PROCEDURE [dbo].[up_Reorg_ZeitKladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Reorg_ZeitKladdenKopf]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_ZeitKladdenKopf] 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_ZeitKladdenKopf] 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_ZeitKladdenKopf] )
  END
GO


IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_ZeitKladdenKopf'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_ZeitKladdenKopf]
(Code,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Next50_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZeitKladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZeitKladdenKopf in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY Code,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZeitKladdenKopf in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY Code DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Code,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Code = @oldCode AND RecId > @oldRecId
                   ORDER BY Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Code > @oldCode
                   ORDER BY Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Code DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Code = @oldCode AND RecId < @oldRecId
                   ORDER BY Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Code < @oldCode
                   ORDER BY Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Code,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(Code,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Code,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Code = SUBSTRING(@xKey,1,20)
      ORDER BY Code,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Code DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(Code,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Code DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Code = SUBSTRING(@xKey,1,20)
      ORDER BY Code DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_ZeitKladdenKopf'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_ZeitKladdenKopf]
(LogName,Code,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_Next50_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZeitKladdenKopf_Key01')
  DROP PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZeitKladdenKopf in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY LogName,Code,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZeitKladdenKopf in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY LogName DESC,Code DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY LogName,Code,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldLogName char(20)
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT LogName,Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldLogName,@oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName = @oldLogName AND Code = @oldCode AND RecId > @oldRecId
                   ORDER BY LogName,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName = @oldLogName AND Code > @oldCode
                   ORDER BY LogName,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName > @oldLogName
                   ORDER BY LogName,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY LogName DESC,Code DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldLogName char(20)
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT LogName,Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldLogName,@oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName = @oldLogName AND Code = @oldCode AND RecId < @oldRecId
                   ORDER BY LogName DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName = @oldLogName AND Code < @oldCode
                   ORDER BY LogName DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE LogName < @oldLogName
                   ORDER BY LogName DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY LogName,Code,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(LogName,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY LogName,Code,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20)
      ORDER BY LogName,Code,RecId
    RETURN
  END

  IF @xLen < 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20) AND SUBSTRING(Code,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY LogName,Code,RecId
    RETURN
  END

  IF @xLen = 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20) AND Code = SUBSTRING(@xKey,21,20)
      ORDER BY LogName,Code,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY LogName DESC,Code DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(LogName,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY LogName DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20)
      ORDER BY LogName DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen < 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20) AND SUBSTRING(Code,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY LogName DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen = 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE LogName = SUBSTRING(@xKey,1,20) AND Code = SUBSTRING(@xKey,21,20)
      ORDER BY LogName DESC,Code DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_ZeitKladdenKopf'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key02')
CREATE INDEX [Ik_Key02] ON [dbo].[ut_ZeitKladdenKopf]
(Annr,Code,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_Next50_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ZeitKladdenKopf_Key02')
  DROP PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key02]
GO

------------------------------------------------------------
/* ersten Datensatz aus ZeitKladdenKopf in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_First_ZeitKladdenKopf_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY Annr,Code,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus ZeitKladdenKopf in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_Last_ZeitKladdenKopf_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf] ORDER BY Annr DESC,Code DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Annr,Code,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_ZeitKladdenKopf_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldAnnr char(4)
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Annr,Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldAnnr,@oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr = @oldAnnr AND Code = @oldCode AND RecId > @oldRecId
                   ORDER BY Annr,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr = @oldAnnr AND Code > @oldCode
                   ORDER BY Annr,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr > @oldAnnr
                   ORDER BY Annr,Code,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Annr DESC,Code DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_ZeitKladdenKopf_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldAnnr char(4)
  DECLARE @oldCode char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Annr,Code FROM [dbo].[ut_ZeitKladdenKopf] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldAnnr,@oldCode
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr = @oldAnnr AND Code = @oldCode AND RecId < @oldRecId
                   ORDER BY Annr DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr = @oldAnnr AND Code < @oldCode
                   ORDER BY Annr DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
                   WHERE Annr < @oldAnnr
                   ORDER BY Annr DESC,Code DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Annr,Code,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_ZeitKladdenKopf_Key02] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(Annr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Annr,Code,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4)
      ORDER BY Annr,Code,RecId
    RETURN
  END

  IF @xLen < 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Code,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Annr,Code,RecId
    RETURN
  END

  IF @xLen = 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4) AND Code = SUBSTRING(@xKey,5,20)
      ORDER BY Annr,Code,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Annr DESC,Code DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_ZeitKladdenKopf_Key02] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE SUBSTRING(Annr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Annr DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4)
      ORDER BY Annr DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen < 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Code,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Annr DESC,Code DESC,RecId DESC
    RETURN
  END

  IF @xLen = 24
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_ZeitKladdenKopf]
      WHERE Annr = SUBSTRING(@xKey,1,4) AND Code = SUBSTRING(@xKey,5,20)
      ORDER BY Annr DESC,Code DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

