I have a shell script that calls an Oracle Stored Procedure. The SP has two parameters - the first is of type VARCHAR2 and the second is of type DATE
CREATE OR REPLACE PROCEDURE MY_SCHEMA.MY_SP_NAME(firstParameter IN VARCHAR2, dateParameter IN DATE)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Successfully called Procedure');
END;
/
In my shell script, I'm trying to execute the procedure using the following command:
echo "EXECUTE MY_SCHEMA.MY_SP_NAME('TEST', '20170909') " | $ORACLE_HOME/bin/sqlplus $ORAUSER/$ORAPASS
The problem is that when the script runs I get the following error: ORA-01861: literal does not match format string. My guess is that there is an issue with the '20170809' value that I'm passing to the date parameter but I'm unsure how to resolve this. Any help is appreciated.
(PS: The ORACLE_HOME/USER/PASS environment variables are all set correctly and I can successfully run SQLPLUS from the script so there are no problems connecting to the Oracle database)
You can try to use the to_date function:
Execute my_schema.my_sp_name(‘test’,to_date(‘20170909’, ‘yyyymmdd’))
Or use ISO date format in your date parameter.
Related
In the above code, I am giving schemaname as input and using that input it should connect to the database. But In this case the value i entered is not taken by the schemaname. This is how the out put and the error is:
declare schemaname varchar2(20);
exec :schemaname := XYZ;
BEGIN
end;
Error report -
ORA-06550: line 2, column 6:
PLS-00103: Encountered the symbol "" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "CONNECT" when expecting one of the following:
Could any one suggest how to make it work using spool
the code between declare and end is PL/SQL. Commands like CONNECT or SPOOL are SQL*Plus commands. You cannot use SQL*Plus commands in a PL/SQL block.
In your case you don't need PL/SQL at all:
Create a script with following content
connect &1
spool C:\ABC
#c:\ABC
spool off;
and run it
#your_script_name
BTW: there is no reason to run script c:\ABC while you are spooling into it. What exactly do you want to achieve?
exec[ute] is SQL*Plus and SQL Developer (and maybe other clients) shorthand for an anonymous block. It is a client command, it is not part of PL/SQL. You are trying to use it inside a PL/SQL declare section, where it is not valid or recognised.
If you want a client bind variable you need the var[iable] command:
var schemaname varchar2(20);
exec :schemaname := '&1';
BEGIN
...
Notice the single quotes around &1, as it's being assigned to a string variable.
But you can't connect inside a PL/SQL block either, and you can't use a bind variable for the connection.
connect :schemaname
will prompt for a password (even if you defined it's value as user/passwd) and try to connect as a user lieterally called :schemaname.
You can use a substituion variable, but you don't really need to define a new one; as you seem to be passing the credentials in, you can do:
connect &1
(without surrounding quotes)
Can any one please tell me how to execute a simple oracle stored procedure having out parameter inside a shell script.That means it should return a value into the unix environment.
I assume you want to start a script using SQLPLUS. This answer explains how to assign the value of an out parameter to a bind variable in SQLPLUS.
Call stored procedure from sqlplus
You can exit sqlplus with this value and use that value in the calling script.
exit x
But this usually is restricted to numerical values in a limited range.
There are a number of ways, but the one I tend to use is illustrated below.
The sqlplus script the_sql_script.sql
var ret varchar2(2000)
exec the_procedure ( the_out_param => :ret );
set pages 0 head off lines 200 trimspool on
spool sqlplus.return
select 'RETURN_VALUE=' || :ret FROM dual;
spool off
quit
In shell:
sqlplus / # the_sql_script.sql
. ./sqlplus.return
echo $RETURN_VALUE
I am a total beginner when it comes to PL/SQL and Sql+. I am trying to write a function that will extract the contents of a text file into a CLOB (following this as an example). When I issue the following command in Sql+:
dbms_lob.open( 'c:\temp\test.txt', dbms_lob.lob_readonly );
I get the following error message:
SP2-0734: unknown command beginning "dbms_lob.o..." - rest of line ignored.
Is there something wrong with the syntax of the command, or something else entirely?
Thanks much!
I'm not an expert in oracle. For me the problem is that for launch this command in sqlplus you may declare an anonymous block like this:
DECLARE
-- variables
BEGIN
--- your commands here
dbms_lob.open......
END;
To launch the execution you have to digit / and then return
I have a oracle procedure proc1 which adds two values and gives the result.I have to call this procedure from shell and show its result back to the shell.I am able to call that procedure from the shell,but it just shows that PL/SQL procedure successfully completed.But the result is not coming to the shell .
i am doing this to call the procedure from shell...
$ echo "execute proc1(10,10);"|sqlplus -s system/xxxxx#orcl
This is the procedure which is running fine .
create or replace procedure proc1
(N1 in number,N2 in number) is
begin
dbms_output.put_line(N1+N2);
end;
/
I need the output in the shell .Anyone plese help.
I am aware there is another answer showing how to use set serveroutput on and the procedure call on separate lines, however I am writing this answer as a one-liner to do the same thing.
Bascially you need to shove this into sqlplus:
set serveroutput on
execute proc1(10,10);
You might at first think this can be done on one line, separated with a semi-colon.
set serveroutput on; execute proc1(10,10);
However that doesn't work - you really need a newline character.
So the trick is to also use -e flag with echo, which can give you a newline with \n.
Using head -1 trims everyting but the line containing the procedure result.
Final one-line answer:
echo -e "set serveroutput on\n execute proc1(10,10);"|sqlplus -s system/xxxxx#orcl| head -1
P.S. I editted your question to remove the password :)
I have two .sql files both are Oracle stored procedures which both take in input parameters. I would like to first connect to a remote oracle database using sqlplus in command line and want to first use both files to create their respective stored procedures so I see them under procedures for that connection in Oracle SQL Developer.
After this I have two more .sql files which look like this and are designed to take input parameters and execute the stored procedures. This is one of the files that is meant to execute the stored procedure "REPORT".
DECLARE
NAME VARCHAR2(200);
VERSION VARCHAR2(200);
STARTDATE DATE;
ENDDATE DATE;
BEGIN
NAME := '&1';
VERSION := '&2';
STARTDATE := '&3';
ENDDATE := '&4';
exec REPORT(NAME, VERSION, STARTDATE, ENDDATE);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20101,SQLERRM);
END;
/
In command prompt I first try to create the stored procedure in the database by:
C:\Users\Desktop>sqlplus username/password #report_setup.sql
When I try this the output get is just empty lines that are numbered and beginning at the number that is 1 greater then the last line of my .sql file. My report_setup.sql file is 81 lines long and the output of the sqlplus command is blank numbered lines beginning at 83.
Please let me know how I can create and execute these stored procedures properly through sqlplus.
Thanks in advance,
I think you have to remove the 'exec'-word, and it's crucial to have the slash at the bottom at the very start of the line, with no spaces in front of it:
DECLARE
NAME VARCHAR2(200);
VERSION VARCHAR2(200);
STARTDATE DATE;
ENDDATE DATE;
BEGIN
NAME := '&1';
VERSION := '&2';
STARTDATE := '&3';
ENDDATE := '&4';
REPORT(NAME, VERSION, STARTDATE, ENDDATE);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20101,SQLERRM);
END;
/
It would have been more useful to show the report_setup.sql than the script that calls the procedure it creates... but from the symptoms you describe, the report_setup.sql doesn't have a / at the end of the procedure declaration.
It presumably has something like:
CREATE OR REPLACE PROCEDURE REPORT(NAME VARCHAR2, VERSION VARCHAR2,
STARTDATE DATE, ENDDATE DATE) AS
...
BEGIN
...
END REPORT;
It needs to have
...
BEGIN
...
END REPORT;
/
Since you're running it from the command line with # it should also have an EXIT at the end; but without the / that will be treated as part of the procedure, which is never compiled.
You can suppress the line number display, incidentally, by calling SQL*Plus with the -s flag - though at the moment they are useful since they show roughly what the problem is.
I had a similar issue. The problem was the encoding used, sqlplus expects UTF-8 standard enconding, wherever different encoding cause weird behavior.