AllCitations.sql+Error

I am running the following query in SQLite Expert Personal 5.2(x86): -- AllCitations.sql -- 2010-01-28 ve3meo -- Lists citations for each person -- 2010-01-29 rev by ve3meo to use LEFT OUTER JOINS to include the most orphaned citations -- Citations for Alternate Names, added column for NameTable.IsPrimary AS Uniq to all queries -- and negated it for Alt Name and Couple.Wife queries; filter on Uniq for principal name to -- reduce multiple listing of same citation OR Uniq ISNULL for citations unlinked to persons. -- Requires a temp table because of speed degradation when incorporated in main selects; -- filtering can be done on screen in SQLiteDeveloper. -- 2010-01-30 rev by ve3meo. Dropped UNIQUE from INDEX because other SQLite managers objected. -- Put QUOTE around BLOB type fields from CitationTable to display text where some SQLite -- managers merely say BLOB. -- To Do - maybe add eventtable.SortDate as a sorting criterion -- 2011-11-04 ve3meo corrections for spouse, and family fact citations and multiples due Alt Name -- -- BEGIN <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- all Personal citations for Individual <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">DROP TABLE IF EXISTS tmpCitations; <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">CREATE TEMP TABLE tmpCitations AS <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT c.CITATIONID AS CitID, c.sourceid AS SrcID, n.ownerid AS RIN, n.IsPrimary AS Uniq, n.surname COLLATE NOCASE AS Surname, n.suffix COLLATE NOCASE AS Sfx, n.prefix COLLATE NOCASE AS Pfx, n.given COLLATE NOCASE AS Givens, n.birthyear AS Born, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> n.deathyear AS Died, 'Personal' AS Citer, s.NAME COLLATE NOCASE AS Source, s.refnumber AS SrcREFN, s.actualtext AS SrcTxt, s.comments AS SrcComment, c.refnumber AS CitREFN, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> QUOTE(c.actualtext) AS CitTxt, QUOTE(c.comments) AS CitComment <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">FROM citationtable c <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN sourcetable s ON c.sourceid=s.sourceid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN persontable p ON c.ownerid=p.personid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN nametable n ON p.personid=n.ownerid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE c.ownertype=0 AND +n.IsPrimary=1 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">; <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">INSERT INTO tmpCitations <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- all Fact citations for Individual <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT c.CITATIONID, c.sourceid AS SrcID, n.ownerid AS RIN, n.IsPrimary, n.surname COLLATE NOCASE, n.suffix COLLATE NOCASE , n.prefix COLLATE NOCASE , n.given COLLATE NOCASE , n.birthyear, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> n.deathyear, f.NAME AS Citer, s.NAME COLLATE NOCASE, s.refnumber, s.actualtext, s.comments, c.refnumber, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> c.actualtext, c.comments <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">FROM citationtable c <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN sourcetable s ON c.sourceid=s.sourceid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN eventtable e ON c.ownerid=e.eventid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN persontable p ON e.ownerid=p.personid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN nametable n ON p.personid=n.ownerid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN facttypetable f ON e.eventtype=f.facttypeid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE c.ownertype=2 AND e.ownertype=0 AND f.ownertype=0 AND +n.IsPrimary=1 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">;

<span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">INSERT INTO tmpCitations <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- all Spouse citations for Father|Husband|Partner 1 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT c.CITATIONID, c.sourceid AS SrcID, n.ownerid AS RIN, n.IsPrimary, n.surname, n.suffix, n.prefix, n.given, n.birthyear, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> n.deathyear, 'Spouse' as 'Citer', s.NAME, s.refnumber, s.actualtext, s.comments, c.refnumber, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> c.actualtext, c.comments <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">FROM citationtable c <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN sourcetable s ON c.sourceid=s.sourceid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN familytable fm ON c.ownerid=fm.FamilyID <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN persontable p ON fm.fatherid=p.personid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN nametable n ON p.personid=n.ownerid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- LEFT OUTER JOIN eventtable e ON e.ownerid=fm.familyid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- LEFT OUTER JOIN facttypetable f ON e.eventtype=f.facttypeid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE c.ownertype=1 -- AND e.ownertype=1 AND f.ownertype=1 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">AND +n.IsPrimary=1 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">;


 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">INSERT INTO tmpCitations **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">-- all Couple Event citations for Father|Husband|Partner 1 **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT c.CITATIONID, c.sourceid AS SrcID, n.ownerid AS RIN, n.IsPrimary, n.surname, n.suffix, n.prefix, n.given, n.birthyear, **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> n.deathyear, f.NAME, s.NAME, s.refnumber, s.actualtext, s.comments, c.refnumber, **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> c.actualtext, c.comments **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">FROM citationtable c **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN sourcetable s ON c.sourceid=s.sourceid **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN familytable fm ON c.ownerid=e.EventID **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN persontable p ON fm.fatherid=p.personid **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN nametable n ON p.personid=n.ownerid **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN eventtable e ON e.ownerid=fm.familyid **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN facttypetable f ON e.eventtype=f.facttypeid **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE c.ownertype=2 AND e.ownertype=1 AND f.ownertype=1 AND +n.IsPrimary=1 **
 * <span style="color: #ff0000; font-family: &#39;Courier New&#39;; font-size: 10pt;">; **

<span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">INSERT INTO tmpCitations <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- Citations for Alternate Names <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT c.CITATIONID, c.sourceid AS SrcID, n.ownerid AS RIN, NOT n.IsPrimary, n.surname, n.suffix, n.prefix, n.given, n.birthyear, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> n.deathyear, 'Alternate Name' AS Citer, s.NAME AS Source, s.refnumber, s.actualtext, s.comments, c.refnumber, <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> c.actualtext, c.comments <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">FROM citationtable c <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN sourcetable s ON c.sourceid=s.sourceid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> LEFT OUTER JOIN nametable n ON n.nameid=c.ownerid <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE c.ownertype=7 AND +n.IsPrimary=0 <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">; <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">CREATE INDEX tmpCitations_idx ON tmpCitations(CitID); <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- Now filter the results to get rid of duplicate citation IDs due Alt Names <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">SELECT * FROM tmpcitations <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">WHERE uniq=1 OR uniq ISNULL <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">ORDER BY RIN, Citer, SOURCE <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">; <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- CitID <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- RIN, Citer; <span style="font-family: &#39;Courier New&#39;; font-size: 10pt;">-- END

<span style="font-family: &#39;Courier New&#39;; font-size: 10pt;"> I am getting the following error: ON clause references tables to its right.

I broke the query down and ran each section. I got the error on the RED section.