------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WebAnnr')
BEGIN
CREATE TABLE [dbo].[ut_WebAnnr]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Knr] char(20) DEFAULT ' '
  ,[Annr] char(20) DEFAULT ' '
  ,[Name] char(50) DEFAULT ' '
  CONSTRAINT [Pk_WebAnnr]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebAnnr_KeyDel')
  DROP TRIGGER [dbo].[tr_WebAnnr_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WebAnnr_KeyUpd')
  DROP TRIGGER [dbo].[tr_WebAnnr_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WebAnnr_Keys')
  DROP TABLE [dbo].[ut_WebAnnr_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebAnnr' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] 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_WebAnnr' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] ADD [Annr] 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_WebAnnr' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] ADD [Name] char(50) 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_WebAnnr' and sc.[name] = 'Knr')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] ALTER COLUMN [Knr] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebAnnr' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] ALTER COLUMN [Annr] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WebAnnr' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_WebAnnr] ALTER COLUMN [Name] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WebAnnr')
  DROP VIEW [dbo].[WebAnnr]
GO
CREATE VIEW [dbo].[WebAnnr] AS
  SELECT * FROM [dbo].[ut_WebAnnr]
GO
CREATE TABLE [dbo].[ut_WebAnnr_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(50) NOT NULL
  CONSTRAINT [Pk_ut_WebAnnr_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WebAnnr_KeyDel] ON [dbo].[ut_WebAnnr]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WebAnnr_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_WebAnnr_KeyUpd] ON [dbo].[ut_WebAnnr]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_WebAnnr_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_WebAnnr_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[Knr])+convert(char(20),[Annr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WebAnnr_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[Knr])+convert(char(20),[Annr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_WebAnnr] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WebAnnr')
  DROP PROCEDURE [dbo].[up_Add_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Add_WebAnnr] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WebAnnr])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebAnnr] (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_WebAnnr')
  DROP PROCEDURE [dbo].[up_Upd_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Upd_WebAnnr]
  @RI integer
 ,@Parm_Knr char(20)
 ,@Parm_Annr char(20)
 ,@Parm_Name char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WebAnnr] SET
     [Knr] = RTRIM(@Parm_Knr)
    ,[Annr] = RTRIM(@Parm_Annr)
    ,[Name] = RTRIM(@Parm_Name)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WebAnnr')
  DROP PROCEDURE [dbo].[up_Sel_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Sel_WebAnnr]
  @RI integer
AS
SELECT
     [Knr]
    ,[Annr]
    ,[Name]
  FROM [dbo].[ut_WebAnnr]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WebAnnr')
  DROP PROCEDURE [dbo].[up_Del_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Del_WebAnnr]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WebAnnr] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WebAnnr')
  DROP PROCEDURE [dbo].[up_Reorg_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WebAnnr]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WebAnnr] 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_WebAnnr] 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_WebAnnr] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WebAnnr')
  DROP PROCEDURE [dbo].[up_Append_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_Append_WebAnnr]
  @Parm_Knr char(20)
 ,@Parm_Annr char(20)
 ,@Parm_Name char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WebAnnr])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WebAnnr] (
    RecId
   ,[Knr]
   ,[Annr]
   ,[Name]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Knr)
   ,RTRIM(@Parm_Annr)
   ,RTRIM(@Parm_Name)
       )
  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_WebAnnr')
  DROP PROCEDURE [dbo].[up_TrSel_WebAnnr]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WebAnnr]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Knr]))
      +LTRIM(RTRIM([Annr]))
      +LTRIM(RTRIM([Name]))
    AS Data
  FROM     [dbo].[ut_WebAnnr]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WebAnnr_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_First_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_Last_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_Next_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WebAnnr_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WebAnnr_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WebAnnr_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_WebAnnr_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WebAnnr_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_WebAnnr_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_WebAnnr_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_WebAnnr_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_WebAnnr_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_WebAnnr_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_WebAnnr_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_WebAnnr_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_WebAnnr_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_WebAnnr_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_WebAnnr_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_WebAnnr_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WebAnnr_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

