original:https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-without-cursors/
-- this query would return a listing of all Tables in all databases on a SQL instance: declare @command varchar ( ) select @command = " use? Select name from sysobjects WHERE xtype = u " ORDER by name exec sp_msforeachdb @command
--This statement creates a stored procedure in each user database that would return a listing of all users in a database, Sorted by their modification dateDECLARE @command varchar( +)SELECT @command = 'IF"'?"'Not in ("'Master"', "'Model"', "'msdb"', "'tempdb"') BEGIN use? EXEC ("'CREATE PROCEDURE PNewProcedure1 as SELECT name, CreateDate, updatedate from Sys.sysusers ORDER by Updatedate DESC"') END'EXECSp_msforeachdb@command
-- This query would return a listing of all files in the all databases on a SQL instance: EXEC ' "? " "' , Sf.filename, sf.size from Sys.sysfiles SF '
There is no direct way to query for cross-Library stored procedure calls:
DECLARE @sql NVARCHAR (+) = ' "%p_thisisastoredprocedure%"EXEC@sql
Run same command on all SQL Server databases without cursors