------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_ExchgIdent')
BEGIN
CREATE TABLE [dbo].[ut_ExchgIdent]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[LfdNr] char(8) DEFAULT ' '
  ,[ExchgIdent] char(200) DEFAULT ' '
  ,[ExchgUpper] char(200) DEFAULT ' '
  CONSTRAINT [Pk_ExchgIdent]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ExchgIdent_KeyDel')
  DROP TRIGGER [dbo].[tr_ExchgIdent_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_ExchgIdent_KeyUpd')
  DROP TRIGGER [dbo].[tr_ExchgIdent_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_ExchgIdent_Keys')
  DROP TABLE [dbo].[ut_ExchgIdent_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgIdent' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] 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_ExchgIdent' and sc.[name] = 'ExchgIdent')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] ADD [ExchgIdent] 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_ExchgIdent' and sc.[name] = 'ExchgUpper')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] ADD [ExchgUpper] char(200) 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_ExchgIdent' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] ALTER COLUMN [LfdNr] char(8)
END
GO
IF 200 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgIdent' and sc.[name] = 'ExchgIdent')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] ALTER COLUMN [ExchgIdent] char(200)
END
GO
IF 200 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_ExchgIdent' and sc.[name] = 'ExchgUpper')
BEGIN
  ALTER TABLE [dbo].[ut_ExchgIdent] ALTER COLUMN [ExchgUpper] char(200)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'ExchgIdent')
  DROP VIEW [dbo].[ExchgIdent]
GO
CREATE VIEW [dbo].[ExchgIdent] AS
  SELECT * FROM [dbo].[ut_ExchgIdent]
GO
CREATE TABLE [dbo].[ut_ExchgIdent_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(18) NOT NULL
  ,[Key01_Value] CHAR(210) NOT NULL
  CONSTRAINT [Pk_ut_ExchgIdent_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_ExchgIdent_KeyDel] ON [dbo].[ut_ExchgIdent]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_ExchgIdent_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_ExchgIdent_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[LfdNr])+STR(RecId,10))
     ,UPPER(convert(char(200),[ExchgUpper])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_ExchgIdent_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[LfdNr])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(200),[ExchgUpper])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Add_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Add_ExchgIdent] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_ExchgIdent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ExchgIdent] (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_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Upd_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Upd_ExchgIdent]
  @RI integer
 ,@Parm_LfdNr char(8)
 ,@Parm_ExchgIdent char(200)
 ,@Parm_ExchgUpper char(200)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_ExchgIdent] SET
     [LfdNr] = RTRIM(@Parm_LfdNr)
    ,[ExchgIdent] = RTRIM(@Parm_ExchgIdent)
    ,[ExchgUpper] = RTRIM(@Parm_ExchgUpper)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Sel_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Sel_ExchgIdent]
  @RI integer
AS
SELECT
     [LfdNr]
    ,[ExchgIdent]
    ,[ExchgUpper]
  FROM [dbo].[ut_ExchgIdent]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Del_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Del_ExchgIdent]
  @RI integer
AS
  DELETE FROM [dbo].[ut_ExchgIdent] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Reorg_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Reorg_ExchgIdent]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_ExchgIdent] 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_ExchgIdent] 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_ExchgIdent] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_ExchgIdent')
  DROP PROCEDURE [dbo].[up_Append_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_Append_ExchgIdent]
  @Parm_LfdNr char(8)
 ,@Parm_ExchgIdent char(200)
 ,@Parm_ExchgUpper char(200)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_ExchgIdent])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_ExchgIdent] (
    RecId
   ,[LfdNr]
   ,[ExchgIdent]
   ,[ExchgUpper]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_LfdNr)
   ,RTRIM(@Parm_ExchgIdent)
   ,RTRIM(@Parm_ExchgUpper)
       )
  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_ExchgIdent')
  DROP PROCEDURE [dbo].[up_TrSel_ExchgIdent]
GO
CREATE PROCEDURE [dbo].[up_TrSel_ExchgIdent]
AS
  SELECT   RecId,
      LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([ExchgIdent]))
      +LTRIM(RTRIM([ExchgUpper]))
    AS Data
  FROM     [dbo].[ut_ExchgIdent]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_ExchgIdent_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_First_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_Last_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_Prior_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_Next_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_Next50_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_FindL_ExchgIdent_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ExchgIdent_Key00')
  DROP PROCEDURE [dbo].[up_FindF_ExchgIdent_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_ExchgIdent_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_First_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_Last_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_Prior_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_Next_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_Next50_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_FindL_ExchgIdent_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_ExchgIdent_Key01')
  DROP PROCEDURE [dbo].[up_FindF_ExchgIdent_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_ExchgIdent_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgIdent_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_ExchgIdent_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_ExchgIdent_Keys] ORDER BY [Key01_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_ExchgIdent_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_ExchgIdent_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_ExchgIdent_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

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

