Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

sql server - Using bcp utility to export SQL queries to a text file

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.

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 

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.

How to fix this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

bcp out exports tables.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...