title: SQL insert into - from different server and credentials
body: <p>We have one database existing on server A.  Server A also hosts our program code which will be calling the SQL statement.</p>
  
  <p>We have another database <code>VMIntranetTest</code> existing on server B <code>VMC-MMS</code></p>
  
  <p>Server A and server B have different logon user credentials.  Server A and server B both exist on our internal network.</p>
  
  <p>Using PHP, I have the following SQL statement defined.</p>
  
  <pre><code>$strSql = 'INSERT INTO VMC-MMS.VMIntranetTest.dbo.TestTable (FirstName, LastName, Age) ' .
                      'SELECT FNAME, LNAME, AGE ' .
                      'FROM BSLIB.SQLTSTF ';
  </code></pre>
  
  <p><code>FROM -&gt; BSLIB.SQLTSTF &lt;-</code> is on our local server (A), so my connection string used to execute the statement will have the user credentials to connect to server A.</p>
  
  <p><code>INSERT INTO -&gt; VMC-MMS.VMIntranetTest.dbo.TestTable &lt;-</code> is the different server.database.dbo.table (Server B).</p>
  
  <p>How do I specify the user credentials to be used for the  INSERT INTO portion of the statement?  The secondary portion containing the SELECT FROM statement should already be covered by my initial connection string.</p>
  
  <p>Thank you,</p>
  
  <p><strong>Edit 1</strong> in regards to Paul's answer.</p>
  
  <p>I've attempted to use the <code>OPENROWSET</code> as mentioned, and have the following SQL statement.</p>
  
  <pre><code>INSERT INTO VMIntranetTest.TestTable (FirstName, LastName, Age)
  OPENROWSET('vmas400',
             'Server=192.168.1.2;Trusted_Connection=yes;user_id=INTRAIS;password=****',
             'SELECT FNAME, LNAME, AGE FROM BSLIB.SQLTSTF' ) as a 
  </code></pre>
  
  <p>As you can see, I changed things around a little bit.  My connection string through the code opens the connection to Server B "VMC-MMS".  My SQL statement "select" portion, uses the OPENROWSET to open a connection to Server A "192.168.1.2".</p>
  
  <p>However, I am getting this error message:</p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 1 near "OPENROWSET": syntax error</p>
  </blockquote>
  
  <p><strong>Edit 2</strong>  i needed to put the entire OPENROWSET portion inside a VALUES (    )  clause.  Now I'm getting a message:</p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 1 no such table: VMIntranetTest.TestTable</p>
  </blockquote>
  
  <p><strong>Edit 3</strong></p>
  
  <p>I've now got the following SQL</p>
  
  <pre><code> INSERT INTO VMIntranetTest.TestTable (FirstName, LastName, Age)
   select a.FNAME, a.LNAME, a.AGE FROM 
   OPENROWSET('vmas400',
              'Server=192.168.1.2;Trusted_Connection=yes;user_id=INTRAIS;password=****',
              'SELECT FNAME, LNAME, AGE FROM BSLIB.SQLTSTF' ) as a
  </code></pre>
  
  <p>And am getting this error:</p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 1 near "(": syntax error</p>
  </blockquote>