------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebUser')
BEGIN
CREATE TABLE [dbo].[ut_WebUser]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Uid] char(30) DEFAULT ' '
  ,[PwHash] integer DEFAULT 0
  ,[Email] char(250) DEFAULT ' '
  ,[Locked] tinyint DEFAULT 0
  ,[Annr] char(4) DEFAULT ' '
  ,[AlleAnnr] tinyint DEFAULT 0
  ,[Hybrid] tinyint DEFAULT 0
  CONSTRAINT [Pk_WebUser]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUser_KeyDel')
  DROP TRIGGER [dbo].[tr_WebUser_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebUser_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebUser_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebUser_Keys')
  DROP TABLE [dbo].[ut_WebUser_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUser' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] 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_WebUser' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] 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_WebUser' and sc.[name] = 'PwHash')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ADD [PwHash] integer 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_WebUser' and sc.[name] = 'Email')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ADD [Email] char(250) 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_WebUser' and sc.[name] = 'Locked')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] 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_WebUser' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ADD [Annr] char(4) 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_WebUser' and sc.[name] = 'AlleAnnr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ADD [AlleAnnr] 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_WebUser' and sc.[name] = 'Hybrid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ADD [Hybrid] tinyint NOT NULL DEFAULT 0
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUser' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] 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_WebUser' and sc.[name] = 'Uid')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ALTER COLUMN [Uid] char(30)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUser' and sc.[name] = 'Email')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ALTER COLUMN [Email] char(250)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebUser' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_WebUser] ALTER COLUMN [Annr] char(4)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebUser')
  DROP VIEW [dbo].[WebUser]
GO
CREATE VIEW [dbo].[WebUser] AS
  SELECT * FROM [dbo].[ut_WebUser]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebUser')
  DROP PROCEDURE [dbo].[up_Add_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Add_WebUser] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebUser])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebUser] (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_WebUser')
  DROP PROCEDURE [dbo].[up_Upd_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebUser]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_PwHash integer
 ,@Parm_Email char(250)
 ,@Parm_Locked tinyint
 ,@Parm_Annr char(4)
 ,@Parm_AlleAnnr tinyint
 ,@Parm_Hybrid tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebUser] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Uid] = RTRIM(@Parm_Uid)
    ,[PwHash] = @Parm_PwHash
    ,[Email] = RTRIM(@Parm_Email)
    ,[Locked] = @Parm_Locked
    ,[Annr] = RTRIM(@Parm_Annr)
    ,[AlleAnnr] = @Parm_AlleAnnr
    ,[Hybrid] = @Parm_Hybrid
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebUser')
  DROP PROCEDURE [dbo].[up_Sel_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebUser]
  @RI integer
AS
SELECT
     [Knr]
    ,[Uid]
    ,[PwHash]
    ,[Email]
    ,[Locked]
    ,[Annr]
    ,[AlleAnnr]
    ,[Hybrid]
  FROM [dbo].[ut_WebUser]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebUser')
  DROP PROCEDURE [dbo].[up_Del_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Del_WebUser]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebUser] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebUser')
  DROP PROCEDURE [dbo].[up_Reorg_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebUser]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebUser] 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_WebUser] 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_WebUser] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebUser')
  DROP PROCEDURE [dbo].[up_Append_WebUser]
GO
CREATE PROCEDURE [dbo].[up_Append_WebUser]
  @Parm_Knr char(20)
 ,@Parm_Uid char(30)
 ,@Parm_PwHash integer
 ,@Parm_Email char(250)
 ,@Parm_Locked tinyint
 ,@Parm_Annr char(4)
 ,@Parm_AlleAnnr tinyint
 ,@Parm_Hybrid tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebUser])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebUser] (
    RecId
   ,[Knr]
   ,[Uid]
   ,[PwHash]
   ,[Email]
   ,[Locked]
   ,[Annr]
   ,[AlleAnnr]
   ,[Hybrid]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Uid)
   ,@Parm_PwHash
   ,RTRIM(@Parm_Email)
   ,@Parm_Locked
   ,RTRIM(@Parm_Annr)
   ,@Parm_AlleAnnr
   ,@Parm_Hybrid
       )
  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_WebUser')
  DROP PROCEDURE [dbo].[up_TrSel_WebUser]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebUser]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Uid]))
      +LTRIM(RTRIM([Email]))
      +LTRIM(RTRIM([Annr]))
    AS Data
  FROM     [dbo].[ut_WebUser]
  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_WebUser'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebUser]
(Knr,Uid,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_First_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebUser_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebUser_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebUser_Key00]
GO

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

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

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

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

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

