Calling Oracle function from script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling Oracle function from script
# 1  
Old 10-09-2017
Calling Oracle function from script

Hi
I need to call a function in database and update the return value of that function with a value in csv file.

test.csv
Code:
1,2,3,,5,,,8,9,10
1,2,3,4,5,,,8,9,10
1,2,3,,,,,8,9,10

In the above file I want to replace column 2 with a value extracted from database
like (select student_id from stud_master)

The output can be updated the same file or ideally into another file.

Normally I could able to connect to db and and get the value.

Code:
awk 'BEGIN -F, -v OFS=, {print $1,$2,$3}' test.csv

Now I want to know to call a oracle function and replace the output for $2 variable.

Thanks


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-09-2017 at 04:18 PM.. Reason: Added CODE tags.
# 2  
Old 10-09-2017
Bear with me but i don't quite get what you want:

Quote:
Originally Posted by kev94
test.csv
Code:
1,2,3,,5,,,8,9,10
1,2,3,4,5,,,8,9,10
1,2,3,,,,,8,9,10

OK, what do you want to do with this file?

Quote:
Originally Posted by kev94
In the above file I want to replace column 2 with a value extracted from database
like (select student_id from stud_master)
ahem - yes? From a select statement like the above you likely get a list in return, say, this:

Code:
99
100
101
...

What exactly do you want to do to "column two" and what has has this to do with the list the select-statement returned? Could you show an example of the outcome you have in mind?

Quote:
Originally Posted by kev94
Normally I could able to connect to db and and get the value.

Code:
awk 'BEGIN -F, -v OFS=, {print $1,$2,$3}' test.csv

Now I want to know to call a oracle function and replace the output for $2 variable.
Now i am even more confused: if you replace "$2" (the second field) with the output i envisioned above you would get this:

Code:
1,99
100
101
...,3
1,99
100
101
...,3
1,99
100
101
...,3

I suppose this is not what you want, but i can't figure out what it actually is. Please enlighten us.

bakunin
# 3  
Old 10-09-2017
Hi Thanks for your message.

Oralce Function will always return one value, I want to replace that value with second field in csv file.

Input:
Code:
1,2,3,,5,,,8,9,10

Output
Code:
1,<<replaced value from function>>,3,,5,,,8,9,10
1,<<replaced value from function>>,3,4,5,,8,9,10

Thanks

Last edited by rbatte1; 10-10-2017 at 06:47 AM.. Reason: Added CODE tags
# 4  
Old 10-10-2017
Quote:
Originally Posted by kev94
Oralce Function will always return one value, I want to replace that value with second field in csv file.

Input:
Code:
1,2,3,,5,,,8,9,10

Output
Code:
1,<<replaced value from function>>,3,,5,,,8,9,10
1,<<replaced value from function>>,3,4,5,,8,9,10

OK so far, this makes it clearer. If i get you correctly the value this function returns will not change throughout a run of this script and therefore "<<replaced value from function>>" will always be the same, yes?

Next: you want to replace the (second) column in the CSV-file with this value you have retrieved and then store the changed outcome to either (preferably?) a new file or the same file, yes?

If so:

Here is how you query the DB and store the result in a variable: this is done via a procedure called "process substitution". Normally a command would have some output which lands on <stdout>:

Code:
$ /some/command -xyz
hello world

$ _

In shell scripts you can "catch" this outcome into a variable instead of displaying it. Try out the following yourself:

Code:
$ echo "hello world"
hello world

$ myvar=$(echo "hello world")

$ echo $myvar
hello world

$ _

What the "$(...)" does is: the command (or pipeline - "command" is not limited to a single command but everything you can issue on a command line) is executed in a sub-shell. Then whatever the output on <stdout> is, is treated as if it would be a fixed string. Therefore:

Code:
$ myvar=$(echo "hello world")

is ultimately the same as

Code:
$ myvar="hello world"

but with the difference that echo "hello world" is really executed and the output of that is used.

You can do the same for your problem. I don't know which database you use, so this example is how one would do it in Oracle with sqlplus, but you can adapt that to your DB:

Code:
#! /bin/ksh

typeset chRepl="$(sqlplus -s /nolog <<-EOF
                  connect / as sysdba
                  set lines=132
                  set trims on
                  set tabs off
                  set pages 2000
                  select select student_id from stud_master;
                  exit
                  EOF
                 )"
typeset fInput="/path/to/input.file"
typeset fOutput="/path/to/output.file"

if sed 's/[^,]*,/'"$chRepl",/2' "$fInput" > "$fInput".tmp ; then
     mv "$fInput".tmp "$fOutput"
else
     print -u2 - "error working on $fInput, aborting."
     exit 1
fi

exit 0

I hope this helps.

bakunin

Last edited by rbatte1; 10-10-2017 at 06:48 AM.. Reason: Retro fitted CODE tags into quote to match
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling function from another bash script

I would like to call functions from another bash script. How can I do it? Some code More code (11 Replies)
Discussion started by: kristinu
11 Replies

2. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

3. Shell Programming and Scripting

Calling a C-function froma Perl script

Hi All, How can we call a c function from a perl script? Is it the same way as we do for shell script ? Thanks in advance JS (9 Replies)
Discussion started by: jisha
9 Replies

4. Shell Programming and Scripting

c function calling from a pearl script

Hi, Is it possible to call a c function defined in a .c file from a pearl script? How is this possible? Thannks in advance, JS (2 Replies)
Discussion started by: jisha
2 Replies

5. Shell Programming and Scripting

Question for calling oracle function from perl

Dear Sir/Madam, I use the following way to call the oracle stored procedure in a perl script, but I do not know how to call a oracle function by the following way ? # ARGV is the oracle stored procedure name with parameters $str = "sqlplus -s <<-eof \n" . "$db_login... (0 Replies)
Discussion started by: ili
0 Replies

6. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

7. Shell Programming and Scripting

Help needed in function calling in a script

:confused:Hi , I have a script as shown below: rpttxt() { name="$*" awk '/'"${name}"'/ {print $2 $3"=" $4}' file.txt } xx = rpttxt "COL_HEAD_1" awk 'BEGIN {printf("%36s \n ","'"$xx"'")}' rpttxt() is a function..I want to store the final result of this function in... (3 Replies)
Discussion started by: jisha
3 Replies

8. Shell Programming and Scripting

urgent calling function from a script

Hi, Can we define a function say func1 in a script 1.sh and call this function from another script say 2.sh by passing an argument :confused: (2 Replies)
Discussion started by: jisha
2 Replies

9. Shell Programming and Scripting

$0 scope in function and calling script

Hi folks, I'm just running through an oreilly korn shell book but have witnessed an output difference on my own unix machine and the output provided in the book. Can anyone help? create a script called ascript as follows: function afunc { print in function $0: $1 $2 var1="in... (16 Replies)
Discussion started by: beckett
16 Replies

10. Programming

calling c++ function from script

hi, I just started to work on unix, I was wondering if it is possible to call a c++ function from a script. now, i don't mean starting a program, i mean dynamicaly calling a funtion (like working with a dll) thanks (3 Replies)
Discussion started by: Lebamb
3 Replies
Login or Register to Ask a Question