SQL Server: selecting query results into variable

Posted on 2014-08-12
Last Modified: 2014-08-20
I'd like to email results of the sql select query (either 'Results to Text' or 'Results to Grid') using
msdb.dbo.sp_send_dbmail procedure. What are my choices?
Question by:quasar_ee
    1 Comment
    LVL 35

    Accepted Solution

    In this you can get the procedure to run the query for you - see the query parameter.

    (I note that you've asked this in the MS SQL Server topic, which by convention is the SQL 2000 and earlier queries. Now DBMail is a SQL 2005+ feature, so wont work if your server is SQL 2000.)

    Otherwise you could render some html yourself into  varchar variable and use that as the body of the email.

    How big is your results set?

    Is this on-going or a one-off? How is the results going to be used at the receiver?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now