DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + 'SELECT db = ''' + name + ''', o.name, o.type_desc
FROM ' + QUOTENAME(name) + '.sys.sql_modules AS m
INNER JOIN ' + QUOTENAME(name) + '.sys.objects AS o
ON m.[object_id] = o.[object_id]
WHERE m.definition LIKE N''%'' + @Search + ''%''
ORDER BY o.type_desc, o.name;'
FROM sys.databases
WHERE database_id > 4 AND state = 0; -- online
EXEC sp_executesql @sql, N'@Search NVARCHAR(255)', N'sp_reset_data';
严格来说,如果你只想要程序,那就简单一点(上面还会包括函数,触发器,甚至视图):
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + 'SELECT db = ''' + name + ''', o.name
FROM ' + QUOTENAME(name) + '.sys.sql_modules AS m
INNER JOIN ' + QUOTENAME(name) + '.sys.procedures AS o
ON m.[object_id] = o.[object_id]
WHERE m.definition LIKE N''%'' + @Search + ''%''
ORDER BY o.name;'
FROM sys.databases
WHERE database_id > 4 AND state = 0; -- online
EXEC sp_executesql @sql, N'@Search NVARCHAR(255)', N'sp_reset_data';