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":"c62b26bd-c863-421d-a89e-c44169158e44","LogTime":"2019-06-19T10:19:41.2037533-04:00"}