title: I am trying to create a stored procedure to create a login and a database user?
body: <p>I am developing a C# application with SQL Server, and would like to call a stored procedure to create a login and a user. </p>
  
  <p>I am looking for the simplest way to do this, just default stuff but have username and password.</p>
  
  <p>The create next to login is underlined in red?</p>
  
  <p>But I think my login is fine? Please tell me if I'm missing something.</p>
  
  <p>And with the create user, is it just a one liner like that?</p>
  
  <pre><code>CREATE PROCEDURE CreateLoginAndUser 
         @UserName
  
     CREATE LOGIN @UserName 
     WITH PASSWORD = 'password', 
          DEFAULT_DATABASE = [TestAudit],
          DEFAULT_LANGUAGE = [British],
          CHECK_EXPIRATION=OFF,
          CHECK_POLICY=ON;
  
     CREATE USER 'DEV' + @UserName 
         [{ FOR | FROM } LOGIN @UserName] [WITH DEFAULT_SCHEMA = schema]
  </code></pre>
  
  <p>I don't understand <code>FOR</code> and <code>FROM</code> ...</p>
  
  <p>Could I do something like this:</p>
  
  <pre><code>CREATE PROCEDURE CreateLoginAndUser
       @UserName
  
      CREATE LOGIN @UserName  
      WITH PASSWORD = 'password', 
           DEFAULT_SCHEMA = schema    
  
      CREATE USER 'DEV'+@UserName 
         [{ FOR | FROM } LOGIN @UserName] [WITH DEFAULT_SCHEMA = schema]
  </code></pre>
  
  <p>How have Googled the following sources:</p>
  
  <p><a href="http://ss64.com/sql/login_c.html" rel="nofollow">http://ss64.com/sql/login_c.html</a></p>
  
  <p><a href="http://ss64.com/sql/user_c.html" rel="nofollow">http://ss64.com/sql/user_c.html</a>   </p>