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

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

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

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

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebMandantRoule')
  DROP PROCEDURE [dbo].[up_Append_WebMandantRoule]
GO
CREATE PROCEDURE [dbo].[up_Append_WebMandantRoule]
  @Parm_Knr char(20)
 ,@Parm_Roule char(30)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebMandantRoule])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebMandantRoule] (
    RecId
   ,[Knr]
   ,[Roule]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Roule)
       )
  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_WebMandantRoule')
  DROP PROCEDURE [dbo].[up_TrSel_WebMandantRoule]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebMandantRoule]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Roule]))
    AS Data
  FROM     [dbo].[ut_WebMandantRoule]
  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_WebMandantRoule'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebMandantRoule]
(Knr,Roule,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_First_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebMandantRoule_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebMandantRoule_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebMandantRoule_Key00]
GO

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

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

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

