title: How can I remove a specific security user from all databases on a server matching a given name?
body: <p>I have a setup similar to this:</p>
  
  <blockquote>
    <ul>
    <li>SQL-2005-SRVR
    <ul>
    <li>Security
    <ul>
    <li><strong>MYAPP_LOGIN</strong></li>
    </ul></li>
    <li>Database1
    <ul>
    <li>Security
    <ul>
    <li><strong>MYAPP_USER</strong></li>
    <li>OTHERUSER</li>
    </ul></li>
    </ul></li>
    <li>Database2
    <ul>
    <li>Security
    <ul>
    <li>OTHERUSER</li>
    </ul></li>
    </ul></li>
    <li>Database3
    <ul>
    <li>Security
    <ul>
    <li><strong>MYAPP_USER</strong></li>
    <li>TESTUSER</li>
    </ul></li>
    </ul></li>
    <li>Database4
    <ul>
    <li>Security
    <ul>
    <li><strong>MYAPP_USER</strong></li>
    </ul></li>
    </ul></li>
    <li>Database5
    <ul>
    <li>Security
    <ul>
    <li>OTHERUSER</li>
    <li>TESTUSER</li>
    </ul></li>
    </ul></li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>I am trying to figure out how to write a SQL script that will remove all users matching <code>MYAPP_USER</code> from all databases on the server. I am not familiar with how to iterate over multiple databases from within a single script, or even how to query if the user exists within that database. Additionally, I am unsure what context/catalog this script would be run from - <code>master</code>, <code>msdb</code>, <code>model</code>?</p>
  
  <p>In the above example, <code>Database1</code>, <code>Database3</code>, and <code>Database4</code> would all have their users named <code>MYAPP_USER</code> dropped.</p>
  
  <p>I also need to remove the login from the server itself, but I know that's as easy as <code>DROP LOGIN MYAPP_LOGIN</code>.</p>
  
  <p>The script will be run as a server administrator, so permissions are not an issue.</p>
  
  <p>How would I go about removing all of the users in each database that match the name <code>MYAPP_USER</code>?</p>