Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

SQL query using bcp not working with queryout as a variable

$
0
0

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 
Run from a DOS box this produces:

Starting copy...
SQLState = 37000, NativeError = 102
Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near...


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>