------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebLog')
BEGIN
CREATE TABLE [dbo].[ut_WebLog]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Uid] char(30) DEFAULT ' '
  ,[Datum] char(10) DEFAULT ' '
  ,[Uhrzeit] char(8) DEFAULT ' '
  ,[Aktion] varchar(50) DEFAULT ' '
  ,[SessionId] varchar(40) DEFAULT ' '
  ,[IPAddress] varchar(20) DEFAULT ' '
  ,[Success] tinyint DEFAULT 0
  CONSTRAINT [Pk_WebLog]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebLog_KeyDel')
  DROP TRIGGER [dbo].[tr_WebLog_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebLog_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebLog_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebLog_Keys')
  DROP TABLE [dbo].[ut_WebLog_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Knr] 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_WebLog' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Uid] char(30) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Datum] char(10) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Uhrzeit] 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_WebLog' and sc.[name] = 'Aktion')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Aktion] varchar(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_WebLog' and sc.[name] = 'SessionId')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [SessionId] varchar(40) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'IPAddress')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [IPAddress] varchar(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_WebLog' and sc.[name] = 'Success')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ADD [Success] tinyint NOT NULL DEFAULT 0
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [Knr] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [Uid] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [Datum] char(10)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Uhrzeit')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [Uhrzeit] char(8)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'Aktion')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [Aktion] varchar(50)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'SessionId')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [SessionId] varchar(40)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebLog' and sc.[name] = 'IPAddress')
BEGIN
  ALTER TABLE [dbo].[ut_WebLog] ALTER COLUMN [IPAddress] varchar(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebLog')
  DROP VIEW [dbo].[WebLog]
GO
CREATE VIEW [dbo].[WebLog] AS
  SELECT RecId
    ,[Knr]
    ,[Uid]
    ,[Datum]
    ,[Uhrzeit]
    ,[Aktion]
    ,[SessionId]
    ,[IPAddress]
    ,[Success]
  FROM [dbo].[ut_WebLog]
GO

------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_WebLog', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_WebLog
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WebLog() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WebLog] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebLog')
  DROP PROCEDURE [dbo].[up_Sel_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebLog]
  @RI integer
AS
SELECT
     [Knr]
    ,[Uid]
    ,[Datum]
    ,[Uhrzeit]
    ,[Aktion]
    ,[SessionId]
    ,[IPAddress]
    ,[Success]
  FROM [dbo].[ut_WebLog]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_WebLog')
  DROP PROCEDURE [dbo].[up_Sel2_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Sel2_WebLog]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[WebLog]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebLog')
  DROP PROCEDURE [dbo].[up_Append_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Append_WebLog]
  @Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_Datum char(10)
 ,@Parm_Uhrzeit char(8)
 ,@Parm_Aktion varchar(50)
 ,@Parm_SessionId varchar(40)
 ,@Parm_IPAddress varchar(20)
 ,@Parm_Success tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    SET @NeueId = dbo.itcfunc_NextRecId_WebLog()
    INSERT INTO [dbo].[ut_WebLog] /*WITH (SERIALIZABLE)*/ (
      RecId
     ,[Knr]
     ,[Uid]
     ,[Datum]
     ,[Uhrzeit]
     ,[Aktion]
     ,[SessionId]
     ,[IPAddress]
     ,[Success]
       ) VALUES (
      @NeueId
     ,RTRIM(@Parm_Knr)
     ,RTRIM(@Parm_Uid)
     ,RTRIM(@Parm_Datum)
     ,RTRIM(@Parm_Uhrzeit)
     ,RTRIM(@Parm_Aktion)
     ,RTRIM(@Parm_SessionId)
     ,RTRIM(@Parm_IPAddress)
     ,@Parm_Success
         )
    IF @@ERROR = 0
    BEGIN
      COMMIT TRANSACTION
      SELECT @NeueId AS RecId
      RETURN
    END
    ROLLBACK TRANSACTION
    SET @m_CountDown = @m_CountDown - 1
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_WebLog')
  DROP PROCEDURE [dbo].[up_Upd_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebLog]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_Datum char(10)
 ,@Parm_Uhrzeit char(8)
 ,@Parm_Aktion varchar(50)
 ,@Parm_SessionId varchar(40)
 ,@Parm_IPAddress varchar(20)
 ,@Parm_Success tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebLog] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Uid] = RTRIM(@Parm_Uid)
    ,[Datum] = RTRIM(@Parm_Datum)
    ,[Uhrzeit] = RTRIM(@Parm_Uhrzeit)
    ,[Aktion] = RTRIM(@Parm_Aktion)
    ,[SessionId] = RTRIM(@Parm_SessionId)
    ,[IPAddress] = RTRIM(@Parm_IPAddress)
    ,[Success] = @Parm_Success
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WebLog')
  DROP PROCEDURE [dbo].[up_Upd2_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WebLog]
  @Param_RecId varchar(20)
 ,@Param_Knr varchar(20)
 ,@Param_Uid varchar(30)
 ,@Param_Datum varchar(10)
 ,@Param_Uhrzeit varchar(8)
 ,@Param_Aktion varchar(50)
 ,@Param_SessionId varchar(40)
 ,@Param_IPAddress varchar(20)
 ,@Param_Success varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_WebLog] SET
     [Knr] = UPPER(RTRIM(@Param_Knr))
    ,[Uid] = RTRIM(@Param_Uid)
    ,[Datum] = RTRIM(@Param_Datum)
    ,[Uhrzeit] = RTRIM(@Param_Uhrzeit)
    ,[Aktion] = RTRIM(@Param_Aktion)
    ,[SessionId] = RTRIM(@Param_SessionId)
    ,[IPAddress] = RTRIM(@Param_IPAddress)
    ,[Success] = dbo.itcfunc_integer(@Param_Success)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WebLog] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WebLog')
  DROP PROCEDURE [dbo].[up_TrSel_WebLog]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebLog]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Uid]))
      +LTRIM(RTRIM([Datum]))
      +LTRIM(RTRIM([Uhrzeit]))
      +LTRIM(RTRIM([Aktion]))
      +LTRIM(RTRIM([SessionId]))
      +LTRIM(RTRIM([IPAddress]))
    AS Data
  FROM     [dbo].[ut_WebLog]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebLog')
  DROP PROCEDURE [dbo].[up_Add_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Add_WebLog] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WebLog()
  INSERT INTO [dbo].[ut_WebLog] (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_WebLog')
  DROP PROCEDURE [dbo].[up_Del_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Del_WebLog]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebLog] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebLog')
  DROP PROCEDURE [dbo].[up_Reorg_WebLog]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebLog]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebLog] 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_WebLog] 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_WebLog] )
  END
GO


IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_WebLog'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebLog]
(Knr,Uid,Datum,Uhrzeit,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_First_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebLog_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebLog_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebLog_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebLog in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebLog_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebLog] ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebLog in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebLog_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebLog] ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Knr,Uid,Datum,Uhrzeit,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebLog_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKnr char(20)
  DECLARE @oldUid char(30)
  DECLARE @oldDatum char(10)
  DECLARE @oldUhrzeit char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Knr,Uid,Datum,Uhrzeit FROM [dbo].[ut_WebLog] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKnr,@oldUid,@oldDatum,@oldUhrzeit
  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_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum = @oldDatum AND Uhrzeit = @oldUhrzeit AND RecId > @oldRecId
                   ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum = @oldDatum AND Uhrzeit > @oldUhrzeit
                   ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum > @oldDatum
                   ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid > @oldUid
                   ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr > @oldKnr
                   ORDER BY Knr,Uid,Datum,Uhrzeit,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 Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_WebLog_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKnr char(20)
  DECLARE @oldUid char(30)
  DECLARE @oldDatum char(10)
  DECLARE @oldUhrzeit char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Knr,Uid,Datum,Uhrzeit FROM [dbo].[ut_WebLog] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldKnr,@oldUid,@oldDatum,@oldUhrzeit
  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_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum = @oldDatum AND Uhrzeit = @oldUhrzeit AND RecId < @oldRecId
                   ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum = @oldDatum AND Uhrzeit < @oldUhrzeit
                   ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid = @oldUid AND Datum < @oldDatum
                   ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr = @oldKnr AND Uid < @oldUid
                   ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
                   WHERE Knr < @oldKnr
                   ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit 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 Knr,Uid,Datum,Uhrzeit,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_WebLog_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE SUBSTRING(Knr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen < 50
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND SUBSTRING(Uid,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen = 50
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen < 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND SUBSTRING(Datum,1,@xLen-50) = SUBSTRING(@xKey,51,@xLen-50)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen = 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen < 68
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10) AND SUBSTRING(Uhrzeit,1,@xLen-60) = SUBSTRING(@xKey,61,@xLen-60)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  IF @xLen = 68
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10) AND Uhrzeit = SUBSTRING(@xKey,61,8)
      ORDER BY Knr,Uid,Datum,Uhrzeit,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE SUBSTRING(Knr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen = 20
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen < 50
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND SUBSTRING(Uid,1,@xLen-20) = SUBSTRING(@xKey,21,@xLen-20)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen = 50
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen < 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND SUBSTRING(Datum,1,@xLen-50) = SUBSTRING(@xKey,51,@xLen-50)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen = 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen < 68
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10) AND SUBSTRING(Uhrzeit,1,@xLen-60) = SUBSTRING(@xKey,61,@xLen-60)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  IF @xLen = 68
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebLog]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Datum = SUBSTRING(@xKey,51,10) AND Uhrzeit = SUBSTRING(@xKey,61,8)
      ORDER BY Knr DESC,Uid DESC,Datum DESC,Uhrzeit DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

