SQL Server: bcp with dynamic file name with date


SET QUOTED_IDENTIFIER ON


declare
@yesterday varchar(20),
@filename varchar(200),
@bcpcommand varchar(500)

select @yesterday=convert(varchar,DateAdd("d", -1, GETDATE()),112)

set @filename  = 'E:\Temp\MYFILE_' + @yesterday +'.csv'

print @filename

set @bcpcommand = 'bcp "select * from databasename.dbo.table" queryout'+ ' '+ @filename + ' -c -t, -T -SMYSERVER\MYINSTANCE'

print @bcpcommand

exec master..xp_cmdshell @bcpcommand

Comments

Popular posts from this blog

Restoring Master, Model and MSDB from Netbackup

Oracle Job Operations