I want to export data using bcp command and the output of the bcp command should be stored in the variable.
My try:
DECLARE @Result varchar(max)
DECLARE @SQL nvarchar(max)
SET @SQL = N'Execute xp_cmdshell ''bcp "SELECT * FROM EMP" QueryOut "E:\BCP\Result.pec" -T -t@_@ -c -o "E:\BCP\LogReport.txt"'''
EXEC sp_executesql @SQL, N'@Result nvarchar(75) OUTPUT', @Result =@Result output
PRINT(@Result)
But getting an error in the output:
output
------------------------------------------------------------------------------
bcp: Unable to open output file E:\BCP\LogReport.txt: No such file or directory
NULL
Questions:
1. How to store the above output result into the variable?
2. Given permission to the file and folder too, but still getting this error.