How to Send SMS Alerts from Microsoft SQL Server

How to Send SMS Alerts from Microsoft SQL Server

ยท by ajay

Microsoft SQL Server can send email directly from T-SQL using Database Mail and the sp_send_dbmail stored procedure.

And with text.email, those emails can become SMS alerts.

The basic method is simple. SQL Server checks a condition. If it’s bad, SQL Server sends an email using sp_send_dbmail to your special text.email address (your-number@your-subdomain.text.email).

And text.email converts that email into an SMS that comes to your phone immediately, ensuring you don’t miss the alert and you can do something about it ASAP.

Microsoft SQL Server SMS Alerts: Table of Contents

Setting Up Text Alerts in Microsoft SQL Server

Here’s how to get this set up…

Prerequisites

You’ll need the following things before we dive in:

  • A Microsoft SQL Server instance with Database Mail configured (here are Microsoft’s instructions)
  • A Database Mail profile, such as TextEmailAlerts
  • A text.email address
  • Optional but recommended: SQL Server Agent, so the checks can run automatically on a schedule

To get your email-to-SMS address, go to text.email. Sign up for an account with your email address, pick a plan, and choose a private subdomain.

Now you’ll be able to send a text alert to yourself at an address like 15551234567@your-subdomain.text.email.

If you need these alerts to go to multiple people, you can set up a quick distribution list in text.email, with an address like dba-alerts@your-subdomain.text.email. I’ll cover those a bit more later on.

Step 1: Send a Test SMS from SQL Server

Once Database Mail is configured, start with a simple test:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'TextEmailAlerts',
    @recipients = 'your-number@your-subdomain.text.email',
    @subject = 'SQL Server test alert',
    @body = 'This is a test SMS alert from SQL Server using text.email.';

(Obviously, replace TextEmailAlerts with your Database Mail profile name and your-number@your-subdomain.text.email with your actual text.email address.)

If everything is configured correctly, SQL Server will send an email and text.email will deliver it as an SMS message.

Step 2: Choose the condition for your SMS alerts

You want to use these text alerts for the most critical situations (so you don’t grow blind to them).

Here are four examples of how you can use them right.

Alert if the most recent record is too old

A key database health check is making sure a table is still receiving new records.

Like… if you have an Orders table and you expect new orders to arrive at least every 30 minutes. If the newest order is older than 30 minutes, that could mean your website, API, import process, or payment flow is broken.

DECLARE @LastOrderUtc datetime2;
DECLARE @Message nvarchar(max);

SELECT @LastOrderUtc = MAX(CreatedUtc)
FROM dbo.Orders;

IF @LastOrderUtc IS NULL
   OR @LastOrderUtc < DATEADD(MINUTE, -30, SYSUTCDATETIME())
BEGIN
    SET @Message = CONCAT(
        'SQL Server alert: No recent orders. Last order UTC: ',
        COALESCE(CONVERT(varchar(30), @LastOrderUtc, 120), 'none')
    );

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TextEmailAlerts',
        @recipients = 'dba-alerts@your-subdomain.text.email',
        @subject = 'SQL Alert: Orders stale',
        @body = @Message;
END;

This sends an SMS only if the newest order is missing or too old.

Alert if a table has too many rows

Another useful check is monitoring a queue table. If too many rows build up, it may mean a background process is stuck.

DECLARE @QueueCount int;
DECLARE @Message nvarchar(max);

SELECT @QueueCount = COUNT(*)
FROM dbo.MessageQueue
WHERE ProcessedUtc IS NULL;

IF @QueueCount > 1000
BEGIN
    SET @Message = CONCAT(
        'SQL Server alert: MessageQueue backlog is ',
        @QueueCount,
        ' unprocessed rows.'
    );

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TextEmailAlerts',
        @recipients = 'dba-alerts@your-subdomain.text.email',
        @subject = 'SQL Alert: Queue backlog',
        @body = @Message;
END;

This is helpful for email queues, SMS queues, billing queues, import queues, webhook queues, or any other table where records should be processed quickly.

Alert if a table has too few rows

For example, maybe a reporting table should receive at least 500 records per day. If it does not, something upstream may have failed.

DECLARE @TodayCount int;
DECLARE @Message nvarchar(max);

SELECT @TodayCount = COUNT(*)
FROM dbo.ImportedEvents
WHERE CreatedUtc >= CONVERT(date, SYSUTCDATETIME());

IF @TodayCount < 500
BEGIN
    SET @Message = CONCAT(
        'SQL Server alert: ImportedEvents has only ',
        @TodayCount,
        ' rows today. Expected at least 500.'
    );

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TextEmailAlerts',
        @recipients = 'dba-alerts@your-subdomain.text.email',
        @subject = 'SQL Alert: Low import volume',
        @body = @Message;
END;

Alert if new errors appear

If your application writes errors to a database table, SQL Server can text you when new critical errors appear.

DECLARE @ErrorCount int;
DECLARE @Message nvarchar(max);

SELECT @ErrorCount = COUNT(*)
FROM dbo.ApplicationErrors
WHERE Severity = 'Critical'
  AND CreatedUtc >= DATEADD(MINUTE, -5, SYSUTCDATETIME());

IF @ErrorCount > 0
BEGIN
    SET @Message = CONCAT(
        'SQL Server alert: ',
        @ErrorCount,
        ' critical application errors in the last 5 minutes.'
    );

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TextEmailAlerts',
        @recipients = 'dba-alerts@your-subdomain.text.email',
        @subject = 'SQL Alert: Critical errors',
        @body = @Message;
END;

Step 3: Run these checks automatically

Now that you have your check(s) in place, you’ll need them to run automatically. The easiest way is with SQL Server Agent.

In SQL Server Management Studio:

  1. Open SQL Server Agent.
  2. Create a new Job.
  3. Add a T-SQL job step.
  4. Paste in one of the alert scripts above.
  5. Create a schedule, such as every 5 minutes, every 15 minutes, or once per hour.

For example, a stale-orders check might run every 5 minutes. A daily volume check might run once per morning. A queue backlog check might run every 1 or 2 minutes if it is monitoring something critical.

Step 4: Prevent alert spam

You really don’t want to text yourself every minute for the same ongoing problem.

A quick way to prevent alert spam is to create a small table that tracks when each alert was last sent.

CREATE TABLE dbo.SqlSmsAlertLog
(
    AlertName nvarchar(100) NOT NULL PRIMARY KEY,
    LastSentUtc datetime2 NOT NULL
);

Then check that table before sending another SMS.

DECLARE @AlertName nvarchar(100) = 'Orders stale';
DECLARE @LastOrderUtc datetime2;
DECLARE @LastSentUtc datetime2;
DECLARE @Message nvarchar(max);

SELECT @LastOrderUtc = MAX(CreatedUtc)
FROM dbo.Orders;

SELECT @LastSentUtc = LastSentUtc
FROM dbo.SqlSmsAlertLog
WHERE AlertName = @AlertName;

IF (
       @LastOrderUtc IS NULL
       OR @LastOrderUtc < DATEADD(MINUTE, -30, SYSUTCDATETIME())
   )
   AND (
       @LastSentUtc IS NULL
       OR @LastSentUtc < DATEADD(MINUTE, -30, SYSUTCDATETIME())
   )
BEGIN
    SET @Message = CONCAT(
        'SQL Server alert: No recent orders. Last order UTC: ',
        COALESCE(CONVERT(varchar(30), @LastOrderUtc, 120), 'none')
    );

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'TextEmailAlerts',
        @recipients = 'dba-alerts@your-subdomain.text.email',
        @subject = 'SQL Alert: Orders stale',
        @body = @Message;

    MERGE dbo.SqlSmsAlertLog AS target
    USING (SELECT @AlertName AS AlertName) AS source
    ON target.AlertName = source.AlertName
    WHEN MATCHED THEN
        UPDATE SET LastSentUtc = SYSUTCDATETIME()
    WHEN NOT MATCHED THEN
        INSERT (AlertName, LastSentUtc)
        VALUES (@AlertName, SYSUTCDATETIME());
END;

This version sends the SMS only if:

  • The orders table is stale
  • The same alert has not already been sent in the last 30 minutes

Other Smart Moves for SQL Server SMS Alerts

Those are the key setup steps. These are some refinements.

Keep the SMS short

SMS alerts should be brief. Include the system name, the problem, and the most important number or timestamp.

For example, this is good:

SQL Alert: Orders stale. Last order UTC: 2026-06-23 14:02:11

This is too long and will get split into multiple messages:

The order processing subsystem has not inserted a new row into the Orders table recently and this may indicate a possible problem with the website, API, database, or payment processor...

(Though you could do this with text.email’s SMS formatting, it’s probably better to just set up the alerts right in SQL Server.)

Use a distribution list for team alerts

As I mentioned earlier, if you need to send alerts to more than one person, you can use a text.email distribution list.

That makes it easier to add or remove recipients without changing SQL Server code. SQL Server keeps sending to the same address, while you manage the actual phone numbers inside text.email.

Use scheduled routing for on-call teams

If your team has different people responsible at different times, use scheduled SMS routing in text.email. SQL Server can keep sending to the same address, while text.email decides who should receive the SMS based on the schedule.

So, for example, only the one poor sucker who’s on call overnight gets the alerts between 12 AM and 7 AM.

Include the server or database name

If you manage more than one SQL Server, include the server name or database name in the message.

DECLARE @Message nvarchar(max);

SET @Message = CONCAT(
    'SQL alert on ',
    @@SERVERNAME,
    ': Orders table is stale.'
);

Ready to Get Started with Microsoft SQL Server SMS Alerts?

So yeah, a database table having a few extra rows doesn’t sound as critical as, say, the server being on fire. But if you’ve ever been burned (no pun intended) before, you know that many problems show up in the database before they show up anywhere else.

Your database knows when:

  • Customers stopped signing up
  • Orders stopped arriving
  • Payments stopped processing
  • A queue is growing
  • A background worker stopped updating records
  • Error logs are filling up
  • Expected data did not arrive

By combining SQL Server Database Mail with text.email, you can turn those database conditions into SMS alerts without building a separate monitoring application.

Ready to get started with text.email and get your Microsoft SQL Server text alerts going in the next few minutes? Sign up for text.email, pick a plan, follow the instructions in this article, and you and your team will be good to go.

Try text.email free

Send an email to
your-number@text.email
and receive it as a text in seconds. No signup required.