Blog Pages

sp_MSforeachdb - execute a command on all databases in the instance

EXEC sp_MSforeachdb @command
EXEC sp_MSforeachdb 'USE [?]; .....'

The command should include USE [?]; - the ? will be replace by the DBs names.

For example:
DECLARE @command varchar(500) 
SELECT @command = 'USE ? SELECT * FROM sys.objects WHERE xtype = ''U''' 
EXEC sp_MSforeachdb @command

No comments:

Post a Comment