I want to change output format of my oracle sql script.
Consider I have a script named: active_user.sh which inside it I just write my query. Now the problem is when I bash the script the output displayed without caption and only values are shown.
The script is:
export CONNECT_STRING=$1
if [ x$2 == x ]
then echo First Parameter is connection string to DB and Second parameter have to be ORACLE_HOME variable && exit 1
else export ORACLE_HOME=$2
fi
export ORACLE_SID=OMEGA #fake
export PATH=$ORACLE_HOME/bin:$PATH
RAND=$$
sqlplus -s /nolog <<-EOF > /tmp/${RAND}.sql_out.temp
connect $CONNECT_STRING
set HEADING OFF
set PAGESIZE 0
set linesize 120
col metric_name format a40
col value format 999999990.9999
select count(*) from v\$session where username is not null and status='ACTIVE';
EOF
cat /tmp/${RAND}.sql_out.temp
And this is the command to run the script and the output is:
[root#oracle-test scripts]# ./active_users.sh "ora/orapass123#mydb" /opt/oracle/instantclient_11_2
1
23.0000
But when I run the query in sqlplus it returns something like this:
COUNT(*)
----------
1
If you want to print the column headings, you have to put set HEADING ON.
You can also using SPOOL command, something like this (sorry, I can't testint now):
spool /tmp/${RAND}.sql_out.temp
set heading on
set pagesize 1000
set tab on
set linesize 120 wrap off
column "yourcount" format a40
col metric_name format a40
col value format 999999990.9999
select count(*) yourcount from v\$session where username is not null and status='ACTIVE';
Related
This is my shell script.
echo "Start";echo #/opt/apps/Tests/SQLDir/Test1.sql | sqlplus Db1/Db1#//maydomain:port/abc;echo "Finish";
echo "Start";echo #/opt/apps/Tests/SQLDir/Test2.sql | sqlplus Db1/Db1#//maydomain:port/abc;echo "Finish";
I have 30 .sql files like this, added in one .sh file which results 30 .csv files
Test1.sql has
SPOOL /opt/apps/Tests/OF/output1.csv REPLACE;
select name from username where id = 10 and Sname is not NULL and ROWNUM < = 50000;
Test2.sql has
SPOOL /opt/apps/Tests/OF/output2.csv REPLACE;
select strname,ctyname from addr where city = 'NYC' and ROWNUM < = 50000;
My expected OP in output1.csv is
name
Abc
xyz
pqr
My expected OP in output2.csv is
strname | ctyname
10-AP NYC
11-KP MCH
90-ZP SDK
right now I am getting weird o/p in csv
name
-------------------------------
Abc
xyz
pqr
name
-------------------------------
TYU
KLH
50000 rows selected.
SQL>
So is there any way to remove those additional lines [--------- and 50000 rows selected.] with shell script/sql code?
And while executing shell script all sql result rows are getting printed on screen. how to avoid that?
Thanks in advance.
Following SQL*Plus command should do the job:
set markup csv on delimiter ' ' quote off
set feedback off
I must say, your method of passing the name of a script to sqlplus is the strangest I've ever seen. The usual practice (given your names) would be:
sqlplus Db1/Db1#//maydomain:port/abc #/opt/apps/Tests/SQLDir/Test1.sql
I don't see where your 'echo Start' and 'echo finished' accomplish anything, since there is no clarifying info coming along with it.
It looks to me like what you want in your scripts would be
set echo off trimsp on head off pagesize 0
spool /opt/apps/Tests/OF/output2.csv replace
select strname,ctyname from addr where city = 'NYC' and ROWNUM < = 50000;
spool off
BTW, 'spool' is a sqlplus command - a directive to sqlplus itself, not a sql statement. As such, it does not need a semi-colon at the end.
-- edit
Example of using environment variables on sqlplus command line:
username=scott
userpw=tiger
server=myserver
port=1521
dbname=mydb
sqlplus $username/$userpw#//$server:$port/$dbname
Though I would question why you need to set them as variables.
And I prefer to use tnsnames rather than ezconnect.
I'm creating a Unix shell script in solaris on which my task is to run the sql queries for 35 tables and bring the output to a csv file.For Which what I thought is that I will create an array of and by using for loop I will the pass the table name one by one to sql query.So as a sample I'm trying to pass the table name as variable in sql query.
I'm passing table name is sql query as well as the CSV which I m creating.
But is not working.Please help....
table_nm="PRODUCT"
sqlplus -s admin/admin_123#extend12 <<EOF
SPOOL /data2/interfaces/scripts/`$table_nm`.CSV;
set colsep ,
set feedback off
set trimspool on
set linesize 5000
set pagesize 1000
set heading on
set term off
set verify off
set timing off
set echo off
select * from `$table_nm` where PROD_ID = '1618' AND PROD_SER_NUM = 21 ;
spool off;
EXIT;
EOF
echo "end"
Loop through the array. Don't use back ticks.
tablearry=(PRODUCT SALES EMPLOYEES)
for i in "${tablearry[#]}"
do
table_nm=$i
sqlplus -s admin/admin_123#extend12 <<EOF
SPOOL /data2/interfaces/scripts/${table_nm}.CSV;
set colsep ,
set feedback off
set trimspool on
set linesize 5000
set pagesize 1000
set heading on
set term off
set verify off
set timing off
set echo off
select * from ${table_nm} where PROD_ID = '1618' AND PROD_SER_NUM = 21 ;
spool off;
EXIT;
EOF
echo "end"
done
I want to call a procedure in sqlplus oracle using shell script,
my procedure name is getdate_proc with two parameters, startdate and enddate.
I want to set startdate = sysdate and enddate = sysdate + 5 days
for example :execute getdate_proc(to_date('05/05/2015', 'MM-DD-YYYY'),to_date('05/09/2015','MM-DD-YYYY'))
below is my code:
#!/usr/bin/ksh
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute getdate_proc(to_date(sysdate, 'MM-DD-YYYY'),to_date(sysdate + 5days,'MM-DD-YYYY'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
i solved my problem with this code:
#!/usr/bin/ksh
date1='date'
date1=$(/bin/date --date="$date1" -d "+0 day" +"%F")
date2=$(/bin/date --date="$date1" -d "+4 day" +"%F")
echo $date1
echo $date2
sqlplus -s /nolog << EOF
connect scott/tiger
--execute procedure with parameter
execute attendance(to_date('$date1', 'YYYY-MM-DD'),to_date('$date2','YYYY-MM-DD'))
--set spooling to save in csv
set underline off
SET RECSEP OFF
set verify off
set colsep ','
set linesize 300
set trimspool on
spool /home/user/project/samp.csv
select * from att2;
spool off
set verify off
:) cheers
I want to connect DB Using Unix shell script but i an not able to connect
below mention Code
DB
export ORACLE_HOME="oracle"
value=`/oracle/bin/sqlplus -S user/pwd#SHM4EQ << EOF
SET LINESIZE 300
SET PAGESIZE 300
SET HEADING OFF
SELECT * form dual
EOF`
Try like this...
#!/bin/bash
LogDirectory='/var/tmp/logs'
DataDirectory='/var/tmp/data'
DBUSER='scott'
DBUSERPASSWORD='tiger'
DBSID='oracle'
sqlplus -s ${DBUSER}/${DBUSERPASSWORD}#${DBSID} <<EOF
set linesize 32767
set feedback off
set heading off
select * from dual;
EOF
I am trying to fetch a row with more than one column value to different shell variables. Infact I found that at a time all the column values can be stored to single shell variable. But how can I put those column values to seperate shell variables. Below is an example I am trying for time being
function sqlQuery {
sqlplus -S shiyas/********* <<'EOF'
set heading OFF termout ON trimout ON feedback OFF
set pagesize 0
SELECT name,open_mode from v$database;
EOF
}
OUTPUT="$( sqlQuery )"
echo $OUTPUT
Here I am getting the output as
ORCL READ WRITE
But my requirement is column values ORCL, READ WRITE should get assigned to different shell variable.
I tried the below of parsing.
echo "$OUTPUT" | while read name open_mode
but it was throwing unexpected end of file error.
-bash-3.2$ sh call_sql_col_val_1.sh
ORCL READ WRITE
call_sql_col_val_1.sh: line 18: syntax error: unexpected end of file
Please let me know what concept I can use to fetch a single row column values to different shell variables.
I do this via eval myself:
oracle#******:/*****> cat test.sh
#!/bin/bash
function sqlQuery {
sqlplus -S / as sysdba <<'EOF'
set heading OFF termout ON trimout ON feedback OFF
set pagesize 0
SELECT name,open_mode from v$database;
EOF
}
eval x=(`sqlQuery`)
NAME=${x[0]}
OPEN_MODE="${x[1]} ${x[2]}"
echo NAME IS $NAME
echo OPEN_MODE IS $OPEN_MODE
So we are running the same function you have above, passing it into x and running it through eval to handle the delimitation. Then you have an array and call call is as such: x[0] for the first item, for example.
Output is:
oracle#******:/******> sh test.sh
NAME IS ******
OPEN_MODE IS READ WRITE