variable passed to sql from shell is not working - sql

My code is:
#!/bin/sh
cat tmp_ts.log | awk ' {print $8}'
lookup=$8
sqlplus -s "sys/Orcl1234 as sysdba" << EOF
SELECT tablespace_name FROM dba_tablespaces WHERE tablespace_name='$lookup';
exit;
EOF
and my output is:
IAM_OIM
no rows selected
In this variable lookup I have passed to select statement but it's not working.
My end result should be with select statement. See below the output of select query:
See below:
My end result should be this but that variable is not working in select statement.

#!/bin/sh
lookup="$(awk '/tablespace/{print $8;exit}' tmp_ts.log)"
echo "Querying database with lookup = $lookup"
sqlplus -s "sys/Orcl1234 as sysdba" <<EOF
SELECT tablespace_name FROM dba_tablespaces WHERE tablespace_name='$lookup';
exit;
EOF
You have to use awk's output to set lookup. The shell knows nothing about the $8 which was set in awk. Also, I have ensured that awk exits after the first matching line, so that there is no risk of returning multiple values, or simply empty lines as it did in your version.

You can fill lookup with a command like awk, sed or cut.
lookup=$(cut -d" " -f8 tmp_ts.log)
You should add some checks, like #Dario did (with an exit after the first match and only converting lines with tablespace, but what to do when no lines match?).
When you don't add the checks you can skip setting the $lookup:
sqlplus -s "sys/Orcl1234 as sysdba" << EOF
SELECT tablespace_name FROM dba_tablespaces
WHERE tablespace_name='$(sed 's/.*tablespace- //' tmp_ts.log)';
exit;
EOF

Related

Why is my script not printing output on one line?

This is an image of what I'm asking for
I am using the following -echo- in a script and after I execute, the output format is as shown below:
`echo -e "UPDATE table1 SET table1_f1='$Fname' ,table1_f2='$Lname' where table1_f3='$id';\ncommit;" >> $OutputFile`
output: UPDATE table1 SET table1_f1='Fname' ,table1_f2='Lname' where table1_f3='id ';
the '; is appearing on a new line, why is that happening?
The variable $id in your shell script actually contains that newline (\n or \r\n) at the end; so there isn't really anything wrong in the part of the script you've shown here.
This effect is pretty common if the variable is created based on external commands (update:) or by reading external files as you are here.
For simple values, one way to strip the newline off the end of the value, prior to using it in your echo is:
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
or for scripts that already rely on a particular bash IFS value:
OLDIFS="${IFS}";
IFS=$'\n\t ';
id=$( echo "${id}" | tr -s '\r' '' | tr -s '\n' '' );
IFS="${OLDIFS}";

how to write sql query in if condition of ksh shell script

I'm very new to shell scripting and I want to write an SQL query inside IF 'condition', not inside the if 'block'. Can anyone help me with that?
My code:-
if [sqlplus -s /#user_pass_dbname << EOF select param_value from my_table where my_id=1 EXIT; /EOF == '7878'];then
echo "works!!"
else
echo "doesn't work"
Output:-
syntax error at line 3: `<<' unmatched
Get the value from sqlplus in a variable instead:
result=`sqlplus -s /#user_pass_dbname <<EOF
set head off feedback off
select param_value from param_table where param_id=1;
exit
EOF`
if [ $result = 965 ]; then
echo "works"
else
echo "doesn't work"
fi
I don't have environment to test now,But i think you need to put quotes(').
'sqlplus -s /#user_pass_dbname << EOF select param_value from my_table
where my_id=1 EXIT; /EOF'

Escaping special character when SQL output to variable in shell script

Trying to assign output from SQL query on an object containing special characters to a variable in shell script.
Running directly on the database:
db2 -x 'select count(*) from <SCHEMA>."/BIC/TEST"'
11000
Yet when I include this in script I need to use double quotes as I am using variables passed into the sql. Using single quotes
Output=$(db2 -x 'select count(*) from ${_SCHEMA}."/BIC/TEST"')
echo -e "$Output"
Results in:
SQL20521N Error occurred processing a conditional compilation directive near
"_". Reason code="7". SQLSTATE=428HV
When I use double quotes I hit:
SQL0104N An unexpected token "'/BIC/TEST'" was found following "ount(*)
Tried to escape the double quotes using another set of double quotes:
db2 -x 'select count(*) from ${_SCHEMA}.""/BIC/TEST""'
But this doesn't seem to work in script. It works for tables where there is no special characters/requirement to encase in quotations.
Any help is appreciated.
The code below works fine for me, notice the escaped quotes. If it fails for you, you need to give more details of your DB2-version and the DB2-server operating system platform.
#!/bin/ksh
db2 connect to sample
(($? > 0 )) && print "Failed to connect to database" && exit 1
db2 -o- "drop table \"/bin/test\" "
db2 -v "create table \"/bin/test\"(a integer)"
(($? > 0 )) && print "Create table failed" && exit 1
db2 -v "insert into \"/bin/test\"(a) values(1),(2),(3),(4)"
(($? > 0 )) && print "insert rows failed" && exit 1
db2 -v describe table \"/bin/test\"
typeset -i count_rows=$(db2 -x "select count(*) from \"/bin/test\"" )
(($? > 0 )) && print "query count rows failed" && exit 1
print "\nRow count is: ${count_rows}\n"
db2 -o- connect reset

File input for select statement in shell scipt

I have a text file which has inputs for select statement.
sqlplus -s $USERNAME/$PASSWORD#$HOST<< EOF
spool $DIRECTORY/UPDATE.xls
select acc,cr,dr from count where acc in $DIRECTORY/acc.txt;
spool off;
exit
EOF
Please let me know how to use the text file as input at highlighted part.
You can just use the back tics for that in combination with the cat command. So
select acc,cr,dr from count where acc in `cat $DIRECTORY/acc.txt`;

Assign output of sql query to a variable in unix

I am using the following simple UNIX script to assign the output to a variable.
count=`sqlplus -s ${DB_USER}/${DB_PASS}#${DB_INST} << END
SELECT COUNT(column_name) from table_name;
END`
echo $count
But I am getting the following error on executing:
SP2-0042: unknown command "END" - rest of line ignored.
count=`sqlplus -s ${DB_USER}/${DB_PASS}#${DB_INST} << END
When I tried to execute the above statement in putty, it was saying as "bad substitution"
So I am using UNIX script to assign the output to a variable.
count=sqlplus DB_USER/DB_PASS << END
SELECT VERSION_NUMBER from GA_PERIODIC_REFRESH where MODULE_NAME in 'RoaminfoService';
exit;
END
echo $count
You need exit also:
count=`sqlplus -s ${DB_USER}/${DB_PASS}#${DB_INST} <<END
set pages 0 echo off feed off
SELECT COUNT(column_name)
exit;
END`