Question

This query runs fine within Management Studio, but got a few errors when trying to use bulk copy utility.

Here is what I am getting...

C:\>BCP     "SELECT title1, [precinct percent] AS [PrecinctPercent], leader, [leader
     percent] AS [LeaderPercent], Winner, WinningVotes, leader2, [leader2 percent] A
    S [LeaderPercent2], Loser, LosingVotes FROM 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 t
    hen [candidate num] end) as Loser, max(case when seqnum = 2 then Votes end) as L
    ossingVotes (select rc.*, row_number() over (partition by rc.[race.number] order
     by votes desc) as seqnum from dbo.[RACE CANDIDATES] rc ) rc group by rc.[race n
     umber] ) rc on r.[race number] = rc.[race.number] FOR XML PATH ('WQAD'), ROOT('r
    oot')" QUERYOUT "C:\Users\andersse\RESULTS222.XML" -c -t -T -S WQAD-ENG7\SQLEXPR
    ESS
    "SQLState = 37000, NativeError = 102
    Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax n
    ear '('.
    SQLState = 37000, NativeError = 102
    Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax n
    ear 'rc'.
    SQLState = 37000, NativeError = 8180
    Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could
    not be prepared."

Does anyone have any ideas why the () are not being read correctly as a query und BCP?

Thanks for reading this.

UPDATED QUERY

SELECT title1, 
    [precinct percent] AS [PrecinctPercent], 
    leader, 
    [leader percent] AS [LeaderPercent], 
    Winner, 
    WinningVotes, 
    leader2, 
    [leader2 percent] AS [Leader2Percent], 
    Loser, 
    LosingVotes 
    FROM 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 dbo.[RACE CANDIDATES] rc
           ) rc
      group by rc.[race number]
     ) rc
     on r.[race number] = rc.[race number]
     FOR XML PATH ('WQAD'), ROOT('root')

BCP QUERY

C:\>BCP "SELECT title1, [precinct percent] AS [PrecinctPercent], leader, [leader
 percent] AS [LeaderPercent], Winner, WinningVotes, leader2, [leader2 percent] A
S [LeaderPercent2], Loser, LosingVotes FROM 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 t
hen [candidate num] end) as Loser, max(case when seqnum = 2 then Votes end) as L
ossingVotes, (select rc.*, row_number() over (partition by rc.[race.number] orde
r by votes desc) as seqnum from dbo.[RACE CANDIDATES] rc ) rc group by rc.[race
number] ) rc on r.[race number] = rc.[race.number] FOR XML PATH ('WQAD'), ROOT('
root')" QUERYOUT "C:\Users\andersse\RESULTS222.XML" -c -t -T -S WQAD-ENG7\SQLEXP
RESS

ERROR CODES

SQLState = S0002, NativeError = 208
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object nam
e 'dbo.RACE'.
SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could
 not be prepared.

No correct solution

OTHER TIPS

You need to make sure BCP has the same database context as SSMS. For most queries, you can do this simply by saying:

FROM [databasename].dbo.[RACE]
...
FROM [databasename].dbo.[RACE CANDIDATES]

You may also need to ensure the correct database context by passing in the -d argument (as you need to do when using FOR XML with sqlcmd, as you discovered in this answer, though I couldn't reproduce that either with the 2012 tools), e.g.

BCP "your query with db prefixes" QUERYOUT "file" -c -t -Sserver -T -dDatabase

(See the full syntax and argument list in the documentation.)

You also need to make sure you are using the right copy of BCP; the -d argument wasn't added until 2012, so you may need to update your client tools to the most recent version. Here are the differences between the 2008 and 2012 version of BCP; note that -d is not available :

enter image description here

Note that for me the older version is the one that is called by default, due to my environment paths being set up based on the first versions of SQL Server I installed, not the last. So make sure that you are using the newest version explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top