using SQL query on unix [duplicate] - oracle

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Select columns into local variable from sql script using shell script
im trying to write a unix script that will retrieve a parameter using sql query and run a script afterwards with this parameter.
for the time being, im just tryin to make it echo the retrieved parameter.
the sql query that works fine on toad (oracle 8) is :
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043
the above query give a number.
now the script i wrote is:
#!/bin/bash
echo "this script will print the billcycle date"
v_bc=`sqlplus -s /#bscsprod <<EOF
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043`
echo "billcycle number is $v_bc"
the result when i run the file is
billcycle number is
with no number that follows.
any ideas what's wrong ? maybe the syntax for connecting to the sql server ?
thanks
Assaf.

The duplicate question APC linked to shows a working example, but to clarify you have two problems. The first is non-fatal and is just that you don't have EOF, as Rembunator pointed out (though it's in the wrong place in that answer).
More importantly though you don't have a terminating ; in your query, so SQL*Plus won't execute it - it just exits with no output.
If you typed your original query in as the SQL*Plus command prompt it would leave you at a further prompt waiting for input, and then go back to a normal prompt if you just hit return again, without actually executing the query:
SQL> select billcycle from bc_run
2 where billcycle not in (50,16)
3 and control_group_ind is null
4 and billseqno=6043
5
SQL>
You also probably want at least some formatting of the output. So this should work:
v_bc=`sqlplus -s /#bscsprod <<EOF
set pagesize 0
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043;
EOF`

I think you should end with EOF as well:
v_bc=`sqlplus -s /#bscsprod <<EOF
select billcycle from bc_run
where billcycle not in (50,16)
and control_group_ind is null
and billseqno=6043
EOF`
Edited: Oops, as corrected by Alex Poole.

Related

Oracle sqlplus execute sql script from command line while passing parameters to the sql script

I have one script script.sql which I want to execute from command line using oracle and passing it two parameters, as shown below
sqlplus user/pass # script.sql my_parameter1_value my_parameter2_value
What should it be in script.sql in order to be able to run it with the parameter values?
The solution can be prepared looking at oracle blogs:
https://blogs.oracle.com/opal/sqlplus-101-substitution-variables#2_7
For the question above, the solution would be to create a script.sql like this:
DEFINE START_VALUE = &1;
DEFINE STOP_VALUE = &2;
SELECT * FROM my_table
WHERE
value BETWEN &&START_VALUE AND &&STOP_VALUE;
I wanted to run a script that would return all orders raised during the last seven days. Here's how...
the script
SELECT * FROM orders_detail WHERE order_date BETWEEN '&1' AND '&2';
EXIT;
the command
sqlplus ot/Orcl1234#xepdb1 #"/opt/oracle/oradata/Custom Scripts/orders_between_dates.sql" $(date +%d-%b-%Y -d '-7 days') $(date +%d-%b-%Y)
Hope that helps someone. Luck.

sqlplus query with comment giving duplicate rows

Updating my question:
When I run this query from unix using sqplus it gives me duplicate outputs
Query:
select sysdate from dual
/
/*comment*/
EXIT
Output
SQL> #tt.sql
12-AUG-16
12-AUG-16
Here is what is happening.
The first two lines (including the /) form a complete SQL statement. SQLPlus executes it and displays the first line.
Then /*comment*/ is OUTSIDE the SQL statement. So it is interpreted by SQLPlus, not as a SQL comment.
The point is, comments in the SQLPlus scripting language have a different syntax; they are lines that begin with REM[ARK]. /*comment*/ is NOT a comment in SQLPlus.
Instead, in SQLPlus, a slash / as the first character on a new line means repeat the last SQL command (if after that you had SQLPlus commands, like DESCRIBE or COLUMN, those are ignored). Everything AFTER the slash is ignored. To convince yourself of this, try again, but with the comment line changed to /*comment* (delete the second slash). You will see the same result.
Then you have the command EXIT, but since it is NOT followed by a slash, it is not seen as a completed command. If I copy and paste your code in my SQLPlus window, I get the screenshot below. However, if at this point you press ENTER (or if this was in a sql script and not copied and pasted from a text editor), the SQLPlus window would shut down.
Hope this helps!
SQL> select sysdate from dual
2 /
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.00
SQL> /*comment*/
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.01
SQL> EXIT

Fetching SQL execution plan. PL/SQL Developer = strange behaviour. SQL*Plus = no rows selected

I have a problem with fetching SQL plans.
In the final form, I have to fetch them through PL*SQL.
So I have a bash script, a loop to iterate through sql plan names and parameters, and a code like this:
sqlplus -s /nolog > /dev/null 2>&1 <<EOF
CONNECT BLAH/BLAH#BLAH
*CLEAR AND SET COMMANDS HERE*
VARIABLE RC REFCURSOR;
SPOOL ${BERICHT}_${configArray[0]}.DATA
SET TIMING ON;
EXEC :RC := $aktuellesBericht;
SET TIMING OFF;
PRINT RC;
DISCONNECT
QUIT
EOF
And the second part: (logging in as SYS, but without the SYSDBA permissions, I dont have them and I dont think that I will have them...)
sqlplus -s /nolog > /dev/null 2>&1 <<EOF
CONNECT SYSBLAH/SYSBLAH#BLAH
SPOOL ${BERICHT}_${configArray[0]}.SQLPLAN.TXT
CLEAR BREAK
CLEAR COMP
CLEAR COL
select
sqlplan.operation,
sqlplan.options,
sqlplan.object_name,
sqlplan.cost,
sqlplan.depth
from v\$sqlarea sqlarea,
v\$session sesion,
v\$sql_plan sqlplan
where sesion.sql_hash_value = sqlarea.hash_value
and sesion.sql_address = sqlarea.address
and sqlarea.plan_hash_value = sqlplan.plan_hash_value
and sesion.username = 'BLAH' order by sqlplan.depth;
QUIT
EOF
All I can get from this is *.SQLPLAN.TXT files containing just one sentence: no rows selected
What is strange here, that when I do the same in PL/SQL Developer - i get the same results, BUT when I just click on the Auto Refresh timer button on both SQL windows, both queries are running in parallel, and SOMETIMES the second query (the one to fetch SQL plan) is giving me results. And sometimes it doesnt.
It seems that theese commands need to be run in parallel... or am I missing something?
The sql query in my question was never needed.
All I needed is to write to the admin team to grant me the SELECT premissions on:
V$SQL_PLAN, V$SESSION and V$SQL_PLAN_STATISTICS_ALL
The answer to my question was found here.
I will need a deeper understanding of the problem in the future but for now, FWIK, looking deeper into the dbms_xplan package gave me the idea.
The approach to get the queries from the SYSACC account was the wrong idea. This acc has access to all the queries history and it is hard and painful (if even possible) to husk out the sql plan I needed.

Execute plsql dynamiclly via SSH in remote server (using dynamic login info) and get returned value to executing server

I am trying to execute a dynamic plsql command in a remove machine (say 192.168.x.x) and get it's return value to local machine from where I initiate shell script. I am testing two approaches to do this. But none of them seems to work properly.
Approach - I
in this approach if i supply values for ssh and sqlplus (login user/ip and schema user/ip) via shell variable it won't work. working code with hardcoded value is below.
#!/bin/sh
varfld="SYSDATE"
retVal=`echo "SELECT $varfld FROM dual;" | ssh utility#192.168.x.x 'sqlplus -S utility/pwd' | tail -2 | head -1`
echo "return value 1: "$retVal
Approach - II
In this approach I can pass everything i need in a variable. But the plsql command (i.e value of variable $ssh_execute_command) is not recognized inside plsql. Only hardcoded plsql command gets executed.
P.S.This works fine with vsql with few modification to connect Vertica.
v_server_user=utility
v_server_name=192.168.x.x
ssh_v_schema_name=utility
ssh_v_schema_pwd=pwd
varfld="SYSDATE"
execute_command="SELECT $varfld FROM dual;"
retVal=$(ssh $v_server_user#$v_server_name ssh_v_schema_name=$v_schema_name ssh_v_schema_pwd=$v_schema_pwd ssh_execute_command=\""$execute_command"\" 'bash -s' <<SSHSQLTEXT
sqlplus -S $ssh_v_schema_name/$ssh_v_schema_pwd
SET ECHO OFF
SET HEADING OFF
SET FEEDBACK OFF
SELECT SYSDATE FROM dual;
SSHSQLTEXT
)
echo "return value 2: "$retVal
Queries
1. How can we make sql command passed in a variable make work inside pqlplus? or
2. How can we pass required values for ssh and sqlplus (login user/ip and schema user/ip) dynamically and make it work?
The sql statement "SELECT SYSDATE FROM dual;" used here is for testing purpose only. I will be calling package function instead to get return value. And this is yet to be tried in any of these scenarios. If anyone could address that too with example, that would be great!
Thanks in advance.
You need to assign the result of sqlplus to a variable which must be displayed within ssh so that the result is passed to variable retVal. Try the following code:
retVal=$(ssh $v_server_user#$v_server_name ssh_v_schema_name=$v_schema_name ssh_v_schema_pwd=$v_schema_pwd ssh_execute_command=\""$execute_command"\" 'bash -s' <<SSHSQLTEXT
sqlresult=`sqlplus -S $ssh_v_schema_name/$ssh_v_schema_pwd <<EOF
SET ECHO OFF
SET HEADING OFF
SET FEEDBACK OFF
SELECT SYSDATE FROM dual;
exit
EOF`
echo $sqlresult
SSHSQLTEXT
)
echo "return value 2: "$retVal
If you execute the above block of codes, you will have the sysdate as result in $retval

Issue with the SQL* PLUS connections in a Shell Script

Can we have multiple SQL* PLUS connections in a shell script?
I have written a shell script to copy the data of tables from one database to another using COPY command of SQL* PLUS. I don't have the privilege to create Database Link so, I am using the COPY command.
I need to copy the data of around 50 tables. When the dataset is small, it runs and copies the data of all the tables. But when the dataset is huge, it gets stuck and I get session inactive message in the unix machine.
I thought of splitting the statements and wrote it as below: But I am getting the error "SP2-0042: unknown command "END1" - rest of line ignored." and "SP2-0042: unknown command "END" - rest of line ignored."
#!/bin/bash
export ORACLE_HOME=/ora00/app/oracle/product/9.2.0.8
export PATH=$PATH:$ORACLE_HOME/bin
args=$#
if [ $args == 1 ]
then
echo "Shell script started"
else
echo "Wrong number of arguments"
exit 1
fi
time_start=`date +%H%M%S`
echo $time_start
sqlplus -s srcUN/srcPwd#srcSID <<END1
COPY from srcUN/srcPwd#srcSID to dstUN/dstPwd#dstSID INSERT tab1 USING SELECT * FROM tab1 WHERE col1 = $1;
COPY from srcUN/srcPwd#srcSID to dstUN/dstPwd#dstSID INSERT tab2 USING SELECT * FROM tab2 WHERE col1 = $1;
END1
sqlplus -s srcUN/srcPwd#srcSID <<END2
COPY from srcUN/srcPwd#srcSID to dstUN/dstPwd#dstSID INSERT tab3 USING SELECT * FROM tab3 WHERE col1 = $1;
END2
#END
Could you please help me resolve this?
Thanks,
Savitha
The problem is that END1 and END2 are not recognized as the end of the input redirection because they have leading whitespace.
Remove all the whitespace on these two lines and it should work.

Resources