Home Forums SQL Server 7,2000 T-SQL Run same SQL or Stored Proc for multiple databases RE: Run same SQL or Stored Proc for multiple databases

  • Hi,

    Try this function to loop through all databases

    --This query will return a listing of all tables in all databases on a SQL instance:

    DECLARE @command varchar(1000)

    SELECT @command = 'USE ? SELECT name FROM mycustomtable WHERE name = ''Foo'' ORDER BY name'

    EXEC sp_MSforeachdb @command

    This will run the SQL command for each database containing mycustomtable.

    Hope this helps.