Parameterizing Sort Order

To practice what you’ve learned so far, try to provide a solution to the following task: write a stored procedure called usp_GetSortedShippers that accepts a column name from the Shippers table in the Northwind database as one of the inputs (@colname), and that returns the rows from the table sorted by the input column name. Assume also that you have a sort direction as input (@sortdir), with the value ′A′ representing ascending order and ′D′ representing descending order. The stored procedure should be written with performance in mind–that is, it should use indexes when appropriate (for example, a clustered or nonclustered covering index on the sort column).

Example 7-4 shows the first suggested solution for the task. ...

Get Inside Microsoft® SQL Server™ 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.