Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to a bcp command in sql server

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,'
like image 449
Kamal Avatar asked Mar 11 '13 09:03

Kamal


People also ask

How do you pass a parameter to a procedure?

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.

Can we pass parameters to stored procedure in SQL?

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.


1 Answers

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
like image 169
Aleksandr Fedorenko Avatar answered Sep 28 '22 09:09

Aleksandr Fedorenko