I'm new to SQL, I'm trying to run a query to extract alarms from DB using certain conditions and output the result to a file in csv format.
I created the below SQL script however the header is not generated in the required format.
set markup HTML on ENTMAP on spool off PREFORMAT on;
set pagesize 0 embedded on;
set feedback off;
set termout off;
set trimout on;
spool on;
set trimspool on;
spool /tmp/scripts/outputFiles/calExport.csv REPLACE;
select (y.ALARM_DN||','||x.CO_NAME||','||to_char(y.ALARM_TIME, 'dd-MM-yyyy HH24:MI:SS')||','||y.ALARM_TEXT||','||y.ALARM_TYPE||','||y.PERCEIVED_SEVERITY||','||y.ADDITIONAL_INFO_1||','||y.ADDITIONAL_INFO_3||','||y.ADDITIONAL_INFO_2) from CTP_COMMON_OBJECTS x, FM_ALARM y where y.NE_GID=x.CO_GID and y.ALARM_STATUS=1;
spool off;
exit;
Current output:
''
(Y.ALARM_DN||','||X.CO_NAME||','||TO_CHAR(Y.ALARM_TIME,'DD-MM-YYYYHH24:MI:SS')||
--------------------------------------------------------------------------------
xxxx/xxxx-143/xxxx-143,xxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxx
,03-10-2017 15:19:49,Dead Peer Detected,1,2,shared:N;,,additionalFaultId:0;
xxxx/xxxx-143/xxxx-143,xxxxxxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxxx,13-09-
2017 12:45:48,LOS on unit 0, Ethernet interface 4,1,1,,,
xxxx/xxxx-143/xxxx-143,xxxxxxxxxxxxxxxxxxxx,03-10-2017 15:19:39,BA
NOTIFICATION,3,3,Files collected,,100 100 100 6450xxxx 1 0
path=/xxxx-1(Left) additionalFaultId:6450;
Problems:
How to correct header format to match with below?
ALARM_DN,NAME,ALARM_TIME,TEXT,TYPE,SEVERITY,A,B,C
How to remove "pre" tag at the beginning and end of the output file?
How to remove the blank lines after each row in the output? I used set trimspool on but blank lines are still there.
Appreciate your help.
BR,
A.M.
To change the heading to what you want change your SQL*Plus script to:
set markup HTML on ENTMAP on spool off PREFORMAT on;
set pagesize 0 embedded on;
set feedback off;
set termout off;
set trimout on;
SET HEADING OFF;
spool on;
set trimspool on;
spool /tmp/scripts/outputFiles/calExport.csv REPLACE;
SELECT 'ALARM_DN,NAME,ALARM_TIME,TEXT,TYPE,SEVERITY,A,B,C' FROM DUAL
UNION ALL
select (y.ALARM_DN||','||x.CO_NAME||','||to_char(y.ALARM_TIME, 'dd-MM-yyyy HH24:MI:SS')||','||y.ALARM_TEXT||','||y.ALARM_TYPE||','||y.PERCEIVED_SEVERITY||','||y.ADDITIONAL_INFO_1||','||y.ADDITIONAL_INFO_3||','||y.ADDITIONAL_INFO_2) from CTP_COMMON_OBJECTS x, FM_ALARM y where y.NE_GID=x.CO_GID and y.ALARM_STATUS=1;
spool off;
exit;
Best of luck.
Related
I've got a problem when spooling into a csv from sqlplus
Here is my script:
set heading on;
set colsep ',';
set pause off;
set pages 0;
set pagesize 50000;
set feedback off;
set term off;
set echo off;
set verify off;
set linesize 20000;
set trimspool on;
SELECT
"Van Name",
"Date",
"Shift_ID",
"Shift",
"Departure Weight",
"Reload",
"Maximum Capacity (Weight)",
"Planned Distance(KM)",
"Shift Start Time",
"Departure Time",
"Depart Last Stop" from
Out put of above spool is not properly resulting into csv. facing below issues.
Example: "Shift" in my select statement (4columns) is just coming
up as 'Shi' but other which has long description is coming file
Resulting empty row at 1st and as expected 2nd row header
3rd, I am getting underline as ---------
Could some one suggest how to fix above 3 points greatly appreciated.
Is it possible to run an sql script in an anonymous block?
I would like to be able to conditionally run an sql script.
#!/bin/bash
.
.
.
`sqlplus -S /nolog > log3 << EOFSQL
connect ${userName}/${userPassword}#${urlDataBase};
set serveroutput on
set pagesize 0
set feedback off
set verify off
set heading off
set wrap off
set linesize 150
DECLARE
lv_error VARCHAR2(100):='';
BEGIN
SELECT TRIM(STATUS) INTO lv_error FROM tab1
WHERE ...
IF lv_error NOT LIKE 'ERROR%' THEN
START $(pwd)/script.sql;
/
END IF;
END;
/
In your case it is better to write a PL/SQL procedure and invoke it from the shell script. That way, it is easier to conditionally call the second sql function/procedure.
Or, if you want to keep the logic in shell script, spool the output of the first SQL and then read it and call the second sql script.
Something like this.
#!/bin/bash
.
.
.
sqlplus -S /nolog > log3 << EOFSQL
connect ${userName}/${userPassword}#${urlDataBase};
spool spool_out.txt
set linesize 150
DECLARE
lv_error VARCHAR2(100):='';
BEGIN
SELECT TRIM(STATUS) FROM tab1
WHERE ...
END;
/
EOFSQL
errorPresent=`cat spool_out.txt|grep "ERROR"`
if [ ! -z $errorPresent ]
the
#Call $(pwd)/script.sql;
fi
I want to pass shell variables to sql statement. Both shell script and SQL statement are present in the same script file.
I want the values of the variables retMonth, retLastDay and retPrvYear in the SQL statement.
Below is the code.
If I execute this, it prints - " partition_date between '01--' and '--' \ 0 0] 1 1] 12-DEC-14 1"
How can I have values of retMonth, retLastDay and retPrvYear in SQL statement?
echo $retMonth //This prints 07
echo $retLastDay //This prints 31
echo $retPrvYear //This prints 2015
count=$(sqlplus -s ${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}#${ORACLE_SID} <<END
#connect ${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}#${ORACLE_SID}
set serveroutput on
set linesize 1000
set heading off
set feedback off
define lastMonth=$retMonth
define lastYear=$retPrvYear
define lastDay=$retLastDay
SELECT count(1)
FROM MYTABLE
WHERE partition_date between '01-$lastMonth-$lastYear' and '$lastDay-$lastMonth-$lastYear'
);
END
)
Try using quoted shell variables directly without using define directives:
count=$(sqlplus -s "${DBA_ORACLE_USER}/${DBA_ORACLE_PWORD}#${ORACLE_SID}" <<END
set serveroutput on
set linesize 1000
set heading off
set feedback off
SELECT count(1)
FROM MYTABLE
WHERE partition_date between
"01-$retMonth-$retPrvYear" and "$retLastDay-$retMonth-$retPrvYear";
END
)
I am trying to find out the difference between two tables in unix.
Here is the snippet of my shell script
sqlplus -s `echo $user`/`echo $password`#`echo $host`:`echo $port`/`echo $sid` #property.sql table1 table2 >1.table
sqlplus -s `echo $user`/`echo $password`#`echo $host`:`echo $port`/`echo $sid` #property.sql table2 table1 >2.table
..........................................
..........................................
diff -wy 1.table 2.table (here, option 'y' can be ommitted also)
Property.sql
SET ECHO ON
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET WRAP ON
SET VERIFY OFF
SET TAB OFF
SET HEADING ON
SET LINESIZE 4000
select property_id ,endpoint,key,value,value1,value2 from &2 where property_id in (
select table1.property_id from
(SELECT property_id,KEY,value,endpoint FROM &2
MINUS
SELECT property_id,KEY,value,endpoint FROM &1)
table1 left join &1 table2 on table1.property_id=table2.property_id) order by TO_NUMBER(property_id);
exit;
The above is printing me the difference.
I want the corresponding column names also to get printed. How to do that?
I have a select statement inside a bash script that returns the latest date in the DB. I run this query 4 times so I want to define it just once and assing the text to a variable.
#!/bin/bash
linux commands;
database_date=$(sqlplus -s/nolog $USER/$USER#BRMDPP <<END
set pagesize 0 feedback off verify off heading off echo off;
SELECT ...
exit;
END
)
commands that change the database date;
last_date=$(sqlplus -s/nolog $USER/$USER#BRMDPP <<END
set pagesize 0 feedback off verify off heading off echo off;
SELECT ...
exit;
END
)
commands that change the database date;
How can I store this big string $(sqlplus ... into one variable and use it again?
Thank you
One way would be to make use of a function:
foo() {
sqlplus -s/nolog $USER/$USER#BRMDPP <<END
set pagesize 0 feedback off verify off heading off echo off;
SELECT ...
exit;
END
}
and later invoke it by saying:
value=$(foo)
In order to get the value returned by the function, say echo "$value" (note that quoting variables is important).