I am trying to create a SQL query using bcp to output a text file that uses today's date as the file name but when I try to set queryout to output the value of the date variable I either get an error or a text file that is the name of the variable - not it's value. Can anyone help?
Below is my best attempt:
Text
bcp "DECLARE @AcctNum as varchar(10), @FilePath as varchar(100) SET @FilePath = '"'+REPLACE('c:\temp\'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-')+'"' @AcctNum = '1234567890' SELECT @AcctNum, CheckNumber, CheckDate, Description FROM DBSVR.DBNAME..APCHECK AS APCHECK_1 WHERE (CheckDate = CAST(GETDATE() AS DATE)) AND BankOID = '3'" queryout @FilePath -c -T
Starting copy...
SQLState = 37000, NativeError = 102
Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near...