SQL Script Output

If you use SQL scripts with Microsoft SQL Server, you may want to clean up the output to make your script log messages more readable. Here are some recommendations to beautify the output:

  • SET NOCOUNT ON -- suppress output of "rows affected" messages
  • @@ROWCOUNT -- get rows affected by previous statement
  • PRINT -- send a useful message to script output (can include @@ROWCOUNT, etc)

If you want to track script execution time, try printing a date/time string at key places during your script execution. There are many ways to run SQL scripts. From the command line you can use the popular “sqlcmd” utility. If you prefer the GUI, you can run your script within SQL Server Management Studio (SSMS).

If you’re printing a string including the row count, try something like this:

PRINT 'Congratulations, you got '+CAST(@@ROWCOUNT AS VARCHAR)+' records';
Advertisements

About notesbytom

Keeping technology notes on WordPress.com to free up my mind to solve new problems rather than figuring out the same ones repeatedly :-).
This entry was posted in Database and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s