/* update citations for RM8
 add in new field - CitItemD for citation name and put in source name - creates good citation name in RM8
*/
UPDATE SourceTemplateTable
SET (FieldDefs) = cast(Replace(cast(FieldDefs as Text),'<Root><Fields>','<Root><Fields><Field><FieldName>CitItemD</FieldName><DisplayName>Item Name</DisplayName><Type>Text</Type><Hint>Item Name</Hint><LongHint>To distinguish citations</LongHint><CitationField>True</CitationField></Field>') as blob)
WHERE TemplateID > 10000;

-- add extra field for name - at the start so it is used for citation name when converting to v8
UPDATE CitationTable 
SET (Fields) = (SELECT cast(Replace(cast(CitationTable.Fields as Text),'<Root><Fields>','<Root><Fields><Field><Name>CitItemD</Name><Value>' || SourceTable.Name ||  '</Value></Field>') as blob) 
                                FROM  SourceTable 
                                WHERE SourceTable.SourceID = CitationTable.SourceID )
WHERE
    EXISTS (
       SELECT *
       FROM SourceTable 
        WHERE (SourceTable.SourceID = CitationTable.SourceID) 
		AND SourceTable.TemplateID > 10000
   );
