-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, 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_SkrMap')
BEGIN
CREATE TABLE [dbo].[ut_SkrMap]
(
  [RecId]  integer NOT NULL
 ,[SKR03] char(8) DEFAULT ''
 ,[SKR04] char(8) DEFAULT ''
 ,[Bez03] char(50) DEFAULT ''
 ,[Bez04] char(50) DEFAULT ''
 ,[Bilanz] char(1) DEFAULT ''
 ,[KzKasse] char(1) DEFAULT ''
 ,[UStSchl] char(3) DEFAULT ''
  CONSTRAINT [Pk_SkrMap]  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_SkrMap_KeyDel')
  DROP TRIGGER [dbo].[tr_SkrMap_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_SkrMap_KeyUpd')
  DROP TRIGGER [dbo].[tr_SkrMap_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_SkrMap_Keys')
  DROP TABLE [dbo].[ut_SkrMap_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_SkrMap' and sc.[name] = 'SKR03')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [SKR03] 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_SkrMap' and sc.[name] = 'SKR04')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [SKR04] 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_SkrMap' and sc.[name] = 'Bez03')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [Bez03] char(50) 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_SkrMap' and sc.[name] = 'Bez04')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [Bez04] char(50) 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_SkrMap' and sc.[name] = 'Bilanz')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [Bilanz] char(1) 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_SkrMap' and sc.[name] = 'KzKasse')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [KzKasse] char(1) 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_SkrMap' and sc.[name] = 'UStSchl')
BEGIN
  ALTER TABLE [dbo].[ut_SkrMap] ADD [UStSchl] char(3) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'SKR03')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [SKR03] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'SKR04')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [SKR04] char(8)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'Bez03')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [Bez03] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'Bez04')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [Bez04] char(50)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'Bilanz')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [Bilanz] char(1)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'KzKasse')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [KzKasse] char(1)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_SkrMap' and sc.[name] = 'UStSchl')
BEGIN
   ALTER TABLE [dbo].[ut_SkrMap] ALTER COLUMN [UStSchl] char(3)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'SkrMap')
  DROP VIEW [dbo].[SkrMap]
GO
CREATE VIEW [dbo].[SkrMap] AS
  SELECT [RecId]
 ,[SKR03]
 ,[SKR04]
 ,[Bez03]
 ,[Bez04]
 ,[Bilanz]
 ,[KzKasse]
 ,[UStSchl]
  FROM [dbo].[ut_SkrMap]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_SkrMap')
  DROP PROCEDURE [dbo].[up_Sel_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Sel_SkrMap]  @mRI integer
AS
  SELECT
    [SKR03]
   ,[SKR04]
   ,[Bez03]
   ,[Bez04]
   ,[Bilanz]
   ,[KzKasse]
   ,[UStSchl]
  FROM [dbo].[ut_SkrMap] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_SkrMap')
  DROP PROCEDURE [dbo].[up_Append_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Append_SkrMap]
   @Param_SKR03 char(8)
  ,@Param_SKR04 char(8)
  ,@Param_Bez03 char(50)
  ,@Param_Bez04 char(50)
  ,@Param_Bilanz char(1)
  ,@Param_KzKasse char(1)
  ,@Param_UStSchl char(3)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_SkrMap()
      INSERT INTO [dbo].[ut_SkrMap] (RecId
        ,[SKR03]
        ,[SKR04]
        ,[Bez03]
        ,[Bez04]
        ,[Bilanz]
        ,[KzKasse]
        ,[UStSchl]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_SKR03))
        ,UPPER(RTRIM(@Param_SKR04))
        ,RTRIM(@Param_Bez03)
        ,RTRIM(@Param_Bez04)
        ,UPPER(RTRIM(@Param_Bilanz))
        ,UPPER(RTRIM(@Param_KzKasse))
        ,UPPER(RTRIM(@Param_UStSchl))
          )
      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_SkrMap')
  DROP PROCEDURE [dbo].[up_Add_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Add_SkrMap]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_SkrMap()
  INSERT INTO [dbo].[ut_SkrMap] (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_SkrMap')
  DROP PROCEDURE [dbo].[up_Upd_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Upd_SkrMap] @mRI integer
  ,@Param_SKR03 char(8)
  ,@Param_SKR04 char(8)
  ,@Param_Bez03 char(50)
  ,@Param_Bez04 char(50)
  ,@Param_Bilanz char(1)
  ,@Param_KzKasse char(1)
  ,@Param_UStSchl char(3)
AS
  UPDATE [dbo].[ut_SkrMap] SET
    [SKR03] = UPPER(RTRIM(@Param_SKR03))
   ,[SKR04] = UPPER(RTRIM(@Param_SKR04))
   ,[Bez03] = RTRIM(@Param_Bez03)
   ,[Bez04] = RTRIM(@Param_Bez04)
   ,[Bilanz] = UPPER(RTRIM(@Param_Bilanz))
   ,[KzKasse] = UPPER(RTRIM(@Param_KzKasse))
   ,[UStSchl] = UPPER(RTRIM(@Param_UStSchl))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_SkrMap')
  DROP PROCEDURE [dbo].[up_Upd2_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Upd2_SkrMap] @Param_RecId varchar(20)
  ,@Param_SKR03 varchar(8)
  ,@Param_SKR04 varchar(8)
  ,@Param_Bez03 varchar(50)
  ,@Param_Bez04 varchar(50)
  ,@Param_Bilanz varchar(1)
  ,@Param_KzKasse varchar(1)
  ,@Param_UStSchl varchar(3)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_SkrMap] SET
    [SKR03] = UPPER(RTRIM(@Param_SKR03))
   ,[SKR04] = UPPER(RTRIM(@Param_SKR04))
   ,[Bez03] = RTRIM(@Param_Bez03)
   ,[Bez04] = RTRIM(@Param_Bez04)
   ,[Bilanz] = UPPER(RTRIM(@Param_Bilanz))
   ,[KzKasse] = UPPER(RTRIM(@Param_KzKasse))
   ,[UStSchl] = UPPER(RTRIM(@Param_UStSchl))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_SkrMap] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_SkrMap')
  DROP PROCEDURE [dbo].[up_Del_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Del_SkrMap] @mRI integer
AS
  DELETE FROM [dbo].[ut_SkrMap] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_SkrMap')
  DROP PROCEDURE [dbo].[up_TrSel_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_TrSel_SkrMap]
AS
  SELECT RecId,
    LTRIM(RTRIM([SKR03]))
   +LTRIM(RTRIM([SKR04]))
   +LTRIM(RTRIM([Bez03]))
   +LTRIM(RTRIM([Bez04]))
   +LTRIM(RTRIM([Bilanz]))
   +LTRIM(RTRIM([KzKasse]))
   +LTRIM(RTRIM([UStSchl]))
  AS Data FROM [dbo].[ut_SkrMap] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_SkrMap')
  DROP PROCEDURE [dbo].[up_Reorg_SkrMap]
GO
CREATE PROCEDURE [dbo].[up_Reorg_SkrMap] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_SkrMap] 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_SkrMap] 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_SkrMap] )
  END
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_First_SkrMap_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_SkrMap_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00) ORDER BY SKR03,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_SkrMap_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_SkrMap_Key00] AS EXECUTE [dbo].[up_First_SkrMap_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_Last_SkrMap_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_SkrMap_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00) ORDER BY SKR03 desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_SkrMap_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_SkrMap_Key00] AS EXECUTE [dbo].[up_Last_SkrMap_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_Next_SkrMap_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_SkrMap_Key00] @oldRecId integer AS
  DECLARE @oldSKR03 char(8)
  SELECT @oldSKR03 = SKR03 FROM [dbo].[ut_SkrMap] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00)
    WHERE (SKR03 = @oldSKR03) AND (RecId > @oldRecId)
    ORDER BY SKR03,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00)
    WHERE (SKR03 > @oldSKR03)
    ORDER BY SKR03,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_SkrMap_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_SkrMap_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_SkrMap_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_Prior_SkrMap_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_SkrMap_Key00] @oldRecId integer AS
  DECLARE @oldSKR03 char(8)
  SELECT @oldSKR03 = SKR03 FROM [dbo].[ut_SkrMap] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00)
    WHERE (SKR03 < @oldSKR03)
    ORDER BY SKR03 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_SkrMap_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_SkrMap_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_SkrMap_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_Next50_SkrMap_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_SkrMap_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_SkrMap_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_SkrMap_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_SkrMap_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_SkrMap_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_SkrMap_Key00]
GO

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00) WHERE 
      SKR03 = @mSKR03
      ORDER BY SKR03,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00) WHERE 
      SKR03 >= @mSKR03 AND SUBSTRING(SKR03,1,LEN(@mSKR03)) = @mSKR03
      ORDER BY SKR03,RecId
    RETURN
  END

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key00) WHERE 
      SKR03 = @mSKR03
      ORDER BY SKR03 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_SkrMap] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(SKR03,1,LEN(@mSKR03)) = @mSKR03
      ORDER BY SKR03 desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_First_SkrMap_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_SkrMap_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01) ORDER BY SKR04,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_SkrMap_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_SkrMap_Key01] AS EXECUTE [dbo].[up_First_SkrMap_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_Last_SkrMap_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_SkrMap_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01) ORDER BY SKR04 desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_SkrMap_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_SkrMap_Key01] AS EXECUTE [dbo].[up_Last_SkrMap_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_Next_SkrMap_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_SkrMap_Key01] @oldRecId integer AS
  DECLARE @oldSKR04 char(8)
  SELECT @oldSKR04 = SKR04 FROM [dbo].[ut_SkrMap] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01)
    WHERE (SKR04 = @oldSKR04) AND (RecId > @oldRecId)
    ORDER BY SKR04,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01)
    WHERE (SKR04 > @oldSKR04)
    ORDER BY SKR04,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_SkrMap_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_SkrMap_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_SkrMap_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_Prior_SkrMap_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_SkrMap_Key01] @oldRecId integer AS
  DECLARE @oldSKR04 char(8)
  SELECT @oldSKR04 = SKR04 FROM [dbo].[ut_SkrMap] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01)
    WHERE (SKR04 < @oldSKR04)
    ORDER BY SKR04 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_SkrMap_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_SkrMap_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_SkrMap_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_Next50_SkrMap_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_SkrMap_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_SkrMap_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_SkrMap_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_SkrMap_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_SkrMap_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_SkrMap_Key01]
GO

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01) WHERE 
      SKR04 = @mSKR04
      ORDER BY SKR04,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01) WHERE 
      SKR04 >= @mSKR04 AND SUBSTRING(SKR04,1,LEN(@mSKR04)) = @mSKR04
      ORDER BY SKR04,RecId
    RETURN
  END

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_SkrMap] WITH(INDEX = Ik_Key01) WHERE 
      SKR04 = @mSKR04
      ORDER BY SKR04 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_SkrMap] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(SKR04,1,LEN(@mSKR04)) = @mSKR04
      ORDER BY SKR04 desc,RecId desc
    RETURN
  END

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


