title: Failed scheduling a task involving linked servers with SQL Server Agent
body: <p>How can I make a scheduled task work?  I want to schedule a task that runs a query nightly.  The query parses correctly and essentially grabs data from the SQL Server 2005 database on server2 and inserts the data into a table in a database on server1.  The query and the task run on server1. </p>
  
  <p>I am able to execute the query in Microsoft SQL Server Management Studio on server1.   Using SQL Server Agent, I am able to create and schedule a job to execute the query but the job fails with the following error:  Executed as user NT AUTHORITY\SYSTEM. Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON' [SQLSTATE 28000] [Error 18456].  IT at my company speculates that I need to be added to the interactive logon list for server2, which is part of the policy.</p>
  
  <p>I have Admin privileges on both server1 and server2, though the latter should not be necessary I think.  Both server1 and server2 use Windows authentication, which I gather is set up upon installation of SQL Server.</p>
  
  <p>Here is what I did.  First I added server2 to sys.sysservers on server1 with the following command:<br>
  EXEC sp_addlinkedserver @server='server2'</p>
  
  <p>Then I created a mapping between logins on server1 and server2 with:  EXEC sp_addlinkedsrvlogin @rmtsrvname = 'server2', @useself = 'true'</p>
  
  <p>That allowed me to to execute the query in Microsoft SQL Server Management Studio on server1.  It worked.  What I found to be curious was that the following didn’t work although "Command(s) completed successfully":</p>
  
  <p>EXEC sp_addlinkedsrvlogin @rmtsrvname = 'server2', @useself = 'false', @locallogin = 'MYDOMAIN\myname',  @rmtuser = 'myname',  @rmtpassword = '<strong><em>*</em>**<em>*</em>*</strong>' </p>
  
  <p>(Note that real password is unencrypted because of Windows authentication.).  </p>
  
  <p>I got the error
  Msg 18456, Level 14, State 1, Line 1
  Login failed for user 'myname'.</p>
  
  <p>A value of @useself = TRUE specifies that logins use their own credentials to connect to rmtsrvname, with the rmtuser and rmtpassword arguments being ignored. FALSE specifies that the rmtuser and rmtpassword arguments are used to connect to rmtsrvname for the specified locallogin.   I would have expected both to work since what I specified for rmtuser and rmtpassword are exactly what I use to log in to the remote server directly. </p>