SDSS error message

Incorrect syntax near '#SELECT'.

Your SQL command was:
SELECT
 LTRIM(STR(mjd_r,20,2)) AS MJD,
 dbo.fSDSS(p.objId) AS ID,
 modelMag_g, modelMagErr_g,
 modelMag_r, modelMagErr_r,
 modelMag_i, modelMagErr_i, p.ra, p.dec 
INTO #list
FROM detectionindex d 
 JOIN PhotoObjAll p ON d.objid=p.objid
 JOIN Field f ON p.fieldid=f.fieldid
WHERE d.thingid=97423000
ORDER BY 1
SELECT a.*, b.* 
FROM #list a
 CROSS APPLY dbo.fGetNearbyObjEq(a.ra, a.dec, 0.1) b





Technical info:
{"ErrorCode":500,"ErrorType":"InternalServerError","ErrorMessage":"Incorrect syntax near '#SELECT'.","LogMessageID":"b4b274c3-5240-4894-bd2b-5896c83c5d24","LogTime":"2019-05-22T05:26:15.1721317-04:00"}