SqlPlus (Oracle) Spool Formatting Issue - oracle

I have scheduled a shell script which is outputting the following email. I want to remove the unwanted/additional duplicated text which is being printed using the dual table below to give a heading to my SQL select output.
How can I do it?
Following is the spool SQL script. After the script execution is done (/home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out) file is added to the email body and sent to recipients.
Can you guide which set parameter command is missing OR which one is extra or unnecessary below?
This is shell script file code.
sqlplus user/pass#pgwdb #/home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.sql
sleep 5
/bin/mail -s "MFS PGW Fraud Alerts (Day)" -r “test-email-1#email-domain.com” "test-email-2#email-domain.com" < /home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out
PGW_ALERTS.sql Code
set echo off;
set feedback off ;
set verify off ;
set serveroutput off;
set heading on;
set trimspool on;
set headsep off;
set PAGESIZE 60;
set LINESIZE 400;
SET WRAP OFF;
SET COLSEP ' ';
set numw 20;
SPOOL /home/oracle/MFS_ALERT/PGW_ALERTS_FRAUDS/PGW_ALERTS.out append;
select '====== Transactions with count > 2 from same MSISDN ======' from dual;
SELECT '"'
|| MOBILENUMBER
|| '"' MSISDN,
AMOUNT/100 AMOUNT,
DECODE(TXNTYPEID,'6','MW', '0') TXN_TYPE,
MERCHANTCODE,
COUNT (*) COUNT
FROM TBL_TXN_LOG
WHERE TXNTYPEID ='6'
AND AMOUNT <=10000
AND TXNDATETIME>= SYSDATE -1
AND STATUS ='xxxx'
GROUP BY '"'
|| MOBILENUMBER
|| '"', AMOUNT/100, DECODE(TXNTYPEID,'6','MW', '0'), MERCHANTCODE
HAVING COUNT (*) > 2
UNION ALL
SELECT '"'
|| MOBILENUMBER
|| '"' MSISDN ,
AMOUNT/100 AMOUNT,
DECODE(TXNTYPEID,'164', 'Card', '0') TXN_TYPE,
MERCHANTCODE,
COUNT (*) COUNT
FROM TBL_TXN_LOG
WHERE TXNTYPEID ='164'
AND AMOUNT >=2500000
AND TXNDATETIME>= SYSDATE - 1
AND STATUS ='xxxx'
GROUP BY MOBILENUMBER,
AMOUNT,
DECODE(TXNTYPEID,'164','Card', '0') ,
MERCHANTCODE
HAVING COUNT (*) > 1
;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
quit
The email which recipient is getting.,
-----Original Message----- From: destination.email#emaildomain.com Sent: Friday, December 8, 2017 7:36 PM To:
destination.email#emaildomain.com Subject: MFS PGW Fraud Alerts (4
Hourly)
***> '======TRANSACTIONSWITHAMOUNT<100FORMW&>25000======'
--------------------------------------------------------------***
====== Transactions with Amount < 100 for MW & > 25000 ======
MSISDN AMOUNT TXN_ MERCHANTCODE
COUNT
---------------------- -------------------- ---- ------------------------------ -------------------- "924008482888" 70 MW 00342386 1
"924008345433" 20 MW 002218387
1
I want to remove these extra lines
'======TRANSACTIONSWITHAMOUNT<100FORMW&>25000======'

Change your query a bit, use an alias for your heading, and turn off/on heading as shown:
set heading off
select '====== Transactions with count > 2 from same MSISDN ======' as info from dual;
select '<pre>' from dual;
set heading on
-- rest of your query
set heading off
select '</pre>' from dual;
That should do the trick if the message is being sent in HTML format.

Related

Oracle SQL Spool Result file add spaces in columns

I am spooling a big result in a file but the spool add a lot of space between columns :
one line is more than 6850 characters (mostly empty space), one column take 500 characters, for instance the first one :
23053607 ;
Here is the query :
spool result.txt;
set pagesize 0
set linesize 8000
SET TRIMSPOOL ON
set heading off
set space 0
set trimout off
set termout off
set verify off
set echo off
set feed off
set trimspool on
-- set echo off;
-- set termout off;
set colsep ';'
set feedback off;
set pages 0;
spool result.txt;
select
trim(rec.Code) AS Code,
trim(A.label) AS Number,
trim(B.text) AS Syst
FROM record rec
join tableA A on A.rec_id = rec.rec_id
join tableB B on B.kla_id = rec.kla_id;
spool off;
exit;
In the database there is no space.
How to avoid any spaces ?
Simplest thing to do is to just concatenate the values. Remove colsep and try out:
SELECT rec.Code ||';'||
A.label ||';'||
B.text
FROM record rec
JOIN tableA A ON A.rec_id = rec.rec_id
JOIN tableB B ON B.kla_id = rec.kla_id;

How do we configure email alerts when a tablespace is full in oracle database in windows os

any script where i can create a batch job to run via windows task scheduler
i am able to get a script when the tablespace is full but wanted that output to trigger an email to a specific address any suggestions please
below is the script which i have mentioned above
\\\\\\\\\\\\\\\\\\\\\\\\\\\
SET ECHO OFF; SET TERM OFF;
SET TIMING OFF;
SET HEAD OFF;
SET FEED OFF;
CREATE TABLE temp_ts(tablespace_name,total_bytes,free_bytes,max_chunk) AS SELECT
tablespace_name, NVL(SUM(bytes), 1), 1, 1 FROM dba_data_files
GROUP BY tablespace_name; UPDATE temp_ts a SET a.free_bytes =
(SELECT NVL(SUM(b.bytes), 1)
FROM dba_free_space b
WHERE b.tablespace_name = a.tablespace_name); COMMIT;
UPDATE temp_ts a SET a.max_chunk = (SELECT NVL(MAX(b.bytes), 1)
FROM dba_free_space b
WHERE b.tablespace_name = a.tablespace_name); COMMIT; REM ************************ REM Display tablespaces
more than 95% full. SELECT tablespace_name
||
' is ' ||
TO_CHAR(ROUND(100-(free_bytes*100/total_bytes), 2)) ||
'% full.' Tablename FROM temp_ts WHERE 95 < 100-(free_bytes*100/total_bytes) ORDER BY tablespace_name;
the output will be the tablespacename along with the percentage
now i want that output to trigger an email if it is crossing the more than 95%
any suggestions ??

SQL*Plus Custom Summary Report

I have a requirement to generate the following summary report that look like the following:
My problem is that,
I have no idea on how do I fill the count data in this custom report.
I do not know how to put it in a table view like the above in a text document. It is not HTML.
So far, I only know how to do the first row, and column without the table view.
Here are my codes.
SET HEADING OFF;
SET LINESIZE 200;
SET SPACE 0;
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET MARKUP HTML OFF SPOOL OFF;
SET TERMOUT OFF; --Do not show output to the screen.
SET NEWPAGE NONE; --Remove the first blank line at the top of the page and between queries.
TTITLE LEFT "REPORT NAME" RIGHT "PAGE : " SQL.PNO SKIP 1 -
LEFT "--------------------------------" RIGHT "DATE : " _DATE SKIP 1 -
LEFT "A) TOTAL RECORDS " RIGHT total_records; -- Cannot output variable in the title.
LEFT "B) MATCHED RECORDS " RIGHT matched_records; -- Cannot output variable in the title.
LEFT "C) UNMATCHED RECORDS " RIGHT matched_records; -- Cannot output variable in the title.
BTITLE LEFT "E N D";
total_records is an insert into statement.
SELECT COUNT(*) INTO total_records FROM TABLE;
I have not done matched records and unmatched records. But the only way I can think of
Select a statement into a cursor.
Loop into the cursor.
Increase matched count when there is a match.
Once loop finish. unmatched count = total count - matched count.
I don't think this is the most efficient way. But, if you have a better way, let me know.
Does something like this ring a bell? Example is based on Scott's sample schema:
SQL> select 'Total records' name, count(*) cnt
2 from emp
3 union all
4 select 'Matched count', sum(case when deptno = 10 then 1 else 0 end)
5 from emp
6 union all
7 select 'Unmatched count', sum(case when deptno = 10 then 0 else 1 end)
8 from emp;
NAME CNT
--------------- ----------
Total records 14
Matched count 3
Unmatched count 11
SQL>

how to show data in CSV in different colums using plsql?

I have written sql to generate the output data in CSV format.I have used spool to generate in CSV.
SET LINESIZE 1000 TRIMSPOOL ON FEEDBACK OFF
SPOOL E:\oracle\extract\emp2.csv
SELECT emp_id,
emp_name
FROM offc.employee
ORDER BY emp_id;
SPOOL OFF
SET PAGESIZE 14
The output is coming in location.The output is generated correctly but when I see the CSV file ,then all the data are coming in same column A in CSV.
I want emp_id in column A and emp_name in columnB.Why they are coming in same column? What is the problem here?
Here, the issue is the space is considered as the separator for your column data and excel can not separate the column values which are space separated.
You can use colsep attribute along with other attributes as following:
set colsep , -- defines column data separator
set pagesize 14 -- defines size of the page. Keep it large so that header is not repeated
set trimspool on -- remove trailing blanks
set lines 1000 -- linesize should be more than sum of width of the all columns
set FEEDBACK OFF -- removes the comment at the end of the data
SPOOL E:\oracle\extract\emp2.csv
SELECT emp_id,
emp_name
FROM offc.employee
ORDER BY emp_id;
SPOOL OFF
Cheers!!
What is the problem here?
Excel expects a Comma Separated Values file to have values in columns separated by commas. Your query outputs two columns of data but doesn't include an explicit separator. So when Excel reads the file it doesn't find any commas so it renders a single column of data.
There are various ways of solving this. One is include your own explicit separator in the query:
SELECT emp_id
|| ', ' ||
emp_name
FROM offc.employee
ORDER BY emp_id;
Another is to use the SQL*Plus colsep command to format the output in the file. A third option is to use a tool like SQL Developer, whose Export feature handles all this for us.
Talking about my own experience: although Excel knows how to open a CSV file, it is kind of stupid and still puts everything into the first column. Therefore, I prefer creating a TXT file instead, using a column separator (so - yes, it basically is a comma-separated-file (or whichever separator you choose)).
For example:
SQL> set pagesize 100
SQL> set linesize 100
SQL> set colsep ";"
SQL>
SQL> spool emp.txt
SQL>
SQL> select * from emp;
EMPNO;ENAME ;JOB ; MGR;HIREDATE; SAL; COMM; DEPTNO
----------;----------;---------;----------;--------;----------;----------;----------
7369;SMITH ;CLERK ; 7902;17.12.80; 800; ; 20
7499;ALLEN ;SALESMAN ; 7698;20.02.81; 1600; 300; 30
7521;WARD ;SALESMAN ; 7698;22.02.81; 1250; 500; 30
7566;JONES ;MANAGER ; 7839;02.04.81; 2975; ; 20
7654;MARTIN ;SALESMAN ; 7698;28.09.81; 1250; 1400; 30
7698;BLAKE ;MANAGER ; 7839;01.05.81; 2850; ; 30
7782;CLARK ;MANAGER ; 7839;09.06.81; 2450; ; 10
7788;SCOTT ;ANALYST ; 7566;09.12.82; 3000; ; 20
7839;KING ;PRESIDENT; ;17.11.81; 5000; ; 10
7844;TURNER ;SALESMAN ; 7698;08.09.81; 1500; 0; 30
7876;ADAMS ;CLERK ; 7788;12.01.83; 1100; ; 20
7900;JAMES ;CLERK ; 7698;03.12.81; 950; ; 30
7902;FORD ;ANALYST ; 7566;03.12.81; 3000; ; 20
7934;MILLER ;CLERK ; 7782;23.01.82; 1300; ; 10
14 rows selected.
SQL> spool off;
Now, start Excel and go to Open; choose "All files" (i.e. not only Excel-type files) so that you'd see emp.txt listed. Excel then - in its "Text Import Wizard" - asks you which kind of a file it is (choose delimited):
set the separator (semi-colon in our example)
and - open the file:
Everything is now in its own column.

Unreadable character in generated sqlplus file

I have a shell with one command line opening sql plus and calling a sql file
$ORACLE_HOME/bin/sqlplus id/psw #SQL_DIRECTORY/myfile.sql
in this file I write this :
spool myfile.csv
select 'column 01', ';', 'column 02' from dual;
select myColumn1, ';', mycolum2 from mytable
spool off
It works good BUT on the first line of the the first column of each select there is a special character.
Why is that? how can I get rid of this
Thanks
From the description it sounds like you have a login.sql or glogin.sql user profile that is issuing set newpage 0:
Sets the number of blank lines to be printed from the top of each page to the top title. A value of zero places a formfeed at the beginning of each page (including the first page) and clears the screen on most terminals. If you set NEWPAGE to NONE, SQL*Plus does not print a blank line or formfeed between the report pages.
In Excel that does show up as an unprintable character with a question mark in a small square; in Notepad++ it seems to show as FF in reverse colouring; and in some editors (e.g. Vim) it shows as ^L. This is the ASCII form feed character, decimal 12 or 0xC.
You can either reset that with set newpage none, or make it irrelevant with set pagesize 0, which has the convenient side effect of removing the column headers and separators. You may also want to set feedback off if you aren't already.
Samples with contrasting settings:
set newpage 0;
set feedback on;
set pagesize 100;
spool ctrl.csv
select 'head 1;head 2' from dual;
select sysdate, ';', systimestamp from dual;
spool off
^L'HEAD1;HEAD2'
-------------
head 1;head 2
1 row selected.
^LSYSDATE ' SYSTIMESTAMP
------------------- - ---------------------------------------------------------------------------
2015-09-16 15:45:42 ; 16-SEP-15 15.45.42.333627 +01:00
1 row selected.
And
set newpage none;
set feedback off;
set pagesize 0;
spool ctrl.csv
select 'head 1;head 2' from dual;
select sysdate, ';', systimestamp from dual;
spool off
head 1;head 2
2015-09-16 15:46:11 ; 16-SEP-15 15.46.11.274863 +01:00

Resources