此查询在Management Studio中运行正常,但在尝试使用批量复制实用程序时遇到一些错误。
这是我得到的......
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."
有没有人知道为什么()作为查询和BCP没有被正确读取?
感谢您阅读本文。
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
错误代码
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.
答案 0 :(得分:3)
您需要确保BCP与SSMS具有相同的数据库上下文。对于大多数查询,您只需说:
即可FROM [databasename].dbo.[RACE]
...
FROM [databasename].dbo.[RACE CANDIDATES]
您可能还需要通过传递-d
参数来确保正确的数据库上下文(正如您在FOR XML
中使用sqlcmd
所需要的那样,正如您在{{3}中发现的那样虽然我无法使用2012工具重现这一点,例如
BCP "your query with db prefixes" QUERYOUT "file" -c -t -Sserver -T -dDatabase
(见this answer。)
您还需要确保使用正确的BCP副本;直到2012年才添加-d
参数,因此您可能需要将客户端工具更新到最新版本。以下是2008年和2012年版BCP之间的差异;请注意-d
不可用:
请注意,对我来说,旧版本是默认调用的版本,因为我的环境路径是根据我安装的SQL Server的第一个版本设置的,而不是最后一个版本。因此,请确保您明确使用最新版本。