Dynamic sorting of database query results (1)_PHP tutorial
Jul 13, 2016 pm 05:01 PM
A frequently asked question in public newsgroups is "How can I return a sorted output based on the parameters passed to a stored procedure?". With the help of some high-level experts, I've put together several solutions to this problem.
1. Use IF...ELSE to execute pre-written queries
For most people, the first thing that comes to mind may be: execute several pre-written queries through IF...ELSE statements one of the. For example, suppose you want to query the Northwind database to get a sorted list of shippers. The calling code specifies a column in the form of a stored procedure parameter, and the stored procedure sorts the output results based on this column. Listing 1 shows one possible implementation of such a stored procedure (GetSortedShippers stored procedure).
[Listing 1: Execute one of multiple pre-written queries with IF...ELSE]
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 method is that the code is very simple and easy to understand. The query optimizer of SQL Server can create a query optimization plan for each SELECT query to ensure that the code has optimal performance. The main disadvantage of this approach is that if the query requirements change, you must modify multiple independent SELECT queries - in this case, three.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to check your academic qualifications on Xuexin.com

12306 How to check historical ticket purchase records How to check historical ticket purchase records

How does Go language implement the addition, deletion, modification and query operations of the database?

Detailed tutorial on establishing a database connection using MySQLi in PHP

How does Hibernate implement polymorphic mapping?

Advanced sorting of PHP arrays: custom comparators and anonymous functions

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

An in-depth analysis of how HTML reads the database
