Sponsored Content
Top Forums Shell Programming and Scripting Passing Oracle function as file input to sqlplus Post 302991862 by durden_tyler on Friday 17th of February 2017 01:30:15 AM
Old 02-17-2017
A couple of points first:

Quote:
Originally Posted by amvip
...
Code:
create table fn_test
(a number, 
b number, 
c number
);

...
---- changes in Unix::
Code:
# create the input file
echo "insert into test_a values(5555,4646,353454);" > t_fn.txt
echo "commit;" >> t_fn.txt
echo test_f >> t_fn.txt

...
...
- The table names in the database and the script "t_fn.txt" are not the same. ("fn_test" and "test_a")
- "test_f" in the script "t_fn.txt" is only going to confuse sqlplus. What is it?

Regarding the main script:
Quote:
Originally Posted by amvip
...
--- Create the main script

Code:
#!/bin/ksh
sqlplus -s user/pass@server << ! < t_fn.txt
set head off
set echo off
set define off
set feedback off
set serveroutput on
var l_refcursor refcursor
exec :l_refcursor := @t_fn.txt
print l_refcursor
!

I get Error:
Code:
PLS-00103: Encountered the symbol "INSERT" when expecting one of the following:

...
Do you want to:
(a) execute "t_fn.txt" thereby inserting 1 record into table "fn_test" and then
(b) invoke function "test_fn" so that it fetches all 3 records (2 existing + 1 new)?

If you redirect the t_fn.txt script ("< t_fn.txt") to sqlplus, it will only run the script and disregard the commands inside the here-doc. You will have to execute the script within the here-doc.

Secondly, "l_refcursor" can be only be assigned the value of a compiled object. In this case, it is the function "test_fn". You cannot execute an external SQL script (anonymous PL/SQL block, "t_fn.txt" in this case) and assign it to "l_refcursor".

Considering all the points above, the following works in my system.

In Oracle:
Code:
SQL> 
SQL> create table fn_test (a number, b number, c number);

Table created.

SQL> insert into fn_test values(1,2,3);

1 row created.

SQL> insert into fn_test values(3,4,5);

1 row created.

SQL> commit;

Commit complete.

SQL> -- create function
SQL> create or replace function test_fn
  2  return sys_refcursor
  3  as
  4      p_ref_out sys_refcursor;
  5  begin
  6      open p_ref_out for select * from fn_test;
  7      return p_ref_out;
  8  end;
  9  /

Function created.

SQL> show errors
No errors.
SQL> 
SQL> --
SQL> select * from fn_test;

	 A	    B	       C
---------- ---------- ----------
	 1	    2	       3
	 3	    4	       5

2 rows selected.

SQL> 
SQL>

In Linux:
Code:
$ 
$ cat -n t_fn.txt
     1	insert into fn_test values(5555,4646,353454);
     2	commit;
$ 
$ 
$ cat -n main_script.ksh
     1	#!/bin/ksh
     2	sqlplus -s user/password@database << !
     3	set head off
     4	set echo off
     5	set define off
     6	set feedback off
     7	set serveroutput on
     8	set timing off
     9	set pages 0
    10	@t_fn.txt
    11	var l_refcursor refcursor
    12	exec :l_refcursor := test_fn
    13	print l_refcursor
    14	!
    15	
$ 
$ . main_script.ksh
      5555	 4646	  353454
	 1	    2	       3
	 3	    4	       5
$ 
$

The sqlplus variable "timing" is set to off by default, but in my setup it is set to on in the sqlplus profile script. So I set it off in the testcase above.
I set the variable "pages" to 0 in order to remove the extra blank line at the top where the header would have been.

I actually do not encounter the "PLS-00103" error. Try the above and see if it works in your system. Otherwise, more investigation will be required.
These 2 Users Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

passing unix variable to sqlplus without a file name

Hi, I want to input unix variable to sqlplus.The following is working fine sqlplus username/password @dummy.sql param1 param2 << EOF create user $1 identified by $2; EOF But I dont want any file name to be passed,I just want to pass the parameter. Is there any way to that?? Thanks... (3 Replies)
Discussion started by: sakthi.abdullah
3 Replies

2. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

3. UNIX for Dummies Questions & Answers

Reading from a file(passing the file as input parameter)

hi I have a shell script say primary.sh . There is a file called params my scenario is primary.sh should read all the values and echo it for example i should pass like $primary.sh params output would be Abc ... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

4. Shell Programming and Scripting

redirecting oracle sqlplus select query into file

So, I would like to run differen select queries on multiple databases.. I made a script wich I thought to be called something like.. ./script.sh sql_file_name out.log or to enter select statement in a command line.. (aix) and I did created some shell script wich is not working.. it... (6 Replies)
Discussion started by: bongo
6 Replies

5. Shell Programming and Scripting

Passing variable as an input file to AWK comand

Hi, I would like to compare 2 files using awk, which I can do by using: awk 'NR==FNR{a;next} (NR > 32 && $2 in a) {print $0}' File1 and File2. If the name of the File1 is in another file (for example, column 4 in File 3) then how can I pass this column 4 to the awk command. Thanks in... (1 Reply)
Discussion started by: ezhil01
1 Replies

6. Shell Programming and Scripting

Passing variables to an input file

Hi All, I have to insert 2 values to a text file in specific places. I have been able to extract each variable value via a script but am not able to send these variable values to the text file. Pasted is the script for extracting the variable values: for i in `ls -1` ... (2 Replies)
Discussion started by: danish0909
2 Replies

7. UNIX for Dummies Questions & Answers

Passing Input To Function

May i please know why is it printing the script name for $0 when i pass those parameters to function. #!/bin/bash -x usage() { echo "In Usage Function" echo $0 echo $1 echo $2 } echo "printing first time" echo $0 echo $1 echo $2 usage $0 $1 $2 Output: (2 Replies)
Discussion started by: Ariean
2 Replies

8. Shell Programming and Scripting

Passing variable from file to Oracle

cat a1 scott robert tom test script : #!/usr/bin/ksh for NAME in `cat a1` do VALUE=`sqlplus -silent "nobody/bobody01@testq" <<END set pagesize 0 feedback off verify off heading off echo off select username from dba_users where username=upper('$NAME'); END` if ; then echo... (3 Replies)
Discussion started by: jhonnyrip
3 Replies

9. Shell Programming and Scripting

Coredump when passing file name to ksh function

Hi, I'm playing with ksh. I'm trying to do a simple task: read file name from cli and call a function which calculated number of lines in file. I'm getting coredump every time when I try to read that file. Korn shell version $ print ${.sh.version} Version AJM 93u+ 2012-08-01 Main... (5 Replies)
Discussion started by: solaris_user
5 Replies

10. Shell Programming and Scripting

Oracle/SQLPlus help - ksh Script calling .sql file not 'pausing' at ACCEPT, can't figure out why

Hi, I am trying to write a script that calls an Oracle SQL file who in turns call another SQL file. This same SQL file has to be run against the same database but using different username and password at each loop. The first SQL file is basically a connection test and it is supposed to sort... (2 Replies)
Discussion started by: newbie_01
2 Replies
All times are GMT -4. The time now is 07:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy