title: Node.js SQL How to Run Query that Spans Over Two Databases
body: <p>I am building an application that has a backend that uses SQL queries to get data from a SQL Server database. However, I need to write a query that truncates and repopulates a table in that database using data from a second database. Here is what my code looks like:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// establishes a connection to serverName and uses DB1 as the database. But how can you access two?
  global.config = {
      user: 'username',
      password: 'password',
      server: 'serverName', 
      database: 'DB1'
  };
  
  // run this query. It's already been tested in SQL server and works fine there
  let query = "TRUNCATE TABLE [DB1].[dbo].[Shop]; INSERT INTO [DB1].[dbo].[Shop] (Shop, shopDescription, Address, City)" +
          " SELECT Shop, Description, Address, City FROM [DB2].[dbo].[ShopTable]"
          
              new sql.ConnectionPool(config).connect().then(pool =&gt; {
               return pool.request().query(query) }).then(
                 result =&gt; {
                     console.log(result.recordset)
                     //result returns as "undefined"
                     res.setHeader('Access-Control-Allow-Origin', '*')
                     res.status(200);
                     sql.close();
                   }).catch(err =&gt; { //error is not thrown
                     res.status(500).send({ message: err})
                     sql.close();
                   });</code></pre>
  </div>
  </div>
  </p>
  
  <p>I get an "undefined" result, and find that no update to the table was made. The issue here isn't exactly clear whether it can't reach the table in DB2, or if perhaps the command doesn't work with the Node.js mssql package?</p>