-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

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
 ,[Plz] char(8) DEFAULT ''
 ,[PlzSub] char(2) DEFAULT ''
 ,[Ort] char(40) DEFAULT ''
  CONSTRAINT [Pk_Postleitzahlen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
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

-- add new fields
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

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Plz'))
  AND (175 = (select sc.xtype 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 (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Plz'))
  AND (167 = (select sc.xtype 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] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'PlzSub'))
  AND (175 = (select sc.xtype 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 (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'PlzSub'))
  AND (167 = (select sc.xtype 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] varchar(2)
END
GO
IF (40 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Ort'))
  AND (175 = (select sc.xtype 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 (40 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Postleitzahlen' and sc.[name] = 'Ort'))
  AND (167 = (select sc.xtype 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] varchar(40)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Postleitzahlen')
  DROP VIEW [dbo].[Postleitzahlen]
GO
CREATE VIEW [dbo].[Postleitzahlen] AS
  SELECT [RecId]
 ,[Plz]
 ,[PlzSub]
 ,[Ort]
  FROM [dbo].[ut_Postleitzahlen]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_Postleitzahlen', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_Postleitzahlen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Postleitzahlen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Postleitzahlen] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
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]  @mRI integer
AS
  SELECT
    [Plz]
   ,[PlzSub]
   ,[Ort]
  FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Sel2_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Sel2_Postleitzahlen]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[Postleitzahlen] WHERE RecId = @mRI
GO

-- insert with classic client
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]
   @Param_Plz char(8)
  ,@Param_PlzSub char(2)
  ,@Param_Ort char(40)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Postleitzahlen()
      INSERT INTO [dbo].[ut_Postleitzahlen] (RecId
        ,[Plz]
        ,[PlzSub]
        ,[Ort]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Plz))
        ,UPPER(RTRIM(@Param_PlzSub))
        ,RTRIM(@Param_Ort)
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
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 = dbo.itcfunc_NextRecId_Postleitzahlen()
  INSERT INTO [dbo].[ut_Postleitzahlen] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
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] @mRI integer
  ,@Param_Plz char(8)
  ,@Param_PlzSub char(2)
  ,@Param_Ort char(40)
AS
  UPDATE [dbo].[ut_Postleitzahlen] SET
    [Plz] = UPPER(RTRIM(@Param_Plz))
   ,[PlzSub] = UPPER(RTRIM(@Param_PlzSub))
   ,[Ort] = RTRIM(@Param_Ort)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Postleitzahlen')
  DROP PROCEDURE [dbo].[up_Upd2_Postleitzahlen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Postleitzahlen] @Param_RecId varchar(20)
  ,@Param_Plz varchar(8)
  ,@Param_PlzSub varchar(2)
  ,@Param_Ort varchar(40)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Postleitzahlen] SET
    [Plz] = UPPER(RTRIM(@Param_Plz))
   ,[PlzSub] = UPPER(RTRIM(@Param_PlzSub))
   ,[Ort] = RTRIM(@Param_Ort)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Postleitzahlen] @mRI
GO

-- delete
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] @mRI integer
AS
  DELETE FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @mRI
GO

-- text index
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

-- reindex
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

-- logical key  ORDER BY Plz,PlzSub,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Postleitzahlen' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  DROP INDEX [Ik_Key0] ON [dbo].[ut_Postleitzahlen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Postleitzahlen' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  CREATE INDEX [Ik_Key0] ON [dbo].[ut_Postleitzahlen] (Plz,PlzSub,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_First_Postleitzahlen_Key0]
GO
CREATE PROCEDURE [dbo].[up_First_Postleitzahlen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) ORDER BY Plz,PlzSub,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Postleitzahlen_Key0] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_Postleitzahlen_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) ORDER BY Plz desc,PlzSub desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Postleitzahlen_Key0] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_Postleitzahlen_Key0] @oldRecId integer AS
  DECLARE @oldPlz char(8)
  DECLARE @oldPlzSub char(2)
  SELECT @oldPlz = Plz, @oldPlzSub = PlzSub FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz = @oldPlz) AND (PlzSub = @oldPlzSub) AND (RecId > @oldRecId)
    ORDER BY Plz,PlzSub,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz = @oldPlz) AND (PlzSub > @oldPlzSub)
    ORDER BY Plz,PlzSub,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz > @oldPlz)
    ORDER BY Plz,PlzSub,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Postleitzahlen_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_Postleitzahlen_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key0] @oldRecId integer AS
  DECLARE @oldPlz char(8)
  DECLARE @oldPlzSub char(2)
  SELECT @oldPlz = Plz, @oldPlzSub = PlzSub FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz = @oldPlz) AND (PlzSub = @oldPlzSub) AND (RecId < @oldRecId)
    ORDER BY Plz desc,PlzSub desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz = @oldPlz) AND (PlzSub < @oldPlzSub)
    ORDER BY Plz desc,PlzSub desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0)
    WHERE (Plz < @oldPlz)
    ORDER BY Plz desc,PlzSub desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Postleitzahlen_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Postleitzahlen_Key0] @oldRecId')
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] = 'qp_Next50_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_Postleitzahlen_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mPlz char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mPlzSub char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz AND 
      PlzSub = @mPlzSub
      ORDER BY Plz,PlzSub,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz
      ORDER BY Plz,PlzSub,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz >= @mPlz AND SUBSTRING(Plz,1,LEN(@mPlz)) = @mPlz
      ORDER BY Plz,PlzSub,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz AND 
      PlzSub >= @mPlzSub AND SUBSTRING(PlzSub,1,LEN(@mPlzSub)) = @mPlzSub
      ORDER BY Plz,PlzSub,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Postleitzahlen_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Postleitzahlen_Key0] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Postleitzahlen_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mPlz char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mPlzSub char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz AND 
      PlzSub = @mPlzSub
      ORDER BY Plz desc,PlzSub desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz
      ORDER BY Plz desc,PlzSub desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(Plz,1,LEN(@mPlz)) = @mPlz
      ORDER BY Plz desc,PlzSub desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key0) WHERE 
      Plz = @mPlz AND 
      SUBSTRING(PlzSub,1,LEN(@mPlzSub)) = @mPlzSub
      ORDER BY Plz desc,PlzSub desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Postleitzahlen_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Postleitzahlen_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Postleitzahlen_Key0] @xKey, @xLen')
GO


-- logical key  ORDER BY Ort,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Postleitzahlen' and so.[id] = si.[id] and si.[name] = 'Ik_Key1')
  DROP INDEX [Ik_Key1] ON [dbo].[ut_Postleitzahlen]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Postleitzahlen' and so.[id] = si.[id] and si.[name] = 'Ik_Key1')
  CREATE INDEX [Ik_Key1] ON [dbo].[ut_Postleitzahlen] (Ort,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_First_Postleitzahlen_Key1]
GO
CREATE PROCEDURE [dbo].[up_First_Postleitzahlen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) ORDER BY Ort,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Postleitzahlen_Key1] AS EXECUTE [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
CREATE PROCEDURE [dbo].[up_Last_Postleitzahlen_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) ORDER BY Ort desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Postleitzahlen_Key1] AS EXECUTE [dbo].[up_Last_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
CREATE PROCEDURE [dbo].[up_Next_Postleitzahlen_Key1] @oldRecId integer AS
  DECLARE @oldOrt char(40)
  SELECT @oldOrt = Ort FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1)
    WHERE (Ort = @oldOrt) AND (RecId > @oldRecId)
    ORDER BY Ort,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1)
    WHERE (Ort > @oldOrt)
    ORDER BY Ort,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Postleitzahlen_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Next_Postleitzahlen_Key1] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key1]
GO
CREATE PROCEDURE [dbo].[up_Prior_Postleitzahlen_Key1] @oldRecId integer AS
  DECLARE @oldOrt char(40)
  SELECT @oldOrt = Ort FROM [dbo].[ut_Postleitzahlen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1)
    WHERE (Ort = @oldOrt) AND (RecId < @oldRecId)
    ORDER BY Ort desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1)
    WHERE (Ort < @oldOrt)
    ORDER BY Ort desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Postleitzahlen_Key1] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Postleitzahlen_Key1] @oldRecId')
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] = 'qp_Next50_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[qp_Next50_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] = 'qp_Prior50_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[qp_Prior50_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_FindF_Postleitzahlen_Key1] @xKey char(250), @xLen integer AS
  DECLARE @mOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,40)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) WHERE 
      Ort = @mOrt
      ORDER BY Ort,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) WHERE 
      Ort >= @mOrt AND SUBSTRING(Ort,1,LEN(@mOrt)) = @mOrt
      ORDER BY Ort,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Postleitzahlen_Key1] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Postleitzahlen_Key1] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Postleitzahlen_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key1]
GO
CREATE PROCEDURE [dbo].[up_FindL_Postleitzahlen_Key1] @xKey char(250), @xLen integer AS
  DECLARE @mOrt char(40) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,40)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) WHERE 
      Ort = @mOrt
      ORDER BY Ort desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Postleitzahlen] WITH(INDEX = Ik_Key1) WHERE 
      SUBSTRING(Ort,1,LEN(@mOrt)) = @mOrt
      ORDER BY Ort desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Postleitzahlen_Key1')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Postleitzahlen_Key1] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Postleitzahlen_Key1] @xKey, @xLen')
GO


