How to store massage of SQL server Query Results

  • Hi ,

    I want to store Massages of SQL server Query Results in variable or table .

    Or Is there any way to send these massages in mail ??

    Please help !!

  • How does your query look like?

    You can store your results in a variable by using it in the SELECT statement:

    SELECT @myVariable = columnA FROM myTable;

    You can store results of the query in a table by prefixing it with INSERT INTO or by adding INTO in the SELECT statement:

    INSERT INTO myTable (columnA) -- will only work if the table has the same structure as the resultset

    SELECT columnA from myOtherTable;

    SELECT columnA INTO myTable FROM myOtherTable; -- this will create a new table

    For sending results out with mail, check out sp_send_dbmail.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • I need massage to be stored not the results..

    Eg:-

    (1 row(s) affected)

    DBCC execution completed. If DBCC printed error messages, contact your system administrator.

  • Check out this thread:

    http://stackoverflow.com/questions/1195324/using-the-result-of-the-sql-messages-pane

    It's not possible in SQL Server itself, but it might work in a client application.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Hello

    You could achieve this through osql.

    1. Run osql in DOS and extract the last message.

    D:\>osql -S INSTANCE -U USER -P PASSWORD -d DATABASE -Q "SELECT * FROM TABLE" | find "affected"

    (4 rows affected)

    2. You can then parse the result (by sending it to a file) and then send it in a mail using DOS or VBS.

    twoeyed

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply