------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebRoulePermission')
BEGIN
CREATE TABLE [dbo].[ut_WebRoulePermission]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Roule] char(30) DEFAULT ' '
  ,[Permission] char(50) DEFAULT ' '
  ,[Granted] tinyint DEFAULT 0
  ,[PermKey] char(50) DEFAULT ' '
  CONSTRAINT [Pk_WebRoulePermission]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebRoulePermission_KeyDel')
  DROP TRIGGER [dbo].[tr_WebRoulePermission_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebRoulePermission_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebRoulePermission_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebRoulePermission_Keys')
  DROP TABLE [dbo].[ut_WebRoulePermission_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoulePermission' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] 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_WebRoulePermission' and sc.[name] = 'Permission')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ADD [Permission] char(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_WebRoulePermission' and sc.[name] = 'Granted')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ADD [Granted] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoulePermission' and sc.[name] = 'PermKey')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ADD [PermKey] char(50) NOT NULL DEFAULT ' '
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoulePermission' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ALTER COLUMN [Roule] char(30)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoulePermission' and sc.[name] = 'Permission')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ALTER COLUMN [Permission] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoulePermission' and sc.[name] = 'PermKey')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoulePermission] ALTER COLUMN [PermKey] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebRoulePermission')
  DROP VIEW [dbo].[WebRoulePermission]
GO
CREATE VIEW [dbo].[WebRoulePermission] AS
  SELECT * FROM [dbo].[ut_WebRoulePermission]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Add_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Add_WebRoulePermission] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebRoulePermission])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebRoulePermission] (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_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Upd_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebRoulePermission]
  @RI integer
 ,@Parm_Roule char(30)
 ,@Parm_Permission char(50)
 ,@Parm_Granted tinyint
 ,@Parm_PermKey char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebRoulePermission] SET
     [Roule] = RTRIM(@Parm_Roule)
    ,[Permission] = RTRIM(@Parm_Permission)
    ,[Granted] = @Parm_Granted
    ,[PermKey] = RTRIM(@Parm_PermKey)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Sel_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebRoulePermission]
  @RI integer
AS
SELECT
     [Roule]
    ,[Permission]
    ,[Granted]
    ,[PermKey]
  FROM [dbo].[ut_WebRoulePermission]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Del_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Del_WebRoulePermission]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebRoulePermission] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Reorg_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebRoulePermission]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebRoulePermission] 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_WebRoulePermission] 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_WebRoulePermission] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_Append_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_Append_WebRoulePermission]
  @Parm_Roule char(30)
 ,@Parm_Permission char(50)
 ,@Parm_Granted tinyint
 ,@Parm_PermKey char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebRoulePermission])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebRoulePermission] (
    RecId
   ,[Roule]
   ,[Permission]
   ,[Granted]
   ,[PermKey]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Roule)
   ,RTRIM(@Parm_Permission)
   ,@Parm_Granted
   ,RTRIM(@Parm_PermKey)
       )
  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_WebRoulePermission')
  DROP PROCEDURE [dbo].[up_TrSel_WebRoulePermission]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebRoulePermission]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Roule]))
      +LTRIM(RTRIM([Permission]))
      +LTRIM(RTRIM([PermKey]))
    AS Data
  FROM     [dbo].[ut_WebRoulePermission]
  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_WebRoulePermission'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebRoulePermission]
(Roule,PermKey,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_First_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebRoulePermission_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebRoulePermission_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebRoulePermission_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebRoulePermission in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebRoulePermission_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission] ORDER BY Roule,PermKey,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebRoulePermission in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebRoulePermission_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission] ORDER BY Roule DESC,PermKey DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Roule,PermKey,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebRoulePermission_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldRoule char(30)
  DECLARE @oldPermKey char(50)

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

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

  IF @xLen < 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE SUBSTRING(Roule,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Roule,PermKey,RecId
    RETURN
  END

  IF @xLen = 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30)
      ORDER BY Roule,PermKey,RecId
    RETURN
  END

  IF @xLen < 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND SUBSTRING(PermKey,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY Roule,PermKey,RecId
    RETURN
  END

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND PermKey = SUBSTRING(@xKey,31,50)
      ORDER BY Roule,PermKey,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE SUBSTRING(Roule,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Roule DESC,PermKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30)
      ORDER BY Roule DESC,PermKey DESC,RecId DESC
    RETURN
  END

  IF @xLen < 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND SUBSTRING(PermKey,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY Roule DESC,PermKey DESC,RecId DESC
    RETURN
  END

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND PermKey = SUBSTRING(@xKey,31,50)
      ORDER BY Roule DESC,PermKey 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_WebRoulePermission'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_WebRoulePermission]
(Roule,Permission,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_First_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_Last_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_Next_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_Next50_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WebRoulePermission_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebRoulePermission_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WebRoulePermission_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebRoulePermission in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebRoulePermission_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission] ORDER BY Roule,Permission,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus WebRoulePermission in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_WebRoulePermission_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission] ORDER BY Roule DESC,Permission DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Roule,Permission,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_WebRoulePermission_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldRoule char(30)
  DECLARE @oldPermission char(50)

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

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

  IF @xLen < 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE SUBSTRING(Roule,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Roule,Permission,RecId
    RETURN
  END

  IF @xLen = 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30)
      ORDER BY Roule,Permission,RecId
    RETURN
  END

  IF @xLen < 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND SUBSTRING(Permission,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY Roule,Permission,RecId
    RETURN
  END

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND Permission = SUBSTRING(@xKey,31,50)
      ORDER BY Roule,Permission,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE SUBSTRING(Roule,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Roule DESC,Permission DESC,RecId DESC
    RETURN
  END

  IF @xLen = 30
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30)
      ORDER BY Roule DESC,Permission DESC,RecId DESC
    RETURN
  END

  IF @xLen < 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND SUBSTRING(Permission,1,@xLen-30) = SUBSTRING(@xKey,31,@xLen-30)
      ORDER BY Roule DESC,Permission DESC,RecId DESC
    RETURN
  END

  IF @xLen = 80
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WebRoulePermission]
      WHERE Roule = SUBSTRING(@xKey,1,30) AND Permission = SUBSTRING(@xKey,31,50)
      ORDER BY Roule DESC,Permission DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

