Oracle trigger to compare dates - oracle

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.

Related

PL/SQL That runs on 30th of every month

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.

Validating Date Prompt To be less than Current Date OBIEE

I have a requirement to create two validations on the date prompt:
1) The From Date has to be less than To Date
2) The To Date has to be less than or equal to current date
I created a conditional analysis wherein From Date is < To Date, which works, but when I try to create an advanced filter wherein #To_Date <= Current_Date I am getting an error.
Error getting drill information: SELECT date'2016-08-24' saw_0 FROM "Workforce Management - Processed Time Cards Real Time" WHERE(date'#{To_Date}' <= (SELECT VALUEOF("CURRENT_DATE_REP_OTBI") FROM "Workforce Management - Processed Time Cards Real Time" FETCH FIRST 1 ROWS ONLY))
If anyone can help solve this, it'd be really helpful!
Thanks
You need to add a default value when referencing presentation variables in logical SQL queries or formulas. Especially if these are dates.
I created an analysis based on the following LogicalSQL and it worked.
SELECT date'2016-08-26' saw_0 from "subject_area" WHERE (date
#{to_date}{'2016-08-26'} < CURRENT_DATE)
Notice the following:
The presentation variable #{to_date} goes with a default value (noted by the second curly brackets). This helps OBIEE to validate the query. Failing to add the default value will give you the "getting drill information" error.
Instead of a session RPD variable, you can use CURRENT_DATE. It simplifies the query.
The above query will return the date in the SELECT clause, but if the to_date is greater than CURRENT_DATE will return no data.

How to get this data permission configuration on Oracle?

I'm writing an application and I would like to know whether is it possible to get this data permission configuration on the Database side (Oracle) not the application side.
It's basically related to some tables that have to store historical data that can not be edited the day after they were typed...
Table : X
Fields :
WhatEver as varchar2
MyDate as DateTime
For each row, if (Current Date = MyDate), editing is allowed. Otherwise, No.
Is that possible please ? Am I required to use Oracle Label Security ?
I'm looking for something that can be managed as Access Rights (Grant/Revoke...) The usage would be that the administrator can Create, Grant or Revoke such a permission to a user or a role.
Thanks.
A row level trigger should do the trick.
create or replace trigger my_trigger
before update on my_table
for each row
begin
-- Only compare the date part (trunc removes the time)
if trunc(:old.mydate)!=trunc(sysdate)
then raise_application_error(-20000,'Updates only allowed on the day of entry');
end if;
end;
Of course, entries added just before midnight have a very short edit time.
Also, you should somehow disable the possibility to update the mydate field. Some extra lines in the trigger will take care of that.

Oracle - retrieve date having timestamp values

We have a situation, where we have a table (say Forms) in Oracle DB which has a column (say edition_date) of type date. It was strictly meant to hold the date information in YYYY-MM-DD (ex: 2012-11-23)format with no timestamp value.
Unfortunately, due to a code problem, lot of rows got created with timestamp values. There are tons of records in this table, and I want to update only those rows which had this bad data. I can do that using the query
UPDATE forms SET edition_date = TRUNC( edition_date )
I want to add a where clause to it, so it updates only the bad data. The problem, I am not sure how to retrieve those rows that has timestamp added to it. Below is a snapshot of the data I have:
FORM_ID EDITION_DATE
5 2012-11-23
6 2012-11-23 11:00:15
..
11 2010-07-11 15:23:22
..
13 2011-12-31
I want to retrieve only the row with form ids 6 and 11. I trioed using the length functions but I think that is good for Strings only. Is there any way to do this. Thanks anyone who can help me.
A date has no format; you're only seeing how it's displayed. However, the answer to your question is, effectively, what you've said:
I want to add a where clause to it, so it updates only the bad data.
So, where the date is not equal to the date without time:
update forms
set edition_date = trunc(edition_date)
where edition_date <> trunc(edition_date)
To ensure that this doesn't happen again you can add a check constraint to your table:
alter table forms
add constraint chk_forms_edition_date
check ( edition_date = trunc(edition_date) );
I would advise against all of the above. Don't destroy potentially useful data. You should simply select trunc(edition_date) where you do not want time. You may want to use the time in the future.
You're correct, do not use LENGTH() etc for dates, it depends on your NLS_DATE_FORMAT settings and so could be different on a session-by-session basis. Always use date functions when dealing with dates.

how I can get log from database in oracle?

Last day my head judge me because he face with some problem that he didnot has it before!
but I didnt do any thing with database and also our company CM database is so critical !
In this case can he proof what I do with database???
Oracle have any log!
some functaion updating in last 2days that I didnt update it and he also...
please help me,how I can proof who do the mistake.
how I can find who with which user connect to database and update or change some thing...
You can see when database objects were last modified (recompiled/altered/created) this way:
SELECT object_name, last_ddl_time
FROM dba_objects
ORDER BY last_ddl_time DESC;
There are redo logs (which are used to recover from a backup), but they don't record the username, workstation or anything else that identifies who made a data change.

Resources