I want to send a query result to a file, so tried to use bcp command. But can't pass any parameters to that. It shows error.
EXEC xp_cmdshell 'bcp "SELECT * FROM CG.dbo.cdyy where EndTime between DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,'+@date+'),0)) and DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,1,'+@date+')+1,0)) " queryout "D:\cdr_cg.txt" -T -c -t,'
To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.
There are two ways to pass parameters to a stored procedure using SQLExec. One way, which works across all versions of Visual FoxPro, is to build the SQL command as a string variable. The advantage of this method is that you can check the string and see exactly which SQL command you are passing to the back end.
Put your parametrs before a call master..xp_cmdshell
DECLARE @date varchar(10) = '20130311',
@bcp varchar(8000)
SELECT @bcp = 'bcp "SELECT * FROM CG.dbo.cdyy WHERE EndTime between DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,''' + @date + '''),0)) AND DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,1,''' + @date + ''')+1,0)) " queryout "D:\cdr_cg.txt" -T -c -t,'
EXEC master..xp_cmdshell @bcp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With