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)?


wanting 20081112 to be a variable declared above.

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

Who is Participating?
thewayne73Connect With a Mentor Author Commented:
using EXEC master..xp_cmdshell.  I did find a solution here:

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
how are you executing the bcp command?
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.