Pass spool file data to a function - unix

I am fetching a SQL query into a spool file. That spool file has 10 account numbers i need to pass each account number to a function as an input parameter My script looks like this
Spool test.log
Select account_num from account where customer = 1234556;
Spool off
Exit;
Now i will get 10 account in the spool file...i need to pass each account to a function as input parameter as
Function(account_num)
Help me how to achieve this
I haven't tried anything

Related

How can I choose to show only failed result and result must be fail not pass

there are 22 records on both tables. at records 1-21 it's was right but on record 22 on both table can't compare cause it's not equal. I would like to show result only failed record that's can't compare and let result fail.
connect to database using custom params cx_Oracle ${DB_CONNECT_STRING}
#{queryResults1}= Query Select * from QA_USER.SealTest_Security_A Order by SECURITY_ID
Log ${queryResults1}
#{queryResults2}= Query Select * from QA_USER.SealTest_Security_B Order by SECURITY_ID
Log ${queryResults2}
should be equal ${queryResults1} ${queryResults2}
Disconnect From Database

how to save the data of a query in a spool file in oracle

I have created a script which is containing one query and , that query output should print in a spool file.
spool D:\sqlpractice\abcd
it creates the abcd file but that output data is not printing on that file. that output of query is printing on the sqlplus command prompt.
how to save that data in spooled file?
You need to use spool off, at that moment the output gets saved to the spoolfile.
For example:
spool D:\sqlpractice\abcd
SELECT * FROM Dual;
spool off;

Extracting data files for different dates from database table

I am on windows and on Oracle 11.0.2
I have a table TEMP_TRANSACTION consisting of transactions for 6 months or so. Each record has a transaction date and other data with it.
Now I want to do the following:
1. Extract data from the table for each transaction date
2. Create a flat file with a name of the transaction date;
3. Output the data for this transaction date to the flat file;
4. Move on to the next date and then do the steps 1-3 again.
I create a simple sql script to spool the data out for a transaction date and it works. Now I want to put this in a loop or something like that so that it iterates for each transaction date.
I know this is asking for something from scratch but I need pointers on how to proceed.
I have Powershell, Java at hand and no access to Unix.
Please help!
Edit: Removed powershell as my primary goal is to get it out from Oracle (PL/SQL) and if not then explore Powershell OR Java.
-Abhi
I was finally able to achieve what I was looking for. Below are the steps (may be not the most efficient ones, but it did work :) )
Created a SQL script which spools the data I was looking for (for a single day).
set colsep '|'
column spoolname new_val spoolname;
select 'TRANSACTION_' || substr(&1,0,8) ||'.txt' spoolname from dual;
set echo off
set feedback off
set linesize 5000
set pagesize 0
set sqlprompt ''
set trimspool on
set headsep off
set verify off
spool &spoolname
Select
''||local_timestamp|| ''||'|'||Field1|| ''||'|'||field2
from << transaction_table >>
where local_timestamp = &1;
select 'XX|'|| count(1)
from <<source_table>>
where local_timestamp = &1;
spool off
exit
I created a file named content.txt where I populated the local timestamp values (i.e. the transaction date time-stamps as
20141007000000
20140515000000
20140515000000
Finally I used a loop on powershell which picked up one value from content.txt and then called the sql script (from step 1) and passed the parameter:
PS C:\TEMP\data> $content = Get-Content C:\TEMP\content.txt
PS C:\TEMP\data> foreach ($line in $content){sqlplus user/password '#C:\temp\ExtractData.sql' $line}
And that is it!
I still have to refine few things but at least the idea of splitting the data is working :)
Hope this helps others who are looking for similar thing.

LOB storage parameter query

This is for Oracle 11
I created a table by running a script like this:
CREATE TABLE "SYSTEM"."Table12" (
"Column1" CLOB
)
PCTFREE 0
PCTUSED 0
LOGGING
NOCOMPRESS
LOB("Column1") STORE AS (TABLESPACE "TEMP"
STORAGE (INITIAL 65537)
ENABLE STORAGE IN ROW
CHUNK 8193
PCTVERSION 11
CACHE
LOGGING);
Now, I am trying to run queries to obtain the values of storage parameters of Column1.
I started with the tablespace name and I used
SELECT TABLESPACE_NAME FROM ALL_LOBS WHERE COLUMN_NAME = 'Column1'
I was able to get the name successfully. I tried to obtain the value of "INITIAL" which is 65537; however, can't seem to find the right query to get it.
Please kindly advise.
Samir

Oracle DB: Suggestion for Email Trigger

Can someone provide a link or an example of an Oracle DB trigger that sends an email if a specific column in the table gets updated AND updates a different column in the same table?
Example:
A table has multiple issues and there are two columns 'Issue Added' and 'Email Sent' that default to '0'
Issue Added Email Sent
0 0
When the 'Issue Added' column gets updated to '1'
Issue Added Email Sent
1 0
Trigger sends email and updates column 'Email Sent'
Issue Added Email Sent
1 1
It would generally be a bad idea to try to send an email in a trigger.
If the system is unable to send the email (for example, because the SMTP server is temporarily down), the trigger will fail and the triggering statement will fail and be rolled back. It is very rare that you would really want to stop the underlying transaction simply because you weren't able to send an email.
Sending an email is non-transactional. That means that you'll send emails for changes that never get committed. And you'll send emails multiple times because Oracle chooses to rollback and re-execute all or part of an INSERT statement in order to maintain write consistency.
You'll generally be much better served with a database job that periodically looks for rows that need to have an email sent, sends the emails, and then updates the table. You can use either the older DBMS_JOB package or the newer and more sophisticated DBMS_SCHEDULER package. Something along the lines of
CREATE OR REPLACE PROCEDURE process_issues
AS
BEGIN
FOR i IN (SELECT *
FROM your_table_name
WHERE issue_added = 1
AND email_sent = 0)
LOOP
send_email( i.issue_id );
UPDATE your_table_name
SET email_sent = 1
WHERE issue_id = i.issue_id;
END LOOP;
END;
which is then scheduled to run, say, every 5 minutes (you could also use the DBMS_SCHEDULER package)
DECLARE
l_jobno PLS_INTEGER:
BEGIN
dbms_job.submit( l_jobno,
'BEGIN process_issues; END;',
sysdate + interval '5' minute,
'sysdate + interval ''5'' minute' );
commit;
END;
You can use the UTL_MAIL package to implement the send_email procedure. You probably just need to call UTL_MAIL.SEND with appropriate parameters (assuming you've configured your SMTP_OUT_SERVER parameter and your user has been granted appropriate access to the UTL_MAIL package and to an ACL that allows you to communicate with that SMTP server).

Resources