Can anyone guide me tin passing parameters into user defined function of shell script (KSH).
Here is my code,
InsertRecord()
{
DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF
set head off
set feed off
set serveroutput on
INSERT INTO TBL1 ( OLD_VAL,
NEW_VAL,
DATE)
VALUES (
$Old_VAL,
$New_VAL,
$DATE
);
SELECT COUNT (*) FROM TBL1;
commit;
/
EOF)
echo $DB_TBL
}
OLD_VAL=blabla
NEW_VAL=blabla
DATE=blabla
value=$(InsertRecord "$OLD_VAL" "$NEW_VAL" "$DATE")
Please let me know the correct way to pass parameters to function? also let me know how can I use it in INSERT statement?
Could you please help me out.
getting parameters in a function is the same as in a script. If you call the script like
InsertRecord "$OLD_VAL" "$NEW_VAL" "$DATE"
you can retrieve the values inside the function (as in your example)
...
VALUES (
$1,
$2,
$3
);
...
But the variables in the script are also visible from inside the function, so you can refer directly to them by their names "$OLD_VAL" "$NEW_VAL" "$DATE"
now its working fine.
Could you please let me know, how can I pass multiple values to INSERT statement?
I am parsing a file line by line and adding required field values into
"$OLD_VAL" "$NEW_VAL" "$DATE"
But for each line I am connecting to DB and doing insertion and disconnecting.
Is there any way that I can store all values of file lines and collectively insert them into database?
Please help me out.
This will prompt you to enter the values, but this would suppose an interactive mode.
You'd better go for an alternative using the eval command to force the $variable to be evaluated before you give it to sqlplus.
You also can dynamically build an file.sql that you can then pass to your sqlplus with @file.sql
---------- Post updated at 09:56 PM ---------- Previous update was at 09:15 PM ----------
maybe something like this :
InsertRecord()
{
echo "set head off
set feed off
set serveroutput on
INSERT INTO TBL1 ( OLD_VAL,
NEW_VAL,
DATE)
VALUES (
$1,
$2,
$3
);
SELECT COUNT (*) FROM TBL1;
commit;
/
exit" > db_tbl.sql
DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE @db_tbl.sql )
rm db_tbl.sql
}
OLD_VAL=blabla
NEW_VAL=blabla
DATE=blabla
value=$(InsertRecord "$OLD_VAL" "$NEW_VAL" "$DATE")
Note that for massive load, i would not recommend this approach.
You should look sql plus and pl/sql synthax.
If you run oracle, you have a lot of example under admin directory (look the code of the AWR report generator $ORACLE_HOME/rdbms/admin/awrrpt.sql, and see how it uses bind variables)