THANK YOU! These replies really helped. Yes, I was doing this in ad-hoc SQL rather than in a stored procedure.
I realized that in order to do various calculations, I needed to use a temp table so I decided to do this batch script with a single cursor that references a multi-table select statement, and then I use the temp table to hold temporary values for my calculations.
One thing I'm noticing though: even though at the end I have
CLOSE mycursor
DEALLOCATE CURSOR mycursor
...when I try to run the script a second time I sometimes get the error:
There is already another cursor with the name 'mycursor' at the nesting level '0'.
And I always get the error "There is already an object named '#temp' in the database."
I'm not sure why the CLOSE sometimes isn't closing the cursor, and why the temp table persists after the script terminates. Thanks for any help.