PL/SQL That runs on 30th of every month - oracle

I am working on a Data Base for a library type project.
I have These tables from which I need data: Books, Reports, Articles.
Using oracle btw.
I need something (trigger or procedure or something else) that runs on the 30th of every month (or the last day), it will then analyze the date of creation of every file in those 3 tables (Books, Reports, Articles) and if the date is older than sysdate - 5 years, then a message should appear with the details of that file (name,date,author).

You have mentioned that message should appear with the details of that file (name,date,author) I am not sure where this should appear.
Still I will give an approach for this problem, you can write a procedure/function in oracle which would write such records(name,date,author) to a log table from which you can see all the details, you can also add a create date, timestamp field to the table (if you want to pull out records based on date). You can use DBMS_SCHEDULER to run the procedure/function every last day of the month. Hope this approach helps.

Related

Oracle flashback data archive get last x records

We're using Oracle FDA for auditing, and I am trying to retrieve the last x records that a user had so I can compare them and see what change with each transaction. I've been googling around for quite some time now, and I can't quite figure out how to do that with an FDA query. I know how to retrieve a particular row as of a particular date and time, like:
select * from user
AS OF TIMESTAMP
TO_TIMESTAMP ('12302017:17:52:00','MMDDYYYY:HH24:MI:SS')
where guid = hextoraw('0A96318C1E0E45ADB5EAE1C94EA8F7B8');
But how would I be able to leverage this to get me a list of all the transaction that user has had so I can get, say, the last 10?

Create PL/SQL WatchDog/Daemon in Oracle 11gr2

I should create a watchdog that monitor a field in a table every 5 minutes, in Oracle DB. If field has a specific value (a date older than x) an action should be performed.
Is it possible to do it in PL/SQL?
If it's not, I should create a script shell and call it from crontab, or maybe use the Scheduler.
If field has a specific value (a date older than x) an action should be performed.
You could create a TRIGGER. If a new row is inserted such that the date column has a specific value, then you perform some action in the trigger.
Perhaps, you need an AFTER INSERT TRIGGER FOR EACH ROW, since you need to reference the :NEW values.
Here is the link to documentation regarding CREATE TRIGGER.
If you really want to do it as a scheduled job, then you could use DBMS_SCHEDULER. Prior to 10g releases, it was DBMS_JOB.
What you need is to use the DBMS_SCHEDULER package
The DBMS_SCHEDULER package provides a collection of scheduling functions and procedures that are callable from any PL/SQL program. (...)
The Scheduler uses a rich calendaring syntax to enable you to define repeating schedules, such as "every Tuesday and Friday at 4:00 p.m." or "the second Wednesday of every month."
Sorry, maybe I've omitted a detail the field to monitor is for a specific row. I mean if the result of the query:
select up_date from mytab where name_id='test'
is older than 15minutes
then
update mytab set value_col='no' where name_id='test'
But I don't know if it's possible to do it with a trigger. With SCHEDULER I could check at interval time and could be a good work-around.
You may still have a AFTER UPDATE trigger and in that trigger check if the updated row is the one you want to watch. Something like this:
IF :old.name_id = 'test' THEN
-- do something here
END IF;

Oracle: Monitoring changes in v_$parameter

Long time user, first time "asker".
I am attempt to construct an Oracle procedure and/or trigger that will compare two tables with the MINUS operation and then insert any resulting rows into another table. I understand how to do the query in standard SQL, but I am having trouble coming up with an efficient way to do this using PL/SQL.
Admittedly, I am very new to Oracle and pretty green with SQL in general. This may be a silly way to go about accomplishing my goal, so allow me to explain what I am attempting to do.
I need to create some sort of alert that will be triggered when the V_$PARAMETER view is changed. Apparently triggers can not respond to changes to view but, instead, can only replace actions on views...which I do not wish to do. So, what I did was create a table that to mirror that view to essentially save it as a "snapshot".
create table mirror_v_$parameter as select * from v_$parameter;
Then, I attempted to make a procedure that would minus these two so that, whenever a change is made to v_$parameter, it will return the difference between the snapshot, mirror_v_$parameter. I trying to create a cursor with the command:
select * from v_$parameter minus select * from mirror_v_$parameter;
to be used inside a procedure, so that it could be used to fetch any returned rows and insert them into another table called alerts_v_$parameter. The intent being that, when something is added to the "alert" table, a trigger can be used to somehow (haven't gotten this far yet) notify my team that there has been a change to the v_$parameter table, and that they can refer to alerts_v_$parameter to see what has been change. I would use some kind of script to run this procedure at a regular interval. And maybe, some day down the line when I understand all this better, manipulate what goes into the alerts_v_$parameter table so that it provides better information such as specifically what column was changed, what was its previous value, etc.
Any advice or pointers?
Thank you for taking the time to read this. Any thoughts will be very appreciated.
I would create a table based on the exact structure of v_$parameter with an additional timestamp column for "last_update", and periodically (via DBMS_Scheduler) merge into it any changes from the real v_$parameter table and capture the timestamp of any detected change.
You might also populate a history table at the same time, either using triggers on update of your table or with SQL.
PL/SQL is unlikely to be required, except as a procedural wrapper to the SQL code.
Examples of Merge are in the documentation here: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9016.htm#SQLRF01606

Oracle trigger to compare dates

So I am making an oracle database for a university coursework. My coursework is to make a database for an airlines.
The problem I am having is that I have to make sure that the passport expiry date on a new passenger record is not less than the current date (that is, make sure that the person's passport is still valid).
could someone please help me out with creating a trigger to do this? I have the entity PASSPORT_EXPIRY in table PASSPORT set to the datatype DATE.
cheers
i am assuming you can create the trigger..(?)
you will be comparing to SYSDATE - the built in holder for today (right now).
inside a trigger - you are allowed to write PL SQL. so you can have IF checks.
your check will be something like
IF SYSDATE > PASSPORT_EXPIRY THEN
or you can probably write this into a select statement where you query maybe the days between now and expiry..
SELECT SYSDATE - PASSPORT_EXPIRY INTO diff
from PASSPORT WHERE ...
then check if diff < 0.. etc.

how to execute procedure with timer in oracle

I need to clear all records in a table if the data exist for one hour.
to know the start time, I have a column "StartTime" with "date" data type.
I think I need a timer to do this,
how can I do this in oracle ?
Depending on what your exact requirements are, I would probably look at using a view to only show the valid rows when queried. This would make it seem like only the last one hour of records were available. This would also mean that you don't need to remove rows exactly an hour after they are created.
Then to remove the rows, I would look at using DBMS_JOB or DBMS_SCHEDULER to remove the rows as has been suggested in some of the other answers.
Remember that just because your requirement is to clear the rows from the table after an hour, you probably really only need to remove the ability to query on them, which you could do with a view.
For what version of Oracle?
For version v7.3.4 to 9i, use DBMS_JOB to schedule a task. 10g+, you want to use DBMS_SCHEDULER. It's not clear to me how often you want/need this to run...
You can create scheduled Jobs in Oracle 10G and above using the DBMS_SCHEDULER
If you are really fastidious, you can schedule this job - which calls your procedure - to run every 1 minute so that the data is cleared out as soon as the 60th minute expires.
Refer this link for an example of how to setup / schedule a job via scripts in Oracle 10G
The requirements are not Quite clear.
Is this a job/program that you have to run once and will delete records that existed for more than an hour? If that is the case, you can use..
delete from <table_name>
where StartTime < (sysdate-1/24);
commit;
If you need to purge records constantly, you'll need to schedule this as a job . The frequency will depend on how often you want the records to be deleted.
What is the business case you are trying to solve?

Resources