这个 bcp 实用程序有什么问题?
我正在尝试使用 bcp 实用程序将数据导出到文本文件中?我无法弄清楚我在这里做错了什么:-
Declare @Cmd nvarchar(1000)
Declare @Query nvarchar(1000)
Declare @Path nvarchar(500)
Declare @ServerName nvarchar(500)
Declare @UserName nvarchar(50)
Declare @Password nvarchar(50)
Declare @Delimiter char(1)
SET @Path = 'c:\abc.txt'
SET @ServerName = '10.10.10.10\instance1'
Set @UserName = 'sa'
Set @Password = '123456'
Set @Delimiter = '!'
SET @Query = 'SELECT [ID]
, NULL
,[NAME]
,[STREET1] + '' '' + [STREET2] as [Address]
,[CITY] + '' '' + [PROVINCE] + '' '' + [POSTAL] as City
,[PHONE]
,[FAX]
,''Title'' as [Title]
,[Country]
FROM [temp01]'
SET @Cmd = 'bcp "' + @Query + '" queryout "' + @Path +
'" -c -S' + @ServerName + ' -U' + @UserName + ' -P' +
@Password + ' -t' + @Delimiter + ''
EXEC master..xp_cmdshell @Cmd
它没有将任何数据写入文件。在结果框中,我得到以下输出:-
用法:bcp {dbtable |查询} {in |出 |查询输出 |格式}数据文件 [-m 最大错误数] [-f 格式文件] [-e 错误文件] [-F 第一行] [-L 最后行] [-b 批量大小] [-n 本机类型] [-c 字符类型] [-w 宽字符类型] [-N 保留非文本本机] [-V 文件格式版本] [-q 带引号的标识符] [-C 代码页说明符] [-t 字段终止符] [-r 行终止符] [-i 输入文件] [-o 输出文件] [-a 数据包大小] [-S 服务器名称] [-U 用户名] [-P 密码] [-T 可信连接] [-v 版本] [-R 区域启用] [-k 保留空值] [-E 保留标识值] [-h“加载提示”] 无效的 [-h“加载提示”] NULL
请帮忙。
I am trying to export data into into a text file using bcp utility? I could n't figure out what i am doing wrong here:-
Declare @Cmd nvarchar(1000)
Declare @Query nvarchar(1000)
Declare @Path nvarchar(500)
Declare @ServerName nvarchar(500)
Declare @UserName nvarchar(50)
Declare @Password nvarchar(50)
Declare @Delimiter char(1)
SET @Path = 'c:\abc.txt'
SET @ServerName = '10.10.10.10\instance1'
Set @UserName = 'sa'
Set @Password = '123456'
Set @Delimiter = '!'
SET @Query = 'SELECT [ID]
, NULL
,[NAME]
,[STREET1] + '' '' + [STREET2] as [Address]
,[CITY] + '' '' + [PROVINCE] + '' '' + [POSTAL] as City
,[PHONE]
,[FAX]
,''Title'' as [Title]
,[Country]
FROM [temp01]'
SET @Cmd = 'bcp "' + @Query + '" queryout "' + @Path +
'" -c -S' + @ServerName + ' -U' + @UserName + ' -P' +
@Password + ' -t' + @Delimiter + ''
EXEC master..xp_cmdshell @Cmd
It is not writing any data into the file. In result box i am getting this output:-
usage: 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
[-h "load hints"] NULL
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个问题很老了,但我也遇到过同样的问题。您需要将整个命令驻留在一行中。删除@Query 和@Cmd 中的所有换行符。
This question is pretty old, but I've had the same issue. You need the entire command to reside on a single line. Take out any line breaks in @Query and @Cmd.
我认为你的 -S 开关后面需要一个空格。另外,一般来说,在调试复杂的东西时,会降低复杂性,直到您能够确定问题出在哪里。在这种情况下,您可以一次移除每个开关(或移除所有开关以开始),以确定这是否导致问题。
I think you need a space after your -S switch. Also, in general, when debugging something complex reduce the complexity until you can identify where the problem is. In this case, you can remove each switch one at a time (or remove all of them to start) to determine whether or not that's causing the problem.
SET @Cmd = 'bcp "' + REPLACE(REPLACE(REPLACE(,CHAR(9),CHAR(32)),CHAR(13),CHAR(32)),CHAR(10),'')@Query + ' “查询输出”'+@Path+
'" -c -S' + @服务器名 + ' -U' + @用户名 + ' -P' +
@密码+'-t'+@Delimiter+''
SET @Cmd = 'bcp "' + REPLACE(REPLACE(REPLACE(,CHAR(9),CHAR(32)),CHAR(13),CHAR(32)),CHAR(10),'')@Query + '" queryout "' + @Path +
'" -c -S' + @ServerName + ' -U' + @UserName + ' -P' +
@Password + ' -t' + @Delimiter + ''
你必须提供一个数据库。您只定义了实例和表,但没有定义数据库。尝试更改您的选择语句,例如: ...from [yourDB].[temp01]...
You do have to give a database. you only defined the instance an a table but no database. try change your select-statemen like : ...from [yourDB].[temp01]...
我在数据库中尝试时发现了同样的问题,
因此我得出的结论是
您正在使用“
如果您使用
将工作正常,查询中没有任何新行”
只是因为它不接受之间的空间。
谢谢
I found the Same Issue While trying in my Database,
Hence I concluded that
You are using
If U use
Will Work Fine Without any new line in the Query
Just because it is not accepting the Space in between.
Thanks