Running a bat file with BCP command, getting connection error - sql

I have a bat file that is using the bcp command to execute a stored procedure to a delimited file. When manually running the bat file, I get the following errors:
I'm using the -T parameter as I log into the database with Windows Authentication. Is there a setting I may need to change to fix this error?

The -S [DATABASE HOST NAME] argument needed to be passed to the bcp command.

Related

How to execute a file.sql (script) and save the results in a file in my computer using CMD (SQLCMD)?

I need to run a script saved in my computer through CMD (SQLCMD) and save the result in other file in my computer.
Use SQLCMD to run a script from command line, see:
https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-2017
To save output to a file, look into using pipes for your output, see:
Redirect Windows cmd stdout and stderr to a single file
or
https://helpdeskgeek.com/how-to/redirect-output-from-command-line-to-text-file/
You can run the script and then create another backup.
You can try mssql-scripter that is a multiplatform SQL command line tools such as sqlcmd.
Use bcp
bcp "SELECT * FROM TABLE" queryout file.csv -t";" -SSERVERNAME -T -c -e errors.txt

BCP unable to open host data file when migrating to Azure

I haver a sample GTFS data file I am attempting to load to Azure. The command I am using is:
bcp %fullTablePath% in %data_dir% -f %format_file% -S %server% -U %username% -P %password% -k -F %first_row%
where the parameters are replaced accordingly.
When I execute the command, I get:
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Unable to open BCP host data-file
This is not a file naming issue because the file is indeed there. If I deliberately spell the file incorrectly, I get the same error. Sounds like a permission issue but who do I grant what permission?
If you see this error in batch file but not from PowerShell check that your
%fullTablePath% or %data_dir% variables are full qualify path/absolute path and not relative path as PowerShell and Batch may have different default path settings which may give you the error of:
Unable to open BCP host data-file

ORA-12545: Connect failed because target host or object does not exist while connecting through the shell

I am trying to run the sql scripts from shell. My scripts are working fine. It is getting connected to database and applying the sql files. Only thing I am not able to understand is why the below error message is getting logged every time.
Error Message :
ERROR:
ORA-12545: Connect failed because target host or object does not exist
Shell Script:
/opt/ORACLE/app/oracle/product/11.2.0/client_1/bin/sqlplus -s <<eoj >>$LOG_FIL 2>&1
${DBUSER1}/${DBPASS}#${hostBillingDBSID}
#${SQLParm} $RPT_FIL
eoj
try the below.
Shell Script:
#let's include oracle installation in the PATH variable
export PATH=$PATH:/opt/ORACLE/app/oracle/product/11.2.0/client_1/bin
#now just use sqlplus, instead of full path reference.
sqlplus -s ${DBUSER1}/${DBPASS}#${hostBillingDBSID} <<eoj >>$LOG_FIL 2>&1
#${SQLParm} $RPT_FIL
eoj
The user/password(connection string) has to be passed as command line arguments to sqlplus.

rsync folder out of apache server in php

This command I can run in command line correctly.
>sshpass -p 'xxxx' rsync -rve ssh /var/www/html/my_profect/image server2#192.168.xxx.xxx:/var/www/html/project2
But I want to call this command from php page using exec(). but when i call from php page rsync wasn't moving any file and no error was given.
Use passthru() instead of exec() to get raw shell output.
Use var_dump(passthru($command)); to view output.

How to write an error message from an BCP operation to a log file and stop processing the batch file.

I have a BCP operation in a batch command file. When there is an error in the BCP Operation I need to stop the processing and write the err msg in the log file
I've used to -e option to write the error message during a BCP operation to a err file. The err file is getting created in the location but does not contain any error message written to it.
My BCP statement is like this.
BCP DbName.dbo.tableName In FileLocation -e Errorfile -S ServerName -T -c
Is there a way to get the error level and then stop the processing?
Appreciate quick help.
The -m argument specifies max errors. "A row that cannot be copied by the bcp utility is ignored and is counted as one error. If this option is not included, the default is 10."
The -e argument specifies the file that the data is stored in that bcp cannot write to the output file.
The bcp utility is reporting the error to the DOS environment. Run bcp from a DOS script and capture/write the error from the DOS environment.