What does the following expression do <<EOF>tmp - oracle

A shell script with a database connection using sqlplus contains this line:
sqlplus username/password#DBNAME<<EOF>tmp.
(here there is a select sql query)
I know <<EOF indicates the start of the command. I have the below doubts in the >tmp part.
1)Does it indicate storing into a file?
2) If yes will it put the results of the SQL query alone into the tmp file or the query and the results both.
Could you please clear me on this one?

<<EOF is known as "heredocs". It means that from the next line in the script, up to a line that says "EOF", will be read in by the shell, and offered to the command on standard input.
In your case, the sqlplus command is being fed the SQL query as if it came from standard input. Then, the output of sqlplus is being saved to the file called tmp.
The input and output are separate. However, sqlplus as a command has a habit of repeating its input to the output, unless instructed otherwise with SET ECHO OFF.

Related

Create batch file to run sql

I'm trying to create a batch file to run a sql query (which then spools the result).
However, at the moment nothing happens, I just get usage info about sqlplus in the cmd window. Here is my batch code:
SQLPLUS login/password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<url>)(Port=<port>))(CONNECT_DATA=(SID=<sid>)))#C:\Users\Documents\SQL\test2.sql
pause
and here is my test2.sql file:
spool C:\Users\Documents\Testing.csv
select *
from test;
spool off;
exit;
Please help - what am I doing wrong? (I'm very new to this so please answer/ask/ridicule in simple terms so I can understand)
It's something obvious you're doing wrong. You need a space between the end of the connect string and the # with the script file name:
SQLPLUS login/password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<url>)(Port=<port>))(CONNECT_DATA=(SID=<sid>))) #C:\Users\Documents\SQL\test2.sql
The <url> reference is odd here; hopefully that is actually hostname, fully-qualified hostname (that can be resolved), or an IP addrss.
However, I'd suggest you use a TNS alias so you can shorten the command line, putting all that information in a tnsnames.ora file instead; or if you know the service name (which might be the same as the SID) use the easy connect syntax:
SQLPLUS -l -s login/password#//<hostname>:<port>/<service_name> #C:\Users\Documents\SQL\test2.sql
I've also thrown in the -s flag to suppress the banner, and the -l flag so it stops if the credentials or connect string are wrong. Your original ersion would have tried to use the script contents to log in after the initial failure, producing even more confusing output.

Is there any way to run impala shell with sql script with parameters?

Is there any way to run impala shell with SQL script with parameters?
For example:
impala-shell -f /home/john/sql/load.sql /dir1/dir2/dir3/data_file
I got errors:
Error, could not parse arguments "-f /home/john/sql/load.sql /dir1/dir2/dir3/data_file”
This feature is available in CDH 5.7 / Impala 2.5 and higher.
The --var option lets you pass substitution variables to the statements that are executed by that impala-shell session, for example the statements in a script file processed by the -f option. You encode the substitution variable on the command line using the notation --var=variable_name=value. Within a SQL statement, you substitute the value by using the notation ${var:variable_name}.
See more details directly in the documentation : https://www.cloudera.com/documentation/enterprise/latest/topics/impala_set.html
No, you can specify a file of sql statements with -f, but it does not take a file of parameters. See the impala-shell documentation for more details:
http://www.cloudera.com/content/cloudera/en/documentation/core/latest/topics/impala_impala_shell.html

how to know that update statement in sqlplus updated the tabel or not(from shell script)

I need to update a table with some where conditon. If update fails then I need to insert the same. I am using here document from a shell script to run sqlplus, please help me to find the way so I can insert in case of faliur of update.
Redirect the output to a temporary file and don't forget to include stderr as well. Then you can parse the output and look for characteristics. In your SQL you can also do a
select "SUCCESS" from dual;
at the end, the shell script could look for.
sqlplus user#db #my_sqlfile >output 2>&1
grep "SUCCESS" output
sqlplus also usually writes in the output something like
N records updated.
So you can also check if there are as many updates as you expect if you need this.
Just be sure to set the options in your sql file appropriately because this affects what sqlplus will spill out.
set term ...

Writing sqlplus output to a file

Using sqlplus.exe I'm looking for a way to write sqlplus output to a file.
Is there anyway I can do that, currently the output is written only to the console.
You may use the SPOOL command to write the information to a file.
Before executing any command type the following:
SPOOL <output file path>
All commands output following will be written to the output file.
To stop command output writing type
SPOOL OFF
Also note that the SPOOL output is driven by a few SQLPlus settings:
SET LINESIZE nn - maximum line width; if the output is longer it will wrap to display the contents of each result row.
SET TRIMSPOOL OFF|ON - if set OFF (the default), every output line will be padded to LINESIZE. If set ON, every output line will be trimmed.
SET PAGESIZE nn - number of lines to output for each repetition of the header. If set to zero, no header is output; just the detail.
Those are the biggies, but there are some others to consider if you just want the output without all the SQLPlus chatter.
Make sure you have the access to the directory you are trying to spool. I tried to spool to root and it did not created the file (e.g c:\test.txt). You can check where you are spooling by issuing spool command.
just to save my own deductions from all this is (for saving DBMS_OUTPUT output on the client, using sqlplus):
no matter if i use Toad/with polling or sqlplus, for a long running script with occasional dbms_output.put_line commands, i will get the output in the end of the script execution
set serveroutput on; and dbms_output.enable(); should be present in the script
to save the output SPOOL command was not enough to get the DBMS_OUTPUT lines printed to a file - had to use the usual > windows CMD redirection. the passwords etc. can be given to the empty prompt, after invoking sqlplus. also the "/" directives and the "exit;" command should be put either inside the script, or given interactively as the password above (unless it is specified during the invocation of sqlplus)
In windows type this command:
sqlplus.exe username/password#servicename #yourquery.sql > out.txt
besides, you should write an exit command at the end of "yourquery.sql" file.

DOS/sqlplus "Handle is invalid" error?

I have the following batch script:
sqlplus ms/ms#orcl < drop.sql
sqlplus ms/ms#orcl < create.1.0.sql
This works fine when I double click on the bat file in Windows Explorer and run it.
But when I type the command name from the DOS prompt I get an error:
C:\>create.bat
C:\>sqlplus ms/ms#orcl 0<drop.sql
The handle is invalid.
C:\>sqlplus ms/ms#orcl 0<create.1.0.sql
The handle is invalid
Any ideas?
Update: Made the change to use # instead of <. This gets around the error but now the script only executes the first file and then leaves you at the SQL> prompt. To get the second file to execute you have to type exit at the prompt, then the second file runs. Not sure how to get both files to execute. ??
If you want SQL*PLUS to execute a script, a better way than command-line redirection is the # syntax:
sqlplus ms/ms#orcl #drop.sql
sqlplus ms/ms#orcl #create.1.0.sql
Also read up on the ## syntax, which you can use to execute a .sql script from another .sql script in the same directory.
SQL> help #
SQL> help ##
Made the change to use # instead of <.
This gets around the error but now the
script only executes the first file
and then leaves you at the SQL>
prompt. To get the second file to
execute you have to type exit at the
prompt, then the second file runs. Not
sure how to get both files to execute.
??
I ended up writing a python script to run sql plus and then exit it to get around this issue. I have yet to find any way around that limitation. Although you would only have to deal with the issue once if you use ## as was suggested. You would just have to run the second script from the first script.

Resources