Logging .sql file output to log file in shell script - sql

I have an sql file being called from shell script, and I want to capture the output of the execution into a log file. I am trying to capture by appending redirecting to log file like this >> log.txt but it is not working.
What do I need to do to fix it?
sqlplus -s ${USER}/${PWD}#${DATABASE} << EOF
#${LOADDIR}/VV_validation.sql
EOF'

Try this:
sqlplus -s ${USER}/${PWD}#${DATABASE}
spool your_file_name.log
##${LOADDIR}/VV_validation.sql
spool off

Related

Executing BTEQ file via shell script (BTEQ: Command not found error)

I'm trying to set up an environment to execute BTEQ script via shell script in the local machine. On running the shell script I'm getting an error of BTEQ: Command not found. Not sure what I'm doing wrong.
I've created a separate .tdlogon file which contains .LOGON credentials. BTEQ script is a simple create table statement that I'm trying to execute.
My .tdlogon file is something like
.logon servername/uname,pwd
I'm calling the file like this
#!/bin/bash
server_path=/Users/xyz/xyz
log_path=/Users/xyz/xyz/logs
echo -e 'Starting the script'>> ${log_path}/test_log.log
cat ${server_path}/.tdlogon ${server_path}/code/temp_query.btq | bteq >> ${log_path}/test_log.log 2>&1
if [ ${rtn_code} -ne 0 ] ; then
echo -e 'Script completed successfully'>> ${log_path}/test_log.log
exit 0
else
echo -e 'Error in the script'>> ${log_path}/test_log.log
exit 1
fi
On executing the above code I'm getting below error in the log file
line 10: bteq: command not found
Appreciate any guidance related to this.
Seems like your Linux is not pointing to the bteq path. Update the bteq path:
export PATH=/usr/bin/bteq:$PATH
And, in some cases, there will be bteq32 instead of bteq in that case set path as:
export PATH=/usr/bin/bteq32:$PATH

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

Executing a SQL file in interactive impala-shell session

In an interactive impala-shell session, is there a way to load and execute a text file containing one or more SQL statements? In Hive's beeline, for example, you can use !run <filename> to run the SQL commands in that file.
This is not currently possible. You can file a JIRA.
I believe it is possible - see impala-shell -h (version v2.1.1-cdh5):
-f QUERY_FILE, --query_file=QUERY_FILE
Execute the queries in the query file, delimited by ;
[default: none]
combine this with shell command in interactive mode:
shell impala-shell -f file;

How to save a SQLCMD code as a executable file

I have a set of SQLCMD codes which are to be saved as exe files so that people can just execute the file rather than copy paste the code and run the command in command line.
Write your sql code to file for example script.sql and than create a batch file (for example run.bat) with below command
sqlcmd -U user -P passwrod -S SerwerName -i C:\script.sql

Execute multiple files at one go using a single bat file

I am using a batch file to execute multiple sql files.
So I have created a bat file like:
osql -S ServerName -U user -P password -d DBTest -i C:\SQLFILES\Test1.sql
pause
The above code executes a single file Test1.sql and if I need to execute the next file I have to again modify the bat file and change the file name.I am having 10 such sql files and I want to execute them one after another. Is there any way to do this at one go?
Well, what you could definitely do is give your BAT file a parameter (so you don't have to constantly change the BAT file contents...):
ExecSQL.bat:
osql -S ServerName -U user -P password -d DBTest -i %1
pause
and then you can call this batch file like this:
c:\> ExecSQL C:\SQLFILES\Test1.sql
and then
c:\> ExecSQL C:\SQLFILES\Test2.sql
and so forth