I am running a query with the sp_MSforeachdb
stored procedure. I used three databases which exist in my server instance: ExcelineDev, EXCE_NR1, and EXCE_AKI.
declare @t table (dbname varchar(50),ss varchar(max),sss varchar(max))
DECLARE @command varchar(max)
SELECT @command = 'IF ''?'' IN (''ExcelineDev'',''EXCE_NR1'',''EXCE_AKI'')
BEGIN USE ?
EXEC
(''
select DB_NAME(DB_ID()), category,name from usrpt.report
where name like ''''%leders%''''
'')
END'
print @command
insert into @t
EXEC sp_MSforeachdb @command
select * from @t
I'm getting the following error:
'Msg 911, Level 16, State 1, Line 2 Database 'Migration_Release_1' does not exist. Make sure that the name is entered correctly.'
I'm confused, because there is no DB called 'Migration_Release_1' in my server instance. Why is SQL searching for this database?