Issue with the SQL* PLUS connections in a Shell Script - oracle

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.

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.

how to insert csv data to an oracle table?

Hi I am new to shell scripting and trying to insert data in a csv file which i generated to an oracle database table. my code is below
FilePath =/oracle/some path
Filename=test.csv
X='sqlplus -s username/password#dbname<< EOF
set heading off pages 0 feedback off
SELECT a.user_id,a.email_id FROM addresses a where
a.user_id in('ABCD','EFGH','IJKL','MNOP');
EXIT
EOF`
While read line
do
echo $line
sqlplus -s username/password#dbname<< EOF
insert into sometable (uid,email) values ($line);
commit;
exit
EOF
done<<
Can someone tell me what i amdoing wrong as data is not getting inserted.A sample working code will be deeply appreciated.

Hive query in Shell Script

I have an external hive table on top of a parquet file.
CREATE EXTERNAL TABLE parquet_test LIKE avro_test STORED AS PARQUET LOCATION 'hdfs://myParquetFilesPath';
I want to get the count of table using shell script.
I tried with following command
myVar =$(hive -S -e " select count(*) from parquet_test;")
echo $myVar
Added -S to run hive in silent mode still I get whole map reduce log and count in the myVar variable. How to get only count.
I don't have access to any of the configuration file to enable or disable the level of logging. Is there any other way?
Finally found a work around.
First flushed the query result into a file in HDFS then read answer from file.
The file only contains the result of the query.
(hive -S -e " INSERT OVERWRITE LOCAL DIRECTORY '/home/test/result/'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' select count(*) from parquet_test;")
Then reading the file into a variable
Count var=$(hdfs dfs -tail /home/test/result/)
echo $var
Thank you
myVar=$(eval "hive -S -e 'select count(*) from parquet_test;' ")
echo $myVar

using SQL query on unix [duplicate]

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.

Multiple Line Variable into SQLPlus from Shell Script

What is the best way to pass multiple values from one variable into separate records in an oracle db?
I want to take the output from:
hddlist=`iostat -Dl|awk '{print ""$1"="$(NF)}'
This returns output like this:
hdisk36=0.8
hdisk37=0.8
hdisk38=0.8
hdisk40=5.5
hdisk52=4.9
I want to insert them into a database like so:
sqlplus -s /nolog <<EOF1
connect / as sysdba
set verify off
insert into my_table ##Single Record Here
EOF1
How can I systematically separate out the values so i can create individual records that look like this:
Disk Value
--------- -------
hdisk36 0.8
hdisk37 0.8
hdisk38 0.8
hdisk40 5.5
hdisk52 4.9
I originally tried a while loop with a counter but could not seem to get it to work. An exact solution would be nice but some directional advice would be just as helpful.
Loop and generate insert statements.
sql=$(iostat -Dl | awk '{print ""$1"="$(NF)}' | while IFS== read -r k v ; do
printf 'insert into mytable (k, v) values (%s, %s);\n' "$k" "$v"
done)
This output can be passed in some manner to sqlplus, perhaps like this
sqlplus -s /nolog <<EOF1
connect / as sysdba
set verify off
$sql
EOF1
Although, depending on the line format of iostat, it might be simpler to just omit awk and parse with read directly.
You can redirect the output to a file and then use an external table
It should look something like this:
CREATE TABLE hddlist_ext_table (
disk CHAR(16),
value CHAR(3)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER DEFAULT DIRECTORY tab_dir
ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY '=')
LOCATION ('your_file_name'));
Then you can either use this table for your data or insert-select from it to your table;
insert into my_table
select disk, value from hddlist_ext_table;
You can insert multiple rows in a single SQL statement in Oracle like this
INSERT ALL
INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;
If you intend to run this script automatically at intervals to then see the results of each disk, you will probably need additional columns to hold the date and time.
You might also look at sqlldr as you can specify a control file telling it what your data contains and then this will load the data into a table. It is more suited to the purpose if you are loading lots of data than SQL Plus.

Resources