使用bcp实用程序将SQL查询导出到文本文件 [英] Using bcp utility to export SQL queries to a text file

查看:197
本文介绍了使用bcp实用程序将SQL查询导出到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试存储过程(SQL Server 2005),我需要在数据表中找到一些值。

I debug a stored procedure (SQL Server 2005) and I need to find out some values in a datatable.

该过程由应用程序的事件运行我只是看了调试输出。

The procedure is run by an event of the application and I watch just the debugging output.

我做了以下我的存储过程(SQL Server 2005),我以一个系统表(master.dbo.spt_values)为例:

I do the following my stored procedure (SQL Server 2005), I took a system table (master.dbo.spt_values) as example:

set @logtext = 'select name, type from master.dbo.spt_values where number=6'
--set @logtext = 'master.dbo.spt_values'
SET @cmd = 'bcp ' + @logtext + ' out "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL @cmd 

所以,当我取消注释第二个像所有的工作,一个文件在C:\驱动器上的惊喜,但如果我回来只留下第一行,就会产生任何输出。

So, when I uncomment the second like everything works, a file apprears on the C:\ drive... but if I coment it back leaving only the first line, any output is generated.

如何解决这个问题?

推荐答案

bcp out export tables。

bcp out exports tables.

要导出查询,请使用 queryout 你需要用双引号包装你的查询。

To export a query use queryout instead - you'll need to wrap your query in "double quotes"

set @logtext = '"select name, type from master.dbo.spt_values where number=6"' 
--set @logtext = 'master.dbo.spt_values' 
SET @cmd = 'bcp ' + @logtext + ' queryout "c:\spt_values.dat" -U uId -P uPass -c' 
EXEC master..XP_CMDSHELL @cmd  

http://msdn.microsoft.com/en-us/library/ms162802。 aspx

这篇关于使用bcp实用程序将SQL查询导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆