SQL Server Exporting a Table to a Text File Part 2


This post continues from the first part.

	DECLARE @sql varchar(1000)
	DECLARE @sqlselect as varchar(1000)

	SET @sqlselect = 'select firstname from myDatabase.dbo.Authors'

	SELECT @sql = 'bcp "' + @sqlselect 
			+ '" queryout "c:\data\temp stuff\Test.txt" -c -T -S'
			+ @@servername
	EXEC master..xp_cmdshell @sql

In this example the directory has a space in it so we need to enclose the path and file name in double quotes.