-- 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_WartungsPosten')
BEGIN
CREATE TABLE [dbo].[ut_WartungsPosten]
(
  [RecId]  integer NOT NULL
 ,[AuftragId] integer DEFAULT 0
 ,[AnlagenId] integer DEFAULT 0
 ,[LfdNr] integer DEFAULT 0
 ,[Grund] integer DEFAULT 0
 ,[SammelRng] tinyint DEFAULT 0
 ,[Berechnet] tinyint DEFAULT 0
 ,[Menge] float DEFAULT 0
 ,[Einheit] integer DEFAULT 0
 ,[Lkz] char(2) DEFAULT ''
 ,[ArtNr] char(18) DEFAULT ''
 ,[Bez1] char(42) DEFAULT ''
 ,[Bez2] char(42) DEFAULT ''
 ,[Merkmal] char(42) DEFAULT ''
 ,[LohnJN] tinyint DEFAULT 0
 ,[Minuten] float DEFAULT 0
 ,[VkLohn] float DEFAULT 0
 ,[MatJN] tinyint DEFAULT 0
 ,[EkMat] float DEFAULT 0
 ,[VkMat] float DEFAULT 0
 ,[RabattVH] float DEFAULT 0
 ,[RabattEUR] float DEFAULT 0
 ,[Pauschal] integer DEFAULT 0
 ,[Check] tinyint DEFAULT 0
 ,[MfrAdded] tinyint DEFAULT 0
 ,[MfrDone] tinyint DEFAULT 0
 ,[MfrFail] tinyint DEFAULT 0
 ,[MfrType] integer DEFAULT 0
 ,[ZeitAdded] tinyint DEFAULT 0
 ,[Annr] char(4) DEFAULT ''
 ,[Lohnart] char(3) DEFAULT ''
 ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  CONSTRAINT [Pk_WartungsPosten]  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_WartungsPosten_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsPosten_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsPosten_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsPosten_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsPosten_Keys')
  DROP TABLE [dbo].[ut_WartungsPosten_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_WartungsPosten' and sc.[name] = 'AuftragId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [AuftragId] integer 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_WartungsPosten' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [AnlagenId] integer 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_WartungsPosten' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [LfdNr] integer 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_WartungsPosten' and sc.[name] = 'Grund')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Grund] integer 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_WartungsPosten' and sc.[name] = 'SammelRng')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [SammelRng] tinyint 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_WartungsPosten' and sc.[name] = 'Berechnet')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Berechnet] tinyint 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_WartungsPosten' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Menge] 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_WartungsPosten' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Einheit] integer 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_WartungsPosten' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Lkz] 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_WartungsPosten' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [ArtNr] char(18) 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_WartungsPosten' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Bez1] char(42) 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_WartungsPosten' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Bez2] char(42) 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_WartungsPosten' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Merkmal] char(42) 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_WartungsPosten' and sc.[name] = 'LohnJN')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [LohnJN] tinyint 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_WartungsPosten' and sc.[name] = 'Minuten')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Minuten] 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_WartungsPosten' and sc.[name] = 'VkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [VkLohn] 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_WartungsPosten' and sc.[name] = 'MatJN')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [MatJN] tinyint 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_WartungsPosten' and sc.[name] = 'EkMat')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [EkMat] 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_WartungsPosten' and sc.[name] = 'VkMat')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [VkMat] 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_WartungsPosten' and sc.[name] = 'RabattVH')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [RabattVH] 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_WartungsPosten' and sc.[name] = 'RabattEUR')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [RabattEUR] 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_WartungsPosten' and sc.[name] = 'Pauschal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Pauschal] integer 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_WartungsPosten' and sc.[name] = 'Check')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Check] tinyint 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_WartungsPosten' and sc.[name] = 'MfrAdded')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [MfrAdded] tinyint 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_WartungsPosten' and sc.[name] = 'MfrDone')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [MfrDone] tinyint 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_WartungsPosten' and sc.[name] = 'MfrFail')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [MfrFail] tinyint 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_WartungsPosten' and sc.[name] = 'MfrType')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [MfrType] integer 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_WartungsPosten' and sc.[name] = 'ZeitAdded')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [ZeitAdded] tinyint 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_WartungsPosten' and sc.[name] = 'Annr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Annr] char(4) 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_WartungsPosten' and sc.[name] = 'Lohnart')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Lohnart] char(3) 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_WartungsPosten' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsPosten] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
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_WartungsPosten' and sc.[name] = 'Lkz'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Lkz] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lkz'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Lkz] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'ArtNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [ArtNr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'ArtNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'ArtNr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [ArtNr] varchar(18)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Bez1] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez1'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Bez1] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Bez2] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Bez2'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Bez2] varchar(42)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Merkmal'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Merkmal] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Merkmal'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Merkmal'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Merkmal] varchar(42)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Annr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Annr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Annr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Annr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Annr'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Annr] varchar(4)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lohnart'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lohnart'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Lohnart] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lohnart'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_WartungsPosten' and sc.[name] = 'Lohnart'))
BEGIN
   ALTER TABLE [dbo].[ut_WartungsPosten] ALTER COLUMN [Lohnart] varchar(3)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsPosten')
  DROP VIEW [dbo].[WartungsPosten]
GO
CREATE VIEW [dbo].[WartungsPosten] AS
  SELECT [RecId]
 ,[AuftragId]
 ,[AnlagenId]
 ,[LfdNr]
 ,[Grund]
 ,[SammelRng]
 ,[Berechnet]
 ,[Menge]
 ,[Einheit]
 ,[Lkz]
 ,[ArtNr]
 ,[Bez1]
 ,[Bez2]
 ,[Merkmal]
 ,[LohnJN]
 ,[Minuten]
 ,[VkLohn]
 ,[MatJN]
 ,[EkMat]
 ,[VkMat]
 ,[RabattVH]
 ,[RabattEUR]
 ,[Pauschal]
 ,[Check]
 ,[MfrAdded]
 ,[MfrDone]
 ,[MfrFail]
 ,[MfrType]
 ,[ZeitAdded]
 ,[Annr]
 ,[Lohnart]
 ,[Datum]
  FROM [dbo].[ut_WartungsPosten]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsPosten')
  DROP PROCEDURE [dbo].[up_Sel_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsPosten]  @mRI integer
AS
  SELECT
    [AuftragId]
   ,[AnlagenId]
   ,[LfdNr]
   ,[Grund]
   ,[SammelRng]
   ,[Berechnet]
   ,[Menge]
   ,[Einheit]
   ,[Lkz]
   ,[ArtNr]
   ,[Bez1]
   ,[Bez2]
   ,[Merkmal]
   ,[LohnJN]
   ,[Minuten]
   ,[VkLohn]
   ,[MatJN]
   ,[EkMat]
   ,[VkMat]
   ,[RabattVH]
   ,[RabattEUR]
   ,[Pauschal]
   ,[Check]
   ,[MfrAdded]
   ,[MfrDone]
   ,[MfrFail]
   ,[MfrType]
   ,[ZeitAdded]
   ,[Annr]
   ,[Lohnart]
   ,[Datum]
  FROM [dbo].[ut_WartungsPosten] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsPosten')
  DROP PROCEDURE [dbo].[up_Append_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsPosten]
   @Param_AuftragId integer
  ,@Param_AnlagenId integer
  ,@Param_LfdNr integer
  ,@Param_Grund integer
  ,@Param_SammelRng tinyint
  ,@Param_Berechnet tinyint
  ,@Param_Menge float
  ,@Param_Einheit integer
  ,@Param_Lkz char(2)
  ,@Param_ArtNr char(18)
  ,@Param_Bez1 char(42)
  ,@Param_Bez2 char(42)
  ,@Param_Merkmal char(42)
  ,@Param_LohnJN tinyint
  ,@Param_Minuten float
  ,@Param_VkLohn float
  ,@Param_MatJN tinyint
  ,@Param_EkMat float
  ,@Param_VkMat float
  ,@Param_RabattVH float
  ,@Param_RabattEUR float
  ,@Param_Pauschal integer
  ,@Param_Check tinyint
  ,@Param_MfrAdded tinyint
  ,@Param_MfrDone tinyint
  ,@Param_MfrFail tinyint
  ,@Param_MfrType integer
  ,@Param_ZeitAdded tinyint
  ,@Param_Annr char(4)
  ,@Param_Lohnart char(3)
  ,@Param_Datum datetime
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsPosten()
      INSERT INTO [dbo].[ut_WartungsPosten] (RecId
        ,[AuftragId]
        ,[AnlagenId]
        ,[LfdNr]
        ,[Grund]
        ,[SammelRng]
        ,[Berechnet]
        ,[Menge]
        ,[Einheit]
        ,[Lkz]
        ,[ArtNr]
        ,[Bez1]
        ,[Bez2]
        ,[Merkmal]
        ,[LohnJN]
        ,[Minuten]
        ,[VkLohn]
        ,[MatJN]
        ,[EkMat]
        ,[VkMat]
        ,[RabattVH]
        ,[RabattEUR]
        ,[Pauschal]
        ,[Check]
        ,[MfrAdded]
        ,[MfrDone]
        ,[MfrFail]
        ,[MfrType]
        ,[ZeitAdded]
        ,[Annr]
        ,[Lohnart]
        ,[Datum]
          ) VALUES (@NeueId
        ,@Param_AuftragId
        ,@Param_AnlagenId
        ,@Param_LfdNr
        ,@Param_Grund
        ,@Param_SammelRng
        ,@Param_Berechnet
        ,@Param_Menge
        ,@Param_Einheit
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_ArtNr))
        ,RTRIM(@Param_Bez1)
        ,RTRIM(@Param_Bez2)
        ,RTRIM(@Param_Merkmal)
        ,@Param_LohnJN
        ,@Param_Minuten
        ,@Param_VkLohn
        ,@Param_MatJN
        ,@Param_EkMat
        ,@Param_VkMat
        ,@Param_RabattVH
        ,@Param_RabattEUR
        ,@Param_Pauschal
        ,@Param_Check
        ,@Param_MfrAdded
        ,@Param_MfrDone
        ,@Param_MfrFail
        ,@Param_MfrType
        ,@Param_ZeitAdded
        ,UPPER(RTRIM(@Param_Annr))
        ,UPPER(RTRIM(@Param_Lohnart))
        ,@Param_Datum
          )
      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_WartungsPosten')
  DROP PROCEDURE [dbo].[up_Add_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsPosten]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsPosten()
  INSERT INTO [dbo].[ut_WartungsPosten] (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_WartungsPosten')
  DROP PROCEDURE [dbo].[up_Upd_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsPosten] @mRI integer
  ,@Param_AuftragId integer
  ,@Param_AnlagenId integer
  ,@Param_LfdNr integer
  ,@Param_Grund integer
  ,@Param_SammelRng tinyint
  ,@Param_Berechnet tinyint
  ,@Param_Menge float
  ,@Param_Einheit integer
  ,@Param_Lkz char(2)
  ,@Param_ArtNr char(18)
  ,@Param_Bez1 char(42)
  ,@Param_Bez2 char(42)
  ,@Param_Merkmal char(42)
  ,@Param_LohnJN tinyint
  ,@Param_Minuten float
  ,@Param_VkLohn float
  ,@Param_MatJN tinyint
  ,@Param_EkMat float
  ,@Param_VkMat float
  ,@Param_RabattVH float
  ,@Param_RabattEUR float
  ,@Param_Pauschal integer
  ,@Param_Check tinyint
  ,@Param_MfrAdded tinyint
  ,@Param_MfrDone tinyint
  ,@Param_MfrFail tinyint
  ,@Param_MfrType integer
  ,@Param_ZeitAdded tinyint
  ,@Param_Annr char(4)
  ,@Param_Lohnart char(3)
  ,@Param_Datum datetime
AS
  UPDATE [dbo].[ut_WartungsPosten] SET
    [AuftragId] = @Param_AuftragId
   ,[AnlagenId] = @Param_AnlagenId
   ,[LfdNr] = @Param_LfdNr
   ,[Grund] = @Param_Grund
   ,[SammelRng] = @Param_SammelRng
   ,[Berechnet] = @Param_Berechnet
   ,[Menge] = @Param_Menge
   ,[Einheit] = @Param_Einheit
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[LohnJN] = @Param_LohnJN
   ,[Minuten] = @Param_Minuten
   ,[VkLohn] = @Param_VkLohn
   ,[MatJN] = @Param_MatJN
   ,[EkMat] = @Param_EkMat
   ,[VkMat] = @Param_VkMat
   ,[RabattVH] = @Param_RabattVH
   ,[RabattEUR] = @Param_RabattEUR
   ,[Pauschal] = @Param_Pauschal
   ,[Check] = @Param_Check
   ,[MfrAdded] = @Param_MfrAdded
   ,[MfrDone] = @Param_MfrDone
   ,[MfrFail] = @Param_MfrFail
   ,[MfrType] = @Param_MfrType
   ,[ZeitAdded] = @Param_ZeitAdded
   ,[Annr] = UPPER(RTRIM(@Param_Annr))
   ,[Lohnart] = UPPER(RTRIM(@Param_Lohnart))
   ,[Datum] = @Param_Datum
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsPosten')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsPosten] @Param_RecId varchar(20)
  ,@Param_AuftragId varchar(8)
  ,@Param_AnlagenId varchar(8)
  ,@Param_LfdNr varchar(4)
  ,@Param_Grund varchar(1)
  ,@Param_SammelRng varchar(1)
  ,@Param_Berechnet varchar(1)
  ,@Param_Menge varchar(10)
  ,@Param_Einheit varchar(4)
  ,@Param_Lkz varchar(2)
  ,@Param_ArtNr varchar(18)
  ,@Param_Bez1 varchar(42)
  ,@Param_Bez2 varchar(42)
  ,@Param_Merkmal varchar(42)
  ,@Param_LohnJN varchar(1)
  ,@Param_Minuten varchar(10)
  ,@Param_VkLohn varchar(10)
  ,@Param_MatJN varchar(1)
  ,@Param_EkMat varchar(10)
  ,@Param_VkMat varchar(10)
  ,@Param_RabattVH varchar(5)
  ,@Param_RabattEUR varchar(10)
  ,@Param_Pauschal varchar(1)
  ,@Param_Check varchar(1)
  ,@Param_MfrAdded varchar(1)
  ,@Param_MfrDone varchar(1)
  ,@Param_MfrFail varchar(1)
  ,@Param_MfrType varchar(1)
  ,@Param_ZeitAdded varchar(1)
  ,@Param_Annr varchar(4)
  ,@Param_Lohnart varchar(3)
  ,@Param_Datum varchar(10)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_WartungsPosten] SET
    [AuftragId] = dbo.itcfunc_integer(@Param_AuftragId)
   ,[AnlagenId] = dbo.itcfunc_integer(@Param_AnlagenId)
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[Grund] = dbo.itcfunc_integer(@Param_Grund)
   ,[SammelRng] = dbo.itcfunc_integer(@Param_SammelRng)
   ,[Berechnet] = dbo.itcfunc_integer(@Param_Berechnet)
   ,[Menge] = dbo.itcfunc_float(@Param_Menge)
   ,[Einheit] = dbo.itcfunc_integer(@Param_Einheit)
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[ArtNr] = UPPER(RTRIM(@Param_ArtNr))
   ,[Bez1] = RTRIM(@Param_Bez1)
   ,[Bez2] = RTRIM(@Param_Bez2)
   ,[Merkmal] = RTRIM(@Param_Merkmal)
   ,[LohnJN] = dbo.itcfunc_integer(@Param_LohnJN)
   ,[Minuten] = dbo.itcfunc_float(@Param_Minuten)
   ,[VkLohn] = dbo.itcfunc_float(@Param_VkLohn)
   ,[MatJN] = dbo.itcfunc_integer(@Param_MatJN)
   ,[EkMat] = dbo.itcfunc_float(@Param_EkMat)
   ,[VkMat] = dbo.itcfunc_float(@Param_VkMat)
   ,[RabattVH] = dbo.itcfunc_float(@Param_RabattVH)
   ,[RabattEUR] = dbo.itcfunc_float(@Param_RabattEUR)
   ,[Pauschal] = dbo.itcfunc_integer(@Param_Pauschal)
   ,[Check] = dbo.itcfunc_integer(@Param_Check)
   ,[MfrAdded] = dbo.itcfunc_integer(@Param_MfrAdded)
   ,[MfrDone] = dbo.itcfunc_integer(@Param_MfrDone)
   ,[MfrFail] = dbo.itcfunc_integer(@Param_MfrFail)
   ,[MfrType] = dbo.itcfunc_integer(@Param_MfrType)
   ,[ZeitAdded] = dbo.itcfunc_integer(@Param_ZeitAdded)
   ,[Annr] = UPPER(RTRIM(@Param_Annr))
   ,[Lohnart] = UPPER(RTRIM(@Param_Lohnart))
   ,[Datum] = dbo.itcfunc_datetime(@Param_Datum)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsPosten] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsPosten')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsPosten]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsPosten]
AS
  SELECT RecId,
    LTRIM(RTRIM([Lkz]))
   +LTRIM(RTRIM([ArtNr]))
   +LTRIM(RTRIM([Bez1]))
   +LTRIM(RTRIM([Bez2]))
   +LTRIM(RTRIM([Merkmal]))
   +LTRIM(RTRIM([Annr]))
   +LTRIM(RTRIM([Lohnart]))
  AS Data FROM [dbo].[ut_WartungsPosten] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPosten_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsPosten_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsPosten_Key00] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId = @oldAuftragId) AND (LfdNr > @oldLfdNr)
    ORDER BY AuftragId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId > @oldAuftragId)
    ORDER BY AuftragId,LfdNr,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_WartungsPosten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsPosten_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsPosten_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPosten_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPosten_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsPosten_Key00] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId = @oldAuftragId) AND (LfdNr < @oldLfdNr)
    ORDER BY AuftragId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00)
    WHERE (AuftragId < @oldAuftragId)
    ORDER BY AuftragId desc,LfdNr 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_WartungsPosten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsPosten_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsPosten_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId,LfdNr,RecId
    RETURN
  END

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



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

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

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key00) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId desc,LfdNr desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPosten_Key01')
  DROP PROCEDURE [dbo].[up_First_WartungsPosten_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsPosten_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) ORDER BY AuftragId,Pauschal,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsPosten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsPosten_Key01] AS EXECUTE [dbo].[up_First_WartungsPosten_Key01]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPosten_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsPosten_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsPosten_Key01] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldPauschal integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldPauschal = Pauschal, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal = @oldPauschal) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY AuftragId,Pauschal,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal = @oldPauschal) AND (LfdNr > @oldLfdNr)
    ORDER BY AuftragId,Pauschal,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal > @oldPauschal)
    ORDER BY AuftragId,Pauschal,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId > @oldAuftragId)
    ORDER BY AuftragId,Pauschal,LfdNr,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_WartungsPosten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsPosten_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsPosten_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPosten_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPosten_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsPosten_Key01] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldPauschal integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldPauschal = Pauschal, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal = @oldPauschal) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal = @oldPauschal) AND (LfdNr < @oldLfdNr)
    ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId = @oldAuftragId) AND (Pauschal < @oldPauschal)
    ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01)
    WHERE (AuftragId < @oldAuftragId)
    ORDER BY AuftragId desc,Pauschal desc,LfdNr 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_WartungsPosten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsPosten_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsPosten_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsPosten_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsPosten_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsPosten_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mPauschal integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,1)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,10,4)

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

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId AND 
      Pauschal = @mPauschal AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId,Pauschal,LfdNr,RecId
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId AND 
      Pauschal = @mPauschal
      ORDER BY AuftragId,Pauschal,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId,Pauschal,LfdNr,RecId
    RETURN
  END

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




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsPosten_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsPosten_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsPosten_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mPauschal integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,9,1)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,10,4)

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

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId AND 
      Pauschal = @mPauschal AND 
      LfdNr = @mLfdNr
      ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId AND 
      Pauschal = @mPauschal
      ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key01) WHERE 
      AuftragId = @mAuftragId
      ORDER BY AuftragId desc,Pauschal desc,LfdNr desc,RecId desc
    RETURN
  END

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




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPosten_Key02')
  DROP PROCEDURE [dbo].[up_First_WartungsPosten_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsPosten_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 1 = MfrAdded ORDER BY AuftragId,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsPosten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsPosten_Key02] AS EXECUTE [dbo].[up_First_WartungsPosten_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsPosten_Key02')
  DROP PROCEDURE [dbo].[up_Last_WartungsPosten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsPosten_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 1 = MfrAdded ORDER BY AuftragId desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsPosten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsPosten_Key02] AS EXECUTE [dbo].[up_Last_WartungsPosten_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPosten_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsPosten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsPosten_Key02] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId = @oldAuftragId) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND (1 = MfrAdded)
    ORDER BY AuftragId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId = @oldAuftragId) AND (LfdNr > @oldLfdNr)) AND (1 = MfrAdded)
    ORDER BY AuftragId,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId > @oldAuftragId)) AND (1 = MfrAdded)
    ORDER BY AuftragId,LfdNr,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_WartungsPosten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsPosten_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsPosten_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPosten_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPosten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsPosten_Key02] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldLfdNr integer
  SELECT @oldAuftragId = AuftragId, @oldLfdNr = LfdNr FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId = @oldAuftragId) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND (1 = MfrAdded)
    ORDER BY AuftragId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId = @oldAuftragId) AND (LfdNr < @oldLfdNr)) AND (1 = MfrAdded)
    ORDER BY AuftragId desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02)
    WHERE ((AuftragId < @oldAuftragId)) AND (1 = MfrAdded)
    ORDER BY AuftragId desc,LfdNr 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_WartungsPosten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsPosten_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsPosten_Key02] @oldRecId')
GO

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

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

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

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 
      AuftragId = @mAuftragId AND 
      LfdNr = @mLfdNr
      AND (1 = MfrAdded)
      ORDER BY AuftragId,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 
      AuftragId = @mAuftragId
      AND (1 = MfrAdded)
      ORDER BY AuftragId,LfdNr,RecId
    RETURN
  END

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



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

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

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 
      AuftragId = @mAuftragId AND 
      LfdNr = @mLfdNr
      AND (1 = MfrAdded)
      ORDER BY AuftragId desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key02) WHERE 
      AuftragId = @mAuftragId
      AND (1 = MfrAdded)
      ORDER BY AuftragId desc,LfdNr desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_First_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_WartungsPosten_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE (1 = ZeitAdded) AND (0 = Berechnet) ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_WartungsPosten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_WartungsPosten_Key03] AS EXECUTE [dbo].[up_First_WartungsPosten_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_Last_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_WartungsPosten_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE (1 = ZeitAdded) AND (0 = Berechnet) ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_WartungsPosten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_WartungsPosten_Key03] AS EXECUTE [dbo].[up_Last_WartungsPosten_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_Next_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_WartungsPosten_Key03] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldAnnr char(4)
  DECLARE @oldLohnart char(3)
  DECLARE @oldDatum datetime
  SELECT @oldAuftragId = AuftragId, @oldAnnr = Annr, @oldLohnart = Lohnart, @oldDatum = Datum FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart = @oldLohnart) AND (Datum = @oldDatum) AND (RecId > @oldRecId)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart = @oldLohnart) AND (Datum > @oldDatum)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart > @oldLohnart)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr > @oldAnnr)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId > @oldAuftragId)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId,Annr,Lohnart,Datum,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_WartungsPosten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_WartungsPosten_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_WartungsPosten_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_Prior_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_WartungsPosten_Key03] @oldRecId integer AS
  DECLARE @oldAuftragId integer
  DECLARE @oldAnnr char(4)
  DECLARE @oldLohnart char(3)
  DECLARE @oldDatum datetime
  SELECT @oldAuftragId = AuftragId, @oldAnnr = Annr, @oldLohnart = Lohnart, @oldDatum = Datum FROM [dbo].[ut_WartungsPosten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart = @oldLohnart) AND (Datum = @oldDatum) AND (RecId < @oldRecId)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart = @oldLohnart) AND (Datum < @oldDatum)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr = @oldAnnr) AND (Lohnart < @oldLohnart)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId = @oldAuftragId) AND (Annr < @oldAnnr)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03)
    WHERE ((AuftragId < @oldAuftragId)) AND ((1 = ZeitAdded) AND (0 = Berechnet))
    ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum 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_WartungsPosten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_WartungsPosten_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_WartungsPosten_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_FindF_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_WartungsPosten_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mAnnr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mLohnart char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,3)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      Lohnart = @mLohnart AND 
      Datum = @mDatum
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      Lohnart = @mLohnart
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END

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


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr >= @mAnnr AND SUBSTRING(Annr,1,LEN(@mAnnr)) = @mAnnr
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      Lohnart >= @mLohnart AND SUBSTRING(Lohnart,1,LEN(@mLohnart)) = @mLohnart
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId,Annr,Lohnart,Datum,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsPosten_Key03')
  DROP PROCEDURE [dbo].[up_FindL_WartungsPosten_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_WartungsPosten_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mAuftragId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,8)
  DECLARE @mAnnr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mLohnart char(3) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,3)
  DECLARE @mDatum datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,16,10)

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

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      Lohnart = @mLohnart AND 
      Datum = @mDatum
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END

  IF @xLen = 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      Lohnart = @mLohnart
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END

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


  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      SUBSTRING(Annr,1,LEN(@mAnnr)) = @mAnnr
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END

  IF @xLen < 15 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_WartungsPosten] WITH(INDEX = Ik_Key03) WHERE 
      AuftragId = @mAuftragId AND 
      Annr = @mAnnr AND 
      SUBSTRING(Lohnart,1,LEN(@mLohnart)) = @mLohnart
      AND ((1 = ZeitAdded) AND (0 = Berechnet))
      ORDER BY AuftragId desc,Annr desc,Lohnart desc,Datum desc,RecId desc
    RETURN
  END


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


