Create a trigger to check inserted date is before or after SYSDATE

Hi,
I am trying to create a trigger that will check an inserted date against SYSDATE and alter the value (i.e. make it SYSDATE) when the entered date is incorrect.
For example, I have a Customer table with a record named MemberDate which, when a date is entered to it, will be checked by the trigger to ensure it is not before the current date.
My code so far;
CREATE OR REPLACE TRIGGER Customer
BEFORE INSERT ON
Customer
FOR EACH ROW
DECLARE
e_invaliddate_ud EXCEPTION;
BEGIN
IF :NEW.MemberDate < SYSDATE THEN
RAISE e_invaliddate_ud;
END IF;
EXCEPTION
WHEN e_invaliddate_ud THEN
DBMS_OUTPUT.PUT_LINE('The date entered for MemberDate is invalid. The
MemberDate has been set to the current date');
END;
So far, this trigger only returns an error message but allows the date to be entered even if it is incorrect. I have tried a few ways of altering the entered date to SYSDATE but to no avail.
I am running scripts using SQL PLUS and inserting data using
INSERT INTO Customer
VALUES(CustomerID, ..., MemberDate);
Where the values entered will be checked to ensure the MemberDate is not before SYSDATE.
The MemberDate data-type is DATE and has a rule that it must not be before the current date. I am aware that a constraint may not be used to perform a check using SYSDATE, this is why I am trying to make a trigger.
However, my knowledge of triggers is limited
If anyone can help, I will be very Happy.
Thank you in advance.

Use this if you want an error message that halts the execution:
create or replace trigger customer
before insert on customer
for each row
begin
  if :new.memberdate < sysdate
  then
    raise_application_error(-20000,'The date entered for MemberDate is invalid.');
  end if;
end;Use this if you want to automatically to overwrite the MemberDate with sysdate without an error message:
create or replace trigger customer
before insert on customer
for each row
begin
  :new.memberdate := greatest(sysdate,:new.memberdate);
end;Note: untested
Regards,
Rob.

Similar Messages

  • How do you create a short cut to insert date in Numbers 3.1

    Hi,
    Does any one know how to create a keyboard shortcut for inserting the date in Numbers 3.1

    The install-by-doubleclicking Automator Service (where it becomes a menu pick if you use it often and find that more convenient than a formula in a spreadhseet) is here (Dropbox download). Depending on your settings you may need to go to System Preferences > Privacy & Security and click the 'Open Anyway' button. If you want you can assign it a keyboard shortcut in System Preference > Keyboard > Shortcuts.  If you don't like it or find you the formula is just as easy for you, then in Finder hold down the option key, choose Go in the menu and navigate to Library > Services, where you can trash it the way you would any file or package.
    SG

  • Does The Create Table Privilege allow to insert data ?

    Hi,
    I wish to know if it's possible that a user UserA allows another user UserB to create a table but does not allow him (does not allow UserB ) to insert data in a table UserB has created .If it's possible, I would want to know the commands to do that.
    Thanks .

    sys@LINUX>create user user1 identified by user1;
    User created.
    sys@LINUX>grant connect, resource to user1;
    Grant succeeded.
    sys@LINUX>create user user2 identified by user2;
    User created.
    sys@LINUX>grant connect, resource to user2;
    Grant succeeded.
    sys@LINUX>conn user1/user1
    Connected.
    user1@LINUX>create table t (id number);
    Table created.
    user1@LINUX>disc
    Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.5.0 - Production
    With the Partitioning, OLAP and Data Mining options
    user1@LINUX>conn / as sysdba
    Connected.
    sys@LINUX>grant create any table to user2;
    Grant succeeded.
    sys@LINUX>conn user2/user2
    Connected.
    user2@LINUX>create table user1.tab (id number);
    Table created.
    user2@LINUX>insert into user1.tab values(1);
    insert into user1.tab values(1)
    ERROR at line 1:
    ORA-01031: insufficient privileges
    user2@LINUX>select * from user1.tab;
    select * from user1.tab
    ERROR at line 1:
    ORA-01031: insufficient privileges
    user2@LINUX>delete from user1.tab;
    delete from user1.tab
    ERROR at line 1:
    ORA-01031: insufficient privileges
    user2@LINUX>update user1.tab set id=1;
    update user1.tab set id=1
    ERROR at line 1:
    ORA-01031: insufficient privilegesInteresting issue. Although user2 was able to create a table in user1 schema (because he has "create any table" privilege), he's NOT able to run ANY DML on that table (NO insert, update, delete and select)!
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com
    [Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/]

  • Data Loading(Before and After Image)

    I heard that a datasource which has both "Before and After Image " ,then the data can be sent directly to the infocube or from the DSO to the Infocube but where as
    If a datasource supports after image then first it has to be loaded to the DSO and then to the Infocube ,
    My question is how to know the image types of the datasource ?

    Hi Ravi,
    Check in ROOSOURCE tables in ECC. You can find the behvariaour options in DELTA field, so based on this table we can say will it support Cube/ODS.
        Delta Only Via Full Upload (ODS or InfoPackage Selection)
    A    ALE Update Pointer (Master Data)
    ABR    Complete Delta with Deletion Flag Via Delta Queue(Cube-Comp)
    ABR1    Like Method 'ABR' But Serialization Only by Requests
    ADD    Additive Extraction Via Extracto (e.g. LIS Info Structures)
    ADDD    Like 'ADD' But Via Delta Queue (Cube-Compatible)
    AIE    After-Images Via Extractor (FI-GL/AP/AR)
    AIED    After-Images with Deletion Flag Via Extractor (FI-GL/AP/AR)
    AIM    After-Images Via Delta Queue (e.g. FI-AP/AR)
    AIMD    After-Images with Deletion Flag Via Delta Queue (e.g. BtB)
    CUBE    InfoCube Extraction
    D    Unspecific Delta Via Delta Queue (Not ODS-Compatible)
    E    Unspecific Delta Via Extractor (Not ODS-Compatible)
    FIL0    Delta Via File Import with After-Images
    FIL1    Delta Via File Import with Delta Images
    NEWD    Only New Records (Inserts) Via Delta Queue (Cube-Compatible)
    NEWE    Only New Records (Inserts) Via Extractor (Cube-Compatible)
    O  
    ODS    ODS Extraction
    X    Delta Unspecified (Do Not Use!)

  • HOW TO CREATE A TRIGGER TO UPDATE A DATE FIELD WHEN RECORD IS MODIFIED

    Hi,
    I have a field (column)
    SalesLeadLastModifiedDate
    in the table
    SalesPipeline
    A record's field (SalesLEadLastModifiedDate) should be updated with current date and time every time any column in a record is modified. I'm new to SQL and can't figure out how to write this trigger. Could someone help me and also
    refer literature that would give me independence? Online MSDN language without matching examples is not helping.
    Tnx.,
    IJ

    Break it down into pieces.  Write a small script that updates some small (but more than 1) number of rows.  After updating the rows, write another update statement that will set your modified date column to the current date and time.  Once
    you have that working, you can easily convert that into a trigger.  Pseudo-code would be:
    begin tran;
    select * from dbo.SalesPipeline where SalesPipelineID in (45, 76);
    update dbo.SalesPipeline set <some column> = <something> where SalesPipelineID in (45, 76);
    update dbo.SalesPipeline set <your date colum> = ?? where SalesPipelineID in (45, 76);
    select * from dbo.SalesPipeline where SalesPipelineID in (45, 76);
    rollback tran;
    Notice the transaction control statements.  This will allow you to run the script, evaluate the result, and then revert all changes (leaving your database unchanged).  In turn, this will allow you to run the same script over and over and over again
    and achieve (for the most part) the same results with each execution.  You can search the forums for examples of trigger code - just beware that many "solutions" are not necessarily well-written.

  • How to check  the date with current day after 5 days

    Hello,
    I need to make the visible of delete button in my struts application. I've application in which user can be deactivated. If the user is deactivated and the admin can't delete the user for the specific period of time (set in property file). while deactivating the user, system time is set. and 5 days after the system time's set, i need to make the button visible.
    This is my code.
    Calendar cal = Calendar.getInstance();
    Date deActivatedDate = null;
    deActivatedDate =new Date(userData.getDeActivatedTime().getTime());// Set it in the Calendar object
    cal.setTime(deActivatedDate);                    // Add 5 days
    cal.add(Calendar.DATE, 5);// get the current date
    Calendar currentCal = Calendar.getInstance();
    Date currentDate = new Date();
    currentCal.setTime(currentDate);// check if the de-activation time is over.
    if (cal.get(Calendar.DATE) < currentCal.get(Calendar.DATE)) {
    userData.setDeletionAllowed(true);}
    can anyone help to solve this issue?

    What's the problem with the code that you posted?

  • Javascritpt to check if data exists before entering

    I have a form that users fill out..
    I want the first (user_ID)field to kick off a javascript that:
    1. makes it upper case while typeing (this I have working):
    onKeyUp="javascript:this.value=this.value.toUpperCase();"
    2. runs a select statement to see if it exists in the table, if not goto next select, if it does exist goto page 2 which pulls it in for editing..
    3. runs a select that errors if its not a valid ID..
    I don't want the field on a page by itself that is submitted first.. (that would obviously be the easiest solution)
    I've tried creating a hidden button, and submitting that button with javascript doSubmit(HIDDEN), but I can't seen to get the onKeyUp and onChange to work at the same time
    and I don't want to query the database with every keystroke..

    was able to get it to work with onBlur (dont know why onChange wouldn't work)
    onKeyUp="javascript:this.value=this.value.toUpperCase()" onBlur="javascript:doSubmit('HIDDEN');"

  • How can i insert data into DB from my page programatically in Oracle ADF..?

    Hai, this is praveen.
    I have created  an EO and VO, when i have inserted data by dragging and dropping from DataControl -->Operations-->Create. I have successfully inserting data. But how can i do it programatically. What are the pre-defined steps that i can use over there to insert data into table programatically. Could u plz help me?

    Hi,
    You have to create an action Listener in the bean for any button.
    Then call an AM method.
    In that you have to do the following
    ViewObject yourVO = getYourVO();
    Row r = yourVo.createRow();
    r.setAttribute("Column1", value1); //the name of column should be as it is in your vo attribute.
    yourVO.insertRow(r);
    this.getDbTransaction().commit();
    Thanks

  • OIM11g: Notification Event, before and after data elements

    Hello
    I have several notification events currently working correctly. I would like to, however, create a new notification event that contains the before and after value of a changed attribute.
    For example: if user email address changes, notification should be similar to:
    This is a notification that your email address has been successfully updated in the system. Old value: [email protected] New value: [email protected]
    Notifications seem to only be fired as post process event handlers, process tasks, and scheduled tasks, is it possible to have both the before and after datasets available to the notification in any of these tasks?
    Thank you.

    If it's on a user object field, you can check for if the attribute you are watching is contained in the orchestration.getParameters(). If it is found, you can pull the old value out of the following:
    HashMap eventDataHashMap = orchestration.getInterEventData();
    Identity currentUserState = (Identity) eventDataHashMap.get("CURRENT_USER");
    Then you can generate the email/notification however you'd like.
    -Kevin

  • Create Table Trigger to replicate data from MSSQL2K5 to Oracle 11G on Linux

    I am trying to create a trigger on my MSSQL 2k5 server so that when a record is inserted, a replicated record is created in a table on an Oracle 11g database on a Linux server (Oracle Linux 6).
    Creating the trigger is easy, but when I test it I am getting an error stating the following:
    .NetSqlClient Data Provider The operation could not be performed because OLE DB Provider 'OraOLEDB.Oracle' for linked server "<myserver>" was unable to begin the distributed transaction.
    OLEDB Provider "OraOLEDB.Oracle" for linked server "<myserver>" returned: "New transaction cannot enlist in the specified transaction coordinator"
    Here is the trigger (MSSQL):
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE insert_aban8_state
        @an8 int,
        @st nvarchar(3)
    AS
    BEGIN
        SET NOCOUNT ON;
        declare @c numeric
        select @c = count(*) from [e9db]..[CRPDTA].[ABAN8_STATE$] where alan8=@an8 and aladds=@st
        if(@c =0)
         begin
            insert into [e9db]..[CRPDTA].[ABAN8_STATE$]
            values(@an8, @st)
         end
        END
    GO
    After reviewing the MS Transaction Coordinator, I am now totally confused. I checked the services and have the MS DTC enabled and running, but am not sure what to do on the Linux side.
    Does the Oracle Services for Microsoft Transaction Server (OraMTS) work on Linux? I could only find references for this for Oracle 11g on Windows.
    What do I need to do to enable this replication via mssql table trigger to Oracle11g on Linux?

    nsidev wrote:
    While I would agree in part, it appears from the message that the trigger is requiring the Transaction Service to be enabled on both the host and target. The point of this post is to determine what, if anything, I need to do on my Oracle DB to allow the trigger to complete successfully.
    There are many posts found with Google concerning the OraMTS service on the Oracle system, but they all appear to be for Windows based systems. My question is, is this service part of the Linux based Oracle DB and if so, how do I initialize it?
    If I am mistaken and this is truly an issue with the MSSQL server, I will replicate the post in those forums. I am just looking for direction and help.
    1) I have NEVER heard that Oracle has, knows about, or supports any "Transaction Service".
    2) Consider what I previously posted regarding the flavor of client source.
    If your assertion about this mythical service were correct, then the Oracle DB would have to be able to "know" that this client connection was originated by SQL Server.
    I don't understand how or why Oracle should behave differently depending upon whether INSERT is done inside or outside a MS SQL Server trigger.
    Please explain & elaborate why Oracle should behave different depending upon the source of any INSERT statement.
    3) From Oracle DB standpoint an INSERT is an INSERT; regardless of the client.

  • Creating a trigger to enforce data validation

    I would like to create a trigger to do some basic data validation. For example, to join an honors society a student must have a gpa between 3.7 and 4.0. I want this to fire before an insert on the table NatlHonSoc, and the field name is gpa.
    Thanks!

    user8775995 wrote:
    I'm trying to learn how to use triggers and just made this example up. It always helps to state your real intent from the beginning.
    All I have is the general syntax from a book <snip>"A" book? Which book? Just any old book?
    There is no book better to start with than the official documentation.
    Learning where to look things up in the documentation is time well spent investing in your career. To that end, you should drop everything else you are doing and do the following:
    Go to tahiti.oracle.com. Drill down to your product and version.
    Spend a few minutes just getting familiar with what is available here. Take special note of the "books" and "search" tabs. Under the "books" tab you will find the complete documentation library.
    Spend a few minutes just getting familiar with what kind of documentation is available there by simply browsing the titles under the "Books" tab.
    Open the Reference Manual and spend a few minutes looking through the table of contents to get familiar with what kind of information is available there.
    Do the same with the SQL Reference Manual.
    Do the same with the Utilities manual.
    You don't have to read the above in depth. They are reference manuals. Just get familiar with what is there to be referenced. Ninety percent of the questions asked on this forum can be answered in less than 5 minutes by simply searching one of the above manuals.
    As pointed out by Centinul, for your purpose, you should include the Application Developer's Guide as part of your "must reference" books.
    Then set yourself a plan to dig deeper.
    - Read a chapter a day from the Concepts Manual.
    - Look in your alert log and find all the non-default initialization parms listed at instance startup. Then read up on each one of them in the Reference Manual.
    - Take a look at your listener.ora, tnsnames.ora, and sqlnet.ora files. Go to the Network Administrators manual and read up on everything you see in those files.
    - When you have finished reading the Concepts Manual, do it again.
    Give a man a fish and he eats for a day. Teach a man to fish and he eats for a lifetime.

  • Insert data into msql thru table trigger

    Hi,
    I installed dg4msql 11.2 and I would like to insert some data into our msql thru one of our Oracle table after insert table,
    I used the syntax below
    insert into test@dg4msql ("PN") values (:new.PN)
    but the below error problem if I insert data into my Oracle table,
    ORA-02054: transaction 4.17.15898 in-doubt
    ORA-028500, connect from ORACLE to a non-Oracle system returned this message
    [Oracle][ODBC SQL Server Driver][SQL Server] Invalid object name "RECOVER.HS_TRANSACTION_LOG
    Do I have to create the HS_TRANSACTION_LOG table in my msql server to resolve this problem?
    Thanks
    Vincent

    Vincent,
    If the gateway insert is being done as part of a trigger then it will almost certainly be part of a distributed transaction so yes, you will have to create the transaction log table and the recovery user in the SQL*Server database.
    As well as the gateway documentation have a look at this note in My Oracle Support -
    Note.227011.1 How to Setup DG4MSQL to Use Distributed Transactions          (Doc ID 227011.1)
    Regards,
    Mike

  • Trigger that checks if value exists in another table before insert

    Hi,
    Can any one help me with the correct code.
    I am trying to write a trigger that checks if the new value (:new.T1) does not exist in another table (T2) before entering it (in T1).
    Create or replace trigger T1
    BEFORE
    insert on T1
    for each row
    Declare
    x Number;
    begin
    SELECT Count (*) INTO x
    From T2 WHERE T2.Col1 = :new.T1;
    IF (x > 1 or x = 1) Then
    DBMS_OUTPUT.PUT_LINE('This value has been used before try another one.');
    END IF;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    INSERT INTO T2 (Col1) values (:new.T1);
    WHEN TOO_MANY_ROWS THEN
    DBMS_OUTPUT.PUT_LINE('This value has been used before try another one.');
    end;

    I think this question is suitable for the SQL and PL/SQL forum.

  • Which trigger to use for insert data into db table in Forms

    Hi,
    My form is current having a database block with table reference. When enter data into form field and click on save button. Automatically the record is inserted into database table.
    I want to make this as manual insert. I changed the data block to a non-database. Where should i write the insert statement in order to insert data into table.
    Is it Key-commit trigger at form level?
    Please advise.
    Thanks,
    Yuvaraaj.

    Hi Yuvaraaj.
    Insert should happen when we click on the save which is inbuilt in the form. In this case where should i write the insert statement.Forms in built save commit's the form data where block is based on database not non database.
    @2nd reply
    Ypu are right. The reason i chnaged the database block to non-database is Currently i have a database block with form field canvas which insert only 1 record in to >table when we click on standard save button. The requirement was to add a field called CHANNEL which should have multiple values displayed. (i created this channel >field in a seperate datablock (non database) and used the same canvas.) When we insert data in all fields (single record) and channel we should be able to selected >multiple channel (say A,B and C) when we click on save then 3 records should be inserted in to the table which looping values for each channel. This was the actual >requirement and this is the reason why iam changing the block to non-database block.You are talking about two blocks.. 1. Master block and 2. Details block name channel
    You are inserting one record in master block then insert 3 record name A,B,C for that master record.
    Now you want master record should insert to each A,B,C record. Means
    'how are you' --master record
    and you want
    'A'- 'how are you'
    'B'- 'how are you'
    'C'- 'how are you'OR
    ?Ok. If you want master record save in database and then want to save non-database(channel) data into database USE Post-Insert trigger at block level and do the rest.
    Hope this helps...
    Hamid
    Mark correct/helpful to help others to get right answer(s).*
    Edited by: HamidHelal on Jan 26, 2013 1:20 AM

  • Create a trigger to copy data from table A to table B

    Hi,
    I been trying with no success to create a trigger on table a , so when the user update or delete or insert data on that table, I would like to copy all data from table a to table b BEFORE, the statement execute, I am using Oracle database 10g XE. It's URGENT and I would appreciate any quick response.
    Thank you in advance.

    > All what I needed to do is to copy / beckup the tableA before any changes done to the table
    Why? You are not giving us a problem to look at, analyse, and assist with - you are giving us a solution. That solution says "backup table before changes are made".
    This solution sounds very strange in the context of a RDBMS. Even stranger when dealing with relational design as one would use date and time to keep track of how data is changing.
    This, and your implementation of the solution (using triggers and DDL inside it!), makes me question whether this solution is indeed at all a solution. I see flaws and problems all the way.
    What happens if two users inserts at the same time into table A? Two truncates will be fired on table B. Then the entire contents of A (and not the changes) inserted into table B? Ridiculous...
    Thus from my point of view, looking at what you have presented to the forum in this thread, you do not seem to have any grasp on how to correctly use a RDBMS, never mind use Oracle.
    Ignorance is not a problem. It is fairly easily curable. Which is why I referred you to the manuals.
    Alternatively, instead of asking the forum how to make a flawed solution works, how about instead telling us the actual problem you are trying to solve?

Maybe you are looking for