[ACCEPTED]-Using bcp utility to export SQL queries to a text file-bcp

Accepted answer
Score: 17

bcp out exports tables.

To export a query use queryout instead 2 - you'll need to wrap your query in "double 1 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  


More Related questions