------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_AdressBemerkungen')
BEGIN
CREATE TABLE [dbo].[ut_AdressBemerkungen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[AdrNr] char(15) DEFAULT ' '
  ,[ZNr] char(8) DEFAULT ' '
  ,[DatumAnl] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[DatumAen] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Bemerkung] char(80) DEFAULT ' '
  CONSTRAINT [Pk_AdressBemerkungen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_AdressBemerkungen_KeyDel')
  DROP TRIGGER [dbo].[tr_AdressBemerkungen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_AdressBemerkungen_KeyUpd')
  DROP TRIGGER [dbo].[tr_AdressBemerkungen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_AdressBemerkungen_Keys')
  DROP TABLE [dbo].[ut_AdressBemerkungen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_AdressBemerkungen' and sc.[name] = 'AdrNr')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ADD [AdrNr] char(15) 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_AdressBemerkungen' and sc.[name] = 'ZNr')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ADD [ZNr] 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_AdressBemerkungen' and sc.[name] = 'DatumAnl')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ADD [DatumAnl] 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_AdressBemerkungen' and sc.[name] = 'DatumAen')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ADD [DatumAen] 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_AdressBemerkungen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ADD [Bemerkung] char(80) NOT NULL DEFAULT ' '
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_AdressBemerkungen' and sc.[name] = 'AdrNr')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ALTER COLUMN [AdrNr] char(15)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_AdressBemerkungen' and sc.[name] = 'ZNr')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ALTER COLUMN [ZNr] char(8)
END
GO
IF 80 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_AdressBemerkungen' and sc.[name] = 'Bemerkung')
BEGIN
  ALTER TABLE [dbo].[ut_AdressBemerkungen] ALTER COLUMN [Bemerkung] char(80)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'AdressBemerkungen')
  DROP VIEW [dbo].[AdressBemerkungen]
GO
CREATE VIEW [dbo].[AdressBemerkungen] AS
  SELECT * FROM [dbo].[ut_AdressBemerkungen]
GO
CREATE TABLE [dbo].[ut_AdressBemerkungen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(33) NOT NULL
  ,[Key01_Value] CHAR(43) NOT NULL
  CONSTRAINT [Pk_ut_AdressBemerkungen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_AdressBemerkungen_KeyDel] ON [dbo].[ut_AdressBemerkungen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_AdressBemerkungen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_AdressBemerkungen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[AdrNr])+convert(char(8),[ZNr])+STR(RecId,10))
     ,UPPER(convert(char(15),[AdrNr])+CONVERT(char(10),[DatumAnl],120)+convert(char(8),[ZNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_AdressBemerkungen_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(15),[AdrNr])+convert(char(8),[ZNr])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(15),[AdrNr])+CONVERT(char(10),[DatumAnl],120)+convert(char(8),[ZNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Add_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Add_AdressBemerkungen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_AdressBemerkungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_AdressBemerkungen] (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_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Upd_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Upd_AdressBemerkungen]
  @RI integer
 ,@Parm_AdrNr char(15)
 ,@Parm_ZNr char(8)
 ,@Parm_DatumAnl datetime
 ,@Parm_DatumAen datetime
 ,@Parm_Bemerkung char(80)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_AdressBemerkungen] SET
     [AdrNr] = RTRIM(@Parm_AdrNr)
    ,[ZNr] = RTRIM(@Parm_ZNr)
    ,[DatumAnl] = @Parm_DatumAnl
    ,[DatumAen] = @Parm_DatumAen
    ,[Bemerkung] = RTRIM(@Parm_Bemerkung)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Sel_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Sel_AdressBemerkungen]
  @RI integer
AS
SELECT
     [AdrNr]
    ,[ZNr]
    ,[DatumAnl]
    ,[DatumAen]
    ,[Bemerkung]
  FROM [dbo].[ut_AdressBemerkungen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Del_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Del_AdressBemerkungen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_AdressBemerkungen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Reorg_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_AdressBemerkungen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_AdressBemerkungen] 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_AdressBemerkungen] 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_AdressBemerkungen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_Append_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_Append_AdressBemerkungen]
  @Parm_AdrNr char(15)
 ,@Parm_ZNr char(8)
 ,@Parm_DatumAnl datetime
 ,@Parm_DatumAen datetime
 ,@Parm_Bemerkung char(80)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_AdressBemerkungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_AdressBemerkungen] (
    RecId
   ,[AdrNr]
   ,[ZNr]
   ,[DatumAnl]
   ,[DatumAen]
   ,[Bemerkung]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_AdrNr)
   ,RTRIM(@Parm_ZNr)
   ,@Parm_DatumAnl
   ,@Parm_DatumAen
   ,RTRIM(@Parm_Bemerkung)
       )
  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_AdressBemerkungen')
  DROP PROCEDURE [dbo].[up_TrSel_AdressBemerkungen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_AdressBemerkungen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([AdrNr]))
      +LTRIM(RTRIM([ZNr]))
      +LTRIM(RTRIM([Bemerkung]))
    AS Data
  FROM     [dbo].[ut_AdressBemerkungen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_AdressBemerkungen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_First_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_Last_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_Next_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_AdressBemerkungen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_AdressBemerkungen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_AdressBemerkungen_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_AdressBemerkungen_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_First_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_Last_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_Next_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_AdressBemerkungen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_AdressBemerkungen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_AdressBemerkungen_Key01]
GO

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

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

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

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

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

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

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

