------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebRoule')
BEGIN
CREATE TABLE [dbo].[ut_WebRoule]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Roule] char(30) DEFAULT ' '
  ,[Descr] char(50) DEFAULT ' '
  CONSTRAINT [Pk_WebRoule]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebRoule_KeyDel')
  DROP TRIGGER [dbo].[tr_WebRoule_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebRoule_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebRoule_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebRoule_Keys')
  DROP TABLE [dbo].[ut_WebRoule_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebRoule' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoule] 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_WebRoule' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoule] ADD [Descr] 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_WebRoule' and sc.[name] = 'Roule')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoule] 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_WebRoule' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_WebRoule] ALTER COLUMN [Descr] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebRoule')
  DROP VIEW [dbo].[WebRoule]
GO
CREATE VIEW [dbo].[WebRoule] AS
  SELECT * FROM [dbo].[ut_WebRoule]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebRoule')
  DROP PROCEDURE [dbo].[up_Add_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Add_WebRoule] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebRoule])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebRoule] (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_WebRoule')
  DROP PROCEDURE [dbo].[up_Upd_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebRoule]
  @RI integer
 ,@Parm_Roule char(30)
 ,@Parm_Descr char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebRoule] SET
     [Roule] = RTRIM(@Parm_Roule)
    ,[Descr] = RTRIM(@Parm_Descr)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebRoule')
  DROP PROCEDURE [dbo].[up_Sel_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebRoule]
  @RI integer
AS
SELECT
     [Roule]
    ,[Descr]
  FROM [dbo].[ut_WebRoule]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebRoule')
  DROP PROCEDURE [dbo].[up_Del_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Del_WebRoule]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebRoule] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebRoule')
  DROP PROCEDURE [dbo].[up_Reorg_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebRoule]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebRoule] 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_WebRoule] 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_WebRoule] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebRoule')
  DROP PROCEDURE [dbo].[up_Append_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_Append_WebRoule]
  @Parm_Roule char(30)
 ,@Parm_Descr char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebRoule])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebRoule] (
    RecId
   ,[Roule]
   ,[Descr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Roule)
   ,RTRIM(@Parm_Descr)
       )
  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_WebRoule')
  DROP PROCEDURE [dbo].[up_TrSel_WebRoule]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebRoule]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Roule]))
      +LTRIM(RTRIM([Descr]))
    AS Data
  FROM     [dbo].[ut_WebRoule]
  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_WebRoule'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebRoule]
(Roule,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_First_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebRoule_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebRoule in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebRoule_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebRoule] ORDER BY Roule,RecId
GO

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

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

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

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Roule FROM [dbo].[ut_WebRoule] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @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_WebRoule]
                   WHERE Roule = @oldRoule AND RecId < @oldRecId
                   ORDER BY 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_WebRoule]
                   WHERE Roule < @oldRoule
                   ORDER BY 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 Roule,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_WebRoule_Key00] @xKey char(128),@xLen integer AS

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

