title: Creating SQL Server login via script using variable
body: <p>I am trying to create a SQL Server login following the creation of a database through script.
  The login is local to the host PC and this script will be run on multiple hosts.</p>
  
  <p>What I want to do is the following:</p>
  
  <pre><code>USING MyDatabase
  CREATE MyUser FOR LOGIN USER &lt;computer name&gt;/MyUser 
  </code></pre>
  
  <p>What I don't know how to do is incorporate the computer name (ex. Location0001) into this statement.  </p>
  
  <p>The login does exist on each machine AND is already listed on the SQL Server as it has access to other databases there.</p>
  
  <p>If it's relevant, the script should be able to run on SQL Server 2008.</p>
  
  <p>The login would look like this for each machine:</p>
  
  <pre><code>Location0001\MyUser
  Location0002\MyUser
  Location0999\MyUser
  </code></pre>
  
  <p>Any help would be appreciate, thanks.</p>
  
  <p><em><strong>Edit: My final solution based on the accepted answer is as follows:</em></strong></p>
  
  <pre><code>DECLARE @cmd VARCHAR(200)
  SET @cmd = N'CREATE USER [MyUser] FOR LOGIN [' + HOST_NAME() + '\MyUser]'
  EXEC (@cmd)
  </code></pre>