Click on the banner to learn about and purchase my database training on Azure

SQL Server - How to create a hidden friend draw using Transact-SQL

Views: 1.205 views
Reading Time: 3 minutes

Hello people,
Good Morning!
All right with you ?

In this post, I will demonstrate a script to perform various sweepstakes, as a hidden friend, which is so common among Brazilians at the end of the year. This script was created by the BI analyst, T-SQL expert, Lucas Arrigoni.

The script has some checks, such as validating if the number of participants allows a random draw and it ensures that the person does not take themselves or that two participants take the same person.

To use this script, simply edit the list of participants, fill in their name and email, and run the script. This way, the routine will generate the draw listing and send an email to each participant, containing the name of their secret friend.

Source code:

Example of the result of a draw:

Currently, the routine sends emails to each draw participant containing the name of their secret friend. If you do not know how to configure or enable Database Mail XP to send emails to SQL Server, see my post. How to Enable, Send, and Monitor Emails by SQL Server (sp_send_dbmail).

Sample Email Sent:
Participante1: Seu amigo secreto é: Participante2

If you prefer to send messages by text message instead of email, see how to send text messages via SQL Server by accessing the post Using the Pushbullet API to send SMS messages in C #, PHP, Java, or SQL Server (with CLR).

That's it folks!
I hope you enjoyed this post and even more.