Oracle 11g Report Problem - oracle

Hey friends i am using Oracle 11G. I wanted to generate reports for printing , SO i write this script
rem Employee Salary Report
set headsep !
ttitle 'Salary Report'
btitle 'From Employees'
column employee_id format 999.99
column first_name format a20
column last_name format a20
column Salary format 999.99
break on employee_id skip 1 on report
set linesize 80
set pagesize 5
set newpage 0
set feedback off
set pause 'More...'
set pause on
spool activity.lst
select employee_id,first_name,last_name,salary from Employees order by employee_id ;
spool off
When running this script oracle gives
line 3: SQLPLUS Command Skipped: set headsep !
line 15: SQLPLUS Command Skipped: set linesize 80
line 16: SQLPLUS Command Skipped: set pagesize 5
line 17: SQLPLUS Command Skipped: set newpage 0
and then it executes query and gives output. But my report doesn't include any title in it.Means reports is not generated properly. It just simply executes the select query and gives an output which is not a report.

Did you have a question?
What you show us looks very much like the output we would expect if your script was run from the SQL Worksheet in Oracle SQL Developer.
Those commands that are being skipped are specific to SQL*Plus, and are not supported in SQL Developer (at least, in the version I'm running).
To get a formatted report produced by SQL*Plus, I would run the sqlplus executable from the OS e.g.
> $ORACLE_HOME/bin/sqlplus /
SQL> #/home/spencer7593/myreport.sql
SQL> exit
>
I'm not sure that answers a question, since I don't know what your question was.

Related

Export data as XSV from oracle

I want to export some data (i.e. some queries results') from an old oracle database into a file, values separated with a specified character or sequence of characters. A CSV file is an example.
Only integers, varchars (without newline included), dates, other basic values (?) into result set, so no LOBs or another fancy stuff. Just plain data, NO PADDING, NO WRAPPING, fields separated and maybe enclosed.
Practicaly, I'm looking for a simple "SELECT ... INTO OUTFILE FIELDS TERMINATED BY ... [OPTIONALLY] ENCLOSED BY 'char'" from mysql, but for Oracle.
I tried to use sqlplus, but any program should be ok, as long as it runs on linux console and it's not java based :-P.
Unfortunately I cannot use "SET SQLFORMAT csv", seems to be not supported.
So far I've got best results with something like:
printf "set echo off newpage 0 pagesize 0 arraysize 5000 feed off head off trimspool on trimout on\nselect field1 || '|' || field2 from table;" | sqlplus -S database > output_file
Set arraysize 5000 (maximum) gives the best performance, unfortunately wraps the result if lines are longer than 80 chars.
Set linesize 30000 (or even 32767) cancels wrapping, unfortunately the performance becomes horrible. I don't want to specify a maximum linesize for each query (calculating maximum size for each field in result), hoping that performance will rise.
After reading tons of answers I'm still not getting close to a solution that should be both CORRECT and PERFORMANT. My next call will be writing a php file to do the job, but it's a nonsense, IMHO using the default application sqlplus should give the best performances ...
Any ideas ?
I can't tell whether this set of SET commands will help (I use it, usually), but - try it.
set termout off
set trimspool on
set echo off
set verify off
set autoprint off
set serveroutput off
set arraysize 1000
set pagesize 0
set linesize 100
set long 10000
set numwidth 10
set feedback off
set colsep ';'
col empno format 99999
col ename format a10
col sal format 999G990
spool emp.txt
select empno, ename, sal from emp;
spool off
At the bottom, you can see some COL formattings; remove it if you don't need it.
Don't set linesize to that high number (30.000) if you don't really need it.
Also, there's the set colsep ';' which will separate columns by that separator (semi-colon in this example; can be any other character, such as | you mentioned).
In order to speed up spool:
set termout off
set trimspool on
run it on server, not on the client PC
Ok, for everyone that had this problem, here is the answer:
Make sure you have at least oracle client 12.2 installed.
If not, you can extract the files from instantclient-basic-linux.x64-12.2.0.1.0.zip (library files) and from jar archives from sqlplus related directories from linuxx64_12201_client.zip (client/stage/Components/oracle.sqlplus*). Those two zips are available to download for free (registration required) at oracle download site. I didn't want to install that version of the client because it could mixed up with local installed one. It's kind of an ugly hack, but at least no file on system was modified.
From 12.2 onwards the "set markup csv" command is supported, and the command was:
printf "set head off\nset feedback off\nset arraysize 5000\nselect field1, field2 from table;" | LD_LIBRARY_PATH=/path/to/oracle12/lib ORACLE_HOME=/path/to/oracle12 /path/to/oracle12/bin/sqlplus -M "CSV ON" -S database
adjust the arraysize variable (5000 is maximum and fastest, but pay attention to the memory), choose the "SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]" parameters and enjoy

How to spool explain results from Oracle via sqlplus?

I'm having issues with spooling out results from queries, more or less it shows always no real cost, shows rows as one and no cost or anything added, and not the statistics of the actual query.
I'm using this setup:
current_date=$(date +%Y-%m-%d)
sqlplus -S "Username/password#mydatabase.bag" <<EOF >/output/Testoutput_$current_date.log
set verify off;
set colsep ,
SET AUTOTRACE ON
set headsep off
set pagesize 0
set trimspool on
set termout off
col v_spool noprint new_value v_spool
select 'Spoolfile'||
to_char(sysdate,'yyyy_mm_dd_hh24_mi_ss')||'.csv' v_spool from dual;
set termout on
spool /folder/subfolder/&&v_spool
set lines 12345 pages 12345;
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
SET TIMING OFF
spool off
EXIT
EOF
My wish is to somehow get the explain results spooled to a file, what do I need to modify to achieve this or there is a different approach?
If you try running this in SQL*Plus:
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
You'll notice that it just outputs:
Explained.
You have to actually select the results of the EXPLAIN PLAN using DBMS_XPLAN - that will get output and spooled to your file.
-- generate the explain plan
EXPLAIN PLAN SET statement_id = 'example_plan1' FOR
select * from dbms.database1_1 where numberline like '%4214%';
-- actually display the results
select plan_table_output
from table(dbms_xplan.display('plan_table',null,'typical'));

sqlplus query with comment giving duplicate rows

Updating my question:
When I run this query from unix using sqplus it gives me duplicate outputs
Query:
select sysdate from dual
/
/*comment*/
EXIT
Output
SQL> #tt.sql
12-AUG-16
12-AUG-16
Here is what is happening.
The first two lines (including the /) form a complete SQL statement. SQLPlus executes it and displays the first line.
Then /*comment*/ is OUTSIDE the SQL statement. So it is interpreted by SQLPlus, not as a SQL comment.
The point is, comments in the SQLPlus scripting language have a different syntax; they are lines that begin with REM[ARK]. /*comment*/ is NOT a comment in SQLPlus.
Instead, in SQLPlus, a slash / as the first character on a new line means repeat the last SQL command (if after that you had SQLPlus commands, like DESCRIBE or COLUMN, those are ignored). Everything AFTER the slash is ignored. To convince yourself of this, try again, but with the comment line changed to /*comment* (delete the second slash). You will see the same result.
Then you have the command EXIT, but since it is NOT followed by a slash, it is not seen as a completed command. If I copy and paste your code in my SQLPlus window, I get the screenshot below. However, if at this point you press ENTER (or if this was in a sql script and not copied and pasted from a text editor), the SQLPlus window would shut down.
Hope this helps!
SQL> select sysdate from dual
2 /
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.00
SQL> /*comment*/
SYSDATE
----------
2016-08-12
1 row selected.
Elapsed: 00:00:00.01
SQL> EXIT

How to export half a million records from PL/SQL

I've a table with around 500,000 records. I need all records to be exported in excel. When I query I'm not able to get all as I was said as Out Of memory
Table doesn't have any primary key/Index.
Is there any way to extract?
it would be very easy in to write file output form sqlplus .
mycsv.sql:
SET DEFINE OFF
SET ECHO OFF
SET SERVEROUTPUT OFF
SET TERMOUT OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 10000
SET ARRAYSIZE 5000
REM SET HEAD OFF
SET LINE 500
spool /tmp/mycsvfile.csv;
select * from MY_table;
spool off;
exit;
and from Linux prompt you can run like
$> sqlplus username/password #/tmp/mycsv.sql

Remove Column Header into the Output Text file

I want to create a flat file (text file) of my query from Oracle SQL Developer.
I have successfully created the text file using SPOOL, thru a script text file, but i want to remove the header of each column into my output.
I am getting this output:
Header000001 Header000002
------------ ------------
Adetail1 Bdetail1
Adetail2 Bdetail2
Adetail3 Bdetail3
But, I want to get this output:
Adetail1Bdetail1
Adetail2Bdetail2
Adetail3Bdetail3
I already tried the command "set heading off", but a message says:
"SQLPLUS COMMAND Skipped: set heading off".
These are the inputs I've issued:
spool on;
spool C:\SQLFiles\PSB_ATMLKP.txt;
set newpage 0;
set echo off;
set feedback off;
set heading off;
select terminal_number, terminal_name from terminal_table;
spool off;
SQLPLUS COMMAND Skipped: set heading off
That message is most likely because you are not executing it through SQL*Plus, but some GUI based tool. You are using SQLPlus command in SQL Developer. Not all SQL*Plus commands are guaranteed to work with SQL Developer.
I would suggest you execute the script in SQLPlus and you would see no issues.
You need:
SET HEADING OFF
This will not include the column headers in the output.
Alternatively, you could also do this:
SET PAGESIZE 0
Using SQL Developer Version 3.2.20.10:
spool ON
spool D:\test.txt
SET heading OFF
SELECT ename FROM emp;
spool off
Spool file got created with no issues:
> set heading OFF
> SELECT ename FROM emp
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected
Add:
set underline off
to the beginning of the SQL script.
In my SQL scripts I have:
SET TERMOUT OFF
set colsep |
set pagesize 0
set trimspool on
set pagesize 0 embedded on
SET heading on
SET UNDERLINE OFF
spool file_path
-- your SQL here
spool off
See this book for reference.

Resources