Sql-server – How to use BCP Utility queryout in a network path with crededential (login required) for SQL Server

authenticationbcpNetworksql serverxp-cmdshell

I'm having a hard time figuring out on how can I export my selected table in a network path that requires login credentials.

'bcp "select ''StudentName'' UNION ALL SELECT StudentName AS StudentName from [School].[dbo].[ClassRoom]" "queryout \\IPAddress\d$\Log\StudentLog_'+@fileTimeStamp+'_01'+'.'+@fileExtension+'" -c -t, -T -S' + @@servername 

Running this gives me an error of

SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 11 for SQL Server]Unable to open BCP host data-file
NULL

Where should i put in the script the authentication for my network path?

Best Answer

The BCP utility does not have option to provide credentials for network path authentication where output file will reside. One alternative is to map the network path as local drive with "net use" providing User/password to authenticate. Then use local drive in BCP command. e.g. net use e: \IPAddress\d$\ /user:<domain\login> /savecred

Related Question