Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even if your trigger didn't have a syntax error, it wouldn't work: It would just output the "Can't have more than 10 orders" message and the insert would still go ahead; moreover, if it passed the test, you'd be put into a loop of inserts. You need to make it throw an exception, in the event of too many records, and your application needs to catch that, and do nothing if it passes the test.</p> <p>As for the error, I think the problem is with this line:</p> <pre><code>SELECT COUNT(DISTINCT FK1_customer_id) FROM placed_order into v_count; </code></pre> <p>It should be:</p> <pre><code>SELECT COUNT(DISTINCT FK1_customer_id) into v_count FROM placed_order; </code></pre> <p>However, this query is wrong anyway: It will return the number of unique customers that have made orders. What you're looking for is:</p> <pre><code>select count(order_id) into v_count from placed_order where fk1_customer_id = :new.fk1_customer_id </code></pre> <p>Presuming that <code>order_id</code> has a uniqueness constraint; which seems likely! Anyway, therefore, your trigger code should be something like:</p> <pre><code>create or replace trigger trg_order_limit before insert on placed_order for each row declare v_count number; begin -- Get current order count select count(order_id) into v_count from placed_order where fk1_customer_id = :new.fk1_customer_id; -- Raise exception if there are too many if v_count &gt;= 10 then raise_application_error(-20000, 'You currently have 10 or more orders processing.'); end if; end; </code></pre> <p>However, as per <a href="https://stackoverflow.com/questions/15546074/oracle-pl-sql-trigger-to-limit-orders-per-customer#comment22027392_15546074">@DazzaL's comment</a>, this approach probably isn't a good idea.</p> <hr> <p><strong>EDIT</strong> Sudden realisation that your query to count the number of orders per client is totally wrong. Updated my answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload