BCP命令的开关 -Q

------------------------------------------------------------------------

-- Author:  happyflystone 

-- Date  :  2009-04-14 16:39:19

-- Ver:     Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)

--        Apr 14 2006 01:12:25

--          Copyright (c) 1988-2005 Microsoft Corporation

--          Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)

-- Blog:blog.csdn.net/happyflystone

-- 注意:转载保留此信息

--------------------------------------------------------------------------

 

 

先看看bcp的帮助吧

exec xp_cmdshell 'bcp/?’

 

/*

output

--------------------------------------

用法:  bcp {dbtable | query} {in | out | queryout | format} datafile

  [-m maxerrors]            [-f formatfile]          [-e errfile]

  [-F firstrow]             [-L lastrow]             [-b batchsize]

  [-n native type]          [-c character type]      [-w wide character type]

  [-N keep non-text native] [-V file format version] [-q quoted identifier]

  [-C code page specifier]  [-t field terminator]    [-r row terminator]

  [-i inputfile]            [-o outfile]             [-a packetsize]

  [-S server name]          [-U username]            [-P password]

  [-T trusted connection]   [-v version]             [-R regional enable]

  [-k keep null values]     [-E keep identity values]

  [-h "load hints"]

NULL

*/

测试环境:

create database [test-2009]

go

use [test-2009]

go

create table ta(id int)

insert ta select 1

insert ta select 2

go

好,利用bcp输出ta表的数据,(注意:我只用了信息连接,测试时自行修改,可使用-S –U –P,同时注意如果有多个实例时记得使用-S开关)

exec xp_cmdshell 'bcp test-2009.dbo.ta out "c:/t.txt" -T -c -Sflystone/sql2005 '

 

/*

output

------------------------------------

SQLState = 37000, NativeError = 102

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]'-' 附近有语法错误。

NULL

 

*/

好嘛,这个错误很好搞定的呀,加一个[]就可以了呀

 

exec xp_cmdshell 'bcp [test-2009].dbo.ta out "c:/t.txt" -T -c -Sflystone/sql2005 '

/*

output

-------------------------------------------

SQLState = 37000, NativeError = 4060

Error = [Microsoft][ODBC SQL Server Driver][SQL Server]无法打开登录所请求的数据库"[test-2009]"。登录失败。

NULL

 

*/

    晕,无法登录,这不是胡说嘛,不过你要是用queryout测试却是成功的

exec xp_cmdshell 'bcp "select * from [test-2009].dbo.ta" queryout "c:/t.txt" -T -c -Sflystone/sql2005 '

 

/*

output

------------------------------------

NULL

开始复制...

NULL

已复制了4 行。

数据包的大小(字节): 4096

时钟时间(毫秒):     10

NULL

 

 

*/

    那么如何直接使用表名导出数据呢,好,下面我看这个-q这个开关项,先给出解决方案

 

exec xp_cmdshell 'bcp "test-2009".dbo.ta out "c:/t.txt" -T -c -Sflystone/sql2005 -q'

 

/*

output

-----------------------------------------

NULL

开始复制...

NULL

已复制了4 行。

数据包的大小(字节): 4096

时钟时间(毫秒):     10

NULL

 

*/

 

使用此选项可以指定包含空格或单引号的数据库、所有者、表或视图的名称。将由三部分组成的整个表名或视图名用英文双引号 ("") 括起来。 若要指定包含空格或单引号的数据库名称,必须使用 -q 选项。其实这和对象名中包括‘-‘也不在备注说明中呀,可是事实上却可以解决这个问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值