Dynamic ordering of database query results (1) _php tutorial

Source: Internet
Author: User
In public newsgroups, a recurring question is "How can I return a sorted output based on the parameters passed to the stored procedure?" ”。 With the help of some high-level experts, I have sorted out several solutions to this problem.
First, with the If ... Else execute a pre-written query
For most people, the first thing you might think of is: through If ... Else statement, execute one of several pre-written queries. For example, suppose you want to query from the Northwind database for a sorted list of shippers (Shipper), the calling code specifies a column in the form of stored procedure parameters, and the stored procedure sorts the output based on that column. Listing 1 shows a possible implementation of this stored procedure (Getsortedshippers stored procedure).
"Listing 1: With If ... Else executes one of several pre-written queries in the "
CREATE PROC Getsortedshippers
@OrdSeq as int
As
IF @OrdSeq = 1
SELECT * from Shippers ORDER by ShipperID
ELSE IF @OrdSeq = 2
SELECT * from Shippers ORDER by CompanyName
ELSE IF @OrdSeq = 3
SELECT * from Shippers ORDER by Phone

The advantage of this approach is that the code is simple and easy to understand, and the SQL Server query optimizer can create a query optimization plan for each select query, ensuring that the code has optimal performance. The main disadvantage of this approach is that if the requirements of the query change, you have to modify multiple independent select queries--here are three.

http://www.bkjia.com/PHPjc/631158.html www.bkjia.com true http://www.bkjia.com/PHPjc/631158.html techarticle in public newsgroups, a recurring question is "How can I return a sorted output based on the parameters passed to the stored procedure?" ”。 With the help of some high-level experts, ...

  • 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.