title: SQL Server : USE database precedence issue
body: <p>I am having issues with using a <code>USE Database; GO</code> when the statement is within an if..else statement. My guess is that the <code>USE Database; GO</code> was suppose to be used at the top of the script or sql statement. </p>
  
  <p>Maybe someone could help me come up with a different way of approaching this, what I am trying to do is, check if a particular database exists if it does drop a particular user access from that specific database. The way I was thinking of doing it is as follows ...</p>
  
  <pre><code>IF EXISTS(select * from sys.databases where name='MyDB')
   BEGIN
     USE MyDB
     GO
     DROP USER [tester]
     .
     .
     .
   END
  ELSE
   PRINT 'MyDB database is not available'
  </code></pre>
  
  <p>I chopped the sql script to a smaller version so I don't clutter this post so be aware there are some other logic that has been left out after <code>DROP USER</code></p>
  
  <p>EDITED:
  I have tried various ways to check if the db exists so everyone knows
  Tried</p>
  
  <pre><code>if db_id('MyDB') is not null
  or
  if object_id('MyDB', 'U')
  </code></pre>
  
  <p>But, it always goes directly to <code>use MyDb</code> first and I don't know why and how to work around it.</p>