------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Projektstatistik')
BEGIN
CREATE TABLE [dbo].[ut_Projektstatistik]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Nummer] char(8) DEFAULT ' '
  ,[Tag] char(2) DEFAULT ' '
  ,[Pkt01] char(1) DEFAULT ' '
  ,[Monat] char(2) DEFAULT ' '
  ,[Pkt02] char(1) DEFAULT ' '
  ,[Jahr] char(4) DEFAULT ' '
  ,[Stunden] char(2) DEFAULT ' '
  ,[Pkt03] char(1) DEFAULT ' '
  ,[Minuten] char(2) DEFAULT ' '
  ,[Pkt04] char(1) DEFAULT ' '
  ,[Sekunden] char(2) DEFAULT ' '
  ,[EkMatS] float DEFAULT 0
  ,[EkLohnS] float DEFAULT 0
  ,[EkFremdS] float DEFAULT 0
  ,[VkMatS] float DEFAULT 0
  ,[VkLohnS] float DEFAULT 0
  ,[VkFremdS] float DEFAULT 0
  ,[EkMatI] float DEFAULT 0
  ,[EkLohnI] float DEFAULT 0
  ,[EkFremdI] float DEFAULT 0
  ,[VkMatI] float DEFAULT 0
  ,[VkLohnI] float DEFAULT 0
  ,[VkFremdI] float DEFAULT 0
  ,[EkZpS] float DEFAULT 0
  ,[VkZpS] float DEFAULT 0
  ,[AZeit] float DEFAULT 0
  ,[VZeit] float DEFAULT 0
  CONSTRAINT [Pk_Projektstatistik]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Projektstatistik_KeyDel')
  DROP TRIGGER [dbo].[tr_Projektstatistik_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Projektstatistik_KeyUpd')
  DROP TRIGGER [dbo].[tr_Projektstatistik_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Projektstatistik_Keys')
  DROP TABLE [dbo].[ut_Projektstatistik_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Nummer] char(8) 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_Projektstatistik' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Tag] char(2) 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_Projektstatistik' and sc.[name] = 'Pkt01')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Pkt01] char(1) 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_Projektstatistik' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Monat] char(2) 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_Projektstatistik' and sc.[name] = 'Pkt02')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Pkt02] char(1) 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_Projektstatistik' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Jahr] 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_Projektstatistik' and sc.[name] = 'Stunden')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Stunden] char(2) 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_Projektstatistik' and sc.[name] = 'Pkt03')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Pkt03] char(1) 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_Projektstatistik' and sc.[name] = 'Minuten')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Minuten] char(2) 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_Projektstatistik' and sc.[name] = 'Pkt04')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Pkt04] char(1) 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_Projektstatistik' and sc.[name] = 'Sekunden')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [Sekunden] char(2) 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_Projektstatistik' and sc.[name] = 'EkMatS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkMatS] 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_Projektstatistik' and sc.[name] = 'EkLohnS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkLohnS] 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_Projektstatistik' and sc.[name] = 'EkFremdS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkFremdS] 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_Projektstatistik' and sc.[name] = 'VkMatS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkMatS] 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_Projektstatistik' and sc.[name] = 'VkLohnS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkLohnS] 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_Projektstatistik' and sc.[name] = 'VkFremdS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkFremdS] 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_Projektstatistik' and sc.[name] = 'EkMatI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkMatI] 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_Projektstatistik' and sc.[name] = 'EkLohnI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkLohnI] 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_Projektstatistik' and sc.[name] = 'EkFremdI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkFremdI] 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_Projektstatistik' and sc.[name] = 'VkMatI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkMatI] 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_Projektstatistik' and sc.[name] = 'VkLohnI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkLohnI] 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_Projektstatistik' and sc.[name] = 'VkFremdI')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkFremdI] 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_Projektstatistik' and sc.[name] = 'EkZpS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [EkZpS] 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_Projektstatistik' and sc.[name] = 'VkZpS')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VkZpS] 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_Projektstatistik' and sc.[name] = 'AZeit')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [AZeit] 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_Projektstatistik' and sc.[name] = 'VZeit')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ADD [VZeit] float NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Nummer')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Nummer] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Tag] char(2)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Pkt01')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Pkt01] char(1)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Monat] char(2)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Pkt02')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Pkt02] char(1)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Jahr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Stunden')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Stunden] char(2)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Pkt03')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Pkt03] char(1)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Minuten')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Minuten] char(2)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Pkt04')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Pkt04] char(1)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Projektstatistik' and sc.[name] = 'Sekunden')
BEGIN
  ALTER TABLE [dbo].[ut_Projektstatistik] ALTER COLUMN [Sekunden] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Projektstatistik')
  DROP VIEW [dbo].[Projektstatistik]
GO
CREATE VIEW [dbo].[Projektstatistik] AS
  SELECT * FROM [dbo].[ut_Projektstatistik]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Add_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Add_Projektstatistik] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Projektstatistik])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Projektstatistik] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Upd_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Upd_Projektstatistik]
  @RI integer
 ,@Parm_Nummer char(8)
 ,@Parm_Tag char(2)
 ,@Parm_Pkt01 char(1)
 ,@Parm_Monat char(2)
 ,@Parm_Pkt02 char(1)
 ,@Parm_Jahr char(4)
 ,@Parm_Stunden char(2)
 ,@Parm_Pkt03 char(1)
 ,@Parm_Minuten char(2)
 ,@Parm_Pkt04 char(1)
 ,@Parm_Sekunden char(2)
 ,@Parm_EkMatS float
 ,@Parm_EkLohnS float
 ,@Parm_EkFremdS float
 ,@Parm_VkMatS float
 ,@Parm_VkLohnS float
 ,@Parm_VkFremdS float
 ,@Parm_EkMatI float
 ,@Parm_EkLohnI float
 ,@Parm_EkFremdI float
 ,@Parm_VkMatI float
 ,@Parm_VkLohnI float
 ,@Parm_VkFremdI float
 ,@Parm_EkZpS float
 ,@Parm_VkZpS float
 ,@Parm_AZeit float
 ,@Parm_VZeit float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Projektstatistik] SET
     [Nummer] = RTRIM(@Parm_Nummer)
    ,[Tag] = RTRIM(@Parm_Tag)
    ,[Pkt01] = RTRIM(@Parm_Pkt01)
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[Pkt02] = RTRIM(@Parm_Pkt02)
    ,[Jahr] = RTRIM(@Parm_Jahr)
    ,[Stunden] = RTRIM(@Parm_Stunden)
    ,[Pkt03] = RTRIM(@Parm_Pkt03)
    ,[Minuten] = RTRIM(@Parm_Minuten)
    ,[Pkt04] = RTRIM(@Parm_Pkt04)
    ,[Sekunden] = RTRIM(@Parm_Sekunden)
    ,[EkMatS] = @Parm_EkMatS
    ,[EkLohnS] = @Parm_EkLohnS
    ,[EkFremdS] = @Parm_EkFremdS
    ,[VkMatS] = @Parm_VkMatS
    ,[VkLohnS] = @Parm_VkLohnS
    ,[VkFremdS] = @Parm_VkFremdS
    ,[EkMatI] = @Parm_EkMatI
    ,[EkLohnI] = @Parm_EkLohnI
    ,[EkFremdI] = @Parm_EkFremdI
    ,[VkMatI] = @Parm_VkMatI
    ,[VkLohnI] = @Parm_VkLohnI
    ,[VkFremdI] = @Parm_VkFremdI
    ,[EkZpS] = @Parm_EkZpS
    ,[VkZpS] = @Parm_VkZpS
    ,[AZeit] = @Parm_AZeit
    ,[VZeit] = @Parm_VZeit
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Sel_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Sel_Projektstatistik]
  @RI integer
AS
SELECT
     [Nummer]
    ,[Tag]
    ,[Pkt01]
    ,[Monat]
    ,[Pkt02]
    ,[Jahr]
    ,[Stunden]
    ,[Pkt03]
    ,[Minuten]
    ,[Pkt04]
    ,[Sekunden]
    ,[EkMatS]
    ,[EkLohnS]
    ,[EkFremdS]
    ,[VkMatS]
    ,[VkLohnS]
    ,[VkFremdS]
    ,[EkMatI]
    ,[EkLohnI]
    ,[EkFremdI]
    ,[VkMatI]
    ,[VkLohnI]
    ,[VkFremdI]
    ,[EkZpS]
    ,[VkZpS]
    ,[AZeit]
    ,[VZeit]
  FROM [dbo].[ut_Projektstatistik]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Del_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Del_Projektstatistik]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Projektstatistik] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Reorg_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Projektstatistik]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Projektstatistik] 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_Projektstatistik] 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_Projektstatistik] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Projektstatistik')
  DROP PROCEDURE [dbo].[up_Append_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_Append_Projektstatistik]
  @Parm_Nummer char(8)
 ,@Parm_Tag char(2)
 ,@Parm_Pkt01 char(1)
 ,@Parm_Monat char(2)
 ,@Parm_Pkt02 char(1)
 ,@Parm_Jahr char(4)
 ,@Parm_Stunden char(2)
 ,@Parm_Pkt03 char(1)
 ,@Parm_Minuten char(2)
 ,@Parm_Pkt04 char(1)
 ,@Parm_Sekunden char(2)
 ,@Parm_EkMatS float
 ,@Parm_EkLohnS float
 ,@Parm_EkFremdS float
 ,@Parm_VkMatS float
 ,@Parm_VkLohnS float
 ,@Parm_VkFremdS float
 ,@Parm_EkMatI float
 ,@Parm_EkLohnI float
 ,@Parm_EkFremdI float
 ,@Parm_VkMatI float
 ,@Parm_VkLohnI float
 ,@Parm_VkFremdI float
 ,@Parm_EkZpS float
 ,@Parm_VkZpS float
 ,@Parm_AZeit float
 ,@Parm_VZeit float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Projektstatistik])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Projektstatistik] (
    RecId
   ,[Nummer]
   ,[Tag]
   ,[Pkt01]
   ,[Monat]
   ,[Pkt02]
   ,[Jahr]
   ,[Stunden]
   ,[Pkt03]
   ,[Minuten]
   ,[Pkt04]
   ,[Sekunden]
   ,[EkMatS]
   ,[EkLohnS]
   ,[EkFremdS]
   ,[VkMatS]
   ,[VkLohnS]
   ,[VkFremdS]
   ,[EkMatI]
   ,[EkLohnI]
   ,[EkFremdI]
   ,[VkMatI]
   ,[VkLohnI]
   ,[VkFremdI]
   ,[EkZpS]
   ,[VkZpS]
   ,[AZeit]
   ,[VZeit]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Nummer)
   ,RTRIM(@Parm_Tag)
   ,RTRIM(@Parm_Pkt01)
   ,RTRIM(@Parm_Monat)
   ,RTRIM(@Parm_Pkt02)
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_Stunden)
   ,RTRIM(@Parm_Pkt03)
   ,RTRIM(@Parm_Minuten)
   ,RTRIM(@Parm_Pkt04)
   ,RTRIM(@Parm_Sekunden)
   ,@Parm_EkMatS
   ,@Parm_EkLohnS
   ,@Parm_EkFremdS
   ,@Parm_VkMatS
   ,@Parm_VkLohnS
   ,@Parm_VkFremdS
   ,@Parm_EkMatI
   ,@Parm_EkLohnI
   ,@Parm_EkFremdI
   ,@Parm_VkMatI
   ,@Parm_VkLohnI
   ,@Parm_VkFremdI
   ,@Parm_EkZpS
   ,@Parm_VkZpS
   ,@Parm_AZeit
   ,@Parm_VZeit
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Projektstatistik')
  DROP PROCEDURE [dbo].[up_TrSel_Projektstatistik]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Projektstatistik]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nummer]))
      +LTRIM(RTRIM([Tag]))
      +LTRIM(RTRIM([Pkt01]))
      +LTRIM(RTRIM([Monat]))
      +LTRIM(RTRIM([Pkt02]))
      +LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([Stunden]))
      +LTRIM(RTRIM([Pkt03]))
      +LTRIM(RTRIM([Minuten]))
      +LTRIM(RTRIM([Pkt04]))
      +LTRIM(RTRIM([Sekunden]))
    AS Data
  FROM     [dbo].[ut_Projektstatistik]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_Projektstatistik'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_Projektstatistik]
(Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_First_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_Last_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_Next_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Projektstatistik_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Projektstatistik_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Projektstatistik_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus Projektstatistik in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_Projektstatistik_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Projektstatistik in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Projektstatistik_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Projektstatistik_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNummer char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldStunden char(2)
  DECLARE @oldMinuten char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nummer,Jahr,Monat,Tag,Stunden,Minuten FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNummer,@oldJahr,@oldMonat,@oldTag,@oldStunden,@oldMinuten
  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_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND RecId > @oldRecId
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten > @oldMinuten
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden > @oldStunden
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag > @oldTag
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr > @oldJahr
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer > @oldNummer
                   ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,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 Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Projektstatistik_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldNummer char(8)
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldStunden char(2)
  DECLARE @oldMinuten char(2)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Nummer,Jahr,Monat,Tag,Stunden,Minuten FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldNummer,@oldJahr,@oldMonat,@oldTag,@oldStunden,@oldMinuten
  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_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND RecId < @oldRecId
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten < @oldMinuten
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden < @oldStunden
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat = @oldMonat AND Tag < @oldTag
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer = @oldNummer AND Jahr < @oldJahr
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Nummer < @oldNummer
                   ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten 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 Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Projektstatistik_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Nummer,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND SUBSTRING(Jahr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND SUBSTRING(Monat,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND SUBSTRING(Tag,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND SUBSTRING(Stunden,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2) AND SUBSTRING(Minuten,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2) AND Minuten = SUBSTRING(@xKey,19,2)
      ORDER BY Nummer,Jahr,Monat,Tag,Stunden,Minuten,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Projektstatistik_Key0] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Nummer,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND SUBSTRING(Jahr,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen < 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND SUBSTRING(Monat,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 14
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND SUBSTRING(Tag,1,@xLen-14) = SUBSTRING(@xKey,15,@xLen-14)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND SUBSTRING(Stunden,1,@xLen-16) = SUBSTRING(@xKey,17,@xLen-16)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2) AND SUBSTRING(Minuten,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Nummer = SUBSTRING(@xKey,1,8) AND Jahr = SUBSTRING(@xKey,9,4) AND Monat = SUBSTRING(@xKey,13,2) AND Tag = SUBSTRING(@xKey,15,2) AND Stunden = SUBSTRING(@xKey,17,2) AND Minuten = SUBSTRING(@xKey,19,2)
      ORDER BY Nummer DESC,Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten 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_Projektstatistik'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_Projektstatistik]
(Jahr,Monat,Tag,Nummer,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_First_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_Last_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_Next_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Projektstatistik_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Projektstatistik_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Projektstatistik_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus Projektstatistik in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_Projektstatistik_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Jahr,Monat,Tag,Nummer,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Projektstatistik in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Projektstatistik_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Jahr,Monat,Tag,Nummer,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Projektstatistik_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldNummer char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Tag,Nummer FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldTag,@oldNummer
  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_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Nummer = @oldNummer AND RecId > @oldRecId
                   ORDER BY Jahr,Monat,Tag,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Nummer > @oldNummer
                   ORDER BY Jahr,Monat,Tag,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag > @oldTag
                   ORDER BY Jahr,Monat,Tag,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY Jahr,Monat,Tag,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr > @oldJahr
                   ORDER BY Jahr,Monat,Tag,Nummer,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 Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Projektstatistik_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldNummer char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Tag,Nummer FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldTag,@oldNummer
  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_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Nummer = @oldNummer AND RecId < @oldRecId
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Nummer < @oldNummer
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag < @oldTag
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr < @oldJahr
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer 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 Jahr,Monat,Tag,Nummer,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Projektstatistik_Key1] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND SUBSTRING(Tag,1,@xLen-6) = SUBSTRING(@xKey,7,@xLen-6)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND SUBSTRING(Nummer,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Nummer = SUBSTRING(@xKey,9,8)
      ORDER BY Jahr,Monat,Tag,Nummer,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND SUBSTRING(Tag,1,@xLen-6) = SUBSTRING(@xKey,7,@xLen-6)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND SUBSTRING(Nummer,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 16
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Nummer = SUBSTRING(@xKey,9,8)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Nummer 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_Projektstatistik'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key2')
CREATE INDEX [Ik_Key2] ON [dbo].[ut_Projektstatistik]
(Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_First_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_Last_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_Prior_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_Next_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_Next50_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_FindL_Projektstatistik_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Projektstatistik_Key2')
  DROP PROCEDURE [dbo].[up_FindF_Projektstatistik_Key2]
GO

------------------------------------------------------------
/* ersten Datensatz aus Projektstatistik in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_First_Projektstatistik_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Projektstatistik in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Projektstatistik_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik] ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Projektstatistik_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldStunden char(2)
  DECLARE @oldMinuten char(2)
  DECLARE @oldNummer char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Tag,Stunden,Minuten,Nummer FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldTag,@oldStunden,@oldMinuten,@oldNummer
  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_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND Nummer = @oldNummer AND RecId > @oldRecId
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND Nummer > @oldNummer
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten > @oldMinuten
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden > @oldStunden
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag > @oldTag
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr > @oldJahr
                   ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,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 Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Projektstatistik_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldTag char(2)
  DECLARE @oldStunden char(2)
  DECLARE @oldMinuten char(2)
  DECLARE @oldNummer char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Tag,Stunden,Minuten,Nummer FROM [dbo].[ut_Projektstatistik] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldTag,@oldStunden,@oldMinuten,@oldNummer
  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_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND Nummer = @oldNummer AND RecId < @oldRecId
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten = @oldMinuten AND Nummer < @oldNummer
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden = @oldStunden AND Minuten < @oldMinuten
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag = @oldTag AND Stunden < @oldStunden
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Tag < @oldTag
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
                   WHERE Jahr < @oldJahr
                   ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer 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 Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Projektstatistik_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND SUBSTRING(Tag,1,@xLen-6) = SUBSTRING(@xKey,7,@xLen-6)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND SUBSTRING(Stunden,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND SUBSTRING(Minuten,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2) AND SUBSTRING(Nummer,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2) AND Nummer = SUBSTRING(@xKey,13,8)
      ORDER BY Jahr,Monat,Tag,Stunden,Minuten,Nummer,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_Projektstatistik_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND SUBSTRING(Tag,1,@xLen-6) = SUBSTRING(@xKey,7,@xLen-6)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND SUBSTRING(Stunden,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND SUBSTRING(Minuten,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 12
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2) AND SUBSTRING(Nummer,1,@xLen-12) = SUBSTRING(@xKey,13,@xLen-12)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Projektstatistik]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Tag = SUBSTRING(@xKey,7,2) AND Stunden = SUBSTRING(@xKey,9,2) AND Minuten = SUBSTRING(@xKey,11,2) AND Nummer = SUBSTRING(@xKey,13,8)
      ORDER BY Jahr DESC,Monat DESC,Tag DESC,Stunden DESC,Minuten DESC,Nummer DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

