Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:05:32+00:00 2026-06-10T05:05:32+00:00

I have what I thought was a simple question but havent been able to

  • 0

I have what I thought was a simple question but havent been able to resolve it in months , so here it goes !!!

Environment : Microsoft SQL 2008

I have a qyery that runs every night and gives me a list of all orders that dont have an approval code:

Query :

Select OrderId, First , Last ,Email from Orders where Approval is Null

Results:

OrderID First Last Email

4565 Tom Cruise Tom@email.com

3423 Jaime Fox jaime@email.com

Thats fine becuase now i know which orders dont have an approval but what i want to do is for the query to atomatcally email them something like

Desired Result
Dear Tom ,

Your order does not have an approval code and will be cancelled within the next 48 Hours if the code is not entered.

* Desired Result**

At any given time i may have 30 of these orderd so the query needs to just run and generate an email to each person . if 1 person has multiple orders it is fine if they get an email for each order seperatly . Please Help!! and by help I mean please let me see sample code to how to achieve this since i dont know how to. Thank You

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:05:33+00:00Added an answer on June 10, 2026 at 5:05 am

    You can use the email capabilities in SQL Server for sending the emails.

    Once you configure SQL Server (follow this link); you can simply go through each email in a loop, like so:

    USE msdb
    GO
    EXEC sp_send_dbmail @profile_name='PinalProfile',
    @recipients='tomcruise@Example.com',
    @subject='Your Order',
    @body='Hi there.'
    

    Update

    Here’s an example of how to iterate through the records:

    declare @temp as table
    (
    Id int identity,
    OrderId int, 
    First varchar(255), 
    Last varchar(255),
    Email varchar(512)
    
    )
    
    insert into @temp
    Select OrderId, First , Last ,Email from Orders where Approval is Null
    
    declare @email varchar(512)
           , @index int=1 
           , @upper_bound int
           , @subject varchar(500)
    
    select @upper_bound=max(Id) from @temp
    
    
    while(@index<=@upper_bound)
    BEGIN
    select @email=email,@subject='Your order number '+OrderID from @temp where Id=@index
    
    EXEC msdb.dbo.sp_send_dbmail @profile_name='profile',
    @recipients=N@email,
    @subject=@subject,
    @body='Whatever you want to say here.'
    
    select @index=@index+1
    
    END
    
    -- You are done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like a simple question, but I haven't been able to find a solid
I would have thought this one would be simple but I'm trying to change
This is a really simple question, and I thought .each would work but I
i know this question has been asked here but i am stuck with this
This question is a little n00b-ish, but I haven't been able to find a
This seems like a simple question on its surface but I thought I'd reach
This is probably a simple question, but as I have never gone through the
I would have thought that this would be an easy thing to do, but
Before answering, it is not as easy question as you might have thought about
I have always thought that in order to connect to SQL server using windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.