Export CLOB Data using Shell Script - oracle

I am trying to export CLOB data from tables . I am trying to export the data and store it in a txt file.
There are multiple related questions and I have almost tried everything but I am unable to achieve it . The data gets exported only upto 4000 characters.
Below is my trail :--
test.sql
SELECT A.ID , DBMS_LOB.substr(A.USERS,32767) , DBMS_LOB.substr(B.NAME,32767)
FROM TEST A , TABLE B
WHERE
A.ID = B.ID;
test.sh
#!/usr/bin/bash
FILE=test.txt
nohup sqlplus SCMH/PWD#DB << EOF
set colsep ","
set headsep off
set pagesize 0
set trimspool on
set linesize 30000
set numw 15
SET BUFFER 30000
SPOOL $FILE
#test.sql
SPOOL OFF
EXIT
EOF
exit
I am able to export the data as mentioned but only 4K characters are coming . The total data in CLOB is much more than 4K.
The CLOB data is single line data e.g abc;xyz;..........
Any suggestions would be of great help.
Regards

Related

Needs to pass variable values to sqlplus sql file from shell script

I am using the Properties file to get the DB connection values and below is shell script file.
....
read -p "Please enter start date and end date " para1 para2
source database.prop
for ((i=1;i<=$Dbcount;i++ )); do
sqlplus -S ${user1}/${Password}#${conn} <<EOF &
spool sqlcsvdb_"$i".csv
#squery.sql $para1 $para2
exit;
EOF
done
....
squery.sql file.
....
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 20000
SET headsep ON
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
set verify off
SELECT id|| '|'||date|| '|'|| appid from table where date between '&para1' and '&para2';
exit;
EOF
....
When I execute shell it is not passing the variable values and getting ERROR at line 1:ORA-01722: invalid number error message.
Please help me on how to I resolve this and usage of bind variables.
....
read -p "Please enter start date and end date " para1 para2
Date1=$para1
Date2=$para2
source database.prop
for ((i=1;i<=$Dbcount;i++ )); do
sqlplus -S ${user1}/${Password}#${conn} <<EOF &
spool sqlcsvdb_"$i".csv
#squery.sql $Date1 $Date2
exit;
EOF
done
....
....
SET PAGESIZE 50000
SET COLSEP "|"
SET LINESIZE 20000
SET headsep ON
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET TRIMOUT ON
set verify off
SELECT id|| '|'||date|| '|'|| appid from table where date between '&1' and '&';
exit;
EOF
....
Defining variables again in the shell script file then passing those variables to sql file works, until it is varaibles are defined within the shell script, it does not work.

UNIX/Oracle - how to spool a LONG string whose length is greater than 32767 chars

Below script write data from Oracle table to file.
Problem: When the length of a string exceeds 32767 characters (let say 45000) then I get the string till 32767 characters in the file only.
How to spool such a long string to the file.
#!/bin/sh
.
.
sqlplus -s $USER/$PWD#$SID <<ENDSQL > $SQLOUT
set trimout on
set trimspool on
set HEAD OFF
set TAB OFF
set FEED OFF
set echo off
SET TERMOUT OFF
set linesize 32767 long 32767 longchunksize 32767 pagesize 0
WHENEVER SQLERROR EXIT 1;
spool $EXTFILE
select SQL_FULLTEXT||chr(10)||';' from v$sql;
spool off
.
.
#Unix script ends
UTL may help here but cant use it.

Pass Table name as Variable in sql query in Unix Shell Script Solaris

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

How to connect Data Base

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

Modify oracle sql query output format

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';

Resources