I intent to pass variable as a parameter to my function .(The purpose of this function is to connect to Terdata,fetch top 10 records from a table and write it to a file).But my function is not called instead the names of the files in the home path are being displayed.
Is there any way to pass argument value as a variable.
Below is my script:
#!/bin/ksh
set -x
my_first_fn()
{
query="SEL TOP 10 * FROM XYZ.TABLENAME"
${BTEQ} << EOF
.logon ${pSERVER}/${pUSERNAME},${pPWD};
.set width 6000;
.set separator ',';
.set titledashes off;
.EXPORT FILE = '${home}/BTEQ.txt';
${stmt};
.EXPORT RESET
.IF ERRORCODE <> 0
THEN .GOTO EXITERR
.QUIT 0;
.LOGOFF;
.LABEL EXITERR
.QUIT 1;
.LOGOFF;
EOF
}
LOG_FILE=${home}/myscript.log
my_first_fn $query | tee ${LOG_FILE}
Thanks in advance...:-)
I didn't define stmt variable and expected the script to execute.This is the part which caused the issue.Rather than this I can directly call the function and mention SEL TOP 10 * FROM XYZ.TABLENAME in the function's body. – UnixNewbie
Related
I have a procedure that takes a clob as an out parameter.
I want to pass the clob content to the shell variable so that I can print it in a file, as it is in one line, but I got a different result : some blanks are deleted, and the result is printed in many lines!
This is the shell code :
#!/bin/bash
date_fich_ent=`date '+%Y%m%d'`;
heure_fich_ent=`date '+%H%M'`;
FILE_NAME="RIA_BMCE_TRX_${date_fich_ent}${heure_fich_ent}.TXT";
function_output=`sqlplus -s <<END
$1/$2#$3
set feedback off
set heading off
set pagesize 0
set serveroutput on 100000
DECLARE
my_clob clob := null;
BEGIN
proc_test('it',my_clob);
dbms_output.put_line(my_clob);
END;
/
exit;
END`
echo "$function_output" > $FILE_NAME
Thanks.
I have a user defined oracle function that returns a number that can be greater than 255. I call that function from a shell script using sql plus, it returns the value, for eg 296, but the scripts accepts it as 40, which is because the script can only accept return codes from 0-255.
This is how i am currently receiving the value
echo ${PASSWORD} | sqlplus ${USERNAME}#${SID} #$SQL getnumber.sql $PARAM> ${LOG}
number=$?
getnumber.sql has
set serveroutput on size 100
VARIABLE rc NUMBER;
call function_get_number('&2') into :rc;
print rc;
exit :rc;
How can i preserve the return value? Should i write it to a file? if so how/where
Script getnumber.sh:
cat << EOF | sqlplus /S /nolog >${LOG}
conn ${USERNAME}/${PASSWORD}#${SID}
set serveroutput on size 100
VARIABLE rc NUMBER;
exec :rc := function_get_number('$PARAM');
SELECT 'RETVAL:' || :rc || ':' theval FROM dual;
EOF
RC=$( grep '^RETVAL:' ${LOG} | cutr -d":" -f2 )
echo $RC
I have this function that works for a co-worker, and I've seen where it works for others as well. I have tried both << EOF, and <
I have added a simple use of the function as well for testing purposes.
td_query() { bteq << EOF |grep '^>' |sed -e "s/^>//"
$(cat $HOME/.tdlogon)
DATABASE DBNAME;
.set width 1000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
td_query "select current_date;"
Unfortunately, when I save this and try to run the ksh file, I get the following error:
-ksh: .: syntax error: `<<' unmatched
Can anyone tell me what would cause this?
You should avoid spaces both at <<EOF and the line starting with EOF.
And the line with EOF shouldn't have trailing spaces either.
First test with a simple script (no idents before EOF !).
td_query() {
wc <<EOF
$(cat $HOME/.tdlogon)
DATABASE DBNAME;
.set width 1000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
td_query "select current_date;"
And check the bteq program. Is het reading from stdin?
When I replace "wc" by "echo" in the script above, the script stops working!
how do you pass a global variable into a string inside a function?
I have the following code that works for the most part:
td_query () { bteq << EOF |grep '^>' |sed -e "s/^>/;/g"
$(cat $HOME/.tdlogon)
DATABASE $schemaName;
.set width 10000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
rqstID="1357"
echo $(td_query "select '>'||'UPDATE schema.SEGN_$rqstID_PRCSS_TBL SET POPN_LVL_EXCLN ='||a.CODE_ID||' WHERE ' || b.SQL_FILE_NM ||' AND POPN_LVL_EXCLN IS NULL'
FROM SE_POPN_EXCLSN a
INNER JOIN SE_CODE_LIB b
ON
a.CODE_ID = b.CODE_ID;")
but the results come back:
UPDATE schema.SEGN_ SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
missing this:
$rqstID_PRCSS_TBL
it should be:
UPDATE schema.SEGN_1357_PRCSS_TBL SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
_ is a legal character in a shell variable. The shell is trying to find a variable by the name of $rqstID_PRCSS_TBL and getting an empty string. (That's why _PRCSS_TBL is disappearing from your output.)
You need to tell the shell where the variable name ends: schema.SEGN_${rqstID}_PRCSS_TBL
Some time ago I wrote a small routine to run some quick n' dirty queries (and with that I mean it is not used for large queries) against an Oracle DB, but also wanted to do it a bit easier to parse errors. Follows:
# Executes the query
#
# Will execute a query contained in the variable named
# in the parameter $4 and store the result in the variable
# named in $5.
# In case of errors (even SQL related) the function should
# exit with status 1, making it possible to "if execQuery".
#
# #param $1 = User
# $2 = Pasword
# $3 = Tns Alias
# $4 = Name of the variable containing the query
# $5 = Name of the variable to hold the result
#
# #return query execution status
function execQuery {
typeset eSQLU=$1
typeset eSQLP=$2
typeset eSQLS=$3
typeset etQUERY=$4
eval typeset eQUERY=\$$etQUERY
typeset eQRES=$5
logMessageFile "DEBUG" "Query: $eQUERY"
typeset res=$(sqlplus -s $eSQLU/$eSQLP#$eSQLS <<EOF
set echo off newpage 0 space 0 pagesize 0 feed off head off verify off lines 999
WHENEVER SQLERROR EXIT 1
$eQUERY
exit;
EOF
)
[[ $? -gt 0 ]] && return 1 || eval "$eQRES=\"$res\""
}
The idea of this function is that later I could do something like:
query="select sysdate from dual;"
if execQuery $RAID_APP_PI_USR $RAID_APP_PI_PWD $RAID_APP_PI_SID query result ; then
echo $result
logMessageFile "INFO" "Inserts into XX successful."
else
logMessageFile "ERROR" "Error insertando XXX."
fi
It kinda works... A properly written query will do it fine, and the result variable is all correctly evaluated and all. The problem are the errors. If the query in that example was something like select * potato potato;, It'd still not yield the correct return value thus missing the error test.
I'm not particularly good with sqlplus nor ksh, probably just missing something obvious... Could someone lend me a hand here?
Thanks!
I believe $? is returning the exit status of the typeset command, not the sqlplus command.
It may be easier to output the results of your SQLPLUS statement to a file instead of into a variable. Then you could either read that file with grep, looking for an "ORA-" message, or check the exit status variable.
sqlplus -s $eSQLU/$eSQLP#$eSQLS > querylog.tmp <<EOF
set echo off newpage 0 space 0 pagesize 0 feed off head off verify off lines 999
WHENEVER SQLERROR EXIT 1
$eQUERY
exit;
EOF
echo $?