Spool command breaking header - spool

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.

Related

How to turn STATISTICS TIME ON\OFF using variable in SQL Server?

I need to use A LOT of set statistics time on and set statistics time off so I can check every steps' running time. Is there any way I can usr a variable to replace ON and OFF?
CREATE Procedure CPP_InsertOneRows
(#turnStatistics bit = 0
)
AS
BEGIN
DECLARE #onOrOff CHAR(3);
IF #turnStatistics = 1
BEGIN
SET #onOrOff = 'ON';
END
ELSE
BEGIN
SET #onOrOff = 'OFF';
END
SET STATISTICS TIME OFF -- how can I use #onOrOff to replace the 'OFF' here?
SET STATISTICS IO Off
Am I using it correctly? Or is there other method to do this? Thanks~
You would have to do the actual turning on/off within the IF:
IF #turnStatistics = 1
SET STATISTICS TIME ON;
ELSE
SET STATISTICS TIME OFF;
...
SET STATISTICS TIME OFF;
You cannot use a variable for the value of OFF/ON, it must be a literal.
if you want to check the time taken for each step of your code you can use following mehtod
Print 'Step 1 started'
print CONVERT (TIME, GETDATE())
WAITFOR DELAY '00:00:01'; -- function
Print 'Step 1 ended'
print CONVERT (TIME, GETDATE())
Print 'Step 2 started'
print CONVERT (TIME, GETDATE())
WAITFOR DELAY '00:00:02'; --- function
Print 'Step 2 ended'
print CONVERT (TIME, GETDATE())
make sure you remove this after your testing or uses #trace parameter if this #trace=1 only you will run this by default is set to #trace=0 (with if blocks)

sqlplus output header wrongly formatted and un-necessary blank lines

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.

Pass shell variables to SQL statement

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
)

Print column names along with the difference in output of two tables using shell script

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?

Store SQL query string into variable BASH

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).