AllenValk66 (4/26/2013)
the temporary table should be disposed once your session has been closed, but just in case add this to your script just before the create table
IF (OBJECT_ID('tempdb..#dbs')) IS NOT NULL
BEGIN
DROP TABLE #dbs
END
create table #dbs(a int primary key identity, dbname varchar(100))
declare @nextdb varchar(100)
declare @restorestring varchar(400)
AllenValk66 (4/26/2013)
It will run perfectly as a SQL Server Agent job, however. (not sure why that is!)
Since at that point it will be running under a different account that is why it succeeds.
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉