Trying to get a Trigger and Alert to work

So im trying to get a trigger to work with an alert and the Alert seems to be right and the trigger complies which seems right to me, however the instruction that I have in my book does not produce the same output that I get from my Update.
Here is the deal. I am to log into sql * with a default account as well as login as "SYSTEM"
the trigger should invoke the Alert and output a message to re-order some more product and the status should = 0 since there is no wait time. However I don't get a "Message" from the Alert and the status = 1 which indicates timeout. So if you can take a look at my code and let me know what I did wrong or how to "Connect" the two that would be great.
Trigger I created.
CREATE OR REPLACE TRIGGER order_replace_trg
AFtER UPDATE OF stock on bb_product
FOR EACH ROW
WHEN (OLD.stock = 24 AND NEW.stock = -2)
DECLARE
stock NUMBER(5,1);
idproduct NUMBER(2);
lv_msg_txt VARCHAR2(25);
lv_status_num NUMBER(1);
reorder NUMBER(3);
BEGIN
IF stock <> 24 AND reorder = 25 THEN
lv_msg_txt := 'Product 4 Reorder Time!';
DBMS_OUTPUT.PUT_LINE(lv_msg_txt);
ELSE
lv_status_num := 0;
DBMS_OUTPUT.PUT_LINE(lv_status_num);
END IF;
END;
The Alert:
BEGIN
DBMS_ALERT.REGISTER('reorder');
END;
DECLARE
lv_msg_txt VARCHAR2(25);
lv_status_num NUMBER(1);
BEGIN
DBMS_ALERT.WAITONE('reorder', lv_msg_txt, lv_status_num, 120);
DBMS_OUTPUT.PUT_LINE('Alert: ' ||lv_msg_txt);
DBMS_OUTPUT.PUT_LINE('Status: ' ||lv_status_num);
END;
Here is the block I need to run to test the trigger and alert.
UPDATE bb_product
SET stock = stock -2
WHERE idproduct = 4;
COMMIT;
The message I should get is:
Alert: Product 4 Reorder Time!
Status: 0
PL/SQL procedure successfully completed.
This is what I get.
SQL> /
Alert:
Status: 1
PL/SQL procedure successfully completed.
Thanks for your help!
Mac

Right. Register says "I'm interested in getting alerted to some particular event", Waitone says "I'm waiting until some event happens". Signal is the key thing that indicates that a particular event happened.
As for your trigger, a couple of issues
- I don't know why you're calling DBMS_OUTPUT. I'm guessing that you probably want to send a message along with your alert that the receiver gets and displays, not that you want to print a message to the window from inside the trigger.
- You're using the local variables stock and reorder in your IF statement but you never initialize them. I'm guessing that you would want to eliminate those local variables and just use :new.stock and :new.reorder (assuming that REORDER is a column in the table).
- Your WHEN clause doesn't seem to make sense. It's telling the trigger to fire only if you update stock from 24 to -2, which doesn't make sense. I'm not sure you would even need a WHEN clause here.
Justin

Similar Messages

Maybe you are looking for