how to insert database column names in CSV file in shell script - bash

I am executing below script against oracle database.
sqlplus -s $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID << FIN_SQL > $pi_cancellation.csv
SET HEADING ON
SET FEEDBACK OFF
SET LINESIZE 32767
SET PAGESIZE 0
SET SERVEROUTPUT OFF
SELECT ROOT_PROC_INS_ID || ',' || PROCESS_INSTANCE || ',' || PROCESS_TEMPLATE || ',' || START_TIME || ',' || STATUS || ',' || ATTRIBUTE2 || ',' || ATTRIBUTE14
FROM BPMUSER.EC_PE_STATUS
WHERE START_TIME<(SYSDATE - 15) AND (STATUS='pi_halted' OR STATUS='pi_failed')
ORDER BY START_TIME DESC;
FIN_SQL
I want to insert column names into CSV file but not as given in sql query above but as shown below. For e.g. Order_Ref should be inserted in CSV file for Attribute2
ROOT_PROCESS_INSTANCE,PROCESS_INSTANCE,PROCESS_NAME,START_DATE,STATUS,ORDER_REF,LINE_NUMBER
pvm:0a12dd82,pvm:0a12dd84,TechnicalErrorProcess,21-JUN-19 07.01.58.560000 AM,pi_halted,38930,1
pvm:0a12dd77,pvm:0a12dd79,TechnicalErrorProcess,20-JUN-19 12.36.27.384000 PM,pi_halted,1572846,1
pvm:0a12dd6t,pvm:0a12dd6v,TechnicalErrorProcess,20-JUN-19 12.05.22.145000 PM,pi_halted,38929,1
pvm:0a12dd4h,pvm:0a12dd4l,TechnicalErrorProcess,17-JUN-19 07.11.43.522000 AM,pi_halted,9973686,1

Echo the header row before running SQL*Plus, and turn its heading off.
echo 'ROOT_PROCESS_INSTANCE,PROCESS_INSTANCE,PROCESS_NAME,START_DATE,STATUS,ORDER_REF,LINE_NUMBER' > $pi_cancellation.csv
sqlplus -s $DBUSER/$DBPASS#$DBHOST:$DBPORT/$DBSID << FIN_SQL >> $pi_cancellation.csv
SET HEADING OFF
SET FEEDBACK OFF
SET LINESIZE 32767
SET PAGESIZE 0
SET SERVEROUTPUT OFF
SELECT ROOT_PROC_INS_ID || ',' || PROCESS_INSTANCE || ',' || PROCESS_TEMPLATE || ',' || START_TIME || ',' || STATUS || ',' || ATTRIBUTE2 || ',' || ATTRIBUTE14
FROM BPMUSER.EC_PE_STATUS
WHERE START_TIME<(SYSDATE - 15) AND (STATUS='pi_halted' OR STATUS='pi_failed')
ORDER BY START_TIME DESC;
FIN_SQL

Related

SQL query returning rows while executing it on SQL dedveloper but is not working in the PLSQL procedure

This is the procedure
PROCEDURE NON_BLOCKING_STATUS
IS
cursor badcur is
select query;
BEGIN
dbms_output.put_line('sch_id sch_revision SCD_SEQUENCE SCH_STATUS scD_status scd_action scd_object SCD_NAME scl_text');
for badrow in badcur LOOP
dbms_output.put_line ( badrow.sch_id || ' ' || badrow.sch_revision || ' ' || badrow.SCD_SEQUENCE || ' ' || badrow.SCH_STATUS || ' ' || badrow.scD_status || ' ' || badrow.scd_action || ' ' || badrow.scd_object || ' ' || badrow.SCD_NAME || ' ' || badrow.scl_text);
end loop;
end;
The select sql query is returning rows while running it on SQL developer but while using it in the above procedure, its not working.
Thanks in Advance.

OBIEE Prompt and particular SQL Result

reportDimMCP and reportValMCP are two different presentation variable for two prompt. i want to represent using a string the output of a select case.
i try this :
SELECT CASE '#{reportDimMCP}{Utilizzato}' WHEN 'Numero' THEN '( case '#{reportValMCP} {NULL}' ' || when ||' ' Utilizzo > 0' '|| then || 'ho scelto Numero con Utilizzo > 0' || when ||' ' Utilizzo = 0' ' || then 'ho scelto Numero con Utilizzo = 0' || end || ')'
FROM "Myself"
but doesn't work
Any idea?
Is the problem that your second case is incorrectly inside quotes? i.e. should it look more like this:
case '{pvTest1}{A}'
when 'A' then
case '{pvTest2}{X}'
when 'X' then 'Foo'
else 'Bar'
end
else 'wibble'
end

replace only exact words with a list of words from a table

this question is a part of this one
limit records in cursor by using a variable
the original question was a 2 part question, 1 part being, IS THERE A BETTER WAY TO DO THIS
i have code that works but it's EXTREMELY slow, about 2 seconds for each record.
so i have a table, NR_POSTAL_ABBR, that has 2 fields and about 400 records
ReplaceWhat ReplaceWith
Ave Avenue
St Street
i want to be able to replace the Address field in another table with the valules from the table above
so if i have an address 123 Main St - it should say 123 Main Street
if i have 123 Main Street - it should stay 123 Main Street, it shouldn't become 123 Main Streetreet
the table with addresses has a few million rows, is there a fast way of doing this?
thank you
So as you asked. My suggestion is go through regular expressions So here is un update code for your case.
Suppose you have a table test with the field address, what you have to do is very simple, create an regular expression that match your requirements.
So: You need to replace every St for Street and Av for Avenue this is what you should do.
update test
set address =
regexp_replace(
regexp_replace(address, 'Av | Av$| Av ', ' Avenue ' ),
'St | St$| St ', ' Street ');
Here is a SQLFiddle example
Explaining the regex:
The regexp_replace replaces a string for another based on a regular expression pattern, see the docs: REGEXP_REPLACE
About the regular expressions you can see it here Regular Expressions Wiki Almost every language follow the POSIX pattern, so once you learn it you will be good.
So I've used to regexp_replace to achieve what you want because you gave two requirements. I will not write about the parameters of the function just the expressions.
At the first expression you have 'Av | Av$| Av ' which means:
PS.: I put the - just to you see the space (so ignore it). SO wont let me put it.
-Av - (with a space at end) = Find every Av with a space after it on the string
-|- (pipe sign) = Equals an or statement
- Av$- = Find all Av with a space before it and when Av is at the end of the string. $
- Av - = Find all Av with a space before and a space after
Then The function replace any of theese ocurrences with the word Avenue. Notice that I put a space before and one after to avoid something like Peer HarborAvenue
The same explanation goes to St string
If you like the regular expressions function you can see more with it here: Oracle Regular Expressions Functions
just posting the automated version based on Jorge's reply, in case someone needs it.
DECLARE
ReplOrder NUMBER;
BEGIN
ReplOrder := 1;
DECLARE
CURSOR getReplsStrng IS
SELECT replacewhat
,replacewith
FROM analyst.NR_POSTAL_ABBR
WHERE ReplaceOrder = ReplOrder;
BEGIN
FOR getInnerRec IN getReplsStrng LOOP
-- DBMS_OUTPUT.put_line('replace what ' || getInnerRec.replacewhat);
-- DBMS_OUTPUT.put_line('Replace Order ' || ReplOrder);
UPDATE NR_TMP_106 tmp
SET NewAddress = LOWER(REGEXP_REPLACE(LOWER(NewAddress)
, '$'
|| getInnerRec.replacewhat
|| ' | '
|| getInnerRec.replacewhat
|| '$| '
|| getInnerRec.replacewhat
|| ' '
,' ' || getInnerRec.replacewith || ' '))
WHERE 1 = 1
AND ( tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || NULL || '%'
OR tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || CHR(32) || '%'
OR tmp.NewAddress LIKE '%' || NULL || '' || getInnerRec.replacewhat || '' || CHR(32) || '%');
COMMIT;
END LOOP;
END;
ReplOrder := 2;
DECLARE
CURSOR getReplsStrng IS
SELECT replacewhat
,replacewith
FROM analyst.NR_POSTAL_ABBR
WHERE ReplaceOrder = ReplOrder;
BEGIN
FOR getInnerRec IN getReplsStrng LOOP
-- DBMS_OUTPUT.put_line('replace what ' || getInnerRec.replacewhat);
-- DBMS_OUTPUT.put_line('Replace Order ' || ReplOrder);
UPDATE NR_TMP_106 tmp
SET NewAddress = LOWER(REGEXP_REPLACE(LOWER(NewAddress)
, '$'
|| getInnerRec.replacewhat
|| ' | '
|| getInnerRec.replacewhat
|| '$| '
|| getInnerRec.replacewhat
|| ' '
,' ' || getInnerRec.replacewith || ' '))
WHERE 1 = 1
AND ( tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || NULL || '%'
OR tmp.NewAddress LIKE '%' || CHR(32) || '' || getInnerRec.replacewhat || '' || CHR(32) || '%'
OR tmp.NewAddress LIKE '%' || NULL || '' || getInnerRec.replacewhat || '' || CHR(32) || '%');
COMMIT;
END LOOP;
END;
END;

How to remove blank spaces from SQLPLUS?

I have a file with this:
set linesize 1000
set trimspool on
set trimout on
set pagesize 0
set feedback off
spool result.csv
SELECT process_id || ';' || file_name || ';' || source || ';' || destination || ';' || type || ';' || transfer_name || ';' || message || ';' || message2 || ';' || destination_sub_group
FROM table
WHERE process_id = '12345';
And SQLPLUS is calling it
But this is returning blank spaces, specially message2 field, any idea on how to remove it?
Here is the output:
12345;filename.txt;X;X;4;X;xx = xxxx
Warning: Using insecure memory!
Decoding data....
Secret key is required to read it.
Key for user ID "X"
Error decrypting file '/apps/egs/gen/file_name.txt.pgp'.
;INBOUND
Thanks!
I replaced some values with X.
Here is the output I would like:
12345;filename.txt;X;X;4;X;xx = xxxx Warning: Using insecure memory! Decoding data.... Secret key is required to read it. Key for user ID "X" Error decrypting file /apps/egs/gen/file_name.txt.pgp'.;INBOUND
i fought this problem for days, when i wanted to get the query results into a csv...
set mark csv ON ended up solving the problem in that case... took my for ever to find that command
Try using TRIM to remove trailing and leading spaces and REPLACE to remove linefeeds and carriage returns:
SELECT process_id || ';' || file_name || ';' || source || ';' || destination || ';' || type || ';' || transfer_name || ';' || message || ';' ||
replace(replace(trim(message2),CHR(10),' '),CHR(13),' ') || ';' || destination_sub_group
FROM table WHERE process_id = '12345';
Add the following:
COL the_stuff form a1000
Then add the "the_stuff" column alias to your query:
SELECT process_id .... destination_sub_group the_stuff from ..
This will explicitly allow you some control over how this output is displayed.
Next, to deal with the embedded linefeeds in your output, wrap lot around a TRANSLATE ( ..., CHR(10), '+' )
E.g.
This show data with linefeeds:
SQL> select 'line 1' || chr(10) || 'line2' || chr(10) || 'line 3' txt from dual;
TXT
-------------------
line 1
line2
line 3
Translate to replace the linefeeds with "+":
SQL> select translate ( 'line 1' || chr(10) || 'line2' || chr(10) || 'line 3',
chr(10), '+' ) txt
from dual;
TXT
-------------------
line 1+line2+line 3
SQL>

Create Tablespace Script in Oracle 10g

I am using the below script for generating a DDL to create tablespaces in the database.
select 'create tablespace ' || df.tablespace_name || chr(10)
|| ' datafile ''' || df.file_name || ''' size ' || df.bytes
|| decode(autoextensible,'N',null, chr(10) || ' autoextend on maxsize '
|| maxbytes)
|| chr(10)
|| 'default storage ( initial ' || initial_extent
|| decode (next_extent, null, null, ' next ' || next_extent )
|| ' minextents ' || min_extents
|| ' maxextents ' || decode(max_extents,'2147483645','unlimited',max_extents)
|| ') ;' "Script To Recreate Tablespaces"
from dba_data_files df, dba_tablespaces t
where df.tablespace_name=t.tablespace_name;
It works good. But when a tablespace contains two datafiles then also it creates seperate command with create tablespace. Simply it creates two create tablespace commands if a tablespace contains two datafiles. Please share your thoughts.
Cheers,
Srinivasan Thirunavukkarasu.
If you're just trying to reverse-engineer an existing tablespace to generate a script, why not just use DBMS_METADATA?
select dbms_metadata.get_ddl('TABLESPACE','yourTablespaceNameOfInterest')
from dual;
You can generate one of these statements for each tablespace in the database with a simple wrapper if you want them all.
SET LONG 1000000
select dbms_metadata.get_ddl('TABLESPACE','tablespace_name')||';' from dual;
select
dbms_metadata.get_ddl('TABLESPACE',tablespace_name)
from
dba_tablespaces
;

Resources