update CONTENT C, CONTENT C2 set C2.KEY_INITIAL_CONTENT=C.ID_INTERNAL WHERE C.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.contents.FunctionalityCall' AND C.KEY_FUNCTIONALITY=C2.KEY_INITIAL_CONTENT AND C2.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.Section';

CREATE TEMPORARY TABLE INITIAL_CONTENT_MIGRATION (
KEY_CONTENT INTEGER,
KEY_INITIAL_CONTENT INTEGER,
HITS INTEGER
);


insert into INITIAL_CONTENT_MIGRATION select C2.ID_INTERNAL, C2.KEY_INITIAL_CONTENT, COUNT(*) FROM CONTENT C, CONTENT C2 WHERE C.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.contents.FunctionalityCall' AND C.KEY_FUNCTIONALITY=C2.KEY_INITIAL_CONTENT AND C2.OJB_CONCRETE_CLASS='net.sourceforge.fenixedu.domain.contents.MetaDomainObjectPortal' GROUP BY C2.ID_INTERNAL;

update CONTENT C, CONTENT C2, INITIAL_CONTENT_MIGRATION ICM set C.KEY_INITIAL_CONTENT=C2.ID_INTERNAL WHERE ICM.HITS=1 AND C.ID_INTERNAL=ICM.KEY_CONTENT AND C2.KEY_FUNCTIONALITY=ICM.KEY_INITIAL_CONTENT;