Using case Syria to do SQL's motion arrangement

Source: Internet
Author: User
SQL Server uses a random way to read data, so the user will use an order clause to arrange the data, and in this article, we're going to come up with a
How to use the case to do motion arrangement

ORDER BY clause

Let's take a look at the general use of the results of the Select, in this article for example I will use SQL Server's North Wind Library, please open Query Analyzer, execute
Yes

Use Northwind
Go
Select CompanyName, ContactName, ContactTitle
From Customers

The results may be ranked according to the data input, which is that you want to follow your own rules, you need to use the ORDER BY clause, such as the previous
For example, what do you want to do according to the company name?

Select CompanyName, ContactName, ContactTitle
From Customers
ORDER BY CompanyName

Case Syria statement

Case Syria is appropriate to control the flow of the program, so we have to use the case Syria to do t-SQL logic control, to see the language of the Danish case

Case <input Expression>
When <when expression> Then <result expression>
[... n]
[Else <result Expression>]

It's not enough to look at the language, now with an example to practice, we still use the North Wind data library, suppose we want to put the staff (Employees) data table in English
Called the Taiwan style, you can do it

Select FirstName, titleofcourtesy, COURTESYINTC =
Case TitleOfCourtesy
When ' Ms. ' Then ' water Girl '
When ' Dr ' Then ' Dr. Bo '
When ' Mr. ' Then '
When ' Mrs. ' Then ' lady '
Else ' sir/Madam '
End
From Employees

Motion arrangement

Once we understand the basic language, we can start learning how to use ORDER by plus case Syria to do the dynamic Data arrangement.

Declare @OrderKey TinyInt
Set @OrderKey = 2

Select CompanyName, ContactName, ContactTitle
From Customers
ORDER BY case
When @OrderKey = 1 Then CompanyName
When @OrderKey = 2 Then ContactName
Else ContactTitle
End

But it doesn't really work, at least in the ASP, so let's do a program

Create Procedure Sp_selectcustomersdynamicorder
@OrderKey = Null
As
Select CompanyName, ContactName, ContactTitle
From Customers
ORDER BY case
When @OrderKey = 1 Then CompanyName
When @OrderKey = 2 Then ContactName
Else ContactTitle
End

Now you can directly call this Stored Procedure

EXEC Sp_selectcustomersdynamicorder
EXEC Sp_selectcustomersdynamicorder 1
EXEC Sp_selectcustomersdynamicorder 2

Is it a lot nicer to press?

Hopefully this article will help you!


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.