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

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.

Related

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

Random error in Unix shell script

I have a shell script which in turn calls sql file. Its a bash shell running in UNIX. Following is the main steps taken in script.
1) Generate Term file
2) Remove previous day's Term and Rpt file from utility directory.
3) Copy Term file from Run directory to Utility directory.
4) Run the sql file
5) Copy the output, RPT file from Utility to Run directory.
Here is the code snippet:
> RUN_DIR/nj.terms
if [[ -s RUN_DIR/nj.terms ]] then
rm -f /utl/nj.terms
rm -f /utl/nj.rpt
cp RUN_DIR/nj.terms utl
/bin/sqlplus USER PSWD #sql
cp utl/nj.RPT RUN_DIR
fi
I get following error from sql output as :
ORA-29283 - Invalid file operation.
Mostly this error is due to absence of Term file whenever the sql runs. Due this error RPT file will not be generated and cause failure in following copy command (cp utl/nj.RPT RUN_DIR).After the failure, when we checked the Term file ,it was present in Utl directory.
This error occurs randomly. Is there any chance system takes more time to copy the Term file to Utility directory and before completing it sql was run? It would be great if someone can help me in this situation.

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.

Running a bat file with BCP command, getting connection error

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.

Syntax error on shell script calling a sql script

I am trying to execute the following command at shell prompt:
nohup sqlplus DB_ID/DB_PWD#DOMAIN #main.sql 490 >> result.out 2>>&1 &
main.sql is a sql script that accepts 490 as argument.
I get error:
bash: syntax error near unexpected token `&'
What is wrong with the syntax?
The syntax error comes from your redirection of STDERR to STDOUT. The required (and only valid) syntax is 2>&1. It still does what you expect from it. The >> you do in the STDOUT redirection only helps for actual files and prevents the file data to be erased. For pipe redirection, this is not required and not even allowed syntax-wise.
The final correct syntax is
nohup sqlplus DB_ID/DB_PWD#DOMAIN #main.sql 490 >> result.out 2>&1 &