SQL Server Results Set

Bill Ehrbar 21 Reputation points
2021-01-28T20:40:17.88+00:00

In the below screenshot, is there any means to "label" a SQL command so that if a service of commands are ran, you know what each result is for (using SQL Server Management Studio):

61565-image.png

I.e.,
(1 row affected) could be something like "Insert STP Entry: (1 row affected)"

We have some scripts that are long, but get reran a lot and would like to be able to easily determine what each result represents.

Thanks,
Bill

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,733 questions
{count} votes

Accepted answer
  1. Williams, Jeffrey A 481 Reputation points
    2021-01-28T22:00:16.893+00:00

    You can use RAISERROR to return messages - and use SET NOCOUNT ON at the beginning to turn off the default:

    Set Nocount On;  
    Declare @stepName varchar(30) = '';  
      
    <some other code here>  
      
    Set @stepName = 'Insert STP Entry';  
      
    <Insert STP Entry code here>  
      
    Raiserror('%-*s: %i', -1, -1, 30, @stepName, @@rowcount) With nowait;  
    

    Results will be displayed as:

    Insert STP Entry : 1 rows affected

    Lookup RAISERROR for additional ways you can format the results.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bill Ehrbar 21 Reputation points
    2021-01-29T14:21:45.777+00:00

    Both of the above answers work. The first one with the print statements works but the output is a little confusing by the new line space:
    Insert into STP History Table

    (1 row affected)
    Insert into File History Table

    (1 row affected)
    Delete from STP Table

    (1 row affected)
    Delete from FileTable

    (1 row affected)

    The second method above was a lot cleaner but takes a little more work:

    Insert STP Batch into History Table : 0
    Insert Files into History Table : 0
    Delete Files : 0
    Delete STP : 0