title: How to Embed Variable Value in SQL Server query
body: <p>I need to be able to query Active Directory from SQL Server - see 
  <a href="https://www.mssqltips.com/sqlservertip/2580/querying-active-directory-data-from-sql-server/" rel="nofollow">https://www.mssqltips.com/sqlservertip/2580/querying-active-directory-data-from-sql-server/</a></p>
  
  <p>The following runs fine:</p>
  
  <pre><code>DECLARE @eid nvarchar(5)
  DECLARE @username nvarchar(5)
  
  SET @eid='123'
  
  SELECT @username=sAMAccountName FROM OpenQuery(ADSI,'SELECT sAMAccountName, employeeID FROM ''LDAP://mydomain.com/DC=mydomain,DC=com'' WHERE objectClass = ''User'' AND employeeID=123')
  PRINT  @username
  </code></pre>
  
  <p>How to replace the hardcoded 123 value with the actual value of the <code>@eid</code>, so that the modified code will run fine as well?</p>
  
  <p>Thanks!</p>