------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebMandant')
BEGIN
CREATE TABLE [dbo].[ut_WebMandant]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Descr] char(50) DEFAULT ' '
  ,[Locked] tinyint DEFAULT 0
  ,[Key] char(16) DEFAULT ' '
  ,[Uri] char(200) DEFAULT ' '
  ,[SubUri] char(200) DEFAULT ' '
  CONSTRAINT [Pk_WebMandant]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebMandant_KeyDel')
  DROP TRIGGER [dbo].[tr_WebMandant_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebMandant_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebMandant_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebMandant_Keys')
  DROP TABLE [dbo].[ut_WebMandant_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebMandant' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] 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_WebMandant' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ADD [Descr] 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_WebMandant' and sc.[name] = 'Locked')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ADD [Locked] 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_WebMandant' and sc.[name] = 'Key')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ADD [Key] char(16) 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_WebMandant' and sc.[name] = 'Uri')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ADD [Uri] char(200) 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_WebMandant' and sc.[name] = 'SubUri')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ADD [SubUri] char(200) 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_WebMandant' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ALTER COLUMN [Knr] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebMandant' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ALTER COLUMN [Descr] char(50)
END
GO
IF 16 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebMandant' and sc.[name] = 'Key')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ALTER COLUMN [Key] char(16)
END
GO
IF 200 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebMandant' and sc.[name] = 'Uri')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ALTER COLUMN [Uri] char(200)
END
GO
IF 200 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebMandant' and sc.[name] = 'SubUri')
BEGIN
  ALTER TABLE [dbo].[ut_WebMandant] ALTER COLUMN [SubUri] char(200)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebMandant')
  DROP VIEW [dbo].[WebMandant]
GO
CREATE VIEW [dbo].[WebMandant] AS
  SELECT * FROM [dbo].[ut_WebMandant]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebMandant')
  DROP PROCEDURE [dbo].[up_Add_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Add_WebMandant] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebMandant])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebMandant] (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_WebMandant')
  DROP PROCEDURE [dbo].[up_Upd_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebMandant]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Descr char(50)
 ,@Parm_Locked tinyint
 ,@Parm_Key char(16)
 ,@Parm_Uri char(200)
 ,@Parm_SubUri char(200)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebMandant] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Descr] = RTRIM(@Parm_Descr)
    ,[Locked] = @Parm_Locked
    ,[Key] = RTRIM(@Parm_Key)
    ,[Uri] = RTRIM(@Parm_Uri)
    ,[SubUri] = RTRIM(@Parm_SubUri)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebMandant')
  DROP PROCEDURE [dbo].[up_Sel_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebMandant]
  @RI integer
AS
SELECT
     [Knr]
    ,[Descr]
    ,[Locked]
    ,[Key]
    ,[Uri]
    ,[SubUri]
  FROM [dbo].[ut_WebMandant]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebMandant')
  DROP PROCEDURE [dbo].[up_Del_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Del_WebMandant]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebMandant] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebMandant')
  DROP PROCEDURE [dbo].[up_Reorg_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebMandant]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebMandant] 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_WebMandant] 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_WebMandant] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebMandant')
  DROP PROCEDURE [dbo].[up_Append_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_Append_WebMandant]
  @Parm_Knr char(20)
 ,@Parm_Descr char(50)
 ,@Parm_Locked tinyint
 ,@Parm_Key char(16)
 ,@Parm_Uri char(200)
 ,@Parm_SubUri char(200)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebMandant])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebMandant] (
    RecId
   ,[Knr]
   ,[Descr]
   ,[Locked]
   ,[Key]
   ,[Uri]
   ,[SubUri]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Descr)
   ,@Parm_Locked
   ,RTRIM(@Parm_Key)
   ,RTRIM(@Parm_Uri)
   ,RTRIM(@Parm_SubUri)
       )
  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_WebMandant')
  DROP PROCEDURE [dbo].[up_TrSel_WebMandant]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebMandant]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Descr]))
      +LTRIM(RTRIM([Key]))
      +LTRIM(RTRIM([Uri]))
      +LTRIM(RTRIM([SubUri]))
    AS Data
  FROM     [dbo].[ut_WebMandant]
  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_WebMandant'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebMandant]
(Knr,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_First_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebMandant_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebMandant_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebMandant_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus WebMandant in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_WebMandant_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WebMandant] ORDER BY Knr,RecId
GO

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

