------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Postleitzahlen')
BEGIN
CREATE TABLE [dbo].[ut_Postleitzahlen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Plz] char(8) DEFAULT ' '
  ,[PlzSub] char(2) DEFAULT ' '
  ,[Ort] char(40) DEFAULT ' '
  CONSTRAINT [Pk_Postleitzahlen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Postleitzahlen_KeyDel')
  DROP TRIGGER [dbo].[tr_Postleitzahlen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Postleitzahlen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Postleitzahlen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Postleitzahlen_Keys')
  DROP TABLE [dbo].[ut_Postleitzahlen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ADD [Plz] 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_Postleitzahlen' and sc.[name] = 'PlzSub')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ADD [PlzSub] char(2) 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_Postleitzahlen' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ADD [Ort] char(40) 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_Postleitzahlen' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ALTER COLUMN [Plz] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'PlzSub')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ALTER COLUMN [PlzSub] char(2)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Postleitzahlen] ALTER COLUMN [Ort] char(40)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Postleitzahlen')
  DROP VIEW [dbo].[Postleitzahlen]
GO
CREATE VIEW [dbo].[Postleitzahlen] AS
  SELECT * FROM [dbo].[ut_Postleitzahlen]
GO
CREATE TABLE [dbo].[ut_Postleitzahlen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(20) NOT NULL
  ,[Key1_Value] CHAR(50) NOT NULL
  CONSTRAINT [Pk_ut_Postleitzahlen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Postleitzahlen_KeyDel] ON [dbo].[ut_Postleitzahlen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Postleitzahlen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Postleitzahlen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value],[Key1_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[Plz])+convert(char(2),[PlzSub])+STR(RecId,10))
     ,UPPER(convert(char(40),[Ort])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Postleitzahlen_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(8),[Plz])+convert(char(2),[PlzSub])+STR([RecId],10))
   ,[Key1_Value] = UPPER(convert(char(40),[Ort])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Add_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Add_Postleitzahlen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Postleitzahlen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Postleitzahlen] (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_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Upd_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Postleitzahlen]
  @RI integer
 ,@Parm_Plz char(8)
 ,@Parm_PlzSub char(2)
 ,@Parm_Ort char(40)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Postleitzahlen] SET
     [Plz] = RTRIM(@Parm_Plz)
    ,[PlzSub] = RTRIM(@Parm_PlzSub)
    ,[Ort] = RTRIM(@Parm_Ort)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Sel_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Postleitzahlen]
  @RI integer
AS
SELECT
     [Plz]
    ,[PlzSub]
    ,[Ort]
  FROM [dbo].[ut_Postleitzahlen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Del_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Del_Postleitzahlen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Reorg_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Postleitzahlen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Postleitzahlen] 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_Postleitzahlen] 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_Postleitzahlen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Append_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Append_Postleitzahlen]
  @Parm_Plz char(8)
 ,@Parm_PlzSub char(2)
 ,@Parm_Ort char(40)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Postleitzahlen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Postleitzahlen] (
    RecId
   ,[Plz]
   ,[PlzSub]
   ,[Ort]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Plz)
   ,RTRIM(@Parm_PlzSub)
   ,RTRIM(@Parm_Ort)
       )
  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_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_TrSel_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Postleitzahlen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Plz]))
      +LTRIM(RTRIM([PlzSub]))
      +LTRIM(RTRIM([Ort]))
    AS Data
  FROM     [dbo].[ut_Postleitzahlen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Postleitzahlen_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_First_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Last_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Next_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Postleitzahlen_Key0]
GO

CREATE PROCEDURE [dbo].[up_First_Postleitzahlen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys] ORDER BY [Key0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Postleitzahlen_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys] ORDER BY [Key0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Postleitzahlen_Key0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Postleitzahlen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Postleitzahlen_Key0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_Postleitzahlen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

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

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

CREATE INDEX [Ik_Key1] ON [dbo].[ut_Postleitzahlen_Keys]
( [Key1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_First_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Last_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Next_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Postleitzahlen_Key1]
GO

CREATE PROCEDURE [dbo].[up_First_Postleitzahlen_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys] ORDER BY [Key1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Postleitzahlen_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys] ORDER BY [Key1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Postleitzahlen_Key1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Postleitzahlen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Postleitzahlen_Key1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Postleitzahlen_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Postleitzahlen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

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

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

