Dynamic ordering of database query results (6)

Source: Internet
Author: User
Tags query
Dynamic | data | Database Now, if we call a stored procedure with one of any three column names as a parameter, the stored procedure can return the result correctly. Richard Romley proposed an ingenious approach, as shown in Listing 6. It no longer requires us to figure out the types of column data that might be involved. This method divides the ORDER BY clause into three separate case expressions, each dealing with a different column, avoiding problems caused by the ability of the case to return only one particular data type.

"Listing 6: Using column names as parameters, Romley proposed method"

ALTER PROC Getsortedshippers

@ColName as sysname



From shippers


Case @ColName when ' ShipperID '


Case @ColName when ' CompanyName '

THEN CompanyName ELSE NULL End,

Case @ColName when ' Phone '


By writing code in this way, SQL Server can return the appropriate data type for each case expression without requiring data type conversions. It should be noted, however, that the index is able to optimize the sorting operation only if the specified column does not need to be evaluated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.