TAILIEUCHUNG - Hướng dẫn học Microsoft SQL Server 2008 part 25

Trường hợp các biểu thức biểu CASE của SQL Server là một phương tiện linh hoạt và tuyệt vời của việc xây dựng các biểu thức năng động. Nếu bạn là một lập trình viên, không có nghi ngờ bạn sử dụng lệnh trường hợp các ngôn ngữ khác. Các biểu hiện CASE SQL, tuy nhiên, là khác nhau. Nó không được sử dụng cho lưu lượng chương trình kiểm soát, nhưng thay vào đó là hợp lý xác định giá trị của một biểu thức dựa trên một điều kiện. . | Part II Manipulating Data With Select Case expressions SQL Server s CASE expression is a flexible and excellent means of building dynamic expressions. If you re a programmer no doubt you use the case command in other languages. The SQL CASE expression however is different. It s not used for programmatic flow of control but rather to logically determine the value of an expression based on a condition. Best Practice When programmers write procedural code it s often because part of the formula changes depending on the data. To a procedural mind-set the best way to handle this is to loop through the rows and use multiple IF statements to branch to the correct formula. However using a CASE expression to handle the various calculations and executing the entire operation in a single query enables SQL Server to optimize the process and make it dramatically faster. Because the case expression returns an expression it may be used anywhere in the SQL DML statement SELECT INSERT UPDATE DELETE where an expression may be used including column expressions join conditions where conditions having conditions in the ORDER BY or even embedded in a longer expression. A case expression can even be used mid-expression to create a dynamic formula - very powerful. The CASE statement has two forms simple and searched described in the following sections. Simple case With the simple CASE the variable is presented first and then each test condition is listed. However this version of CASE is limited in that it can perform only equal comparisons. The CASE expression sequentially checks the WHEN conditions and returns the THEN value of the first true WHEN condition. In the following example based on the OBXKites database one CustomerType is the default for new customers and is set to true in the IsDefault column. The CASE expression compares the value in the default column with each possible bit setting and returns the character string default type or possible based on the bit setting USE .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.