Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateStamp on file output

Status
Not open for further replies.

Llazwas

IS-IT--Management
Feb 16, 2007
58
US
I have a job that queries a view then exports the contents to a text file using cbp and xp_cmdshell. Everything works fine but I'm trying to add a datestamp on the file name and can't figure it out. Any assistance is greatly appreciated.

DECLARE @_FileName VARCHAR(256)
SELECT @_FileName = 'c:\payment_initiative' + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(30), GETDATE(), 20), '-', ''), ' ', ''), ':', '') + '.txt'
EXEC master..xp_cmdshell 'bcp "SELECT * FROM ITMAS500_app.dbo.Payment_Initiative" queryout = @_FileName -T -c '
 
Try
Code:
DECLARE @FileName VARCHAR(256), @cmd varchar(4000)
SELECT @FileName = 'c:\payment_initiative' + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(30), GETDATE(), 20), '-', ''), ' ', ''), ':', '') + '.txt' 
set @cmd = 'bcp "SELECT * FROM ITMAS500_app.dbo.Payment_Initiative" queryout = "' + @FileName +'" -T -c  ' 

EXEC master..xp_cmdshell @cmd

PluralSight Learning Library
 
Thanks Markros. I tried it and it's still not working. It's actually not generating the file at all. If I perform the basic output such as below it works:

EXEC master..xp_cmdshell 'bcp "SELECT * FROM MAS500_app.dbo.vPayment_Initiative" queryout "C:\payment_initiative.txt" -T -c'

Oddly enough it doesn't error out and the logs show success when I try to add the date stamp code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top