Avatar of hyphenpipe
hyphenpipe
Flag for United States of America

asked on 

How can I exlude system databases from a stored procedure.

I am trying to run a stored procedure, sp_MSForEachDB in SQL but I want to exclude the system databases.

I have tried:

EXEC sp_MSForEachDB 'IF ''?'' Not In (''TEMPDB'', ''master'', ''model'', ''msdb'') use ? select top 10 * from tablename'

But it is still searching the system databases for a tablename that does not exist.

What am I doing something wrong?
Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
EugeneZ

8/22/2022 - Mon