-- 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_RechnungsEingangsSchema')
BEGIN
CREATE TABLE [dbo].[ut_RechnungsEingangsSchema]
(
  [RecId]  integer NOT NULL
 ,[Code] char(10) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
 ,[Descr] varchar(50) DEFAULT ''
 ,[VatCode] char(10) DEFAULT ''
 ,[EKto] char(8) DEFAULT ''
 ,[EKtoUSt] char(8) DEFAULT ''
 ,[EKtoSchl] char(3) DEFAULT ''
 ,[LKto] char(8) DEFAULT ''
 ,[LKtoUSt] char(8) DEFAULT ''
 ,[LKtoSchl] char(3) DEFAULT ''
  CONSTRAINT [Pk_RechnungsEingangsSchema]  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_RechnungsEingangsSchema_KeyDel')
  DROP TRIGGER [dbo].[tr_RechnungsEingangsSchema_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RechnungsEingangsSchema_KeyUpd')
  DROP TRIGGER [dbo].[tr_RechnungsEingangsSchema_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RechnungsEingangsSchema_Keys')
  DROP TABLE [dbo].[ut_RechnungsEingangsSchema_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_RechnungsEingangsSchema' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [Code] char(10) 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_RechnungsEingangsSchema' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] 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_RechnungsEingangsSchema' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [Descr] varchar(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_RechnungsEingangsSchema' and sc.[name] = 'VatCode')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [VatCode] char(10) 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_RechnungsEingangsSchema' and sc.[name] = 'EKto')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [EKto] 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_RechnungsEingangsSchema' and sc.[name] = 'EKtoUSt')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [EKtoUSt] 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_RechnungsEingangsSchema' and sc.[name] = 'EKtoSchl')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [EKtoSchl] 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_RechnungsEingangsSchema' and sc.[name] = 'LKto')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [LKto] 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_RechnungsEingangsSchema' and sc.[name] = 'LKtoUSt')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [LKtoUSt] 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_RechnungsEingangsSchema' and sc.[name] = 'LKtoSchl')
BEGIN
  ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ADD [LKtoSchl] char(3) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Code'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [Code] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Code'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [Code] varchar(10)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Descr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [Descr] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Descr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [Descr] varchar(50)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'VatCode'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'VatCode'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [VatCode] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'VatCode'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'VatCode'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [VatCode] varchar(10)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKto] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoUSt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoUSt'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKtoUSt] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoUSt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoUSt'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKtoUSt] varchar(8)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoSchl'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKtoSchl] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoSchl'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'EKtoSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [EKtoSchl] varchar(3)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKto] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoUSt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoUSt'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKtoUSt] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoUSt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoUSt'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKtoUSt] varchar(8)
END
GO
IF (3 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoSchl'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKtoSchl] char(3)
END
GO
IF (3 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoSchl'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RechnungsEingangsSchema' and sc.[name] = 'LKtoSchl'))
BEGIN
   ALTER TABLE [dbo].[ut_RechnungsEingangsSchema] ALTER COLUMN [LKtoSchl] varchar(3)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RechnungsEingangsSchema')
  DROP VIEW [dbo].[RechnungsEingangsSchema]
GO
CREATE VIEW [dbo].[RechnungsEingangsSchema] AS
  SELECT [RecId]
 ,[Code]
 ,[LfdNr]
 ,[Descr]
 ,[VatCode]
 ,[EKto]
 ,[EKtoUSt]
 ,[EKtoSchl]
 ,[LKto]
 ,[LKtoUSt]
 ,[LKtoSchl]
  FROM [dbo].[ut_RechnungsEingangsSchema]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_Sel_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_Sel_RechnungsEingangsSchema]  @mRI integer
AS
  SELECT
    [Code]
   ,[LfdNr]
   ,[Descr]
   ,[VatCode]
   ,[EKto]
   ,[EKtoUSt]
   ,[EKtoSchl]
   ,[LKto]
   ,[LKtoUSt]
   ,[LKtoSchl]
  FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_Append_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_Append_RechnungsEingangsSchema]
   @Param_Code char(10)
  ,@Param_LfdNr integer
  ,@Param_Descr varchar(50)
  ,@Param_VatCode char(10)
  ,@Param_EKto char(8)
  ,@Param_EKtoUSt char(8)
  ,@Param_EKtoSchl char(3)
  ,@Param_LKto char(8)
  ,@Param_LKtoUSt char(8)
  ,@Param_LKtoSchl 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_RechnungsEingangsSchema()
      INSERT INTO [dbo].[ut_RechnungsEingangsSchema] (RecId
        ,[Code]
        ,[LfdNr]
        ,[Descr]
        ,[VatCode]
        ,[EKto]
        ,[EKtoUSt]
        ,[EKtoSchl]
        ,[LKto]
        ,[LKtoUSt]
        ,[LKtoSchl]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,@Param_LfdNr
        ,RTRIM(@Param_Descr)
        ,UPPER(RTRIM(@Param_VatCode))
        ,UPPER(RTRIM(@Param_EKto))
        ,UPPER(RTRIM(@Param_EKtoUSt))
        ,UPPER(RTRIM(@Param_EKtoSchl))
        ,UPPER(RTRIM(@Param_LKto))
        ,UPPER(RTRIM(@Param_LKtoUSt))
        ,UPPER(RTRIM(@Param_LKtoSchl))
          )
      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_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_Add_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_Add_RechnungsEingangsSchema]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_RechnungsEingangsSchema()
  INSERT INTO [dbo].[ut_RechnungsEingangsSchema] (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_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_Upd_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_Upd_RechnungsEingangsSchema] @mRI integer
  ,@Param_Code char(10)
  ,@Param_LfdNr integer
  ,@Param_Descr varchar(50)
  ,@Param_VatCode char(10)
  ,@Param_EKto char(8)
  ,@Param_EKtoUSt char(8)
  ,@Param_EKtoSchl char(3)
  ,@Param_LKto char(8)
  ,@Param_LKtoUSt char(8)
  ,@Param_LKtoSchl char(3)
AS
  UPDATE [dbo].[ut_RechnungsEingangsSchema] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[LfdNr] = @Param_LfdNr
   ,[Descr] = RTRIM(@Param_Descr)
   ,[VatCode] = UPPER(RTRIM(@Param_VatCode))
   ,[EKto] = UPPER(RTRIM(@Param_EKto))
   ,[EKtoUSt] = UPPER(RTRIM(@Param_EKtoUSt))
   ,[EKtoSchl] = UPPER(RTRIM(@Param_EKtoSchl))
   ,[LKto] = UPPER(RTRIM(@Param_LKto))
   ,[LKtoUSt] = UPPER(RTRIM(@Param_LKtoUSt))
   ,[LKtoSchl] = UPPER(RTRIM(@Param_LKtoSchl))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_Upd2_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_Upd2_RechnungsEingangsSchema] @Param_RecId varchar(20)
  ,@Param_Code varchar(10)
  ,@Param_LfdNr varchar(4)
  ,@Param_Descr varchar(50)
  ,@Param_VatCode varchar(10)
  ,@Param_EKto varchar(8)
  ,@Param_EKtoUSt varchar(8)
  ,@Param_EKtoSchl varchar(3)
  ,@Param_LKto varchar(8)
  ,@Param_LKtoUSt varchar(8)
  ,@Param_LKtoSchl varchar(3)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_RechnungsEingangsSchema] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[Descr] = RTRIM(@Param_Descr)
   ,[VatCode] = UPPER(RTRIM(@Param_VatCode))
   ,[EKto] = UPPER(RTRIM(@Param_EKto))
   ,[EKtoUSt] = UPPER(RTRIM(@Param_EKtoUSt))
   ,[EKtoSchl] = UPPER(RTRIM(@Param_EKtoSchl))
   ,[LKto] = UPPER(RTRIM(@Param_LKto))
   ,[LKtoUSt] = UPPER(RTRIM(@Param_LKtoUSt))
   ,[LKtoSchl] = UPPER(RTRIM(@Param_LKtoSchl))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_RechnungsEingangsSchema] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_RechnungsEingangsSchema')
  DROP PROCEDURE [dbo].[up_TrSel_RechnungsEingangsSchema]
GO
CREATE PROCEDURE [dbo].[up_TrSel_RechnungsEingangsSchema]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([Descr]))
   +LTRIM(RTRIM([VatCode]))
   +LTRIM(RTRIM([EKto]))
   +LTRIM(RTRIM([EKtoUSt]))
   +LTRIM(RTRIM([EKtoSchl]))
   +LTRIM(RTRIM([LKto]))
   +LTRIM(RTRIM([LKtoUSt]))
   +LTRIM(RTRIM([LKtoSchl]))
  AS Data FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RechnungsEingangsSchema_Key00')
  DROP PROCEDURE [dbo].[up_Next_RechnungsEingangsSchema_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_RechnungsEingangsSchema_Key00] @oldRecId integer AS
  DECLARE @oldCode char(10)
  DECLARE @oldLfdNr integer
  SELECT @oldCode = Code, @oldLfdNr = LfdNr FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00)
    WHERE (Code > @oldCode)
    ORDER BY Code,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_RechnungsEingangsSchema_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RechnungsEingangsSchema_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_RechnungsEingangsSchema_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RechnungsEingangsSchema_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RechnungsEingangsSchema_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_RechnungsEingangsSchema_Key00] @oldRecId integer AS
  DECLARE @oldCode char(10)
  DECLARE @oldLfdNr integer
  SELECT @oldCode = Code, @oldLfdNr = LfdNr FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00)
    WHERE (Code = @oldCode) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Code 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_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00)
    WHERE (Code = @oldCode) AND (LfdNr < @oldLfdNr)
    ORDER BY Code 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_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00)
    WHERE (Code < @oldCode)
    ORDER BY Code 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_RechnungsEingangsSchema_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RechnungsEingangsSchema_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RechnungsEingangsSchema_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode AND 
      LfdNr = @mLfdNr
      ORDER BY Code,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code,LfdNr,RecId
    RETURN
  END


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

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

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

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode AND 
      LfdNr = @mLfdNr
      ORDER BY Code desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code desc,LfdNr desc,RecId desc
    RETURN
  END


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


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RechnungsEingangsSchema_Key01')
  DROP PROCEDURE [dbo].[up_Next_RechnungsEingangsSchema_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_RechnungsEingangsSchema_Key01] @oldRecId integer AS
  DECLARE @oldCode char(10)
  DECLARE @oldVatCode char(10)
  SELECT @oldCode = Code, @oldVatCode = VatCode FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01)
    WHERE (Code = @oldCode) AND (VatCode > @oldVatCode)
    ORDER BY Code,VatCode,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01)
    WHERE (Code > @oldCode)
    ORDER BY Code,VatCode,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_RechnungsEingangsSchema_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RechnungsEingangsSchema_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_RechnungsEingangsSchema_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RechnungsEingangsSchema_Key01')
  DROP PROCEDURE [dbo].[up_Prior_RechnungsEingangsSchema_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_RechnungsEingangsSchema_Key01] @oldRecId integer AS
  DECLARE @oldCode char(10)
  DECLARE @oldVatCode char(10)
  SELECT @oldCode = Code, @oldVatCode = VatCode FROM [dbo].[ut_RechnungsEingangsSchema] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01)
    WHERE (Code = @oldCode) AND (VatCode < @oldVatCode)
    ORDER BY Code desc,VatCode desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01)
    WHERE (Code < @oldCode)
    ORDER BY Code desc,VatCode 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_RechnungsEingangsSchema_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RechnungsEingangsSchema_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RechnungsEingangsSchema_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode AND 
      VatCode = @mVatCode
      ORDER BY Code,VatCode,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode
      ORDER BY Code,VatCode,RecId
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code,VatCode,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode AND 
      VatCode >= @mVatCode AND SUBSTRING(VatCode,1,LEN(@mVatCode)) = @mVatCode
      ORDER BY Code,VatCode,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode AND 
      VatCode = @mVatCode
      ORDER BY Code desc,VatCode desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode
      ORDER BY Code desc,VatCode desc,RecId desc
    RETURN
  END

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

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code desc,VatCode desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RechnungsEingangsSchema] WITH(INDEX = Ik_Key01) WHERE 
      Code = @mCode AND 
      SUBSTRING(VatCode,1,LEN(@mVatCode)) = @mVatCode
      ORDER BY Code desc,VatCode desc,RecId desc
    RETURN
  END

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


