solidDB Help : Programming : SQL extensions : Events : Example: Queuing events before waiting for them
  
Example: Queuing events before waiting for them
When you wait on an event, you are registered for that event if you did not already explicitly register for it. However, you can register for events if you want them to start being queued now but you do not want to start waiting for them until later.
The following example shows a simple usage of the REGISTER EVENT and UNREGISTER EVENT statements, see REGISTER EVENT and UNREGISTER EVENT.
CREATE EVENT e0;
CREATE EVENT e1 (param1 int);
COMMIT WORK;
-- Create a procedure to register the events so that, when they occur,
-- they are put in this connection's event queue.
"CREATE PROCEDURE eeregister
BEGIN
  REGISTER event e0;
  REGISTER EVENT e1;
END";
CALL eeregister;
COMMIT WORK;
-- Create a procedure to post the events.
"CREATE PROCEDURE eepost
BEGIN
  DECLARE x int;
  x := 1;
  POST EVENT e0;
  POST EVENT e1(x);
END";
COMMIT WORK;
-- Post the events. Even though we haven't yet waited on the events,
-- they will be stored in our queue because we registered for them.
CALL eepost;
COMMIT WORK;
-- Now create a procedure to wait for the events.
"CREATE PROCEDURE eewait RETURNS (whichEvent VARCHAR(100))
BEGIN
  DECLARE i INT;
  WAIT EVENT
    WHEN e0
      BEGIN
        whichEvent := 'event0';
    END EVENT
    WHEN e1(i)
      BEGIN
        whichEvent := 'event1';
  END EVENT
END WAIT
END";
COMMIT WORK;
-- Since we already registered for the 2 events and we already
-- posted the 2 events, when we call the eewait procedure twice
-- it should return immediately, rather than waiting.
CALL eewait;
CALL eewait;
COMMIT WORK;
-- Unregister for the events.
"CREATE PROCEDURE eeunregister
BEGIN
  UNREGISTER EVENT e0;
  UNREGISTER EVENT e1;
END";
CALL eeunregister;
COMMIT WORK;
CREATE EVENT e0;
CREATE EVENT e1 (param1 int);
COMMIT WORK;
-- Create a procedure to register the events to that when they occur
-- they are put in this connection's event queue.
"CREATE PROCEDURE eeregister
BEGIN
  REGISTER EVENT e0;
  REGISTER EVENT e1;
END";
CALL eeregister;
COMMIT WORK;
-- Create a procedure to post the events.
"CREATE PROCEDURE eepost
  BEGIN
    DECLARE x int;
    x := 1;
    POST EVENT e0;
    POST EVENT e1(x);
  END";
COMMIT WORK;
-- Post the events. Even though we haven't yet waited on the events,
-- they will be stored in our queue because we registered for them.
CALL eepost;
COMMIT WORK;
-- Now create a procedure to wait for the events.
"CREATE PROCEDURE eewait RETURNS (whichEvent VARCHAR(100))
  BEGIN
    DECLARE i INT;
    WAIT EVENT
      WHEN e0 BEGIN
      whichEvent := 'event0';
    END EVENT
    WHEN e1(i) BEGIN
      whichEvent := 'event1';
    END EVENT
  END WAIT
END";
COMMIT WORK;
-- Since we already registered for the 2 events and we already
-- posted the 2 events, when we call the eewait procedure twice
-- it should return immediately, rather than waiting.
CALL eewait;
CALL eewait;
COMMIT WORK;
-- Unregister for the events.
"CREATE PROCEDURE eeunregister
BEGIN
  UNREGISTER EVENT e0;
  UNREGISTER EVENT e1;
END";
CALL eeunregister;
COMMIT WORK;
Go up to
Events