At the beginning of the script, put
USE [OtherDatabase];
This will change the scope of the script to the other database. Make sure the script doesn't contain other USE statements that change the scope back.
This will only work when the two databases are on the same server.
Alternatively, you could just select the other database in the dropdown at the SSMS toolbar.
If the other database is at another server, you need to change the connection.