-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, 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_Kalkulationsgruppen')
BEGIN
CREATE TABLE [dbo].[ut_Kalkulationsgruppen]
(
  [RecId]  integer NOT NULL
 ,[Nr] char(2) DEFAULT ''
 ,[VhMat] float DEFAULT 0
 ,[VhLohn] float DEFAULT 0
 ,[VhFremd] float DEFAULT 0
 ,[VhNe] float DEFAULT 0
 ,[VhSonst] float DEFAULT 0
 ,[Name] char(20) DEFAULT ''
 ,[LohnAnt1] float DEFAULT 0
 ,[LohnAnt2] float DEFAULT 0
  CONSTRAINT [Pk_Kalkulationsgruppen]  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_Kalkulationsgruppen_KeyDel')
  DROP TRIGGER [dbo].[tr_Kalkulationsgruppen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Kalkulationsgruppen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Kalkulationsgruppen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Kalkulationsgruppen_Keys')
  DROP TABLE [dbo].[ut_Kalkulationsgruppen_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_Kalkulationsgruppen' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [Nr] 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_Kalkulationsgruppen' and sc.[name] = 'VhMat')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [VhMat] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'VhLohn')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [VhLohn] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'VhFremd')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [VhFremd] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'VhNe')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [VhNe] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'VhSonst')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [VhSonst] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Name')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [Name] char(20) 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_Kalkulationsgruppen' and sc.[name] = 'LohnAnt1')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [LohnAnt1] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'LohnAnt2')
BEGIN
  ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ADD [LohnAnt2] float NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Nr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ALTER COLUMN [Nr] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Nr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ALTER COLUMN [Nr] varchar(2)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Name'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Name'))
BEGIN
   ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ALTER COLUMN [Name] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Name'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Kalkulationsgruppen' and sc.[name] = 'Name'))
BEGIN
   ALTER TABLE [dbo].[ut_Kalkulationsgruppen] ALTER COLUMN [Name] varchar(20)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Kalkulationsgruppen')
  DROP VIEW [dbo].[Kalkulationsgruppen]
GO
CREATE VIEW [dbo].[Kalkulationsgruppen] AS
  SELECT [RecId]
 ,[Nr]
 ,[VhMat]
 ,[VhLohn]
 ,[VhFremd]
 ,[VhNe]
 ,[VhSonst]
 ,[Name]
 ,[LohnAnt1]
 ,[LohnAnt2]
  FROM [dbo].[ut_Kalkulationsgruppen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_Sel_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Kalkulationsgruppen]  @mRI integer
AS
  SELECT
    [Nr]
   ,[VhMat]
   ,[VhLohn]
   ,[VhFremd]
   ,[VhNe]
   ,[VhSonst]
   ,[Name]
   ,[LohnAnt1]
   ,[LohnAnt2]
  FROM [dbo].[ut_Kalkulationsgruppen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_Append_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_Append_Kalkulationsgruppen]
   @Param_Nr char(2)
  ,@Param_VhMat float
  ,@Param_VhLohn float
  ,@Param_VhFremd float
  ,@Param_VhNe float
  ,@Param_VhSonst float
  ,@Param_Name char(20)
  ,@Param_LohnAnt1 float
  ,@Param_LohnAnt2 float
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Kalkulationsgruppen()
      INSERT INTO [dbo].[ut_Kalkulationsgruppen] (RecId
        ,[Nr]
        ,[VhMat]
        ,[VhLohn]
        ,[VhFremd]
        ,[VhNe]
        ,[VhSonst]
        ,[Name]
        ,[LohnAnt1]
        ,[LohnAnt2]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Nr))
        ,@Param_VhMat
        ,@Param_VhLohn
        ,@Param_VhFremd
        ,@Param_VhNe
        ,@Param_VhSonst
        ,RTRIM(@Param_Name)
        ,@Param_LohnAnt1
        ,@Param_LohnAnt2
          )
      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_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_Add_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_Add_Kalkulationsgruppen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Kalkulationsgruppen()
  INSERT INTO [dbo].[ut_Kalkulationsgruppen] (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_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_Upd_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Kalkulationsgruppen] @mRI integer
  ,@Param_Nr char(2)
  ,@Param_VhMat float
  ,@Param_VhLohn float
  ,@Param_VhFremd float
  ,@Param_VhNe float
  ,@Param_VhSonst float
  ,@Param_Name char(20)
  ,@Param_LohnAnt1 float
  ,@Param_LohnAnt2 float
AS
  UPDATE [dbo].[ut_Kalkulationsgruppen] SET
    [Nr] = UPPER(RTRIM(@Param_Nr))
   ,[VhMat] = @Param_VhMat
   ,[VhLohn] = @Param_VhLohn
   ,[VhFremd] = @Param_VhFremd
   ,[VhNe] = @Param_VhNe
   ,[VhSonst] = @Param_VhSonst
   ,[Name] = RTRIM(@Param_Name)
   ,[LohnAnt1] = @Param_LohnAnt1
   ,[LohnAnt2] = @Param_LohnAnt2
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_Upd2_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Kalkulationsgruppen] @Param_RecId varchar(20)
  ,@Param_Nr varchar(2)
  ,@Param_VhMat varchar(6)
  ,@Param_VhLohn varchar(6)
  ,@Param_VhFremd varchar(6)
  ,@Param_VhNe varchar(6)
  ,@Param_VhSonst varchar(6)
  ,@Param_Name varchar(20)
  ,@Param_LohnAnt1 varchar(6)
  ,@Param_LohnAnt2 varchar(6)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Kalkulationsgruppen] SET
    [Nr] = UPPER(RTRIM(@Param_Nr))
   ,[VhMat] = dbo.itcfunc_float(@Param_VhMat)
   ,[VhLohn] = dbo.itcfunc_float(@Param_VhLohn)
   ,[VhFremd] = dbo.itcfunc_float(@Param_VhFremd)
   ,[VhNe] = dbo.itcfunc_float(@Param_VhNe)
   ,[VhSonst] = dbo.itcfunc_float(@Param_VhSonst)
   ,[Name] = RTRIM(@Param_Name)
   ,[LohnAnt1] = dbo.itcfunc_float(@Param_LohnAnt1)
   ,[LohnAnt2] = dbo.itcfunc_float(@Param_LohnAnt2)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Kalkulationsgruppen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Kalkulationsgruppen')
  DROP PROCEDURE [dbo].[up_TrSel_Kalkulationsgruppen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Kalkulationsgruppen]
AS
  SELECT RecId,
    LTRIM(RTRIM([Nr]))
   +LTRIM(RTRIM([Name]))
  AS Data FROM [dbo].[ut_Kalkulationsgruppen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Kalkulationsgruppen_Primary')
  DROP PROCEDURE [dbo].[up_First_Kalkulationsgruppen_Primary]
GO
CREATE PROCEDURE [dbo].[up_First_Kalkulationsgruppen_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) ORDER BY Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Kalkulationsgruppen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Kalkulationsgruppen_Primary] AS EXECUTE [dbo].[up_First_Kalkulationsgruppen_Primary]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Kalkulationsgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Last_Kalkulationsgruppen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Last_Kalkulationsgruppen_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) ORDER BY Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Kalkulationsgruppen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Kalkulationsgruppen_Primary] AS EXECUTE [dbo].[up_Last_Kalkulationsgruppen_Primary]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Kalkulationsgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Next_Kalkulationsgruppen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Next_Kalkulationsgruppen_Primary] @oldRecId integer AS
  DECLARE @oldNr char(2)
  SELECT @oldNr = Nr FROM [dbo].[ut_Kalkulationsgruppen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_Kalkulationsgruppen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Kalkulationsgruppen_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Next_Kalkulationsgruppen_Primary] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Kalkulationsgruppen_Primary')
  DROP PROCEDURE [dbo].[up_Prior_Kalkulationsgruppen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Prior_Kalkulationsgruppen_Primary] @oldRecId integer AS
  DECLARE @oldNr char(2)
  SELECT @oldNr = Nr FROM [dbo].[ut_Kalkulationsgruppen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_Kalkulationsgruppen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Kalkulationsgruppen_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Kalkulationsgruppen_Primary] @oldRecId')
GO

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

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) WHERE 
      Nr = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) WHERE 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) WHERE 
      Nr = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Kalkulationsgruppen] WITH(INDEX = Ik_Primary) WHERE 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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


