------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebUserSession')
BEGIN
CREATE TABLE [dbo].[ut_WebUserSession]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Uid] char(30) DEFAULT ' '
  ,[SessionId] char(40) DEFAULT ' '
  ,[IPAddress] char(20) DEFAULT ' '
  ,[LoginDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[LoginTime] char(5) DEFAULT ' '
  ,[UsingDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[UsingTime] char(5) DEFAULT ' '
  CONSTRAINT [Pk_WebUserSession]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUserSession_KeyDel')
  DROP TRIGGER [dbo].[tr_WebUserSession_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUserSession_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebUserSession_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebUserSession_Keys')
  DROP TABLE [dbo].[ut_WebUserSession_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] 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_WebUserSession' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] 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_WebUserSession' and sc.[name] = 'SessionId')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [SessionId] char(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_WebUserSession' and sc.[name] = 'IPAddress')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [IPAddress] 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_WebUserSession' and sc.[name] = 'LoginDate')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [LoginDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'LoginTime')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [LoginTime] char(5) 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_WebUserSession' and sc.[name] = 'UsingDate')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [UsingDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'UsingTime')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ADD [UsingTime] char(5) 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_WebUserSession' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] 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_WebUserSession' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ALTER COLUMN [Uid] char(30)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'IPAddress')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ALTER COLUMN [IPAddress] char(20)
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'LoginTime')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ALTER COLUMN [LoginTime] char(5)
END
GO
IF 5 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserSession' and sc.[name] = 'UsingTime')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserSession] ALTER COLUMN [UsingTime] char(5)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebUserSession')
  DROP VIEW [dbo].[WebUserSession]
GO
CREATE VIEW [dbo].[WebUserSession] AS
  SELECT RecId
    ,[Knr]
    ,[Uid]
    ,[SessionId]
    ,[IPAddress]
    ,[LoginDate]
    ,[LoginTime]
    ,[UsingDate]
    ,[UsingTime]
  FROM [dbo].[ut_WebUserSession]
GO

------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_WebUserSession', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_WebUserSession
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WebUserSession() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WebUserSession] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebUserSession')
  DROP PROCEDURE [dbo].[up_Sel_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebUserSession]
  @RI integer
AS
SELECT
     [Knr]
    ,[Uid]
    ,[SessionId]
    ,[IPAddress]
    ,[LoginDate]
    ,[LoginTime]
    ,[UsingDate]
    ,[UsingTime]
  FROM [dbo].[ut_WebUserSession]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_WebUserSession')
  DROP PROCEDURE [dbo].[up_Sel2_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Sel2_WebUserSession]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[WebUserSession]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_WebUserSession')
  DROP PROCEDURE [dbo].[up_Append_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Append_WebUserSession]
  @Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_SessionId char(40)
 ,@Parm_IPAddress char(20)
 ,@Parm_LoginDate datetime
 ,@Parm_LoginTime char(5)
 ,@Parm_UsingDate datetime
 ,@Parm_UsingTime char(5)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WebUserSession()
      INSERT INTO [dbo].[ut_WebUserSession] (RecId
       ,[Knr]
       ,[Uid]
       ,[SessionId]
       ,[IPAddress]
       ,[LoginDate]
       ,[LoginTime]
       ,[UsingDate]
       ,[UsingTime]
         ) VALUES (@NeueId
       ,RTRIM(@Parm_Knr)
       ,RTRIM(@Parm_Uid)
       ,@Parm_SessionId
       ,RTRIM(@Parm_IPAddress)
       ,@Parm_LoginDate
       ,RTRIM(@Parm_LoginTime)
       ,@Parm_UsingDate
       ,RTRIM(@Parm_UsingTime)
           )
      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_WebUserSession')
  DROP PROCEDURE [dbo].[up_Upd_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebUserSession]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_SessionId char(40)
 ,@Parm_IPAddress char(20)
 ,@Parm_LoginDate datetime
 ,@Parm_LoginTime char(5)
 ,@Parm_UsingDate datetime
 ,@Parm_UsingTime char(5)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebUserSession] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Uid] = RTRIM(@Parm_Uid)
    ,[SessionId] = @Parm_SessionId
    ,[IPAddress] = RTRIM(@Parm_IPAddress)
    ,[LoginDate] = @Parm_LoginDate
    ,[LoginTime] = RTRIM(@Parm_LoginTime)
    ,[UsingDate] = @Parm_UsingDate
    ,[UsingTime] = RTRIM(@Parm_UsingTime)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WebUserSession')
  DROP PROCEDURE [dbo].[up_Upd2_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WebUserSession]
  @Param_RecId varchar(20)
 ,@Param_Knr varchar(20)
 ,@Param_Uid varchar(30)
 ,@Param_SessionId varchar(40)
 ,@Param_IPAddress varchar(20)
 ,@Param_LoginDate varchar(10)
 ,@Param_LoginTime varchar(5)
 ,@Param_UsingDate varchar(10)
 ,@Param_UsingTime varchar(5)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_WebUserSession] SET
     [Knr] = UPPER(RTRIM(@Param_Knr))
    ,[Uid] = RTRIM(@Param_Uid)
    ,[SessionId] = RTRIM(@Param_SessionId)
    ,[IPAddress] = RTRIM(@Param_IPAddress)
    ,[LoginDate] = dbo.itcfunc_datetime(@Param_LoginDate)
    ,[LoginTime] = RTRIM(@Param_LoginTime)
    ,[UsingDate] = dbo.itcfunc_datetime(@Param_UsingDate)
    ,[UsingTime] = RTRIM(@Param_UsingTime)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WebUserSession] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WebUserSession')
  DROP PROCEDURE [dbo].[up_TrSel_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebUserSession]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Uid]))
      +LTRIM(RTRIM([IPAddress]))
      +LTRIM(RTRIM([LoginTime]))
      +LTRIM(RTRIM([UsingTime]))
    AS Data
  FROM     [dbo].[ut_WebUserSession]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebUserSession')
  DROP PROCEDURE [dbo].[up_Add_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Add_WebUserSession] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WebUserSession()
  INSERT INTO [dbo].[ut_WebUserSession] (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_WebUserSession')
  DROP PROCEDURE [dbo].[up_Del_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Del_WebUserSession]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebUserSession] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebUserSession')
  DROP PROCEDURE [dbo].[up_Reorg_WebUserSession]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebUserSession]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebUserSession] 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_WebUserSession] 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_WebUserSession] )
  END
GO


IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_WebUserSession'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebUserSession]
(SessionId,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_First_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebUserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUserSession_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebUserSession_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebUserSession in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebUserSession_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY SessionId,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebUserSession in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebUserSession_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY SessionId DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SessionId,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebUserSession_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSessionId char(40)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SessionId FROM [dbo].[ut_WebUserSession] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSessionId
  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_WebUserSession]
                   WHERE SessionId = @oldSessionId AND RecId > @oldRecId
                   ORDER BY SessionId,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId > @oldSessionId
                   ORDER BY SessionId,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 SessionId DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_WebUserSession_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSessionId char(40)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SessionId FROM [dbo].[ut_WebUserSession] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSessionId
  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_WebUserSession]
                   WHERE SessionId = @oldSessionId AND RecId < @oldRecId
                   ORDER BY SessionId DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId < @oldSessionId
                   ORDER BY SessionId 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 SessionId,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_WebUserSession_Key00] @xKey char(128),@xLen integer AS

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

  IF @xLen = 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40)
      ORDER BY SessionId 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_WebUserSession'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_WebUserSession]
(Knr,Uid,IPAddress,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_First_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_Last_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_Next_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_Next50_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WebUserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUserSession_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WebUserSession_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebUserSession in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebUserSession_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY Knr,Uid,IPAddress,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebUserSession in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebUserSession_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY Knr DESC,Uid DESC,IPAddress DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Knr,Uid,IPAddress,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebUserSession_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKnr char(20)
  DECLARE @oldUid char(30)
  DECLARE @oldIPAddress char(20)

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

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

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

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

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

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

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

  IF @xLen = 70
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND IPAddress = SUBSTRING(@xKey,51,20)
      ORDER BY Knr,Uid,IPAddress,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

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

  IF @xLen = 70
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND IPAddress = SUBSTRING(@xKey,51,20)
      ORDER BY Knr DESC,Uid DESC,IPAddress 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_WebUserSession'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key02')
CREATE INDEX [Ik_Key02] ON [dbo].[ut_WebUserSession]
(SessionId,IPAddress,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_First_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_Last_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_Next_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_Next50_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WebUserSession_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUserSession_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WebUserSession_Key02]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebUserSession in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebUserSession_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY SessionId,IPAddress,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebUserSession in Folge Key02 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebUserSession_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession] ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY SessionId,IPAddress,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebUserSession_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSessionId char(40)
  DECLARE @oldIPAddress char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SessionId,IPAddress FROM [dbo].[ut_WebUserSession] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSessionId,@oldIPAddress
  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_WebUserSession]
                   WHERE SessionId = @oldSessionId AND IPAddress = @oldIPAddress AND RecId > @oldRecId
                   ORDER BY SessionId,IPAddress,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId = @oldSessionId AND IPAddress > @oldIPAddress
                   ORDER BY SessionId,IPAddress,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId > @oldSessionId
                   ORDER BY SessionId,IPAddress,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 SessionId DESC,IPAddress DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_WebUserSession_Key02] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldSessionId char(40)
  DECLARE @oldIPAddress char(20)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT SessionId,IPAddress FROM [dbo].[ut_WebUserSession] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldSessionId,@oldIPAddress
  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_WebUserSession]
                   WHERE SessionId = @oldSessionId AND IPAddress = @oldIPAddress AND RecId < @oldRecId
                   ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId = @oldSessionId AND IPAddress < @oldIPAddress
                   ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
                   WHERE SessionId < @oldSessionId
                   ORDER BY SessionId DESC,IPAddress 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 SessionId,IPAddress,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_WebUserSession_Key02] @xKey char(128),@xLen integer AS

  IF @xLen < 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SUBSTRING(SessionId,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SessionId,IPAddress,RecId
    RETURN
  END

  IF @xLen = 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40)
      ORDER BY SessionId,IPAddress,RecId
    RETURN
  END

  IF @xLen < 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40) AND SUBSTRING(IPAddress,1,@xLen-40) = SUBSTRING(@xKey,41,@xLen-40)
      ORDER BY SessionId,IPAddress,RecId
    RETURN
  END

  IF @xLen = 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40) AND IPAddress = SUBSTRING(@xKey,41,20)
      ORDER BY SessionId,IPAddress,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SUBSTRING(SessionId,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
    RETURN
  END

  IF @xLen = 40
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40)
      ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
    RETURN
  END

  IF @xLen < 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40) AND SUBSTRING(IPAddress,1,@xLen-40) = SUBSTRING(@xKey,41,@xLen-40)
      ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
    RETURN
  END

  IF @xLen = 60
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserSession]
      WHERE SessionId = SUBSTRING(@xKey,1,40) AND IPAddress = SUBSTRING(@xKey,41,20)
      ORDER BY SessionId DESC,IPAddress DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

