title: Using sqlcmd with FOR XML returns Invalid Object name 'dbo.Table' yet Query runs in SSMS
body: <p>This is my query:</p>
  
  <pre><code>SELECT title1, 
      [precinct percent] AS [PrecinctPercent], 
      leader, 
      [leader percent] AS [LeaderPercent], 
      Winner, 
      WinningVotes, 
      leader2, 
      [leader2 percent] AS [Leader2Percent], 
      Loser, 
      LosingVotes 
      FROM [leader].dbo.[RACE]  r inner join
       (select rc.[race number],
               max(case when seqnum = 1 then [candidate num] end) as Winner,
               max(case when seqnum = 1 then Votes end) as WinningVotes,
               max(case when seqnum = 2 then [candidate num] end) as Loser,
               max(case when seqnum = 2 then Votes end) as LosingVotes
        from (select rc.*,
                     row_number() over (partition by rc.[race number] order by votes desc) as seqnum
              from [leader].dbo.[RACE CANDIDATES] rc
             ) rc
        group by rc.[race number]
       ) rc
       on r.[race number] = rc.[race number]
       FOR XML PATH ('WQAD'), ROOT('root')
  </code></pre>
  
  <p>This query runs and produces some results I desire. I am trying to get the XML file to output as a file. I have been able to accomplish this by opening the xml in Server Management Studio, then running a VB Script to remane the temporary xml file, then move that xml to it's destination. Hey...I gotta do what I gotta do to get these results ON AIR.</p>
  
  <p>Through my travels I have tried to accomlish this using sqlcmd. here is what I am trying to run:</p>
  
  <pre><code>sqlcmd -S WQAD-SEVE\SQLEXPRESS -i C:\Users\localtv\Desktop\QUERIES\THISONE22 .sql -o C:\Users\localtv\Desktop\RESULTS236.xml
  </code></pre>
  
  <p>It executes, but when I open/edit the XML file it outputs, I get: </p>
  
  <blockquote>
    <p>Msg 208, Level 16, State 1, Server WQAD-SEVE\SQLEXPRESS, Line 1
    Invalid object name 'dbo.RACE'.</p>
  </blockquote>
  
  <p>I have tried inserting [database].dbo.RACE, but still run into the same issue. I have been working on this for weeks now and am trying to figure out a way to get this to work.</p>
  
  <p>Also, when I try to run the command without the -o output destination, the command line echos the same error message. From the look of my query, it makes it through quite a bit before it encounter dbo.RACE.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thank you in advance for your valued assistance.</p>
  
  <p>***I have removed the [databasename] and .dbo. as well, still Shows RACE as an invalid object.</p>