-- 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_BelegDocumentType')
BEGIN
CREATE TABLE [dbo].[ut_BelegDocumentType]
(
  [RecId]  integer NOT NULL
 ,[Code] char(10) DEFAULT ''
 ,[Descr] varchar(50) DEFAULT ''
 ,[Kind] char(2) DEFAULT ''
 ,[ApplTo] varchar(50) DEFAULT ''
 ,[AddX89B] tinyint DEFAULT 0
 ,[AddParts] tinyint DEFAULT 0
 ,[AddPDF] tinyint DEFAULT 0
 ,[AddSummar] tinyint DEFAULT 0
 ,[AddDocs] tinyint DEFAULT 0
 ,[Disclaimer] varchar(18) DEFAULT ''
 ,[EmbFile] varchar(50) DEFAULT ''
 ,[FileExt] varchar(10) DEFAULT ''
 ,[Stylesheet] varchar(50) DEFAULT ''
 ,[DisclPDF] varchar(18) DEFAULT ''
 ,[EmbMimeTyp] varchar(50) DEFAULT ''
 ,[EmbRelate] varchar(50) DEFAULT ''
 ,[EmbDescr] varchar(50) DEFAULT ''
 ,[MetaFile] varchar(100) DEFAULT ''
 ,[MetaParams] varchar(250) DEFAULT ''
  CONSTRAINT [Pk_BelegDocumentType]  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_BelegDocumentType_KeyDel')
  DROP TRIGGER [dbo].[tr_BelegDocumentType_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegDocumentType_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelegDocumentType_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelegDocumentType_Keys')
  DROP TABLE [dbo].[ut_BelegDocumentType_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_BelegDocumentType' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] 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_BelegDocumentType' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] 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_BelegDocumentType' and sc.[name] = 'Kind')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [Kind] 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_BelegDocumentType' and sc.[name] = 'ApplTo')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [ApplTo] 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_BelegDocumentType' and sc.[name] = 'AddX89B')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [AddX89B] 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_BelegDocumentType' and sc.[name] = 'AddParts')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [AddParts] 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_BelegDocumentType' and sc.[name] = 'AddPDF')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [AddPDF] 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_BelegDocumentType' and sc.[name] = 'AddSummar')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [AddSummar] 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_BelegDocumentType' and sc.[name] = 'AddDocs')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [AddDocs] 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_BelegDocumentType' and sc.[name] = 'Disclaimer')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [Disclaimer] 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_BelegDocumentType' and sc.[name] = 'EmbFile')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [EmbFile] 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_BelegDocumentType' and sc.[name] = 'FileExt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [FileExt] varchar(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_BelegDocumentType' and sc.[name] = 'Stylesheet')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [Stylesheet] 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_BelegDocumentType' and sc.[name] = 'DisclPDF')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [DisclPDF] 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_BelegDocumentType' and sc.[name] = 'EmbMimeTyp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [EmbMimeTyp] 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_BelegDocumentType' and sc.[name] = 'EmbRelate')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [EmbRelate] 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_BelegDocumentType' and sc.[name] = 'EmbDescr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [EmbDescr] 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_BelegDocumentType' and sc.[name] = 'MetaFile')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [MetaFile] varchar(100) 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_BelegDocumentType' and sc.[name] = 'MetaParams')
BEGIN
  ALTER TABLE [dbo].[ut_BelegDocumentType] ADD [MetaParams] varchar(250) 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_BelegDocumentType' 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_BelegDocumentType' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] 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_BelegDocumentType' 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_BelegDocumentType' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] 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_BelegDocumentType' 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_BelegDocumentType' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] 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_BelegDocumentType' 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_BelegDocumentType' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Descr] varchar(50)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Kind'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Kind'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Kind] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Kind'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Kind'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Kind] varchar(2)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'ApplTo'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'ApplTo'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [ApplTo] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'ApplTo'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'ApplTo'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [ApplTo] varchar(50)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Disclaimer'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Disclaimer'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Disclaimer] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Disclaimer'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Disclaimer'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Disclaimer] varchar(18)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbFile'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbFile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbFile] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbFile'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbFile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbFile] 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_BelegDocumentType' and sc.[name] = 'FileExt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'FileExt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [FileExt] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'FileExt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'FileExt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [FileExt] 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_BelegDocumentType' and sc.[name] = 'Stylesheet'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Stylesheet'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Stylesheet] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Stylesheet'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'Stylesheet'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [Stylesheet] varchar(50)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'DisclPDF'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'DisclPDF'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [DisclPDF] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'DisclPDF'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'DisclPDF'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [DisclPDF] varchar(18)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbMimeTyp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbMimeTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbMimeTyp] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbMimeTyp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbMimeTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbMimeTyp] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbRelate'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbRelate'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbRelate] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbRelate'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbRelate'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbRelate] varchar(50)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbDescr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbDescr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbDescr] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbDescr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'EmbDescr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [EmbDescr] varchar(50)
END
GO
IF (100 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaFile'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaFile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [MetaFile] char(100)
END
GO
IF (100 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaFile'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaFile'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [MetaFile] varchar(100)
END
GO
IF (250 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaParams'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaParams'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [MetaParams] char(250)
END
GO
IF (250 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaParams'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegDocumentType' and sc.[name] = 'MetaParams'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegDocumentType] ALTER COLUMN [MetaParams] varchar(250)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelegDocumentType')
  DROP VIEW [dbo].[BelegDocumentType]
GO
CREATE VIEW [dbo].[BelegDocumentType] AS
  SELECT [RecId]
 ,[Code]
 ,[Descr]
 ,[Kind]
 ,[ApplTo]
 ,[AddX89B]
 ,[AddParts]
 ,[AddPDF]
 ,[AddSummar]
 ,[AddDocs]
 ,[Disclaimer]
 ,[EmbFile]
 ,[FileExt]
 ,[Stylesheet]
 ,[DisclPDF]
 ,[EmbMimeTyp]
 ,[EmbRelate]
 ,[EmbDescr]
 ,[MetaFile]
 ,[MetaParams]
  FROM [dbo].[ut_BelegDocumentType]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Sel_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelegDocumentType]  @mRI integer
AS
  SELECT
    [Code]
   ,[Descr]
   ,[Kind]
   ,[ApplTo]
   ,[AddX89B]
   ,[AddParts]
   ,[AddPDF]
   ,[AddSummar]
   ,[AddDocs]
   ,[Disclaimer]
   ,[EmbFile]
   ,[FileExt]
   ,[Stylesheet]
   ,[DisclPDF]
   ,[EmbMimeTyp]
   ,[EmbRelate]
   ,[EmbDescr]
   ,[MetaFile]
   ,[MetaParams]
  FROM [dbo].[ut_BelegDocumentType] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Append_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Append_BelegDocumentType]
   @Param_Code char(10)
  ,@Param_Descr varchar(50)
  ,@Param_Kind char(2)
  ,@Param_ApplTo varchar(50)
  ,@Param_AddX89B tinyint
  ,@Param_AddParts tinyint
  ,@Param_AddPDF tinyint
  ,@Param_AddSummar tinyint
  ,@Param_AddDocs tinyint
  ,@Param_Disclaimer varchar(18)
  ,@Param_EmbFile varchar(50)
  ,@Param_FileExt varchar(10)
  ,@Param_Stylesheet varchar(50)
  ,@Param_DisclPDF varchar(18)
  ,@Param_EmbMimeTyp varchar(50)
  ,@Param_EmbRelate varchar(50)
  ,@Param_EmbDescr varchar(50)
  ,@Param_MetaFile varchar(100)
  ,@Param_MetaParams varchar(250)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelegDocumentType()
      INSERT INTO [dbo].[ut_BelegDocumentType] (RecId
        ,[Code]
        ,[Descr]
        ,[Kind]
        ,[ApplTo]
        ,[AddX89B]
        ,[AddParts]
        ,[AddPDF]
        ,[AddSummar]
        ,[AddDocs]
        ,[Disclaimer]
        ,[EmbFile]
        ,[FileExt]
        ,[Stylesheet]
        ,[DisclPDF]
        ,[EmbMimeTyp]
        ,[EmbRelate]
        ,[EmbDescr]
        ,[MetaFile]
        ,[MetaParams]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,RTRIM(@Param_Descr)
        ,RTRIM(@Param_Kind)
        ,RTRIM(@Param_ApplTo)
        ,@Param_AddX89B
        ,@Param_AddParts
        ,@Param_AddPDF
        ,@Param_AddSummar
        ,@Param_AddDocs
        ,RTRIM(@Param_Disclaimer)
        ,RTRIM(@Param_EmbFile)
        ,RTRIM(@Param_FileExt)
        ,RTRIM(@Param_Stylesheet)
        ,RTRIM(@Param_DisclPDF)
        ,RTRIM(@Param_EmbMimeTyp)
        ,RTRIM(@Param_EmbRelate)
        ,RTRIM(@Param_EmbDescr)
        ,RTRIM(@Param_MetaFile)
        ,RTRIM(@Param_MetaParams)
          )
      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_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Add_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Add_BelegDocumentType]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BelegDocumentType()
  INSERT INTO [dbo].[ut_BelegDocumentType] (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_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Upd_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelegDocumentType] @mRI integer
  ,@Param_Code char(10)
  ,@Param_Descr varchar(50)
  ,@Param_Kind char(2)
  ,@Param_ApplTo varchar(50)
  ,@Param_AddX89B tinyint
  ,@Param_AddParts tinyint
  ,@Param_AddPDF tinyint
  ,@Param_AddSummar tinyint
  ,@Param_AddDocs tinyint
  ,@Param_Disclaimer varchar(18)
  ,@Param_EmbFile varchar(50)
  ,@Param_FileExt varchar(10)
  ,@Param_Stylesheet varchar(50)
  ,@Param_DisclPDF varchar(18)
  ,@Param_EmbMimeTyp varchar(50)
  ,@Param_EmbRelate varchar(50)
  ,@Param_EmbDescr varchar(50)
  ,@Param_MetaFile varchar(100)
  ,@Param_MetaParams varchar(250)
AS
  UPDATE [dbo].[ut_BelegDocumentType] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Kind] = RTRIM(@Param_Kind)
   ,[ApplTo] = RTRIM(@Param_ApplTo)
   ,[AddX89B] = @Param_AddX89B
   ,[AddParts] = @Param_AddParts
   ,[AddPDF] = @Param_AddPDF
   ,[AddSummar] = @Param_AddSummar
   ,[AddDocs] = @Param_AddDocs
   ,[Disclaimer] = RTRIM(@Param_Disclaimer)
   ,[EmbFile] = RTRIM(@Param_EmbFile)
   ,[FileExt] = RTRIM(@Param_FileExt)
   ,[Stylesheet] = RTRIM(@Param_Stylesheet)
   ,[DisclPDF] = RTRIM(@Param_DisclPDF)
   ,[EmbMimeTyp] = RTRIM(@Param_EmbMimeTyp)
   ,[EmbRelate] = RTRIM(@Param_EmbRelate)
   ,[EmbDescr] = RTRIM(@Param_EmbDescr)
   ,[MetaFile] = RTRIM(@Param_MetaFile)
   ,[MetaParams] = RTRIM(@Param_MetaParams)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Upd2_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelegDocumentType] @Param_RecId varchar(20)
  ,@Param_Code varchar(10)
  ,@Param_Descr varchar(50)
  ,@Param_Kind varchar(2)
  ,@Param_ApplTo varchar(50)
  ,@Param_AddX89B varchar(1)
  ,@Param_AddParts varchar(1)
  ,@Param_AddPDF varchar(1)
  ,@Param_AddSummar varchar(1)
  ,@Param_AddDocs varchar(1)
  ,@Param_Disclaimer varchar(18)
  ,@Param_EmbFile varchar(50)
  ,@Param_FileExt varchar(10)
  ,@Param_Stylesheet varchar(50)
  ,@Param_DisclPDF varchar(18)
  ,@Param_EmbMimeTyp varchar(50)
  ,@Param_EmbRelate varchar(50)
  ,@Param_EmbDescr varchar(50)
  ,@Param_MetaFile varchar(100)
  ,@Param_MetaParams varchar(250)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelegDocumentType] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Kind] = RTRIM(@Param_Kind)
   ,[ApplTo] = RTRIM(@Param_ApplTo)
   ,[AddX89B] = dbo.itcfunc_integer(@Param_AddX89B)
   ,[AddParts] = dbo.itcfunc_integer(@Param_AddParts)
   ,[AddPDF] = dbo.itcfunc_integer(@Param_AddPDF)
   ,[AddSummar] = dbo.itcfunc_integer(@Param_AddSummar)
   ,[AddDocs] = dbo.itcfunc_integer(@Param_AddDocs)
   ,[Disclaimer] = RTRIM(@Param_Disclaimer)
   ,[EmbFile] = RTRIM(@Param_EmbFile)
   ,[FileExt] = RTRIM(@Param_FileExt)
   ,[Stylesheet] = RTRIM(@Param_Stylesheet)
   ,[DisclPDF] = RTRIM(@Param_DisclPDF)
   ,[EmbMimeTyp] = RTRIM(@Param_EmbMimeTyp)
   ,[EmbRelate] = RTRIM(@Param_EmbRelate)
   ,[EmbDescr] = RTRIM(@Param_EmbDescr)
   ,[MetaFile] = RTRIM(@Param_MetaFile)
   ,[MetaParams] = RTRIM(@Param_MetaParams)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelegDocumentType] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_TrSel_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelegDocumentType]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([Descr]))
   +LTRIM(RTRIM([Kind]))
   +LTRIM(RTRIM([ApplTo]))
   +LTRIM(RTRIM([Disclaimer]))
   +LTRIM(RTRIM([EmbFile]))
   +LTRIM(RTRIM([FileExt]))
   +LTRIM(RTRIM([Stylesheet]))
   +LTRIM(RTRIM([DisclPDF]))
   +LTRIM(RTRIM([EmbMimeTyp]))
   +LTRIM(RTRIM([EmbRelate]))
   +LTRIM(RTRIM([EmbDescr]))
   +LTRIM(RTRIM([MetaFile]))
   +LTRIM(RTRIM([MetaParams]))
  AS Data FROM [dbo].[ut_BelegDocumentType] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelegDocumentType')
  DROP PROCEDURE [dbo].[up_Reorg_BelegDocumentType]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelegDocumentType] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelegDocumentType] 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_BelegDocumentType] 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_BelegDocumentType] )
  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_BelegDocumentType' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_BelegDocumentType]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelegDocumentType' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelegDocumentType] (Code,RecId)
GO

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

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

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegDocumentType] 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_BelegDocumentType_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelegDocumentType_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelegDocumentType_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegDocumentType] 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_BelegDocumentType_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelegDocumentType_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelegDocumentType_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegDocumentType] 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 < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegDocumentType] 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_BelegDocumentType_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_BelegDocumentType_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_BelegDocumentType_Key00] @xKey, @xLen')
GO

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

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

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegDocumentType] 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 < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegDocumentType] 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_BelegDocumentType_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelegDocumentType_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelegDocumentType_Key00] @xKey, @xLen')
GO


