In both queries, it looks like you are trying to embed in #temp. #temp is in one of the databases (for sake, databaseA arguments). Therefore, when you try to paste into #temp from databaseB, it reports that it does not exist.
Try changing it from Into #temp From to Into databaseA.dbo. # temp Disable in both statements.
Also, make sure that the connection strings have permissions for another database, otherwise this will not work.
Update: the temp table is out of scope - if you have one connection string that has permissions for both databases, then you can use it for both queries (keeping the connection alive). When querying a table in another database, be sure to use the [DBName] format. [Owner]. [TableName] when accessing a table.
source share