If I want to query all databases on a server to see if a stored procedure is present, I can combine a sp_executesql and run a cursor over a query similar to the following:
SELECT
'select ' + '''' + name + '''' + ', name from [' + name
+ '].sys.procedures WHERE name = ''usp_MyProc'' COLLATE SQL_Latin1_General_CP1_CI_AI '
FROM sys.databases
-- I get a collation error from the following
WHERE name NOT IN ('ReportServer', 'ReportServerTempDb');
Can I do the same thing without dynamic sql and without the cursor?
Best Answer
You can do it without the cursor (or without a cursor-like operation). For example you can do it slightly differently like this:
You can also avoid multiple result sets going back by doing this:
If you're really only after a single, specific stored procedure though, see Martin's answer.