日期:2014-05-17 浏览次数:20728 次
--这是个例子,我假设要把test库的temtable表的applicationID,Category两列导出成CSV文件
if OBJECT_ID('temtable','u')is not null
drop table temtable
select * into temtable from (select 'applicationID'as [applicationID],'Category' as [Category] --表头
union all
select CONVERT(varchar(20),[applicationID]), [Category] from #temtable)a
declare @filename nvarchar(30)
select @filename='Report'+right(replace(convert(date,GETDATE()),'-',''),2)+substring(replace(convert(date,GETDATE()),'-',''),5,2)+substring(replace(convert(date,GETDATE()),'-',''),1,4)+'.csv'
------------------------------------------
declare @strSQL nvarchar(1024)
set @strSQL='bcp "SELECT applicationID,Category FROM test.dbo.temtable" queryout C:\Temp\DBA_date\'+@filename+' -c -T -t","'
print @strSQL
EXEC master..xp_cmdshell @strSQL