Add variable to data_file in bcp.exe

Hello Experts,

I have a bcp commend to export data from a query.  Is it possible to add a variable to the data_file (output file)?

"\\servername\folder\file_20081112.txt"

wanting 20081112 to be a variable declared above.

"\\servername\folder\file_" + @run_date + ".txt"

Thanks
thewayne73Asked:
Who is Participating?
 
thewayne73Connect With a Mentor Author Commented:
using EXEC master..xp_cmdshell.  I did find a solution here:  http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell

DECLARE @FileName varchar(50),
        @bcpCommand varchar(2000)

SET @FileName = REPLACE('c:\authors_'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-')

SET @bcpCommand = 'bcp "SELECT * FROM pubs..authors ORDER BY au_lname" queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U garth -P pw -c'

PRINT @bcpCommand

-- Results --

bcp "SELECT * FROM pubs..authors ORDER BY au_lname"
  queryout "c:\authors_06-30-01.txt" -U garth -P pw -c
0
 
BrandonGalderisiCommented:
how are you executing the bcp command?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.