Issue with "spool"-commands in PL SQL Procedure - oracle

I was trying to create a procedure within one of the Oracle DBs. Procedure is intended for my colleagues, so that everybody could export the result of the query in a csv-file.
I was using "spool" commands. When running the code between BEGIN and END separately in Oracle SQL Developer (running as script, F5) - it worked, and created the file locally.
Then I tried to put it in the procedure, and that is where issue began. Some compilation errors come out. Most of them have code "PLS-00103". I tried searching for it, but not successful so far. I even tried EXECUTE IMMEDIATE statement, putting some script blocks inside it - not working so far.
I will appreciate any hints/help on approaching my situation.
P.s. one of my first attempts is below:
CREATE OR REPLACE PROCEDURE export_to_csv (SOURCE IN VARCHAR2, EXPORT_PATH IN VARCHAR2) is
BEGIN
spool on;
set feedback off;
set heading off; ​ ​ ​
set sqlformat csv;
spool EXPORT_PATH;
select * from SOURCE;
spool off;
END export_to_csv;

Nope, that won't work. SPOOL, as well as all SET commands you use, are SQL*Plus.
Stored procedure is PL/SQL, so - if you want to do it from there, you'll have to use UTL_FILE package. The result will be on the database server, not your local PC.
Alternatively:
put those SPOOL, SET, SELECT, ... commands into a .SQL file on the database server
create a database job using DBMS_SCHEDULER package which is capable of running operating system files. On MS Windows, you'd call CMD which establishes connection to the database using SQLPLUS executable and calls your .SQL script (using #)
just like in the previous option, the result will be on the database server, not on a local PC
See which option you prefer.
Maybe it would be simpler to create that .SQL file and give it to all your colleagues who'd then run it on their PCs and get the result locally.

Related

Oracle PL/SQL dbms_output to Jenkins console

Using Cloudbees SDA CI to implement Jenkins, with Oracle 19 P/SQL running in many jobs. Using dbms_output in the Oracle script, the output does not show up in the Jenkins console at all. I'm looking for functionality that would allow me to print to the Jenkins console from P/SQL the same way I can from say Python using the print statement. I'm generally trying to do this currently:
set serveroutput on;
begin
[...a bunch of code...]
dbms_output.put_line('This is a dbms_output call...');
end;
/
Thank you!

How to create directory from a stored procedure on an Oracle server machine? [duplicate]

I am having a problem using host() command in oracle procedure.
I have written very simple oracle code.
CREATE OR REPLACE PROCEDURE
run_command(command_i IN VARCHAR2)
IS
l_message VARCHAR2 (100);
BEGIN
l_message := 'cmd ' || command_i;
host(l_message);
END run_command;
when host(l_message); is eliminated works fine.
Whats the problem and is there anyway to create a routine which uses host()?
The HOST command is only available in SQL*Plus and not from PL/SQL.
You can use Java stored procedure to call call OS commands. Oracle released a white paper on calling OS commands from within PL/SQL back in 2008 but there's plenty of other stuff out there (including Oracle Base, which is quite good)
Another clunky, but non-Java way would be to create DBMS_SCHEDULER ad-hoc EXECUTABLE job which is your HOST command (e.g. ls ), and then execute the job.
Note these run on the database server, not on your remote/local client.

Is pl/sql don't use sql*plus command in Oracle?

example:
create or replace procedure DEMO is
begin
--host dir
--execute immediate 'host dir';
end DEMO;
I pupose invoke window's procedure in Oracle.
I try to use #command in sql*plus, but it discontent my requirement.
Stored procedures run on the database server. SQL*Plus commands are executed on the client. You cannot, therefore, use SQL*Plus commands in a stored procedure.
There is no way realistically for a stored procedure to get a directory listing for a directory on the client machine. (You could, I suppose, have the client expose a shared directory and mount that from the server and the read the contents from the database but that isn't something that would be realistic in the vast, vast majority of cases). A stored procedure can potentially call out to the database server's operating system using a Java or CLR stored procedure or, depending on the version, using the dbms_scheduler package. But there are a lot of security issues to be concerned about before implementing something like this.

Out of Memory Error : when executing Very Large Scripts in toad

I am using
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod &
Toad for oracle 10.6.1.3
when i try to run insert statement which is having around 84,000 + records, it is showing Out of Memory error. Here is the error image.
Any of you please suggest me, how i should execute this insert script in toad.
P.S : since toad is connecting to remote machine I'm not able to run it with SQLPLUS. If any one knows option to do that, please let me know.
If you need any more information, Please raise your hands in comment box i will provide you.
I got the same error when i want to execute sql script 70k rows.
But i solved it just like this.
Firstly
You should run it with sqlplus commands. Log in the sqlplus and run
this command
#scriptName.sql
Secondly (this is alternative)
You can use DBLINK in oracle.
As bpgergo suggested copy the sql file into remote machine using FTP, then open your SQLPLUS.
Follow this steps in SQLPLUS.
step 1 : change your current session using following query.
alter session set current_schema = Schema_Name;
Here, SCHEMA_NAME is schema name of your insert query table.
step 2 : Execute the sql script file using following query.
#{PATH}/FILE_NAME.SQL
Eg : #D:/oracle/script/FILE_NAME.SQL
Here, D:/oracle/script/ is the file available location and FILE_NAME is your sql script file name.
Now, It will work as expected.

JDBC issue when handling code in SQL file

I have an XML which is being called as part of software installer code. In the XML, I am executing SQL files using JDBC framework. The installer is failing at a point wherein the JDBC gets hole of the below statement inside a SQL file:-
Create or replace procedure test
as
Begin
...
End;
/
show errors
/
At the occurennce of "Show Errors" , the JDBC fails and installer execution finishes.
I have tried using the below syntax, but still JDBC fails.
Begin
show errors;
End;
/
When I remove "Show errors" from the SQL file, the installer finishes successfully. But I need to have "Show errors" in the SQL file.
Looking for some assistance here on how to use it without JDBC failing.
Thanks.
show errors is not a SQL statement, it's a SQL*Plus command, so it only works in SQL*Plus and cannot be used through JDBC.
Under the hood show errors simply queries the view ALL_ERRORS which you can do through JDBC as well:
SELECT line, position, text
FROM all_errors
WHERE owner = user
AND name = 'TEST' -- replace this with the name of your procedure

Resources