If statements in a trigger

Hello

I tried to create a trigger in which when a task is completed, an invoice journal will be generated, use features of employment (date started, finished, cost etc.) to fill the fields of the invoice.

First of all, it's what I started with:
CREATE OR REPLACE TRIGGER gen_invoice
AFTER UPDATE ON jobs FOR EACH ROW
BEGIN
IF :new.status = 'Completed' THEN
DECLARE @Amount NUMBER(8, 2)
SET @Amount = SELECT SUM(unitCost * qtyUsed + rate + hrs) FROM jobAssign
WHERE jobNumber = jobs.jobNumber;
INSERT INTO invoice values (invoice_sequence.nextval, jobs.proposalNo, SYSDATE, SYSDATE + 10,
jobs.dateGen, jobs.dateReq, jobs.location, Amount, SYSDATE, '10', '100', '10', 'Not Paid');
END IF;
END;
/
Basically, I wanted to declare a variable called 'Amount' and some fields of work allows to calculate a total. I get the following error if:
 ERROR at line 9: PLS-00103: Encountered the symbol "IF" when expecting one of the following: ; delete exists prior 
The foregoing, what I really need. However, before I get the above fixed, I thought that I could create another trigger that did not declare all variables or were not in the statements if, just so I can test if the data were inserted properly. So I used:
CREATE OR REPLACE TRIGGER gen_invoice
AFTER UPDATE ON jobs FOR EACH ROW
BEGIN
INSERT INTO invoice VALUES (invoice_sequence.nextval, jobs.proposalNo, SYSDATE, SYSDATE + 10,
jobs.dateGen, jobs.dateReq, jobs.location, Amount, SYSDATE, '10', '100', '10', 'Not Paid');
SELECT proposalNo
FROM proposals
WHERE proposalNo = jobs.proposalNo
AND jobs.status = 'Completed';
END;
/
However, it also did not work. The error come with:
 ERROR at line 2: PL/SQL: ORA-00917: missing comma 
My main goal is to try to get the first set of instructions to work. So any help is appreciated!

Thank you.

P.S. I use the 10.2.0.1.0 oracle version if that helps.

Strange, it worked for me when I created the trigger, I even received a bill out of it! :-)

You have: new.jobnumber - he is known because it is the line that caused the trigger to fire.

Are you sure that you copy this exactly?

After EXACTLY what you have now.

Published by: Paul Horth on 3 may 2012 06:10

Tags: Database

Similar Questions

  • How to write change the statement in a trigger?

    Hi people,
    I know that triggers does not support DDL commands directly.is there any possibility to issue an alter statement in a trigger? for ex,.
    I would like to issue the command ALTER SESSION SET TIME_ZONE =' + 03:00 ' inside a trigger.i want this command so that when the user on her scheme immediately login session time zone must be set to '+ 03:00 '.
    pls help me with suggestions.


    Regarding
    VIDS

    Run immediately:

    SQL> create table mytable (a number);
    
    Table created.
    
    SQL> create or replace trigger mytrg before insert on mytable
      2  for each row
      3  begin
      4  execute immediate 'ALTER SESSION SET nls_date_format=''dd/mm/yyyy''';
      5  end;
      6  /
    
    Trigger created.
    
    SQL> select sysdate from dual;
    
    SYSDATE
    ---------
    23-FEB-10
    
    SQL> insert into mytable values (1);
    
    1 row created.
    
    SQL> select sysdate from dual;
    
    SYSDATE
    ----------
    23/02/2010
    

    Max
    http://oracleitalia.WordPress.com

  • Insert an Update statement in a Trigger

    I'm new to APEX and I'm trying to determine the best place to put an update for an audit table.

    This is the trigger that I work with:

    create or replace trigger "LEASES_T1"
    BEFORE
    insert or update or delete on "LEASES"
    for each row
    begin
        if inserting then
            :new.CREATION_DATE := sysdate;
            :new.created_by := nvl(v('APP_USER'),user);
            :new.last_update_date := sysdate;
            :new.last_update_by := nvl(v('APP_USER'),user);
    
        elsif updating then
            :new.last_update_date := sysdate;
            :new.last_update_by := nvl(v('APP_USER'),user);
        end if;
    
    end;
    INSERT INTO LEASE_AUDIT (LEASE_ID) VALUES (P2_ROWID);
    
    


    It is a basic triggers relaxation I raised a stock APEX.  It works fine without the addition of the audit at the end.  It updates the data created or changed for a table called LEASES.  During the same operation, I want to record this action in the audit table, LEASE_AUDIT.  The barebones for this statement is on line 18, above.

    Passing through some other posts which dealt with similar issues, it seemed that it would be timely to update in the trigger, but I don't know where it would go.  In this case, the statement is at the end, since it does not matter whether registration in the leases table exists or not.  The PK for LEASES is configured to be a ROWID generated by the screen (I have since been notified that a sequence may be a better method).

    So, how framing this statement to work?  Or is this oddball approach?

    I'm on APEX 4.2.4 on 11g.  My experience has been Access and VBA, mainly on autonomous databases (non-SQL Server backend).

    Thank you!

    I think a composed trigger may be the way to go.  I'll throw it against the wall and see if it sticks.

    Thank you!

  • subquery in the IF statement in the trigger, without using foreign keys

    Hello

    I'm studying ways to write a subquery in an IF statement, which is placed inside a trigger.
    I want to write sth like IF (: new.jazz not in (select goldies from T which...)) etc. I don't know if the fact that the IF is in a trigger adds a few additional restrictions. (Does?)

    So far, I've found the solution described here: comparison of subquery in the If statement , which I think is a little cheesy, I could have the "cooleststarinthegalaxy" instead of 1 and seems you need to do extra light, but always very lifting.

    I also read on the possibility to use MERGE, what I'm currently doing research.

    Is there another way?

    Thank you

    Published by: BluShadow on November 14, 2012 13:37
    fixed link

    Edits by me: the question is how (if possible) to do this without a foreign key.

    Published by: questioningq12 on November 14, 2012 06:11

    Published by: questioningq12 on November 14, 2012 06:13

    questioningq12 wrote:
    Still does not address the problem of writing, a subquery in a case within a trigger, which is what was the original question.

    OK, simple answer... you can not write a subquery in a State of the IF statement, it is not part of the syntax of PL/SQL.

  • Determine the SQL statement from a trigger

    Hello

    I currently use triggers to write in an array of history/log.
    Is there anyway of the determination of the trigger - the actual sql statement that fired the trigger?

    Rob.

    This can help...

    CREATE OR REPLACE TRIGGER EMP_TRG1
    BEFORE UPDATE ON EMPLOYEES
    declare
    v_num NUMBER;
    sql_stmt varchar2(2000);
    sql_text ora_name_list_t;
    BEGIN
    v_num:=ora_sql_txt(sql_text);
    FOR i in 1..NVL(v_num,0) LOOP
    sql_stmt:=sql_stmt||sql_text(i);
    END LOOP;
    INSERT INTO EMP_LOG VALUES(sql_stmt);
    --dbms_output.put_line(sql_stmt);
    END;
    

    Re: capture of sql statement that activates the trigger
    Re: Get the text of the query to a trigger

  • question about the call in the trigger statement

    I faced a question on the written examination.

    A CALL statement in a trigger to call
    package (a)
    procedure b)
    c function)
    (d) another trigger

    Can someone give me answer with reason?
    I used the CALL statement inside the trigger, but not allowing to use. Could be sooner in the oracle CALL statement, we can use... This isn't a guess, so I ask in the forum...
    Please guide me...



    Rgds,
    PC

    You can use the CALL in a trigger without using EXECUTE IMMEDIATE

    SQL> create table t1 (
      2    col1 number
      3  );
    
    Table created.
    
    SQL> create procedure t1_proc
      2  as
      3  begin
      4    dbms_output.put_line( 'In T1_PROC' );
      5  end;
      6  /
    
    Procedure created.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  create trigger trg_t1
      2    before insert on t1
      3    for each row
      4* call t1_proc
      5  /
    
    Trigger created.
    
    SQL> set serveroutput on;
    SQL> insert into t1 values( 1 );
    In T1_PROC
    
    1 row created.
    

    I see no reason that you would in fact intentionally structure your code this way in this day and age because it would be likely to lead to confusion for those who had to support it in the future. But it's a valid syntax that probably made sense back in Oracle 5.

    Justin

  • GRANT/REVOKE in system Trigger statement

    Hello

    I create DDL trigger and want to use the DCL statement in the trigger body. I wrote below bu trigger, it is invalid DDL operation s error in the outbreak.

    Could anyone suggest how we can use the under DCL statement in the trigger body.

    CREATE OR REPLACE TRIGGER trg_after_ddl
    AFTER the DDL ON SCHEMA
    BEGIN
    IF (ora_login_user = 'X') THEN
    IF (ora_database_name = ' XYZ.) WORLD ') THEN
    RUN IMMEDIATELY "REVOKE CREATE TABLE X;
    RUN IMMEDIATELY "REVOKE CREATE SEQUENCE X";
    END IF;
    END IF;
    END;
    /

    Thank you.
    Alok

    You cannot run all sorts of DDL statements in Oracle triggers because you cannot perform COMMIT / ROLLBACK in the triggers of the Oracle: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm#sthref1270

    Edited by: P. Forstmann on June 24, 2009 09:09

  • How to start and stop the video capture in a state machine

    Hello

    I am a student of neuroscience and this is what I try to do: 1) trigger a camera to GigE (200 FPS), 2) start the video capture, 3) triggers a light that stays on for x milliseconds, 4) trigger a valve that will blow an air puff, 5) stop the tone and the valve at the same time and 5) stop the video capture 1 second after 4). For this, my VI is a state machine, with a State for each trigger (ex, 1 State for trigger camera 1 State of light of trigger, ect) and a statement to "stop" and a "time-out State." Everything worked except the video acquisition - the program could trigger the camera but only acquire a frame, because it was not running in a while loop and this is my first question: for an acquisition video is it always necessary to have a while loop?
    I guess the answer is Yes, so I built a loop of 'consumer' for the camera. A queue of the pre-trial 'trigger camera' in the loop of producer, I was then able to start the video capture, but now the problem is to stop it at the right time. I have an another queue in the State of 'stop' in the loop of the producer, but is not able to start the State 'stop' in the loop of consumer, I think that maybe it's because the loop of consumer gets "stuck" in the whole loop.

    I hope you can help me, the VI is attached. Thank you!

    Hello

    First of all, in your loop producer: STATE 'Stop', you use the flushing line, then there is no need to use 'item queue at the opposite end. You can use the enqueue function.

    Secondly, you said "I moved the command stop inside the while loop as you said, but it did not work. But if you look closely, you moved "Enqueue function" inside the while loop. Instead you should use Dequeue function. Please, look at the picture I posted an orderly manner.

    And remove the circled (i.e. function. Enqueue function loop of consumption, where I wrote 'Why stop here').

    Try now.

  • out of State driver

    I have no idea why I get this

    Blue screens me and says "driver power state failure" and trigger a dump of memory

    k that I figured it out.

    I changed my external hard drive from firewire to usb

  • need, update the message after inserting the trigger data

    Hi all

    CREATE OR REPLACE TRIGGER xxc_student

    AFTER INSERTION

    WE raise

    BEGIN

    Dbms_output.put_line ('end of Inserted');

    END;

    And I want to insert data into the table:

    INSERT INTO student (name) VALUES ("ABC");

    INSERT INTO student (name) VALUES ("CCC");

    INSERT INTO student (name) VALUES ('DDD');

    INSERT INTO student (name) VALUES ("EEA").

    COMMIT;

    I have the following results:

    1 row inserted

    end of Inserted

    1 row inserted

    end of Inserted

    1 row inserted

    end of Inserted

    1 row inserted

    end of Inserted

    ENGAGE

    And I would have

    1 row inserted

    1 row inserted

    1 row inserted

    1 row inserted

    end of Inserted

    Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production

    Thank you

    No, this isn't possible; e by a trigger.  Relax, you have created is a statement after trigger, so it fires after the insert statement is complete.  In the first example, you have 4 separate SQL statements, so the trigger is activated after each statement.  In your second example, you have a single SQL statement that affects the 4 lines, if the trigger is activated once the end of the statement.

    The number of inserted rows is reported since sqlplus (the client), based on what the engine SQL in the database said he did.  Since there is only a single statement engine SQL reports 4 lines inserted at the client, not four row insertions.

    What problem are you trying to solve with this approach?  In General, dbms_output is not much used in production code, except occasioanlly as a simple debugging tool.

    John

  • Toggle the shutter button only if the trigger is

    Hi all

    Maybe it's a simple question.

    I want to just write a SQL statement to turn the trigger only if this trigger exists...

    I can turn it off by running the below statement

    ALTER TRIGGER MY_TRIGGER DISABLE.

    But if I run this statement where the trigger is not defined, an error.
    Please advise me to deactivate or activate the trigger based on its existence.

    Use user_triggers. For example

    BEGIN
     FOR i IN (SELECT trigger_name FROM user_triggers) LOOP
      Dbms_Output.put_line('ALTER TRIGGER '||i.trigger_name||' DISABLE');
      --Here your logic for enable/disble criteria.
      EXECUTE IMMEDIATE 'ALTER TRIGGER '||i.trigger_name||' DISABLE';
     END LOOP;
    END;
    
  • Back-2090

    Hello

    I bought a digital oscilloscope based on PC (back-2090). And I work with labview software and I need to trigger the two channels simultaneously. But in the example labview, only two States 0 and 1 is for relaxation.
    It is in the basic software trigger Mode is 0, 1, 2 and 3 which is State 2 alt Trigger please help me in this case

    Thanks for driving me.

    I need to trigger the two channels at the same time I want to find the phase of the signal of two

  • Smart way to save large amounts of data using the circular buffer

    Hello everyone,

    I am currently enter LabView that I develop a measurement of five-channel system. Each "channel" will provide up to two digital inputs, up to three analog inputs of CSR (sampling frequency will be around 4 k to 10 k each channel) and up to five analog inputs for thermocouple (sampling frequency will be lower than 100 s/s). According to the determined user events (such as sudden speed fall) the system should save a file of PDM that contains one row for each data channel, store values n seconds before the impact that happened and with a specified user (for example 10 seconds before the fall of rotation speed, then with a length of 10 minutes).

    My question is how to manage these rather huge amounts of data in an intelligent way and how to get the case of error on the hard disk without loss of samples and dumping of huge amounts of data on the disc when recording the signals when there is no impact. I thought about the following:

    -use a single producer to only acquire the constant and high speed data and write data in the queues

    -use consumers loop to process packets of signals when they become available and to identify impacts and save data on impact is triggered

    -use the third loop with the structure of the event to give the possibility to control the VI without having to interrogate the front panel controls each time

    -use some kind of memory circular buffer in the loop of consumer to store a certain number of data that can be written to the hard disk.

    I hope this is the right way to do it so far.

    Now, I thought about three ways to design the circular data buffer:

    -l' use of RAM as a buffer (files or waiting tables with a limited number of registrations), what is written on disk in one step when you are finished while the rest of the program and DAQ should always be active

    -broadcast directly to hard disk using the advanced features of PDM, and re-setting the Position to write of PDM markers go back to the first entry when a specific amount of data entry was written.

    -disseminate all data on hard drive using PDM streaming, file sharing at a certain time and deleting files TDMS containing no abnormalities later when running directly.

    Regarding the first possibility, I fear that there will be problems with a Crescent quickly the tables/queues, and especially when it comes to backup data from RAM to disk, my program would be stuck for once writes data only on the disk and thus losing the samples in the DAQ loop which I want to continue without interruption.

    Regarding the latter, I meet lot with PDM, data gets easily damaged and I certainly don't know if the PDM Set write next Position is adapted to my needs (I need to adjust the positions for (3analog + 2ctr + 5thermo) * 5channels = line of 50 data more timestamp in the worst case!). I'm afraid also the hard drive won't be able to write fast enough to stream all the data at the same time in the worst case... ?

    Regarding the third option, I fear that classify PDM and open a new TDMS file to continue recording will be fast enough to not lose data packets.

    What are your thoughts here? Is there anyone who has already dealt with similar tasks? Does anyone know some raw criteria on the amount of data may be tempted to spread at an average speed of disk at the same time?

    Thank you very much

    OK, I'm reaching back four years when I've implemented this system, so patient with me.

    We will look at has a trigger and wanting to capture samples before the trigger N and M samples after the outbreak.  The scheme is somewhat complicated, because the goal is not to "Miss" samples.  We came up with this several years ago and it seems to work - there may be an easier way to do it, but never mind.

    We have created two queues - one samples of "Pre-event" line of fixed length N and a queue for event of unlimited size.  We use a design of producer/consumer, with State Machines running each loop.  Without worrying about naming the States, let me describe how each of the works.

    The producer begins in its state of "Pre Trigger", using Lossy Enqueue to place data in the prior event queue.  If the trigger does not occur during this State, we're staying for the following example.  There are a few details I am forget how do ensure us that the prior event queue is full, but skip that for now.  At some point, relaxation tilt us the State. p - event.  Here we queue in the queue for event, count the number of items we enqueue.  When we get to M, we switch of States in the State of pre-event.

    On the consumer side we start in one State 'pending', where we just ignore the two queues.  At some point, the trigger occurs, and we pass the consumer as a pre-event.  It is responsible for the queue (and dealing with) N elements in the queue of pre-event, then manipulate the M the following in the event queue for.  [Hmm - I don't remember how we knew what had finished the event queue for - we count m, or did you we wait until the queue was empty and the producer was again in the State of pre-event?].

    There are a few 'holes' in this simple explanation, that which some, I think we filled.  For example, what happens when the triggers are too close together?  A way to handle this is to not allow a relaxation to be processed as long as the prior event queue is full.

    Bob Schor

  • Not getting not automatically created restore points

    Original title - help with Restore Points

    I recently changed the size of my drives C and D using a utility partition.

    He stopped the automatic creation of Restore Points. I noticed this because the restore disk cleanup deleting data/shadow copies running did not clearly get out any space on my hard drive. I run Disk Cleanup about every 10 days.
    I tried to re - turn on this; but I'm not able to do. Here's what I did:
    Under Task Scheduler, I have a set trigger in place to create a point of restoring daily at 14:00. My computer is sometimes off in the morning.
    The State of this trigger is activated. This is used to create regular restore points.
    I disabled the trigger to create a restore at startup a point a couple of years. My system has been the creation of restore points every day with the other Tirgger at 14:00 in Task Scheduler.
    Question No. 1: Would it be useful to delete and recreate the trigger for 14:00?
    Question 2: I went into control panel – system and Maintenance – System – the System Protection window and I am able to manually create a restore point.
    I can see my drives C and D on the list. Maybe I think too much about things and read things correctly; but I feel like it's better to ask this question:
    * Should I put a check mark next to my D drive or my C drive?
    I ask this question because the title says in this little window of the System Protection: "create restore automatically on some points:
    So, does that mean that only check box for drive C indicates a place where the restore point will be created physically ("on drive C"), or it means to a restore point to data on the C drive will be created? Is this clear?
    Right now I checked C drive and D drive and not on the list. I'm assuming he's asking me which drive I want backed up.
    (I don't know where the backup restore points are saved/located to go look for them.)
    I see that the last restore point was when I did it manually. The task scheduled for 14:00 does not appear on the list in this little window. However, when I check the history of Task Scheduler, it shows that the task has been completed.
    Question 3: Is something wrong with this list? I also check to see if the restore point is created at 14:00 from the task history shows that this has been done?
    I would appreciate help with this. I put a reminder on my calendar to create restore points manually. But something does not work with my system; and I want to fix it.
    Ken

    Restore points are made. Yesterday, after posting the original message/questions, I changed the schedule of the trigger to make a restore point. Maybe something jogging in the system in place. (Not exactly a technical term, I know.)

    Today, I checked the disk cleanup and had much less free space of a few days. I ran clean it point restoration/cliché and cleared space. Then I checked the control panel – system and Maintenance – System – the System Protection window and saw that the restore point the most recent was created today.
    Then...
    Question 1: Maybe change the relaxation time makes a difference.
    Question 2: Place a check mark next to the C drive, which is the drive that you want supported during the restoration.
    Question 3: Everything is fine with this list.
  • Engage in relaxation with an AUTONOMOUS transaction

    Hi all

    My question is on the trigger. say if we have a before insert trigger wrote on the employee table who agrees to pay his transaction with autonomous, so if our insert on employee statement fails, the trigger is executed as it is before the outbreak.

    Practically, I checked and found that it is not have triggered. Please clarify my understanding trigger a trigger.

    Thank you

    Vipin Kumar Rai

    It's just the way it is. If a fundamental data type is violated, it fires. If a constraint (as a non null) is violated, then he fired.

    I know that you put in comment the autonomous part - I was pointing out that your use of an autonomous transaction in the part that you said is probably wrong, unless you are trying to log on each attempt insert, regardless of whether it succeeds, and (as you saw), a trigger can not capture each insert attempt if the data type is not valid (for example). Even if she did it, insert it into your trigger would fail (assuming you have the same types of data on emp_v as on emp)

Maybe you are looking for

  • Windows 7 64 bit Ita product key OEM

    Hello It's very simple. I had to replace my laptop hd (doesn't), I lost the recovery partition to do Microsoft Windows 7 label to the bottom of the laptop that is dimmed and I'm not able to recover the product key for the operating system. I have my

  • 800b0100 error code when you try to install the updates KB974145 and KB978251

    I tried to install these 'important' updates several times with the same result - updates failed, error 800b0100 code. I downloaded and installed the KB974821 tool - tells me it installed - same problem. (I don't know if I'm supposed to do something

  • Sony Xperia Z5 compact rings once then goes to voicemail

    I have a Sony Xperia Z5 compact which works well the 1.5 months I got it. But these days I have problems with it. When people call me, I can hear it ring once and then he rejects. People call all say that they are not receiving says that the phone is

  • How to close a window of movie maker.exe

    Remember - this is a public forum so never post private information such as numbers of mail or telephone! Ideas: can someone help me to close a 'bad volume' opinion? "The wrong volume is in the drive. "does not even help remove the DVD. You have prob

  • Error C00D11B1 Windows Media Player (Windows 7 x 64).

    I get error C00D11B1 when you try to play any multimedia file on WMP. I have spent hours looking on the forums and tried all the solutions given and nothing has solved the problem. I tried to use Media Player Classic and there is no audio data with v