Monday, May 4, 2015

Copying data from SQL Server to Excel using BCP utility

Below is the sample code to copy data from SQL server to Excel through BCP (Bulk Copy Program) uitlity.

declare @logtext varchar(100)
declare @cmd varchar(1000)
--set @logtext = 'Select top 10 * from Server.DB.Table'
SET @cmd = 'bcp "Select top 10 * from Server.DB.Table" queryout "c:\test\check.csv" -c -S Test/BK -T -t,'
EXEC master..XP_CMDSHELL @cmd

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.