------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebUserRoule')
BEGIN
CREATE TABLE [dbo].[ut_WebUserRoule]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Uid] char(30) DEFAULT ' '
  ,[Roule] char(30) DEFAULT ' '
  ,[UidKey] char(30) DEFAULT ' '
  CONSTRAINT [Pk_WebUserRoule]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUserRoule_KeyDel')
  DROP TRIGGER [dbo].[tr_WebUserRoule_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUserRoule_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebUserRoule_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebUserRoule_Keys')
  DROP TABLE [dbo].[ut_WebUserRoule_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserRoule' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] 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_WebUserRoule' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] 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_WebUserRoule' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] ADD [Roule] 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_WebUserRoule' and sc.[name] = 'UidKey')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] ADD [UidKey] char(30) 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_WebUserRoule' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] 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_WebUserRoule' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] ALTER COLUMN [Uid] char(30)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserRoule' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] ALTER COLUMN [Roule] char(30)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUserRoule' and sc.[name] = 'UidKey')
BEGIN
  ALTER TABLE [dbo].[ut_WebUserRoule] ALTER COLUMN [UidKey] char(30)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebUserRoule')
  DROP VIEW [dbo].[WebUserRoule]
GO
CREATE VIEW [dbo].[WebUserRoule] AS
  SELECT * FROM [dbo].[ut_WebUserRoule]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Add_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Add_WebUserRoule] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebUserRoule])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebUserRoule] (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_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Upd_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebUserRoule]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_Roule char(30)
 ,@Parm_UidKey char(30)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebUserRoule] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Uid] = RTRIM(@Parm_Uid)
    ,[Roule] = RTRIM(@Parm_Roule)
    ,[UidKey] = RTRIM(@Parm_UidKey)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Sel_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebUserRoule]
  @RI integer
AS
SELECT
     [Knr]
    ,[Uid]
    ,[Roule]
    ,[UidKey]
  FROM [dbo].[ut_WebUserRoule]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Del_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Del_WebUserRoule]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebUserRoule] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Reorg_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebUserRoule]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebUserRoule] 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_WebUserRoule] 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_WebUserRoule] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebUserRoule')
  DROP PROCEDURE [dbo].[up_Append_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_Append_WebUserRoule]
  @Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_Roule char(30)
 ,@Parm_UidKey char(30)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebUserRoule])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebUserRoule] (
    RecId
   ,[Knr]
   ,[Uid]
   ,[Roule]
   ,[UidKey]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Uid)
   ,RTRIM(@Parm_Roule)
   ,RTRIM(@Parm_UidKey)
       )
  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_WebUserRoule')
  DROP PROCEDURE [dbo].[up_TrSel_WebUserRoule]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebUserRoule]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Uid]))
      +LTRIM(RTRIM([Roule]))
      +LTRIM(RTRIM([UidKey]))
    AS Data
  FROM     [dbo].[ut_WebUserRoule]
  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_WebUserRoule'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebUserRoule]
(Knr,Uid,Roule,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_First_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebUserRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUserRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebUserRoule_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebUserRoule in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebUserRoule_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule] ORDER BY Knr,Uid,Roule,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebUserRoule in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebUserRoule_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule] ORDER BY Knr DESC,Uid DESC,Roule DESC,RecId DESC
GO

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

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

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

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

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND Uid = SUBSTRING(@xKey,21,30) AND Roule = SUBSTRING(@xKey,51,30)
      ORDER BY Knr DESC,Uid DESC,Roule 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_WebUserRoule'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_WebUserRoule]
(Knr,UidKey,Roule,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_First_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_Last_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_Next_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_Next50_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WebUserRoule_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUserRoule_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WebUserRoule_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebUserRoule in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebUserRoule_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule] ORDER BY Knr,UidKey,Roule,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebUserRoule in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebUserRoule_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule] ORDER BY Knr DESC,UidKey DESC,Roule DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Knr,UidKey,Roule,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebUserRoule_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldKnr char(20)
  DECLARE @oldUidKey char(30)
  DECLARE @oldRoule char(30)

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

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

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

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

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

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

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

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND UidKey = SUBSTRING(@xKey,21,30) AND Roule = SUBSTRING(@xKey,51,30)
      ORDER BY Knr,UidKey,Roule,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

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

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebUserRoule]
      WHERE Knr = SUBSTRING(@xKey,1,20) AND UidKey = SUBSTRING(@xKey,21,30) AND Roule = SUBSTRING(@xKey,51,30)
      ORDER BY Knr DESC,UidKey DESC,Roule DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

