------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_UserSession')
BEGIN
CREATE TABLE [dbo].[ut_UserSession]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[ProductId] char(23) DEFAULT ' '
  ,[Activity] char(19) DEFAULT ' '
  CONSTRAINT [Pk_UserSession]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UserSession_KeyDel')
  DROP TRIGGER [dbo].[tr_UserSession_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UserSession_KeyUpd')
  DROP TRIGGER [dbo].[tr_UserSession_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_UserSession_Keys')
  DROP TABLE [dbo].[ut_UserSession_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UserSession' and sc.[name] = 'ProductId')
BEGIN
  ALTER TABLE [dbo].[ut_UserSession] ADD [ProductId] char(23) 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_UserSession' and sc.[name] = 'Activity')
BEGIN
  ALTER TABLE [dbo].[ut_UserSession] ADD [Activity] char(19) NOT NULL DEFAULT ' '
END
GO
IF 23 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UserSession' and sc.[name] = 'ProductId')
BEGIN
  ALTER TABLE [dbo].[ut_UserSession] ALTER COLUMN [ProductId] char(23)
END
GO
IF 19 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UserSession' and sc.[name] = 'Activity')
BEGIN
  ALTER TABLE [dbo].[ut_UserSession] ALTER COLUMN [Activity] char(19)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'UserSession')
  DROP VIEW [dbo].[UserSession]
GO
CREATE VIEW [dbo].[UserSession] AS
  SELECT * FROM [dbo].[ut_UserSession]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_UserSession')
  DROP PROCEDURE [dbo].[up_Add_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Add_UserSession] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_UserSession])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UserSession] (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_UserSession')
  DROP PROCEDURE [dbo].[up_Upd_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Upd_UserSession]
  @RI integer
 ,@Parm_ProductId char(23)
 ,@Parm_Activity char(19)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_UserSession] SET
     [ProductId] = RTRIM(@Parm_ProductId)
    ,[Activity] = RTRIM(@Parm_Activity)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_UserSession')
  DROP PROCEDURE [dbo].[up_Sel_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Sel_UserSession]
  @RI integer
AS
SELECT
     [ProductId]
    ,[Activity]
  FROM [dbo].[ut_UserSession]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_UserSession')
  DROP PROCEDURE [dbo].[up_Del_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Del_UserSession]
  @RI integer
AS
  DELETE FROM [dbo].[ut_UserSession] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_UserSession')
  DROP PROCEDURE [dbo].[up_Reorg_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Reorg_UserSession]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_UserSession] 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_UserSession] 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_UserSession] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_UserSession')
  DROP PROCEDURE [dbo].[up_Append_UserSession]
GO
CREATE PROCEDURE [dbo].[up_Append_UserSession]
  @Parm_ProductId char(23)
 ,@Parm_Activity char(19)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_UserSession])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UserSession] (
    RecId
   ,[ProductId]
   ,[Activity]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_ProductId)
   ,RTRIM(@Parm_Activity)
       )
  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_UserSession')
  DROP PROCEDURE [dbo].[up_TrSel_UserSession]
GO
CREATE PROCEDURE [dbo].[up_TrSel_UserSession]
AS
  SELECT   RecId,
      LTRIM(RTRIM([ProductId]))
      +LTRIM(RTRIM([Activity]))
    AS Data
  FROM     [dbo].[ut_UserSession]
  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_UserSession'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_UserSession]
(ProductId,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_First_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_Last_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_Prior_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_Next_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_Next50_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_FindL_UserSession_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UserSession_Key00')
  DROP PROCEDURE [dbo].[up_FindF_UserSession_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus UserSession in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_UserSession_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UserSession] ORDER BY ProductId,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UserSession in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UserSession_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UserSession] ORDER BY ProductId DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY ProductId,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UserSession_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldProductId char(23)

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

  IF @xLen = 23
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UserSession]
      WHERE ProductId = SUBSTRING(@xKey,1,23)
      ORDER BY ProductId 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_UserSession'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_UserSession]
(Activity,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_First_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_Last_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_Prior_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_Next_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_Next50_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_FindL_UserSession_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UserSession_Key01')
  DROP PROCEDURE [dbo].[up_FindF_UserSession_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus UserSession in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_UserSession_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UserSession] ORDER BY Activity,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UserSession in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UserSession_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UserSession] ORDER BY Activity DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Activity,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UserSession_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldActivity char(19)

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

