-- 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_MailSetup')
BEGIN
CREATE TABLE [dbo].[ut_MailSetup]
(
  [RecId]  integer NOT NULL
 ,[Code] char(20) DEFAULT ''
 ,[ClientType] integer DEFAULT 0
 ,[ServerName] varchar(50) DEFAULT ''
 ,[ServerPort] integer DEFAULT 0
 ,[UserName] varchar(128) DEFAULT ''
 ,[Password] varchar(250) DEFAULT ''
 ,[AdrFrom] varchar(128) DEFAULT ''
 ,[AdrBcc] varchar(128) DEFAULT ''
 ,[InvBodyTxt] varchar(18) DEFAULT ''
 ,[InvSubject] varchar(128) DEFAULT ''
 ,[InvAskSend] integer DEFAULT 0
  CONSTRAINT [Pk_MailSetup]  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_MailSetup_KeyDel')
  DROP TRIGGER [dbo].[tr_MailSetup_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MailSetup_KeyUpd')
  DROP TRIGGER [dbo].[tr_MailSetup_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MailSetup_Keys')
  DROP TABLE [dbo].[ut_MailSetup_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_MailSetup' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [Code] char(20) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'ClientType')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [ClientType] 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_MailSetup' and sc.[name] = 'ServerName')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [ServerName] 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_MailSetup' and sc.[name] = 'ServerPort')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [ServerPort] 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_MailSetup' and sc.[name] = 'UserName')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [UserName] varchar(128) 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_MailSetup' and sc.[name] = 'Password')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [Password] varchar(250) 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_MailSetup' and sc.[name] = 'AdrFrom')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [AdrFrom] varchar(128) 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_MailSetup' and sc.[name] = 'AdrBcc')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [AdrBcc] varchar(128) 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_MailSetup' and sc.[name] = 'InvBodyTxt')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [InvBodyTxt] varchar(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_MailSetup' and sc.[name] = 'InvSubject')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [InvSubject] varchar(128) 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_MailSetup' and sc.[name] = 'InvAskSend')
BEGIN
  ALTER TABLE [dbo].[ut_MailSetup] ADD [InvAskSend] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' 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_MailSetup' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [Code] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' 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_MailSetup' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [Code] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'ServerName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'ServerName'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [ServerName] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'ServerName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'ServerName'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [ServerName] varchar(50)
END
GO
IF (128 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'UserName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'UserName'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [UserName] char(128)
END
GO
IF (128 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'UserName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'UserName'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [UserName] varchar(128)
END
GO
IF (250 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'Password'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'Password'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [Password] char(250)
END
GO
IF (250 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'Password'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'Password'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [Password] varchar(250)
END
GO
IF (128 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrFrom'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrFrom'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [AdrFrom] char(128)
END
GO
IF (128 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrFrom'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrFrom'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [AdrFrom] varchar(128)
END
GO
IF (128 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrBcc'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrBcc'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [AdrBcc] char(128)
END
GO
IF (128 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrBcc'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'AdrBcc'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [AdrBcc] varchar(128)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvBodyTxt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvBodyTxt'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [InvBodyTxt] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvBodyTxt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvBodyTxt'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [InvBodyTxt] varchar(18)
END
GO
IF (128 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvSubject'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvSubject'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [InvSubject] char(128)
END
GO
IF (128 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvSubject'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_MailSetup' and sc.[name] = 'InvSubject'))
BEGIN
   ALTER TABLE [dbo].[ut_MailSetup] ALTER COLUMN [InvSubject] varchar(128)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MailSetup')
  DROP VIEW [dbo].[MailSetup]
GO
CREATE VIEW [dbo].[MailSetup] AS
  SELECT [RecId]
 ,[Code]
 ,[ClientType]
 ,[ServerName]
 ,[ServerPort]
 ,[UserName]
 ,[Password]
 ,[AdrFrom]
 ,[AdrBcc]
 ,[InvBodyTxt]
 ,[InvSubject]
 ,[InvAskSend]
  FROM [dbo].[ut_MailSetup]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MailSetup')
  DROP PROCEDURE [dbo].[up_Sel_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_Sel_MailSetup]  @mRI integer
AS
  SELECT
    [Code]
   ,[ClientType]
   ,[ServerName]
   ,[ServerPort]
   ,[UserName]
   ,[Password]
   ,[AdrFrom]
   ,[AdrBcc]
   ,[InvBodyTxt]
   ,[InvSubject]
   ,[InvAskSend]
  FROM [dbo].[ut_MailSetup] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MailSetup')
  DROP PROCEDURE [dbo].[up_Append_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_Append_MailSetup]
   @Param_Code char(20)
  ,@Param_ClientType integer
  ,@Param_ServerName varchar(50)
  ,@Param_ServerPort integer
  ,@Param_UserName varchar(128)
  ,@Param_Password varchar(250)
  ,@Param_AdrFrom varchar(128)
  ,@Param_AdrBcc varchar(128)
  ,@Param_InvBodyTxt varchar(18)
  ,@Param_InvSubject varchar(128)
  ,@Param_InvAskSend integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_MailSetup()
      INSERT INTO [dbo].[ut_MailSetup] (RecId
        ,[Code]
        ,[ClientType]
        ,[ServerName]
        ,[ServerPort]
        ,[UserName]
        ,[Password]
        ,[AdrFrom]
        ,[AdrBcc]
        ,[InvBodyTxt]
        ,[InvSubject]
        ,[InvAskSend]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,@Param_ClientType
        ,RTRIM(@Param_ServerName)
        ,@Param_ServerPort
        ,RTRIM(@Param_UserName)
        ,RTRIM(@Param_Password)
        ,RTRIM(@Param_AdrFrom)
        ,RTRIM(@Param_AdrBcc)
        ,RTRIM(@Param_InvBodyTxt)
        ,RTRIM(@Param_InvSubject)
        ,@Param_InvAskSend
          )
      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_MailSetup')
  DROP PROCEDURE [dbo].[up_Add_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_Add_MailSetup]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_MailSetup()
  INSERT INTO [dbo].[ut_MailSetup] (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_MailSetup')
  DROP PROCEDURE [dbo].[up_Upd_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_Upd_MailSetup] @mRI integer
  ,@Param_Code char(20)
  ,@Param_ClientType integer
  ,@Param_ServerName varchar(50)
  ,@Param_ServerPort integer
  ,@Param_UserName varchar(128)
  ,@Param_Password varchar(250)
  ,@Param_AdrFrom varchar(128)
  ,@Param_AdrBcc varchar(128)
  ,@Param_InvBodyTxt varchar(18)
  ,@Param_InvSubject varchar(128)
  ,@Param_InvAskSend integer
AS
  UPDATE [dbo].[ut_MailSetup] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[ClientType] = @Param_ClientType
   ,[ServerName] = RTRIM(@Param_ServerName)
   ,[ServerPort] = @Param_ServerPort
   ,[UserName] = RTRIM(@Param_UserName)
   ,[Password] = RTRIM(@Param_Password)
   ,[AdrFrom] = RTRIM(@Param_AdrFrom)
   ,[AdrBcc] = RTRIM(@Param_AdrBcc)
   ,[InvBodyTxt] = RTRIM(@Param_InvBodyTxt)
   ,[InvSubject] = RTRIM(@Param_InvSubject)
   ,[InvAskSend] = @Param_InvAskSend
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_MailSetup')
  DROP PROCEDURE [dbo].[up_Upd2_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_Upd2_MailSetup] @Param_RecId varchar(20)
  ,@Param_Code varchar(20)
  ,@Param_ClientType varchar(1)
  ,@Param_ServerName varchar(50)
  ,@Param_ServerPort varchar(6)
  ,@Param_UserName varchar(128)
  ,@Param_Password varchar(250)
  ,@Param_AdrFrom varchar(128)
  ,@Param_AdrBcc varchar(128)
  ,@Param_InvBodyTxt varchar(18)
  ,@Param_InvSubject varchar(128)
  ,@Param_InvAskSend varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_MailSetup] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[ClientType] = dbo.itcfunc_integer(@Param_ClientType)
   ,[ServerName] = RTRIM(@Param_ServerName)
   ,[ServerPort] = dbo.itcfunc_integer(@Param_ServerPort)
   ,[UserName] = RTRIM(@Param_UserName)
   ,[Password] = RTRIM(@Param_Password)
   ,[AdrFrom] = RTRIM(@Param_AdrFrom)
   ,[AdrBcc] = RTRIM(@Param_AdrBcc)
   ,[InvBodyTxt] = RTRIM(@Param_InvBodyTxt)
   ,[InvSubject] = RTRIM(@Param_InvSubject)
   ,[InvAskSend] = dbo.itcfunc_integer(@Param_InvAskSend)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_MailSetup] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_MailSetup')
  DROP PROCEDURE [dbo].[up_TrSel_MailSetup]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MailSetup]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([ServerName]))
   +LTRIM(RTRIM([UserName]))
   +LTRIM(RTRIM([Password]))
   +LTRIM(RTRIM([AdrFrom]))
   +LTRIM(RTRIM([AdrBcc]))
   +LTRIM(RTRIM([InvBodyTxt]))
   +LTRIM(RTRIM([InvSubject]))
  AS Data FROM [dbo].[ut_MailSetup] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

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

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

  DECLARE @newRecId integer

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


