How to send messages to a list of recipients with one SQL insert command

This guide gives you instructions, to setup a configuration, that allows you to send messages to a list of recipients by executing only one insert query for the message. It is based on the SQL to SMS configuration and implements the functionality using SQL.

If you have setup the database user you already have an ozekimessagin and an ozekimessageout database table in your database server. To send messages to a group of recipients with only one insert statement, you will need to two more database tables. One table will hold a list of all recipients and another will hold information about which recipient belongs to which group. The table definitions for these tables are shown on Figure 1.


  CREATE TABLE recipients (
    fullname varchar(30),
    phonenumber varchar(30)
    

  CREATE TABLE groups (
    groupname varchar(30),
    fullname varchar(30)
    

Figure 1 - SQL Create table definitions

To test the functionality we will need to add some test data into these tables

:
 
    INSERT INTO recipients (fullname,phonenumber) VALUES ('gyula','111111');
    INSERT INTO recipients (fullname,phonenumber) VALUES ('peter','222222');
    INSERT INTO recipients (fullname,phonenumber) VALUES ('erik','333333');
    INSERT INTO recipients (fullname,phonenumber) VALUES ('sonyja','444444');
    INSERT INTO recipients (fullname,phonenumber) VALUES ('lis','555555');

    INSERT INTO groups (groupname,fullname) VALUES ('friends','gyula');
    INSERT INTO groups (groupname,fullname) VALUES ('friends','lis');
    INSERT INTO groups (groupname,fullname) VALUES ('enemies','peter');
    INSERT INTO groups (groupname,fullname) VALUES ('enemies','sonyja');
    INSERT INTO groups (groupname,fullname) VALUES ('classmates','lis');
    INSERT INTO groups (groupname,fullname) VALUES ('classmates','peter');
    INSERT INTO groups (groupname,fullname) VALUES ('classmates','erik');

Figure 2 - SQL test data

To be able to send a message to a group we need to modify the SQL query in the "Polling" tab of the "SQL for sending" tab of the "Database user" configuration form in the Ozeki NG SMS Gateway user interface. This query selects messages from the ozekimessageout database table by default. It will be modified to query that will use data from the newly created tables: recipients and groups. The modified query will be:


    Change this:
    SELECT id,sender,receiver,msg,msgtype,operator FROM ozekimessageout
    WHERE status='send'
  
    To this:
    SELECT a.id,a.sender,b.phonenumber,a.msg,a.msgtype,a.operator
    FROM ozekimessageout a, recipients b, groups c
    WHERE (a.receiver=c.groupname) and (c.fullname=b.fullname) and
    (a.status='send')

Figure 3 - Modify the SQL SELECT query

The modified query will collect the telephone numbers from the recipients table using the group name. To send a message to a group you would use the following SQL command:

INSERT INTO ozekimessageout (receiver,msg,status) VALUES ('friends','Hello
world','send');

Figure 4 - Modify the SQL INSERT query

Note that in this SQL command we provide the group name 'friends' instead of a telephone number.

More information