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":"a197ebb1-f68d-4c9f-a668-7f5f205a72ec","LogTime":"2019-01-19T16:02:25.5060442-05:00"}