Dynamic ordering of database query results (2)

Source: Internet
Author: User
Tags expression sql server books sort
Dynamic | sort | data | Database Two, use column name as parameter


Another option is to have the query receive a column name in the form of a parameter. Listing 2 shows the modified getsortedshippers stored procedure. The case expression determines which column value SQL Server uses in the ORDER BY clause, based on the parameters received. Note that the expression in the ORDER BY clause does not appear in the select list. In the ANSI SQL-92 standard, expressions not specified in the select list are not allowed in the ORDER BY clause, but the ANSI SQL-99 standard allows. SQL Server has been allowing this usage.



"Listing 2: Use column name as parameter, first attempt"


CREATE PROC Getsortedshippers

@ColName as sysname

As


SELECT *

From shippers

ORDER BY

Case @ColName

When ' ShipperID ' THEN ShipperID

When ' CompanyName ' THEN CompanyName

When ' phone ' THEN phone

ELSE NULL

End





Now, let's try a new stored procedure, specifying the ShipperID column as a parameter:



EXEC getsortedshippers ' ShipperID '





Everything is fine at this time. However, when we view the CompanyName column as an argument to the stored procedure, it is no longer valid:



EXEC getsortedshippers ' CompanyName '





Take a closer look at the error message:



Server:msg 245, level, State 1, Procedure Getsortedshippers, line 5

Syntax error converting the nvarchar value ' Speedy

Express ' to a column of data type int.





It shows that SQL Server is trying to convert "Speedy Express" (nvarchar data type) to an integer value--Of course, this operation is unlikely to succeed. The error occurs because, according to the data type precedence rule, the data type of the highest priority in the case representation determines the data type of the return value of the expression. The data type precedence rule can be found in SQL Server books Online (BOL), which prescribes that the int data type has a higher precedence than the nvarchar data type. The preceding code requires SQL Server to sort the output according to CompanyName, CompanyName is the nvarchar data type. The return value of this case expression may be shipperid (int type), possibly companyname (nvarchar type), or phone (nvarchar type). Because the int type has a higher priority, the data type of the case expression return value should be int.


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.