1. Trang chủ
  2. » Công Nghệ Thông Tin

Hướng dẫn học Microsoft SQL Server 2008 part 24 docx

10 268 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

Nielsen c08.tex V4 - 07/21/2009 12:37pm Page 192 Part II Manipulating Data with Select TourName Amazon Trek Appalachian Trail Bahamas Dive Gauley River Rafting Outer Banks Lighthouses Whereas the first query returned 16 rows, the DISTINCT predicate in the second query eliminated the duplicate rows and returned only five unique rows. SQL Server’s DISTINCT is different from MS Access’ distinctrow, which eliminates dupli- cates based on data in the source table(s), not duplicates in the result set of the query. Select DISTINCT functions as though a GROUP BY clause (discussed in Chapter 12, ‘‘Aggregating Data’’) exists on every output column. Of course, using DISTINCT is based on the query’s requirements, so there may be no choice; just be aware that depending on the size and mix of the data, there may be a performance impact. Top () By definition, SELECT works with sets of data. Sometimes, however, it’s only the first few rows from the set that are of interest. For these situations, SQL Server includes several ways to filter the results and find the top rows. As mentioned earlier, SQL Server will return all the rows from the SELECT statement by default. The optional TOP() predicate tells SQL Server to return only a few rows (either a fixed number or a per- centage) based upon the options specified, as shown in Figure 8-4. A variable can be passed to TOP(). The older syntax for TOP() did not include the parentheses and did not accept a variable. The newer syntax, with the parentheses, was introduced with SQL Server 2005 and is the best practice moving forward. TOP() works hand-in-hand with ORDER BY.It’stheORDER BY clause that determines which rows are first. If the SELECT statement does not have an ORDER BY clause, then the TOP() predicate still works by returning an unordered sampling of the result set. The OBXKites sample database is a good place to test the TOP() predicate. The following query finds the top 3 percent of prices in the price table. The price table allows each product to have multiple prices, according to the effective date: SELECT TOP (3) PERCENT p.Code, p.ProductName, pr.Price, CONVERT(VARCHAR(10),pr.EffectiveDate,1) AS PriceDate FROM Product AS p JOIN Price AS pr ON p.ProductID = pr.ProductID ORDER BY pr.Price DESC; 192 www.getcoolebook.com Nielsen c08.tex V4 - 07/21/2009 12:37pm Page 193 Introducing Basic Query Flow 8 Result: ProductCode ProductName Price PriceDate 1018 Competition Pro 48" 284.9500 05/01/01 1018 Competition Pro 48" 264.9500 05/01/02 1017 Competition 36" 245.9500 05/20/03 1017 Competition 36" 225.9500 05/01/01 The next query locates the three lowest prices in the price table: SELECT TOP (3) p.Code, p.ProductName, pr.Price, CONVERT(VARCHAR(10),pr.EffectiveDate,1) AS PriceDate FROM Product AS p JOIN Price AS pr ON p.ProductID = pr.ProductID ORDER BY pr.Price; Result: ProductCode ProductName Price PriceDate 1044 OBX Car Bumper Sticker .7500 05/01/01 1045 OBX Car Window Decal .7500 05/20/01 1045 OBX Car Window Decal .9500 05/20/02 The query looks clean and the result looks good, but unfortunately it’s wrong. If you look at the raw data sorted by price, you’ll actually see three rows with a price of 95 cents. The WITH TIES option solves this problem, as described in the following section. Best Practice B y the very nature of the formatting, computer-generated data tends to appear correct. Unit testing the query against a set of data with known results is the only way to check its quality. The number of rows returned by TOP() may be controlled using a variable: SELECT TOP (@Variable) For more details about using variables, turn to Chapter 21, ‘‘Programming with T-SQL.’’ The with ties option The WITH TIES option is important to the TOP() predicate. It allows the last place to include multi- ple rows if those rows have equal values in the columns used in the ORDER BY clause. The following 193 www.getcoolebook.com Nielsen c08.tex V4 - 07/21/2009 12:37pm Page 194 Part II Manipulating Data with Select version of the preceding query includes the WITH TIES option and correctly results in five rows from a TOP 3 predicate: SELECT TOP (3) WITH TIES p.ProductCode, p.ProductName, pr.Price, CONVERT(varchar(10),pr.EffectiveDate,1) AS PriceDate FROM Product AS p JOIN Price AS pr ON p.ProductID = pr.ProductID ORDER BY pr.Price; Result: ProductCode ProductName Price PriceDate 1044 OBX Car Bumper Sticker .7500 05/01/01 1045 OBX Car Window Decal .7500 05/20/01 1045 OBX Car Window Decal .9500 05/20/02 1041 Kite Fabric #6 .9500 05/01/01 1042 Kite Fabric #8 .9500 05/01/01 If you are moving from Access to SQL Server, be aware that Access, by default, adds the WITH TIES option to the TOP() predicate automatically. An alternative to TOP() is the SET ROWCOUNT command, which limits any DML command to affecting only n number of rows until it’s turned off with SET ROWCOUNT 0. The issue is that ROWCOUNT isn’t portable either, and it’s been deprecated for INSERT, UPDATE,andDELETE in SQL Server 2008. Selecting a random row There are times when a single random row is needed. I use this technique when populating a table with random names. Using the TOP(1) predicate will return a single row, and sorting the result set by newid() randomizes the sort. Together they will return a random row each time the query is executed. There is a performance cost to using TOP(1) and newid(). SQL Server has to add a unique identifier to every row and then sort by the uniqueidentifier. An elegant solution is to add a tablesample option to the table when randomly selecting a single row from a very large table. Tablesample works by randomly selecting pages within the table and then returning every row from those pages from the from clause: SELECT TOP(1) LastName FROM dbo.LastNames TableSample (10 Percent) ORDER BY NewID(); 194 www.getcoolebook.com Nielsen c08.tex V4 - 07/21/2009 12:37pm Page 195 Introducing Basic Query Flow 8 Summary A wealth of power and flexibility is hidden in the simple SELECT command. The key to understanding the SQL query is understanding that the query is declarative — you’re only phrasing a question. The Query Optimizer figures out how to execute the query, so SQL allows for some flexibility in the development style of the query. A few of the key points from this chapter include the following: ■ Think through the query in the logical flow of the query, not the syntax flow for the query. ■ The FROM clause can assemble data from ten different types of data sources. Think creatively about where you can find data for your query. ■ Never use SELECT *. ■ Aliases are a good thing, and always use the AS. ■ Be intentional about the WHERE clause. Use parentheses. Keep the expressions away from the source column. ■ Never trust the sort order to the physical order of the data on the disk. If the data needs to be sorted, then use an ORDER BY. From this introduction, the next eight chapters add incrementally more advanced features that augment the power of SELECT: incorporating complex expressions, multiple types of joins, subqueries, and groupings. Welcome to the set-based power of SQL. 195 www.getcoolebook.com Nielsen c08.tex V4 - 07/21/2009 12:37pm Page 196 www.getcoolebook.com Nielsen c09.tex V4 - 07/21/2009 12:40pm Page 197 Data Types, Expressions, and Scalar Functions IN THIS CHAPTER Working with expressions and scalar functions Using logic within a query Working with nulls, strings, and dates W hen my son, David, was younger he built incredible monster trucks and gizmos out of K’NEX construction pieces. If you aren’t familiar with K’NEX, do a Google image search and see the wild things that kids can build with it. What makes K’NEX cool is that nearly any piece can plug into any other piece. This interconnectivity makes K’NEX flexible. In the same way, the interconnectiv- ity of SQL expressions and functions makes SQL so flexible and powerful. Expressions can retrieve data from a subquery, handle complex logic, convert data types, and manipulate data. If the secret to being a competent SQL database developer is mastering SQL queries, then wielding expressions and scalar functions is definitely in the arsenal. An expression is any combination of constants, functions, or formulas that returns a single value. Expressions may be as simple as a hard-coded number, or as complex as a case expression that includes several formulas and functions. Expressions may be employed in several places within the SQL syntax. Nearly anywhere a value may be used, an expression may be used instead. This includes column values, JOIN ON clauses, WHERE and HAVING clauses, and ORDER BY columns. Expressions can’t be substituted for object names such as table names or column names. Building Expressions You can construct SQL expressions from a nearly limitless list of constants, variables, operators, and functions, as detailed in Table 9-1. 197 www.getcoolebook.com Nielsen c09.tex V4 - 07/21/2009 12:40pm Page 198 Part II Manipulating Data With Select The syntax of SQL keywords is not case sensitive. The convention is to use keywords in all upper case. I sometimes will use lower case for keywords for a subquery to improve readability. Depending on the collation setting of the server or database, database, table, and column names, and even the data itself, might be case sensitive. TABLE 9-1 Building Expressions Expression Components Examples Numeric constants 1, 2, 3, -17, -100 String literals ’LastName’, ‘Employee: ‘, ‘Lifes Great!’ Dates ’1/30/1980’, ‘January 30, 1980’, ‘19800130’ Mathematical operators (in order of prece- dence) *, /, % (remainder), +, - String operator (concatenation) + Bitwise operators (in order of precedence) not ∼,and&,or|, exclusive or ˆ Columns LastName, PrimaryKeyID Case expressions CASE Column1 WHEN 1 THEN ‘on’ ELSE ‘off’ END AS Status Subqueries (Select 3) User-defined variables @MyVariable System functions @@Error Scalar functions GetDate(), Radians() User-defined functions dbo.MyUDF() Subqueries are covered in Chapter 11, Including Data with Subqueries and CTEs. Variables are discussed in Chapter 21, Programming with T-SQL. User-defined functions are detailed in Chapter 25, Building User-Defined Functions. Operators While the meaning of many of these expression constants, operators, and expressions is obvious and common to other programming languages, a few deserve special mention. 198 www.getcoolebook.com Nielsen c09.tex V4 - 07/21/2009 12:40pm Page 199 Data Types, Expressions, and Scalar Functions 9 The division mathematical operator (/) is a very common source of errors when integers are divided. This is because there is an implicit truncation of values. For instance, 17/9 will give a result of 1 although it is almost 2 (which 18/9 would yield). The modulo mathematical operator (%) returns only the remainder of the division. The floor() (that’s ‘‘deck’’ for sailors) and ceiling() mathematical functions, which return the integer rounded down or up, respectively, are related to it. The floor() function is the SQL Server equivalent of the BASIC int() function: SELECT 15%4 AS Modulo, FLOOR(1.25) AS [Floor], CEILING(1.25) AS [Ceiling]; Result: Modulo Floor Ceiling 312 The + operator is used for both mathematical expressions and string concatenation. This operator is different from the Visual Basic symbol for string concatenation, the ampersand (&): SELECT 123 + 456 AS Addition, ‘abc’ + ‘defg’ AS Concatenation; Result: Addition Concatenation 579 abcdefg Data from table columns and string literals may be concatenated to return custom data: Use OBXKites SELECT ‘Product: ’ + ProductName AS Product FROM Product; Result: Product Product: Basic Box Kite 21 inch Product: Dragon Flight Product: Sky Dancer Bitwise operators The bitwise operators are useful for binary manipulation. These aren’t typically used in transactional databases, but they can prove useful for certain metadata operations. For example, one way to determine which columns were updated in a trigger (code that is executed as the result of a data insert, update, or delete, as covered in Chapter 26, ‘‘Creating DML Triggers’’) is to inspect the columns_updated() function, which returns a binary representation of those columns. The trigger code can test columns_updated() using bitwise operations and respond to updates on a column-by- column basis. 199 www.getcoolebook.com Nielsen c09.tex V4 - 07/21/2009 12:40pm Page 200 Part II Manipulating Data With Select Boolean bit operators (and, or, and not) are the basic building blocks of digital electronics and binary programming. Whereas digital-electronic Boolean gates operate on single bits, these bitwise operators work across every bit of the integer family data type ( int, smallint, tinyint,andbit)values. Boolean and A Boolean ‘‘and’’ (represented by the ampersand character, &) returns a value of true only if both inputs are true (or 1 for mathematical bit operations). If either or both are false (or 0 for mathematical bit operations), then the ‘‘and’’ will return a value of 1, as follows: SELECT 1 & 1; Result: 1 Another ‘‘and’’ example: SELECT 1 & 0; Result: 0 ‘‘And’’ing two integers is illustrated as follows: decimal 3 = binary 011 decimal 5 = binary 101 3 AND 5 decimal 1 = binary 001 SELECT 3 & 5; Result: 1 Boolean or The Boolean OR operator, the vertical pipe character (|), returns true if either input is true: SELECT 1 | 1; Result: 1 The following SELECT statement combines a set (true or 1) and a cleared (false or 0) bit using the bitwise or operator: SELECT 1 | 0; Result: 1 OR ing two integers can be illustrated as follows: decimal 3 = binary 011 200 www.getcoolebook.com Nielsen c09.tex V4 - 07/21/2009 12:40pm Page 201 Data Types, Expressions, and Scalar Functions 9 decimal 5 = binary 101 3OR5 decimal 7 = binary 111 SELECT 3 | 5; Result: 7 Boolean exclusive or The ‘‘exclusive or’’ (XOR) bitwise operator, the carat ( ˆ ), returns a value of true if either input is true, but not if both are true. The operator is shown here: SELECT 1 ˆ 1; Result: 0 A set bit XORed with a cleared bit results in a set bit: SELECT 1 ˆ 0; Result: 1 XORing two integers can be illustrated as follows: decimal 3 = binary 011 decimal 5 = binary 101 3OR5 decimal 6 = binary 110 Bitwise not The last bitwise operator, denoted by the tilde (∼), is a bitwise NOT function. This bitwise ‘‘not’’ is a lit- tle different. The ‘‘not’’ performs a logical bit reversal for every bit in the expression. The result depends on the data length of the expression. For example, the bitwise ‘‘not’’ of a set bit is a cleared bit: DECLARE @A BIT; SET @A = 1; SELECT ∼@A; Result: 0 The bitwise ‘‘not’’ is not suitable for use with Boolean expressions such as IF conditions. The following code, for example, is invalid: SELECT * FROM Product WHERE ∼(1=1); Note that the ‘‘not’’ operator also serves as the one’s complement operator. The system known as one’s complement can be used to represent negative numbers. The one’s complement form of a negative binary number is the bitwise NOT applied to it — the complement of its positive counterpart. 201 www.getcoolebook.com . that are of interest. For these situations, SQL Server includes several ways to filter the results and find the top rows. As mentioned earlier, SQL Server will return all the rows from the SELECT. is that ROWCOUNT isn’t portable either, and it’s been deprecated for INSERT, UPDATE,andDELETE in SQL Server 2008. Selecting a random row There are times when a single random row is needed. I use this. return all the rows from the SELECT statement by default. The optional TOP() predicate tells SQL Server to return only a few rows (either a fixed number or a per- centage) based upon the options

Ngày đăng: 04/07/2014, 09:20

TỪ KHÓA LIÊN QUAN