Oracle SQLPlus: Echo without line numbers? - sql

I'm working on a solution where several SQL and PL/SQL scripts are being run together, in a batch of sorts, via SQL*Plus.
I'm declaring SET ECHO OFF; and SET ECHO ON; at relevant points in the scripts so as to output relevant code.
Currently the output looks something like this:
SQL> DECLARE
2 ct number := 0;
3 ctChanges number := 0;
4
5 BEGIN
6 select count(*) into ct from ...
7 (...rest of code block...)
"some specific status message"
Commit executed.
We keep this output as a run-log in our build-environment, but can also access it as a plain text file.
One downside of this format however, is that if I'd like to copy a certain section of the code and run it again in an IDE (like Toad or SQL Developer), it's hard to exclude the line numbers.
Is it possible to tell SQL*Plus to output the code as above, but without including the line numbers?

You can use options sqlnumber and sqlprompt:
set sqlprompt ''
set sqlnumber off
SET SQLN[UMBER] {ON|OFF}
SET SQLNUMBER is not supported in iSQL*Plus
Sets the prompt for the second and subsequent lines of a SQL command or PL/SQL block. ON sets the prompt to be the line number. OFF sets the prompt to the value of SQLPROMPT.

Related

Write to Oracle concurrent request output / log from a SQLPlus program

I have an Oracle concurrent request that calls a SQLPlus program. The program itself is working correctly, but I would like to add some logging information to the concurrent request output / log in EBS.
I have tried a number of variations of:
set heading off
--set pagesize 0 embedded on
set pagesize 50000
set linesize 32767
set feedback off
set verify off
set term off
set echo off
set newpage none
set serveroutput on
dbms_output.enable(1000000);
--prepare data
EXECUTE program (&1,&2,&3,&4,&5);
--extract data
#"path/file.SQL";
fnd_file.put_line(FND_FILE.LOG,'do some logging here');
fnd_file.put_line(FND_FILE.OUTPUT,'do some logging here');
/
But everything I've tried so far results with either
no logging added to request output or log
no request output whatsoever
errors like:
SP2-0734: unknown command beginning "dbms_outpu..." - rest of line ignored.
and
PLS-00103: Encountered the symbol "ENABLE" when expecting one of the following: := . ( # % ;
Is it possible to write to the request output or log from a SQLPlus script that is called from concurrent manager?
First of all, your SQL*Plus script does not even run without your attempts at logging.
dbms_output enable(...) is missing a dot ('.').
Your anonymous PL/SQL block has no end; statement
#"path/file.SQL` is a SQL*Plus command -- it cannot be embedded in an anonymous PL/SQL block.
Aside from those basic problems, FND_FILE.PUT_LINE is only for PL/SQL concurrent programs. That is, concurrent programs whose executable points to a PL/SQL package procedure and not a .sql file under $APPL_TOP.
For SQL*Plus concurrent programs, i.e., running a .sql file under $APPL_TOP, FND_FILE.PUT_LINE does not work. Instead, your SQL*Plus output is automatically written to the request output. There is no standard way to write to the request log.
If you really need to write to the request log, you could maybe call FND_FILE.PUT_NAMES to cause FND_FILE.PUT_LINE to write to temporary files that you name. Then, knowing the concurrent request ID and the logic Oracle EBS uses to local output and log files, do a FND_FILE.CLOSE and host command to move the custom-named files you specified to the actual locations. That might work.
It'd be much better to redo your concurrent program as a PL/SQL package. Then FND_FILE works just fine. If you know how to call Java from the database, there is very little you can do in a .sql script that you cannot do in a PL/SQL package.
I have not written a .sql concurrent program in years, and I write concurrent programs all the time.
I have resolved this problem. The solution is incredibly simple - and now I'm bent out of shape because it took so long to realize.
Step 1 - SET ECHO ON
Step 2 - PROMPT whatever you want written to concurrent request output
The following sample writes 'Output is written to this folder' to the concurrent request output.
set heading off
--set pagesize 0 embedded on
set pagesize 50000
set linesize 32767
set feedback off
set verify off
set term off
set echo on
set newpage none
set serveroutput on
prompt Output is written to this folder
--prepare data
EXECUTE program (&1,&2,&3,&4,&5);
--extract data
#"path/file.SQL";
/
This is exactly what I was looking for. Maybe this will be useful to someone in another galaxy.
If this is for testing/debugging purposes, you can specify the location of the log and output files with the routine: FND_FILE.PUT_NAMES and as soon as you log all the required information you need to close the file with: FND_FILE.CLOSE
As Matthew mentioned, logging in SQL*Plus executables doesn't work well. If you can't move your code to a PL/SQL Stored Procedure for some reason, a Host script might work for you instead. From there, you can execute SQL, e.g. sqlplus -s $FCP_LOGIN ... and write log information as required.
If you just need to prepare data by PLSQL and then spool it to CSV via SQL, you can use our company's Blitz Report instead, which does this more convenient and is for free for such use. It also uses a Host type executable and calls sqlplus from there.

What's the equivalent of clrscr(); in Oracle SQL?

I want to write a simple script file to display the employee name in a given department name the department name given is case insensitive ,after executing the script, the commands are not displayed.
I use SQL *PLUS and what I did so far is
EDIT script // script is the file name the default extension is .SQL
and inside the script file I wrote the following
SET VERIFY OFF
SELECT Ename, dname
FROM emp, dept
WHERE emp.deptno = dept.deptno
AND UPPER(Dname) = UPPER('&dname');
SET VERIFY ON
then on SQL *Plus
START script
The query works fine but I don't know how to do this part "after executing the script, the commands are not displayed.
"
Maybe are you looking for:
SET ECHO OFF
An other option would be to start SQL*Plus with the -S (silent) option on the command line. From the documentation:
-S[ILENT]
Suppresses all SQLPlus information and prompt messages, including the command prompt, the echoing of commands, and the banner normally displayed when you start SQLPlus.
As about the question as titled:
What's the equivalent of clrscr()
If you are using an ANSI terminal, using the ANSI escape sequence esc[2J should clear your screen:
SET ECHO OFF
SET SERVEROUTPUT ON
VAR ANSI_TERM_CLEAR VARCHAR2 (10)
BEGIN SELECT CHR(27)||'[2J' INTO :ANSI_TERM_CLEAR FROM DUAL; END;
/
PRINT :ANSI_TERM_CLEAR

Oracle how to print some message?

As we know in MSSQL we can write below line to print some message
print 'Some Message';
How we can do same in Oracle as print not working with Oracle?
Use
DBMS_OUTPUT.put_line('Some Message');
To expand on #mhasan's answer and #AlexPoole's comment: assuming that you're executing your script using SQL*Plus you'll need to add the beginning of your script before the first DECLARE or BEGIN:
SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 255
If you're using a tool other than SQL*Plus to run your script there will be different ways to view output written to DBMS_OUTPUT. For example, in PL/SQL Developer a "Test" window has a "DBMS Output" tab where text written to DBMS_OUTPUT can be viewed after the test script terminates.
Share and enjoy.

How do we print characters line by line and save it to csv or text file in PLSQL

DECLARE
V_NUMBER NUMBER :=23;
BEGIN
LOOP
V_NUMBER:=V_NUMBER+1;
EXIT WHEN V_NUMBER:=25;
--Some kind of function to be applied for printing and nesting lines into CSV or TEXT file.
END LOOP;
COMMIT;
END;
Scripting an Oracle SQL Query for Creating a CSV or Text Typed File Output
Consider running this from a SQL Plus session and use the SPOOL command. All output of the SQL command that follows will be written to the file name you specify.
If you need to append your results each successive time the SQL commands are run, then an OS level command would work appropriately when invoking this sqlplus executable block of PL/SQL:
Where the file name of this script is: "sample_csv_out.sql"
DECLARE
v_total_columns constant number:= 3; -- Number of columns queried
v_column_counter number;
v_csv_record varchar2(1000);
c_csv_column_format constant varchar2(15):=
'<<COLUMN1_VAL>>,<<COLUMN2_VAL>>,<<COLUMN3_VAL>>';
cursor result_cur is
SELECT column1, column2, column3
FROM tablea
WHERE column1 = ... ;
BEGIN
v_csv_record:= 'COLUMN1,COLUMN2,COLUMN3';
dbms_output.put_line (v_csv_record);
FOR i in result_cur LOOP
v_csv_record:= replace(c_csv_column_format, '<<COLUMN1_VAL>>', i.column1);
v_csv_record:= replace(v_csv_record, '<<COLUMN2_VAL>>', i.column2);
v_csv_record:= replace(c_csv_record, '<<COLUMN3_VAL>>', i.column3);
dbms_output.put_line(v_csv_record);
END LOOP;
END;
So, for example in a WINDOWS O/S environment, the call to append the output to a specific file name would be:
C:\> sqlplus sample_csv_out.sql >> mycsv_out.csv
The >> notation instructs the operating system to pipe the output of running sample_csv_out.sql via a sqlplus session.
The command DBMS_OUTPUT does the rest. If you need more details, see more Oracle documentation on DBMS_OUTPUT.
COMMENTS: I chose the RECORD STRING TEMPLATE approach to make this script a little more flexible and reusable. I recommend to keep any data manipulation logic within the CURSOR statement. Often when the two are mixed, it gets harder to debug any typos in syntax within a long string of values.
The construction of an output record was also designed to reduce typos, mistakes and frustration... if there are more than 3 columns in your own scripts, adding another element to the output string is mostly a cut-and-paste operation. Likewise with the "header" row (column titles).
You can read and write files in PL/SQL using the UTIL_FILE package
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm

Clear Screen in SQL*Plus

I'm running the following report but getting an error
/* Simple table formatting */
clear screen;
accept Report_File char prompt 'Enter a file name for summary report ';
/*Set up column headers*/
col StoreCode format A8 heading 'Store Code';
col DESCRIPTION format A8 heading 'Item Description';
col PRICE format $999999.99 heading 'Price';
col QUANTITY format 999 heading 'Quantity';
col (Price*Quantity) format $999999.99 heading 'Value';
/*Format and title pages */
set Pause off;
set Feedback off;
set Space 6;
set newpage 2;
set pagesize 54;
set linesize 200;
set underline =;
title center 'Current Stock Value by Store' skip 2 left -
'prepared by Jason Kemeys' &Report_Officer right -
&Todays_Date skip4;
btitle center format 999 SQL.PNO;
/* Set breaks and computes */
break on StoreCode skip 2 on SuppCode skip 1 on Report;
compute sum of (Price*Quantity) on StoreCode;
compute sum of (Price*Quantity) on Report;
/*Select data & send to file*/
spool &Report_File;
select StoreCode, Description, Quantity, Price, (Price*Quantity)
from Stocks
order by StoreCode;
spool off;
/* Clear all settings */
clear breaks;
clear columns;
clear computes;
set Pause on;
Just need to know why its showing the error and how to get it running; first time doing a report in SQL.
This is the error I'm getting
clear screen;
* ERROR at line 2: ORA-00900: invalid SQL statement
cl scr is the command used to clear screen in SQL.
I suspect this is dependent on the version of Oracle you are using.
This should work in version 11.2 but to quote from the 10g documentation:
CLEAR SCREEN is not available in SQL*Plus.
The same note isn't there in the 11.1 documentation, which would imply that you're using Oracle 10g or earlier. If this supposition is true then there's little that you can do.
It's possible that you can utilise the host command in SQL*Plus to run cls, if you're using Windows, or clear, if you're using Linux, but I'm not certain that it would have exactly the same effect. If it were possible it would simply be:
host cls
host runs an operating system command from SQL*Plus, and so will appear to clear the screen.
simply use cl scr command to clear the SQL plus.
Very few occurances of ";" in your code is actually needed. For example, the "clear screen" command to start with, doesn't need a semicolon. It will work, when you add one, but I wouldn't be sure about all the subsequent commands in that same file. Commands that need them, are limited to INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, and such.
Secondly, if you get weird feedback from SQL-files, and if you have written them outside of Linux/Unix, this often ends up in SQLPLUS complaining about invisible characters. Look at that file via both VI and CAT commands, and note anything weird.