"unknown command" for dbms_lob.open command in Sql+ - oracle

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

Related

How to get the dbms_output.put_line within the store procedure result by unix korn shell

Please refer to my sample,
In oracle DB,these is below SP
CREATE OR REPLACE PROCEDURE SP_TEST_PUTLINE AS
BEGIN
DBMS_OUTPUT.ENABLE;
dbms_output.put_line('Hello world!');
END SP_TEST_PUTLINE;
I use the unix shell to call SP to get the dbms_output.put_line('Hello world!') from the SP, how should I do.
I use sqlplus command to logon to DB ,seems can't get the result I want.
output=$(IFS='';echo connect ${DBUSER}/${DBPASS}#${ORACLE_SID} execute SP_TEST_PUTLINE|sqlplus -s /nolog )
Can anyone help me?
Thanks in advance...
------------------------------------split line 20180705 -------------------------------------
Thanks for the help of Alex and Kaushik Nayak,very helpful.
below are some findings for Kaushik Nayak,Please refer.
Hi Kaushik,It works, But at first, it was failed with below error
unknown command beginning "-e connect..." - rest of line ignored. SP2-0734:
So I change echo -e option to echo,then it works.
So here are the questions
1)why I use the echo without -e option can get below result,Is it cause by IFS?
echo "abc\n def \nghi"
abc
def
ghi
2) according to Alex's comments, whese two options need to write them to two lines? but you did not use \n between them
set serveroutput on
set feedback off
and when I add \n between them as below .it encouterred error as below, but the 'hellow world!' has output,
SP2-0734: unknown command beginning "feedback o..." - rest of line ignored. Hello world! PL/SQL procedure successfully completed.
here is question, why did you not use \n between set serveroutput on and set feedback off, did this DB command (set feedback off) has run successfully?
Look forward to your reply.
thanks in advance!
The command you are using will fail to even connect because you need a line break between the connect and the execute.
But you also need to set serveroutput on, and you probably want to set feedback off too (and maybe other options.
I'd use a heredoc to make it easier to read and maintain:
output=$(
sqlplus -s /nolog <<!EOF
connect ${DBUSER}/${DBPASS}#${ORACLE_SID}
set serveroutput on
set feedback off
execute SP_TEST_PUTLINE
!EOF
)
# then do whatever you want with the output
echo ${output}
You might also want to do some error checking...
If you are using a single line echo to pass to sqlplus, you should put newlines using the -e option of echo
output=$(IFS='';echo -e "connect ${DBUSER}/${DBPASS}#${ORACLE_SID}\nset serveroutput on feedback off\n execute SP_TEST_PUTLINE" |sqlplus -s /nolog )
You also have to specify set serveroutput on and feedback off to see only the output.
A better option would thus be to use a here document like Alex pointed out.

Shell Script & Stored Procedure - Literal Format Error

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.

Hive error when declaring hivevar

Trying to declare a variable in Hive using Hue online. Using the following code:
SET hivevar:TABLE1=location.tablename;
I am getting the following error message:
Error while compiling statement: FAILED: ParseException line 1:12 missing KW_ROLE at 'hivevar' near 'hivevar' line 1:19 missing EOF at ':' near 'hivevar'.
Can anyone tell me what this error message means or even what the KW_ROLE statement means?
Do you by any chance have a comment above that instruction ? Are you running that line and that line only ?
For example, the following will raise a similar Exception :
--This is a comment
SET hivevar:TABLE1=location.tablename;
But it works fine without the comment.
I guess you are making changes in MAC/Windows and moving the script to the server, Double dash "--" in MAC is a different from double dash "--" on Linux server, make changes on server itself and run the script...

Invalid Number with Substitution Variable

I am having trouble trying to get an input to be accepted as a number variable. Here is the code I have:
ACCEPT clientidnum NUMBER PROMPT 'Enter Client Number(s): '
SELECT * FROM PROD.GS_EXTERNAL_CONTACT#prd1.WORLD
WHERE GEC_GS_EXT_CONTACT_ID IN (SELECT GEC_GS_EXT_CONTACT_ID
FROM PROD.CLIENT#prd1.WORLD a
WHERE CT_CLIENT_ID = to_number(trim(replace('&clientidnum',CHR(13)))) AND
a.GEC_GS_EXT_CONTACT_ID NOT IN (SELECT GEC_GS_EXT_CONTACT_ID
FROM PROD.GS_EXTERNAL_CONTACT b
WHERE a.GEC_GS_EXT_CONTACT_ID= b.GEC_GS_EXT_CONTACT_ID));
And when I run this in SQL Plus, it comes back with the below error:
ERROR at line 4:
ORA-01722: invalid number
Thanks for the assistance! Sorry if this is an easy question, but I'm use to SQL Server and was thrown on Oracle to get some things to work correctly.
Figured it out. SET DEFINE was set to OFF in my glogin.sql script. I added SET DEFINE ON to the script I am running and it works.

Passing Contents of File as Parameter to Sql*Plus Command

I'm trying to write a sqlplus command that creates a table from a query that is stored in an .sql file.
The particular .sql file that contains the query would be supplied to my sqlplus command as a variable (&v_InputQuery).
I've tried something like this, but it doesn't work.
CREATE TABLE &v_OutputTable AS
(
< &v_InputQuery
)
;
I get an error saying that there's a missing SELECT keyword.
What I'd really like is for &v_InputQuery to be replaced not with the name of the file specified by the user, but with the actual contents of the file. Is there a way to do that?
Thank you very much.
Yes, you can do that. If your query is in a file called v_InputQuery.sql, you can do this:
CREATE TABLE &v_OutputTable AS (
#v_InputQuery.sql
) ;
It's important that the # is the first character on the line. SQL*Plus will read the file and put its contents at that location. So make sure you don't have any terminating characters in the file such as ; or /.
Unfortunately, You cannot create a SQL*Plus command, but instead create a shell script to do it!
Lets say my_script.sh is below
#you can always complete the user interaction at unix/dos
USER=your_user
PASS=your_pass
DB=your_db
OUTPUT_TABLE=$1;
QUERY_FILE=$2;
SELECT_QUERY=`cat $QUERY_FILE`;
sqlplus -S ${USER}/${PASS}#${DB} << !
SET SERVEROUTPUT ON;
VAR EXITCODE NUMBER;
BEGIN
EXECUTE IMMEDIATE ' CREATE TABLE $OUTPUT_TABLE AS $SELECT_QUERY ';
:EXITCODE := SQLCODE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
:EXITCODE := SQLCODE;
END;
/
exit :EXITCODE
!
Executing the script as below(Depends on OS)
ksh my_script MY_OUTPUT_TABLE my_sql.sql;
Expanding a comment, #MaheswaranRavisankar's approach will work, but the dynamic SQL (i.e. execute immediate) isn't necessary, so the anonymous block isn't necessary either. It can be simplified somewhat to:
USER=your_user
PASS=your_pass
DB=your_db
OUTPUT_TABLE=$1;
QUERY_FILE=$2;
SELECT_QUERY=`cat $QUERY_FILE`;
sqlplus -S ${USER}/${PASS}#${DB} << !
WHENEVER SQLERROR EXIT FAILURE
CREATE TABLE $OUTPUT_TABLE AS $SELECT_QUERY
!
This also allows you to use a query which is already terminated by a ; or /, which the execute immediate version wouldn't like - you just need to decide whether your wrapper script needs one to match what your query files will contain.
Even the whenever ... line isn't vital, but the other answer tried to exit with the error code so I've mimicked that too somewhat. This will always exit with a generic failure status though (1 in Unix, not sure what Windows does). You can then test if it was successful with $? in the script if you want to.
You can exit with the actual SQL error instead of the generic value, by using whenever sqlerror exit sql.sqlcode instead. The problem with doing that is that most (all?) shells limit the return code to the range 0-255, so most errors will wrap and give something unhelpful anyway - a fairly-likely ORA-00955: name is already used by an existing object error would give a shell exit value of 187, for example. And it's possible the wrapped value would be zero, which would mask that an error occurred at all; an also-plausible ORA-01536: space quota exceeded for tablespace '%s' error would give a shell exit code of zero, which is unhelpful. Using exit failure would at least stop that.

Resources