SQL | Archived exercise | Use vbl to remember and redisplay @@rowcount

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> SQL  (198 exercises)
Version ==> Any version of SQL Server
Topic ==> Archived  (70 exercises)
Level ==> Average difficulty
Subject ==> SQL training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

If you haven't already done so, run the stored procedure in the above folder to generate a database of the world's leading websites.

Our task is to customise the standard (n row(s) affected) SQL message which appears after running a query.  To start this, first create a simple SELECT statement to list out all of the columns and rows in the tblWebsite table:

SQL exercise - Variables (image 1)

This will list out 122 rows

When you run this query, it should produce the following message:

SQL exercise - Variables (image 2)

Your task is to replace this with a customised message

Add the following line of code (as always, no need to bother with the comment) to your stored procedure:

SQL exercise - Variables (image 3)

This will suppress the row count message above

Now amend your stored procedure so that it summarises the rows affected as follows (using the hint below):

SQL exercise - Variables (image 4)

The new version looks similar, but it has a heading, and the wording is subtly different

To do this, you'll need to declare a variable and set it equal to @@rowcount - otherwise the PRINT statements will reset the row count, and you'll get the wrong answer!

When your stored procedure is working, optionally save the query which generates it as RowCountWebsites.sql, then close it down.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post