I want to save the stored procedure output directly to a file.
I have used the BCP command, but still its not saving the file.
This is the command using BCP:
bcp " SELECT TOP (1000) column FROM table " queryout "D:\spoutput.csv" -S servername -U username -P password -c -T
I would use Powershell for this instead of BCP unless there's some feature of
bcpthat you specifically need it for. I may be in the minority, but I findbcpkind of clunky to work with. It's great for quickly loading data, but for more routine tasks like this, I prefer a different approach.You can use
Invoke-SqlCmdto execute any arbitrary SQL you want; in this case, a stored procedure. Then pipe the results of that toExport-Csvto directly write it to a file (you could also pipe it toConvertTo-Csvif you wanted to work with the CSV more in Powershell).I've found that most common things you can configure for outputting a file through
bcpyou can also do through PowerShell. And in fact, powershell can do some thingsbcpcannot such as handling quoting of fields.