我这里的测试结果create table #t (
a int,
b int
)
insert #t select 1,1 union all select 1,2
EXEC master..xp_cmdshell 'BCP "SELECT * FROM tempdb..#t" queryout "C:\test.csv" -c -t, -T -S"Haiwer-PC" -U"sa" -P"sa"'
output                                                                                                                                                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
SQLState = 01000, NativeError = 2701
Warning = [Microsoft][ODBC SQL Server Driver][SQL Server]忽略了数据库名称 'tempdb',将引用 tempdb 中的对象。
SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]对象名 '#t' 无效。
SQLState = 37000, NativeError = 8180
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]未能准备语句。
NULL(所影响的行数为 7 行)select * into ##t from #tEXEC master..xp_cmdshell 'BCP "SELECT * FROM tempdb..##t" queryout "C:\test.csv" -c -t, -T -S"Haiwer-PC" -U"sa" -P"sa"'output                                                                                                                                                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
NULL
开始复制...
NULL
已复制了 2 行。
数据包的大小(字节): 4096
时钟时间(毫秒): 共      2
NULL(所影响的行数为 7 行)

解决方案 »

  1.   

    select * into ##temptable from ...
    EXEC master..xp_cmdshell 'BCP "SELECT * FROM  tempdb.dbo.##temptable" queryout "C:\test.csv" -c -t, -T -S"ABCDO01\SQL_SERVER" -U"sa" -P"123"'
    drop table ##temptable
      

  2.   


    对 
    我这里就是这样
    output                                                                                                                                                                                                                                                          
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    SQLState = 01000, NativeError = 2701
    Warning = [Microsoft][ODBC SQL Server Driver][SQL Server]忽略了数据库名称 'tempdb',将引用 tempdb 中的对象。
    SQLState = S0002, NativeError = 208
    Error = [Microsoft][ODBC SQL Server Driver][SQL Server]对象名 '#t' 无效。
    SQLState = 37000, NativeError = 8180
    Error = [Microsoft][ODBC SQL Server Driver][SQL Server]未能准备语句。
    NULL
      

  3.   

    Yang_哪里又冒出来一个小地主啊,好有米。
    ------------------------------你确定你前段时间成功过吗?会不会是你的幻覺哦。BCP使用的是另外一根连接,怎么可能见的到你目前连接中创建的#tmptable呢
      

  4.   


    select * into ##temptable from ... 
    EXEC master..xp_cmdshell 'BCP "SELECT * FROM  tempdb.dbo.##temptable" queryout "C:\test.csv" -c -t, -T -S"ABCDO01\SQL_SERVER" -U"sa" -P"123"' 
    drop table ##temptable
    這樣看看,我測試是成功的
    output                                                                                                                                                                                                                                                          
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    NULL
    Starting copy...
    NULL
    9 rows copied.
    Network packet size (bytes): 4096
    Clock Time (ms.): total        1
    NULL(7 row(s) affected)
      

  5.   

    xp_cmdshell 是一个新的连接, 所以不能访问当前连接中的本地临时表改用全局临时表(两个##开头), 或者正式表就可以了
      

  6.   

    另外, 由于 xp_cmdshell 是开一个新连接
    所以不要把生成表和 xp_cmdshell 放在一个事务中, 否则你会发现你的处理死在那儿不动