Why are my SQL*Plus results only allowing comma separated results? - oracle

I am trying to run a basic shell script to query my Oracle database and give me back results with a pipe | delimiter versus a comma.
No matter what I try it only returns with commas. Any help would be appreciated.
#!/bin/bash
sqlplus -s $DBUSER/$DBPASS#$DBCONN/$DBSID <<EOF
set colsep '|'
set echo off
#$SCRIPT_PATH/QRY_1.SQL
exit;
EOF

Related

How to run multiple SQL script files in a Shell script

I have to create 1 UNIX Shell script. In that shell script i want to run multiple SQL script files from the same directory. I have used like this-
#!usr/bin/ksh
SQLPATH = /usr/sql/
(cd $SQLPATH;
'sqlplus usr/password#sid <<EOF
spool <db_file>.log
#<db_name>.sql
set echo off
set heading off
spool off
&&
spool <db_file2>.log
#<db_name2>.sql
set echo off
set heading off
spool off
&&
spool <db_file3>.log
#<db_name3>.sql
set echo off
set heading off
spool off
exit;
EOF')
exit 0
There are multiple SQL scripts like this and for each SQL script I have to create log files so I used spool here. After every SQL script files execute
I used &&. So Is it good to use && here and in 3rd line ; when I define the PATH. Please provide me the better solution.
Don't override the system PATH (or now SQLPATH) and don't put commands in single quotes. Use lowercase for your private variables, and you can't have spaces around the equals sign in an assignment; but a variable you only use once is useless anyway, so I took it out, and hardcoded the cd argument.
I'm guessing you want something like
#!/usr/bin/ksh
# No spaces around equals sign, and don't use uppercase for private variables
# But why use a variable at all anyway
#sqlpath=/usr/sql
cd /usr/sql # is this really necessary and useful??
sqlplus usr/password#sid <<____EOF &&
spool <db_file>.log
#<db_name>.sql
set echo off
set heading off
spool off
____EOF
sqlplus usr/password#sid <<____EOF &&
spool <db_file2>.log
#<db_name2>.sql
set echo off
set heading off
spool off
____EOF
sqlplus usr/password#sid <<____EOF
spool <db_file3>.log
#<db_name3>.sql
set echo off
set heading off
spool off
____EOF
# exit 0 # Not necessary or useful
If the multiple sqlplus commands can be executed in a single session, that would be an obvious improvement; but I'm guessing sqlplus has no way of expressing what you appear to mean with && (this syntax seems to have a quite distinct meaning in sqlplus).
Make a function
#!/usr/bin/ksh
SQLPATH=/usr/sql # Offtopic: Better use lowercase for your own variables
process_sql_and_log() {
if [ $# -ne 2 ]; then
echo "Usage: $0 sqlfile logfile"
return 1
fi
sqlplus usr/password#sid <<EOF
spool ${2}
#${1}
set echo off
set heading off
spool off
exit;
EOF
}
cd "${SQLPATH}" &&
process_sql_and_log db_name.sql db_file.log &&
process_sql_and_log db_name2.sql db_file2.log &&
process_sql_and_log db_name3.sql db_file3.log
exit 0

Passing parameters to Impala shell

I am running a impala query in while loop and for that I have created one separate query file and I am calling it from my shell script.
My question is: Can we pass shell variable matching with impala query in query file?
A="INSERT_SBP_ME_VS_ME_INCOME_LAST_THIRTY_DAYS_Q"${Count}
echo "value of A is $A"
source ${SBP2_MNY_IN_LAST_THIRTY_DAYS_QF}
${IMPALA_CON} -q "${${A}}"
'A' value is like INSERT_SBP_ME_VS_ME_INCOME_LAST_THIRTY_DAYS_Q1 (as count is 1)
I am doing this in this way but getting bad substitution error and I also tried
${IMPALA_CON} -q "${A}"
but not getting a successful result.
You seem to be looking for --var (IMPALA-2179). To substitute from the command line, you can do:
impala-shell -f test.q --var=L=2;
where test.q is:
select * from p_test limit ${VAR:L};
Your query should be :
impala-shell -q "$A"
Refer:
impala-shell Configuration Options
similar post
impala-shell -i node.domain:port -B --var"table=metadata" --var="db=retail" -f "file.sql"
file.sql:
SELECT * FROM ${var:db}.${var:table}"

How to SET LINESIZE ORACLE with echo

Hello dear stackoverflowers,
I am writing a shell script in which I need to dump a sqlplus database content in a file, I need to have it done in one command line.
echo 'SET LINESIZE 1000; select * from myDb;'| sqlplus -S user/pass#host
But I get
SP2-0268: linesize option not a valid number
I tried without ';' but I have the same result.
I couldn't find any example here or elsewhere on how to do this.
Any help on that?
Thanks!
echo 'SET LINESIZE 1000\n select * from myDb;' |sed 's/\\n/\n/' |sqlplus -S user/pass#host

How can I pass a variable to a query inside a shell script?

while IFS=# read -r process_id source destination type
do
echo "Process id: $process_id"
echo "Source: $source"
echo "Destination: $destination"
case "$type" in
2)
echo "Type is outbound: $type"
contact=$(sqlplus -s ${SQLPLUS_INFO} <<EOF
SET PAGESIZE 0
SELECT email FROM table WHERE partner = '${destination}';
exit
EOF
)
echo
echo ${contact}
echo
;;
Based in the code above, how can I pass the value from $destination to the query? The example above is not working, even these other ones:
SELECT email FROM table WHERE partner = '"${destination}"';
SELECT email FROM table WHERE partner = '$destination';
What happens when you run the script with bash -x? I ask because the here-document notation expects to find the end marker at the start of a line. When I run this code:
#!/bin/bash
contact=$(cat - <<EOF
input from here document
second line
EOF
)
echo "$contact"
I get errors like:
eof.sh: line 3: unexpected EOF while looking for matching `)'
eof.sh: line 10: syntax error: unexpected end of file
If the lines start with tabs, you can use a dash before the end-of-file marker to indicate that leading tabs should be ignored.
#!/bin/bash
contact=$(cat - <<-EOF
input from here document
second line
EOF
)
echo "$contact"
This outputs:
input from here document
second line
Replace those tabs with blanks and you are back into the syntax errors. Although I've couched this in terms of bash, I believe you run into the same issues with Korn and Bourne shells too.
So, my suspicion is that your problem is related to the formatting of the here-document in your code, but you should have been seeing some sort of error, so I'm a bit puzzled. You should be getting the substitutions you wanted made:
#!/bin/bash
description="The Description"
contact=$(cat - <<-EOF
input from here document
second line with '$description' embedded.
EOF
)
echo "$contact"
This yields:
input from here document
second line with 'The Description' embedded.
Using bash -x can be helpful to trace the execution of a command.
All of this is only coincidentally related to Oracle and SQL*Plus.
Please try to remove the single quotes around your variable: they prevent your variable to be interpreted (contrary to double quotes).
That should be:
SELECT email FROM table WHERE partner = ${destination};

How to get proper tabular output like the mysql command line from a bash script

I am querying a database from a bash script using following query:-
Output = echo "$QUERY_STR" | mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME
it gives me the required output which I save in a Variable
However when I echo $output I do not get proper formatted output like in command line of mysql query.
Read one of the post to use -t in the query however for large data set it does not give proper output.
To work around it, I am saving the output in a .csv file.
To maintain all the whitespace that is indeed kept in the variable's value, it is crucial to double-quote the variable:
echo "$output"
Also, you cannot have whitespace around the equal sign in a variable assignment:
output=$(mysql ... <<< "$QUERY_STR")

Resources