UPDATE {databaseOwner}{objectQualifier}DMX_Attributes
SET [Key]='att'+CAST(AttributeId AS NVARCHAR),
ShowInUI=1
WHERE [Key] IS NULL
GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_MoveEntry') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_MoveEntry
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_MoveEntry
 @PortalId Int,
 @EntryId Int,
 @NewCollectionId INT
AS

DECLARE @destpath VARCHAR(4000)
IF @NewCollectionId=0
 SET @destpath = '0;'
ELSE
 SET @destpath = (SELECT [Path] 
  FROM {databaseOwner}{objectQualifier}DMX_Entries
  WHERE EntryId = @NewCollectionId);
IF NOT @destpath LIKE '%;'+CAST(@EntryId AS VARCHAR(10))+';%' AND @EntryId <> @NewCollectionId
UPDATE {databaseOwner}{objectQualifier}DMX_Entries
 SET CollectionId=@NewCollectionId
WHERE
 LastVersionId=@EntryId
 AND PortalId=@PortalId

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_ResortCategories') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_ResortCategories
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_ResortCategories
 @PortalId INT,
 @ParentId INT
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Categories
 SET ViewOrder = subcats.vo
FROM {databaseOwner}{objectQualifier}DMX_Categories c
 INNER JOIN (SELECT
  CategoryId,
  ROW_NUMBER() OVER(ORDER BY ViewOrder ASC) - 1 vo
  FROM {databaseOwner}{objectQualifier}DMX_Categories
  WHERE PortalId=@PortalId AND ParentId=@ParentId) subcats ON subcats.CategoryId=c.CategoryId
  
GO

