------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_StammPosDimensionen')
BEGIN
CREATE TABLE [dbo].[ut_StammPosDimensionen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Lkz] char(2) DEFAULT ' '
  ,[Artnr] char(18) DEFAULT ' '
  ,[DimCode] char(20) DEFAULT ' '
  ,[DimValue] char(20) DEFAULT ' '
  ,[Quantity] float DEFAULT 0
  ,[Comment] char(50) DEFAULT ' '
  ,[CalcSumm] tinyint DEFAULT 0
  ,[AttmStamp] char(32) DEFAULT ' '
  CONSTRAINT [Pk_StammPosDimensionen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_StammPosDimensionen_KeyDel')
  DROP TRIGGER [dbo].[tr_StammPosDimensionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_StammPosDimensionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_StammPosDimensionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_StammPosDimensionen_Keys')
  DROP TABLE [dbo].[ut_StammPosDimensionen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [Lkz] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [Artnr] char(18) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'DimCode')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [DimCode] 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_StammPosDimensionen' and sc.[name] = 'DimValue')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [DimValue] 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_StammPosDimensionen' and sc.[name] = 'Quantity')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [Quantity] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [Comment] char(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_StammPosDimensionen' and sc.[name] = 'CalcSumm')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [CalcSumm] 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_StammPosDimensionen' and sc.[name] = 'AttmStamp')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ADD [AttmStamp] char(32) NOT NULL DEFAULT ' '
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [Lkz] char(2)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [Artnr] char(18)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'DimCode')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [DimCode] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'DimValue')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [DimValue] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [Comment] char(50)
END
GO
IF 32 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_StammPosDimensionen' and sc.[name] = 'AttmStamp')
BEGIN
  ALTER TABLE [dbo].[ut_StammPosDimensionen] ALTER COLUMN [AttmStamp] char(32)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'StammPosDimensionen')
  DROP VIEW [dbo].[StammPosDimensionen]
GO
CREATE VIEW [dbo].[StammPosDimensionen] AS
  SELECT * FROM [dbo].[ut_StammPosDimensionen]
GO
CREATE TABLE [dbo].[ut_StammPosDimensionen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(70) NOT NULL
  CONSTRAINT [Pk_ut_StammPosDimensionen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_StammPosDimensionen_KeyDel] ON [dbo].[ut_StammPosDimensionen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_StammPosDimensionen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_StammPosDimensionen_KeyUpd] ON [dbo].[ut_StammPosDimensionen]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_StammPosDimensionen_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_StammPosDimensionen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(2),[Lkz])+convert(char(18),[Artnr])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_StammPosDimensionen_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(2),[Lkz])+convert(char(18),[Artnr])+convert(char(20),[DimCode])+convert(char(20),[DimValue])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_StammPosDimensionen] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Add_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Add_StammPosDimensionen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_StammPosDimensionen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_StammPosDimensionen] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Upd_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_StammPosDimensionen]
  @RI integer
 ,@Parm_Lkz char(2)
 ,@Parm_Artnr char(18)
 ,@Parm_DimCode char(20)
 ,@Parm_DimValue char(20)
 ,@Parm_Quantity float
 ,@Parm_Comment char(50)
 ,@Parm_CalcSumm tinyint
 ,@Parm_AttmStamp char(32)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_StammPosDimensionen] SET
     [Lkz] = RTRIM(@Parm_Lkz)
    ,[Artnr] = RTRIM(@Parm_Artnr)
    ,[DimCode] = RTRIM(@Parm_DimCode)
    ,[DimValue] = RTRIM(@Parm_DimValue)
    ,[Quantity] = @Parm_Quantity
    ,[Comment] = RTRIM(@Parm_Comment)
    ,[CalcSumm] = @Parm_CalcSumm
    ,[AttmStamp] = RTRIM(@Parm_AttmStamp)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Sel_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_StammPosDimensionen]
  @RI integer
AS
SELECT
     [Lkz]
    ,[Artnr]
    ,[DimCode]
    ,[DimValue]
    ,[Quantity]
    ,[Comment]
    ,[CalcSumm]
    ,[AttmStamp]
  FROM [dbo].[ut_StammPosDimensionen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Del_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Del_StammPosDimensionen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_StammPosDimensionen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Reorg_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_StammPosDimensionen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_StammPosDimensionen] 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_StammPosDimensionen] 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_StammPosDimensionen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_Append_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_Append_StammPosDimensionen]
  @Parm_Lkz char(2)
 ,@Parm_Artnr char(18)
 ,@Parm_DimCode char(20)
 ,@Parm_DimValue char(20)
 ,@Parm_Quantity float
 ,@Parm_Comment char(50)
 ,@Parm_CalcSumm tinyint
 ,@Parm_AttmStamp char(32)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_StammPosDimensionen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_StammPosDimensionen] (
    RecId
   ,[Lkz]
   ,[Artnr]
   ,[DimCode]
   ,[DimValue]
   ,[Quantity]
   ,[Comment]
   ,[CalcSumm]
   ,[AttmStamp]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Lkz)
   ,RTRIM(@Parm_Artnr)
   ,RTRIM(@Parm_DimCode)
   ,RTRIM(@Parm_DimValue)
   ,@Parm_Quantity
   ,RTRIM(@Parm_Comment)
   ,@Parm_CalcSumm
   ,RTRIM(@Parm_AttmStamp)
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_StammPosDimensionen')
  DROP PROCEDURE [dbo].[up_TrSel_StammPosDimensionen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_StammPosDimensionen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Lkz]))
      +LTRIM(RTRIM([Artnr]))
      +LTRIM(RTRIM([DimCode]))
      +LTRIM(RTRIM([DimValue]))
      +LTRIM(RTRIM([Comment]))
      +LTRIM(RTRIM([AttmStamp]))
    AS Data
  FROM     [dbo].[ut_StammPosDimensionen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_StammPosDimensionen_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_First_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Last_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Next_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_StammPosDimensionen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_StammPosDimensionen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_StammPosDimensionen_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_StammPosDimensionen_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_StammPosDimensionen_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_StammPosDimensionen_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_StammPosDimensionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_StammPosDimensionen_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_StammPosDimensionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_StammPosDimensionen_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_StammPosDimensionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_StammPosDimensionen_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_StammPosDimensionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_StammPosDimensionen_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_StammPosDimensionen_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_StammPosDimensionen_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_StammPosDimensionen_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

