------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_ExchgQueue')
BEGIN
CREATE TABLE [dbo].[ut_ExchgQueue]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Counter] integer DEFAULT 0
  ,[LfdNr] char(8) DEFAULT ' '
  ,[EMail] char(80) DEFAULT ' '
  ,[Vorgang] integer DEFAULT 0
  ,[DateDone] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[TimeDone] char(10) DEFAULT ' '
  CONSTRAINT [Pk_ExchgQueue]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ExchgQueue_KeyDel')
  DROP TRIGGER [dbo].[tr_ExchgQueue_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ExchgQueue_KeyUpd')
  DROP TRIGGER [dbo].[tr_ExchgQueue_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ExchgQueue_Keys')
  DROP TABLE [dbo].[ut_ExchgQueue_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgQueue' and sc.[name] = 'Counter')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [Counter] 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_ExchgQueue' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [LfdNr] char(8) 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_ExchgQueue' and sc.[name] = 'EMail')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [EMail] char(80) 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_ExchgQueue' and sc.[name] = 'Vorgang')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [Vorgang] 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_ExchgQueue' and sc.[name] = 'DateDone')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [DateDone] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgQueue' and sc.[name] = 'TimeDone')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ADD [TimeDone] char(10) NOT NULL DEFAULT ' '
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgQueue' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ALTER COLUMN [LfdNr] char(8)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgQueue' and sc.[name] = 'EMail')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ALTER COLUMN [EMail] char(80)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgQueue' and sc.[name] = 'TimeDone')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgQueue] ALTER COLUMN [TimeDone] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ExchgQueue')
  DROP VIEW [dbo].[ExchgQueue]
GO
CREATE VIEW [dbo].[ExchgQueue] AS
  SELECT * FROM [dbo].[ut_ExchgQueue]
GO
CREATE TABLE [dbo].[ut_ExchgQueue_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(18) NOT NULL
  ,[Key01_Filter] TINYINT NOT NULL
  ,[Key01_Value] CHAR(18) NOT NULL
  CONSTRAINT [Pk_ut_ExchgQueue_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_ExchgQueue_KeyDel] ON [dbo].[ut_ExchgQueue]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_ExchgQueue_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_ExchgQueue_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Filter],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([Counter],8,0)+STR(RecId,10))
     ,CASE WHEN (Vorgang < 9) THEN 1 ELSE 0 END
     ,UPPER(STR([Counter],8,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_ExchgQueue_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([Counter],8,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(STR([Counter],8,0)+STR([RecId],10))
   ,[Key01_Filter] = CASE WHEN (Vorgang < 9) THEN 1 ELSE 0 END
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Add_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Add_ExchgQueue] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_ExchgQueue])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ExchgQueue] (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_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Upd_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Upd_ExchgQueue]
  @RI integer
 ,@Parm_Counter integer
 ,@Parm_LfdNr char(8)
 ,@Parm_EMail char(80)
 ,@Parm_Vorgang integer
 ,@Parm_DateDone datetime
 ,@Parm_TimeDone char(10)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_ExchgQueue] SET
     [Counter] = @Parm_Counter
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[EMail] = RTRIM(@Parm_EMail)
    ,[Vorgang] = @Parm_Vorgang
    ,[DateDone] = @Parm_DateDone
    ,[TimeDone] = RTRIM(@Parm_TimeDone)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Sel_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Sel_ExchgQueue]
  @RI integer
AS
SELECT
     [Counter]
    ,[LfdNr]
    ,[EMail]
    ,[Vorgang]
    ,[DateDone]
    ,[TimeDone]
  FROM [dbo].[ut_ExchgQueue]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Del_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Del_ExchgQueue]
  @RI integer
AS
  DELETE FROM [dbo].[ut_ExchgQueue] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Reorg_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Reorg_ExchgQueue]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_ExchgQueue] 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_ExchgQueue] 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_ExchgQueue] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ExchgQueue')
  DROP PROCEDURE [dbo].[up_Append_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_Append_ExchgQueue]
  @Parm_Counter integer
 ,@Parm_LfdNr char(8)
 ,@Parm_EMail char(80)
 ,@Parm_Vorgang integer
 ,@Parm_DateDone datetime
 ,@Parm_TimeDone char(10)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_ExchgQueue])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ExchgQueue] (
    RecId
   ,[Counter]
   ,[LfdNr]
   ,[EMail]
   ,[Vorgang]
   ,[DateDone]
   ,[TimeDone]
     ) VALUES (
    @NeueId
   ,@Parm_Counter
   ,RTRIM(@Parm_LfdNr)
   ,RTRIM(@Parm_EMail)
   ,@Parm_Vorgang
   ,@Parm_DateDone
   ,RTRIM(@Parm_TimeDone)
       )
  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_ExchgQueue')
  DROP PROCEDURE [dbo].[up_TrSel_ExchgQueue]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ExchgQueue]
AS
  SELECT   RecId,
      LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([EMail]))
      +LTRIM(RTRIM([TimeDone]))
    AS Data
  FROM     [dbo].[ut_ExchgQueue]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_ExchgQueue_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_First_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_Last_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_Prior_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_Next_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_Next50_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_FindL_ExchgQueue_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ExchgQueue_Key00')
  DROP PROCEDURE [dbo].[up_FindF_ExchgQueue_Key00]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_ExchgQueue_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_ExchgQueue_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_ExchgQueue_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

CREATE INDEX [Ik_Key01] ON [dbo].[ut_ExchgQueue_Keys]
( [Key01_Filter],[Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_First_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_Last_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_Prior_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_Next_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_Next50_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_FindL_ExchgQueue_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ExchgQueue_Key01')
  DROP PROCEDURE [dbo].[up_FindF_ExchgQueue_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_ExchgQueue_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_ExchgQueue_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_ExchgQueue_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_ExchgQueue_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_ExchgQueue_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_ExchgQueue_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_ExchgQueue_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_ExchgQueue_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_ExchgQueue_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_ExchgQueue_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

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

CREATE PROCEDURE [dbo].[up_FindL_ExchgQueue_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgQueue_Keys]
  WHERE [Key01_Filter] = 1 AND [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

