Wednesday, September 2, 2009

Export data to file using BCP In SQL server 2005

Here I provide T-SQL statement for export table data to a text file using BCP command.
First you must enabe xp_cmdshell option in surface area configuration or using below script
EXEC sp_configure ’show advanced options’, 1
GO

—- To update the currently configured value for advanced options.
RECONFIGURE
GO
—- To enable the feature.
EXEC sp_configure ‘xp_cmdshell’, 1
GO
—- To update the currently configured value for this feature.
RECONFIGURE
GO
Example:
DECLARE @isqlString varchar(255)
SELECT @isqlString = 'isql -Q "select * from sys.sysprocesses" -E -o c:\sp_blocks_log.txt'
EXEC master..xp_cmdshell @isqlString
EXEC master..xp_cmdshell'bcp "SELECT * FROM sys.databases" queryout "c:\text.txt" -c -T -x'
EXEC master..xp_cmdshell'bcp "SELECT * FROM sys.sysprocesses" queryout "c:\text.txt" -c -T -x'


No comments:

Post a Comment