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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access