DECLARE @ErrorVar INT, @RowCountVar INT;  

Declare @creationDtId int, @lockById int, @lockTypeId int,  @lockingDateId int, @modifyById int, @modificationDateId int, @hasAttachmentId int, @hasNoteId int; 
Declare @hasFolderNoteId int;
Declare @OptimizeFolderDocument int;
 
select @creationDtId = Id from ComAttribute where name ='DOCUMENT CREATION DATE';
select @lockById = Id from ComAttribute where name ='DOCUMENT LOCKED BY';
select @lockTypeId  = Id from ComAttribute where name ='DOCUMENT LOCK TYPE';
select @lockingDateId = Id from ComAttribute where name ='DOCUMENT LOCKING DATE';
select @modifyById = Id from ComAttribute where name ='DOCUMENT MODIFIED BY';
select @modificationDateId  = Id from ComAttribute where name ='DOCUMENT MODIFICATION DATE'
select @hasAttachmentId = Id from ComAttribute where name ='EmailAttachment';
select @hasNoteId = Id from ComAttribute where name ='DOCUMENT HAS NOTES';

select @hasFolderNoteId= Id from ComAttribute where Name = 'FOLDER HAS NOTES';

select @OptimizeFolderDocument = ISNULL(Count(Id),0) from ComSystemSetting where name = 'OptimizeFolderDocument'

			
				UPDATE ComDocument 
					SET ComDocument.CreatedDatetime = a.DateTimeValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @creationDtId;  

				UPDATE ComDocument 
					SET ComDocument.LockedBy = a.StringValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @lockById; 
				
				
				UPDATE ComDocument 
					SET ComDocument.LockType = a.StringValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @lockTypeId;  

				UPDATE ComDocument 
					SET ComDocument.LockingDate = a.DateTimeValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @lockingDateId;  

				UPDATE ComDocument 
					SET ComDocument.ModifiedBy = a.StringValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @modifyById;  

				UPDATE ComDocument 
					SET ComDocument.ModificationDateTime = a.DateTimeValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @modificationDateId;

				UPDATE ComDocument 
					SET ComDocument.HasAttachments = 1 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @hasAttachmentId; 

				UPDATE ComDocument 
					SET ComDocument.HasNotes = a.BooleanValue 
					FROM ComDocumentAttribute a 
					INNER JOIN ComDocument ON a.ComDocumentId =ComDocument.Id And a.ComAttributeId = @hasNoteId;

				
				
				UPDATE ComFolder 
					SET ComFolder.HasNotes = a.BooleanValue 
					FROM ComFolderAttribute a 
					INNER JOIN ComFolder ON a.ComFolderId =ComFolder.Id And a.ComAttributeId = @hasFolderNoteId;



				If @OptimizeFolderDocument = 0
					Begin
					Insert Into ComSystemSetting (Id, Name) Values (6, 'OptimizeFolderDocument');
					End