title: Can and how do I use multiple databases at the same time in SQL with the USE statement?
body: <p>My goal is to find the affected stored procedures across multiple databases when a table or view is updated. I am working in an environment with multiple databases where these stored procedures can exist. Below is a query that can do what I want for one database.</p>
  
  <p>How can I achieve the results without having to change the USE statement to DatabaseB, DatabaseC, etc. or lengthy queries involving UNIONs?</p>
  
  <pre><code>USE DatabaseA
  
  SELECT
  
          O.name
      ,   O.type_desc
      ,   M.definition
  
  FROM sys.sql_modules M
  
  LEFT JOIN sys.objects O
  ON O.object_id = M.object_id
  
  WHERE 1=1
      AND definition LIKE '%Error%'
  </code></pre>
  
  <p>I have played around with looping, but to no avail.</p>
  
  <pre><code>DECLARE name_Cursor CURSOR FOR
  SELECT name
  FROM master.dbo.sysdatabases
  OPEN name_Cursor;
  FETCH NEXT FROM name_Cursor;
  WHILE @@FETCH_STATUS = 0
      BEGIN
          FETCH NEXT FROM name_Cursor;
      END;
  CLOSE name_Cursor;
  DEALLOCATE name_Cursor;
  GO
  </code></pre>