How to clear error message buffer in SQLPlus? - oracle

There's an issue when installing several objects via SQL*Plus.
Let's install two objects: package A and view B, and A has compilation errors
SET FEEDBACK OFF
prompt install PACKAGE_A spec
create or replace package package_a is
procedure p;
end;
/
show errors
prompt install PACKAGE_A body
create or replace package body package_a is
procedure p is begin hello; end;
end;
/
show errors
prompt install VIEW_B
create or replace view view_b as
select * from dual;
show errors
Thus we get in the output:
install PACKAGE_A spec
No errors.
install PACKAGE_A body
Warning: Package Body created with compilation errors.
Errors for PACKAGE BODY PACKAGE_A:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/24 PL/SQL: Statement ignored
2/24 PLS-00201: identifier 'HELLO' must be declared
install VIEW_B
Errors for PACKAGE BODY PACKAGE_A:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/24 PL/SQL: Statement ignored
2/24 PLS-00201: identifier 'HELLO' must be declared
I had faced this problem not only in a "view after package" case, but currently I can not reproduce it in a short example for a "package after package" case (which bothers me mostly). So it seems to not appear for all types of compilation errors.
Documentation says When you specify SHOW ERRORS with no arguments, SQL*Plus shows compilation errors for the most recently created or altered stored procedure.
It is also clear that there is an option of specifying a particular object
SHOW ERR[ORS] [{FUNCTION | PROCEDURE | PACKAGE | PACKAGE BODY ...} [schema.]name]
But it does not look usable, because the requirement of giving an object type overcomplicates the situation (it is not obvious why the command cannot be satisfied with an object name alone).
Could you help me find a way to clear the buffer of SHOW ERRORS command, so it could be set in a pristine state before every object compilation?

If there is a way, I don't know it.
SHOW ERRORS is a SQL*Plus command. As fara as I can tell, it fetches data from USER_ERRORS (or, possibly, ALL_ERRORS or DBA_ERRORS). Its description is
SQL> desc user_errors;
Name Null? Type
----------------------------------------- -------- ----------------
NAME NOT NULL VARCHAR2(30)
TYPE VARCHAR2(12)
SEQUENCE NOT NULL NUMBER
LINE NOT NULL NUMBER
POSITION NOT NULL NUMBER
TEXT NOT NULL VARCHAR2(4000)
ATTRIBUTE VARCHAR2(9)
MESSAGE_NUMBER NUMBER
If I do something like this:
SQL> create or replace procedure ptest is
2 l_Res number;
3 begin
4 l_res := 1 * A;
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> show err
Errors for PROCEDURE PTEST:
LINE/COL ERROR
-------- --------------------------------------------------------
4/3 PL/SQL: Statement ignored
4/16 PLS-00201: identifier 'A' must be declared
then SHOW ERR returns the last errors I got. Does it mean that USER_ERRORS doesn't contain something else? No:
SQL> select name, line, substr(text, 1, 50) text from user_Errors;
NAME LINE TEXT
------------ ---------- --------------------------------------------------
PTEST 4 PL/SQL: Statement ignored
PTEST 4 PLS-00201: identifier 'A' must be declared
ADD_COURSE 0 PL/SQL: Compilation unit analysis terminated
ADD_COURSE 2 PLS-00201: identifier 'COURSE.TILTE' must be decla
SQL>
So, can I simply delete its contents? Ideally, I'd do that between every two CREATE PROCEDURE (or whatever) statements in that .SQL script:
SQL> delete user_errors;
delete user_errors
*
ERROR at line 1:
ORA-01031: insufficient privileges
Nope, I can't do that.
Therefore, I think you'll have to use that not-very-usable option and specify whose errors you want to see.

Related

Problem execute procedure in oracle ORA-06550

I try to execute a package in oracle that it works when call him with software, but in sql developer no
show me the following error:
ORA-06550: Line 2 column 11
PLS-00103 Encountered the symbol "package name" when expection one the following
:= . ( # % ; was substituted form "package name" to continue.
06550. 000000 - "line%s column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action
Vendor code 6550 Error at Line:1
the line 11 start of name package
begin
execute packageName.procedureName(parameter1,parameter2,parameter3,...);
end
Remove EXECUTE. It is used when you want to run a procedure at SQL*Plus prompt. In PL/SQL, you don't use it.
Code that should work is:
begin
packageName.procedureName(parameter1,parameter2,parameter3,...);
end;
/

Oracle kill sessions procedure

I want to create procedure which will kill all session. After I run the statement i get error:
[Warning] ORA-24344: success with compilation error 10/13 PL/SQL:
ORA-00942: table or view does not exist 6/6 PL/SQL: SQL Statement
ignored 15/31 PLS-00364: loop index variable 'V_KILL' use is invalid
15/9 PL/SQL: Statement ignored (1: 0): Warning: compiled but with
compilation errors
CREATE OR REPLACE PROCEDURE KILL_ORACLE_SESSIONS
IS
BEGIN
FOR v_kill IN
(SELECT
'alter system kill session '''
||sid||','||serial#||',#1'|| ''' immediate;' as statement
FROM
v$session
WHERE
sql_id='sql_id_here'
)
LOOP
dbms_output.put_line (v_kill.statement);
END LOOP;
END;
/
Where is the catch ?
Thanks
Most likely you don't have permissions to select view v$session because your user received this privilege by a ROLE. Privileges inside a PL/SQL block must be granted directly to the user (i.e. GRANT SELECT ON V$SESSION TO {username};). A role (for example DBA ROLE) does not apply inside PL/SQL.

Extracting output from PLSQL procedure to local drive of my laptop

I have a database connection server "server_dev" in sqldeveloper .
Now i want to create a procedure whose output can be directly saved in a csv file for data comparison later in the local drive of my laptop.
So i tried using UTL_FILE oracle package but when i ran the procedure the UTL_FILE was trying to write in the file of the server "server_dev" whereas i dont have any access to that server hence that command isnt working.
for example: the code is:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
So,I analysed it and found that my SQL developer is connected to a server to my local machin and since its my office laptop I cant alter it.
Can i have any other way in which I can save the output of my stored procedure to my local drive in a text or Csv file?
To write a file to your local machine you may use dbms_output; for example in SQLPlus:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
WIll produce the file d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
If you can select directly from a table or table function, then SQL*Plus 12.2's new SET MARKUP CSV option will be useful. Instead of paginating the query output it will produce CSV. The full syntax is
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
Output generation will faster if you turn on this mode with the sqlplus -m option.
It's also useful for querying JSON types. See https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and

Oracle: call to a function fails weirdly

If being called under owner account, the following syntax works just fine:
SELECT "MY_OWNER"."MY_PACKAGE".Convert(t.Value) FROM My_Table t
but when called from under another user I get the following error:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 1 Column: ??
Where Column: ?? points to
SELECT "MY_OWNER"."MY_PACKAGE".Convert(t.Value) FROM My_Table t
⇑
What am I doing wrong?
UPDATE. It's a function that is being called:
FUNCTION Convert ...
You need to grant privileges on this package to the other user
GRANT EXECUTE ON MY_OWNER.MY_PACKAGE TO the_other_user;
You should also check to see whether a_horse_with_no_name is correct. Is this a function that you can call in the way you specify or a procedure which should be called in this way
DECLARE
v_value VARCHAR2(10);
BEGIN
MY_OWNER.MY_PACKAGE.CONVERT(v_value);
END;
Also check the package to see what rights are defined. Current User or package owner. More details can be found here

Oracle triggers error on sqldeveloper export script

I had built a few tables with sequences and triggers, since I need to share the script with my team at uni I did an export with sqldeveloper, now when I try to import/execute the resulted .sql I'm getting errors with triggers.
This is the error message:
"Error starting at line 250 in command:
CREATE OR REPLACE EDITIONABLE TRIGGER "TRG_ACCOUNTS"
BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
BEGIN
SELECT SEQ_ACCOUNTS.NEXTVAL INTO :NEW.ACCOUNT_ID FROM DUAL
Error report:
SQL Command: editionable TRIGGER
Failed: Warning: execution completed with warning
Error starting at line 258 in command:
END
Error report:
Unknown Command
trigger "TRG_ACCOUNTS" altered."
This is the part of the script it's complaining about. I have a few other triggers and it's giving me the same error on all of them. I've checked how the triggers are created after executing the script, and all of them seem to be missing a semi colon and the "END;" at the end.
Example:
create or replace
TRIGGER "TRG_FL_AR"
BEFORE INSERT ON FLOOR_AREAS
FOR EACH ROW
BEGIN
SELECT SEQ_FL_AR.NEXTVAL INTO :NEW.FLOOR_AREA_ID FROM DUAL <-- missing ";" here
<missing "END;" here>
Could you please help me?
Thank you.
Try to put a back slash / after line 258 and check again

Resources