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

Microsoft SQL Server 2005 Developer’s Guide- P9 ppt

10 165 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 231,95 KB

Nội dung

Chapter 2: Developing with T-SQL 59 The code block that you can see at the top of this listing essentially creates a new table named dbo.FirstHalfDept. This table is based on the rows in the HumanResources .Department table where the DepartmentID is less than or equal to 8. The next code block creates a second new table named dbo.SecondHalfDept using the rows in the HumanResources.Department table where the DepartmentID is greater than 8. The UNION statement will then take these two results sets and join them back together into a single result set. The results of the union of the dbo.FirstHalfDept table and the dbo.SecondHalfDept table are shown in the following listing. As you can see, the UNION operation merged the two tables together back into a single table with the same contents as the original HumanResources.Department table that was used as a basis to create the other two tables: (8 row(s) affected) (8 row(s) affected) DepartmentID Name GroupName 1 Engineering Research and Development 2 Tool Design Research and Development 3 Sales Sales and Marketing 4 Marketing Sales and Marketing 5 Purchasing Inventory Management 6 Research and Development Research and Development 7 Production Manufacturing 8 Production Control Manufacturing 9 Human Resources Executive General and Administration 10 Finance Executive General and Administration 11 Information Services Executive General and Administration 12 Document Control Quality Assurance 13 Quality Assurance Quality Assurance 14 Facilities and Maintenance Executive General and Administration 15 Shipping and Receiving Inventory Management 16 Executive Executive General and Administration (16 row(s) affected) Using Subqueries A subquery is a query that’s nested inside of another T-SQL query. Subqueries can be nested within SELECT, INSERT, UPDATE, or DELETE statements. More information about using INSERT, UPDATE, and DELETE statements is presented later in this chapter. 60 Microsoft SQL Server 2005 Developer’s Guide The following example illustrates using a subquery to retrieve all of the names of the employees in the AdventureWorks database who have the title of Tool Designer: SELECT FirstName, LastName, e.Title FROM Person.Contact c Join HumanResources.Employee e On e.ContactID = c.ContactID WHERE EmployeeID IN (SELECT EmployeeID FROM HumanResources.Employee WHERE Title = 'Tool Designer') The SELECT statement specifies that the result set will contain three columns. The FirstName and LastName columns come from the Person.Contact table, while the Title column comes from the HumanResources.EmployeeID table. The Person. Contact table and the HumanResources.EmployeeID table are joined on the ContactID column. The subquery then further restricts the result set by specifying that only the rows from the HumanResources.EmployeeID table will be used where the value in the Title column is equal to ‘Tool Designer’. FirstName LastName Title Thierry D’Hers Tool Designer Janice Galvin Tool Designer (2 row(s) affected) NOTE In many cases, the same results that are produced using subqueries can also be produced using joins. Row-at-a-Time Processing Using Cursors T-SQL is a set-at-a-time language that is designed for dealing with sets of data at one time. However, there are circumstances where you may need to deal with the data contained in a table or result set on a row-by-row basis. Cursors are the T-SQL mechanism that enable single-row processing. Cursors limit scalability because they hold locks on the table while the cursor is open; however, they do provide a great deal of flexibility in dealing with individual results in a result set. The following example illustrates using a cursor to process a result set based on the HumanResources.Department table one row at a time. Chapter 2: Developing with T-SQL 61 DECLARE @ThisDept INT DECLARE DeptCursor CURSOR FOR SELECT DepartmentID from HumanResources.Department OPEN DeptCursor WHILE @@FETCH_STATUS = 0 BEGIN PRINT 'Processing Department: ' + RTRIM(CAST(@ThisDept AS VARCHAR(10))) FETCH NEXT FROM DeptCursor INTO @ThisDept END CLOSE DeptCursor DEALLOCATE DeptCursor At the top of this listing you can see where two variables are declared. The first variable, named @ThisDept, will be used to store the value of the DepartmentID column that’s returned by the cursor. The next variable is a handle for the cursor named DeptCursor. The rows this cursor will operate over are defined in the following SELECT statement, which returns just the DepartmentID column for all of the rows in the HumanResources.Department table. After the cursor has been defined, it’s then opened using the OPEN statement, and then a WHILE loop is used to process all of the rows returned from the HumanResources.Department table. The WHILE loop will continue processing until the value of the @@FETCH_STATUS variable is not equal to zero, indicating that all of the rows have been read from the result set. BEGIN and END statements delimit the block of T-SQL statements that will perform the processing. In this example a simple PRINT statement is used to print the value of the DepartmentID column read, and then a FETCH NEXT operation is used to read the next row from the table. The output from this cursor processing example is listed here: Processing Department: 1 Processing Department: 2 Processing Department: 3 Processing Department: 4 Processing Department: 5 Processing Department: 6 Processing Department: 7 Processing Department: 8 Processing Department: 9 Processing Department: 10 Processing Department: 11 Processing Department: 12 Processing Department: 13 Processing Department: 14 Processing Department: 15 Processing Department: 16 62 Microsoft SQL Server 2005 Developer’s Guide SQL Server 2005 supports a number of different cursor types. The most common ones are presented in Table 2-3. For a complete listing, refer to Books On-Line. Using Common Table Expressions (CTE) Another new T-SQL feature is support for common table expressions (CTEs). CTEs are a lot like views; however, they are embedded in a query. The main reason Microsoft Cursor Type Description INSENSITIVE Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb. Modifications made to base tables are not reflected in the cursor. SCROLL Specifies that all fetch options (FIRST, LAST, PRIOR, NEXT, RELATIVE, ABSOLUTE) are available. SCROLL cannot be specified if FAST_FORWARD is also specified. READ ONLY Defines the cursor as read-only. The cursor cannot be referenced in a WHERE CURRENT OF clause in an UPDATE or DELETE statement. UPDATE Defines updatable columns that can be used with the cursor. FORWARD_ONLY Specifies that the cursor can only be scrolled from the first to the last row. FETCH NEXT is the only supported fetch option. STATIC Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb. Modifications made to base tables are not reflected in the cursor. KEYSET Specifies that the membership of the rows in the cursor is fixed when the cursor is opened. The set of keys that uniquely identify the rows are built into a table in tempdb. DYNAMIC Defines a cursor that reflects all data changes made to the rows in its result set. The data values, order, and membership of the rows can change on each fetch. FAST_FORWARD Specifies a FORWARD_ONLY, READ_ONLY cursor with performance optimizations enabled. FAST_FORWARD cannot be specified if SCROLL or FOR_UPDATE is specified. READ_ONLY Defines the cursor as read-only. The cursor cannot be referenced in a WHERE CURRENT OF clause in an UPDATE or DELETE statement. SCROLL_LOCKS Specifies that positioned updates made through the cursor are guaranteed to succeed. SQL Server locks the rows as they are read into the cursor to ensure their availability for updating. OPTIMISTIC Specifies that positioned updates made through the cursor do not succeed if the row has been updated, since it was read into the cursor. SQL Server does not lock rows. Instead it uses timestamp column values or a checksum value to determine whether the row was modified. If the row was modified, the attempted update will fail. Table 2-3 Cursor Types Chapter 2: Developing with T-SQL 63 introduced CTEs to SQL Server 2005 is to provide a mechanism for handling recursive queries. Recursion is achieved by the fact that a CTE is allowed to refer to itself. To avoid the possibility of overwhelming the system with a poorly constructed recursive query, SQL Server implements a server-wide limit on the maximum depth of recursion allowed, with a default maximum of 100 levels. A CTE is implemented as a part of the WITH keyword and can be used with SELECT, INSERT, UPDATE, and DELETE statements. To implement recursive queries using the new CTE, you must use a special syntax as shown in the simple code example that follows. This example performs a simple recursive query using the HumanResources.Employee table in the example AdventureWorks database: USE AdventureWorks WITH EmployeeChart(EmployeeID, ManagerID, Title) AS (SELECT EmployeeID, ManagerID, Title FROM HumanResources.Employee WHERE EmployeeID = 3 UNION ALL SELECT L2.EmployeeID, L2. ManagerID, L2.Title FROM HumanResources.Employee AS L2 JOIN EmployeeChart ON L2.ManagerID = EmployeeChart.EmployeeID) SELECT * FROM EmployeeChart To use a CTE, you first write a WITH clause, which you use to name the CTE and specify the columns to bind to a SELECT statement. There must be a semicolon in front of the WITH keyword if it is not the first statement in a batch. The first SELECT statement is called the anchor member, and it must not refer to itself. In this case, it retrieves the EmployeeID, ManagerID, and Title columns from the AdventureWorks Employee table. The second SELECT statement references the CTE and is called the recursive member. In this case it retrieves the same columns and is joined to the anchor member on the ManagerID column. You can see the results of this CTE in the following listing: EmployeeID ManagerID Title 3 12 Engineering Manager 4 3 Senior Tool Designer 9 3 Design Engineer 11 3 Design Engineer 158 3 Research and Development Manager 263 3 Senior Tool Designer 64 Microsoft SQL Server 2005 Developer’s Guide 267 3 Senior Design Engineer 270 3 Design Engineer 5 263 Tool Designer 265 263 Tool Designer 79 158 Research and Development Engineer 114 158 Research and Development Engineer 217 158 Research and Development Manager (13 row(s) affected) Using PIVOT and UNPIVOT The addition of the PIVOT and UNPIVOT relational operators is another new feature found in SQL Server 2005’s T-SQL. The new PIVOT and UNPIVOT operators are most useful for OLAP scenarios where you’re dealing with tabular data rather than relational data. The PIVOT operator transforms a set of rows into columns. As you might expect, the UNPIVOT operator reverses the PIVOT operator, transforming the pivoted columns back into rows. However, depending on the situation, the UNPIVOT operation may not exactly reverse the PIVOT operation. This situation occurs because the PIVOT operation is often set up such that it will omit certain values. If a value is omitted during the PIVOT operation, it obviously cannot be unpivoted. Therefore, the UNPIVOT operator doesn’t always result in an exact mirror image of the original pivot condition. Using SQL Server 2005’s new PIVOT operator, you can transform this result set, which lists each year vertically, into a result set that lists the years horizontally for each customer and sums up the number of orders for each year. The sample PIVOT operation is shown in the following listing: SELECT VendorID, [244] AS POCount1, [231] AS POCount2, [266] AS POCount3 FROM (SELECT PurchaseOrderID, EmployeeID, VendorID FROM Purchasing.PurchaseOrderHeader) p PIVOT ( COUNT (PurchaseOrderID) FOR EmployeeID IN ( [244], [231], [266] ) ) AS pvt ORDER BY VendorID Here the PIVOT operation is used with the SELECT statement to create a new result set. The first value of the pivot operator identifies the value that will be placed in the pivot column. In this example the COUNT(OrderID) aggregation sums up the number of Chapter 2: Developing with T-SQL 65 orders for each pivot value. The FOR keyword identifies the column whose values will be pivoted. This example shows the pivot operation being performed on the OrderYear column. The values identified by the IN keyword list are the values from the pivoted column that will be used as column headings. You can see the pivoted result set in the following listing: CustomerID 2000 2001 2002 2003 2004 1 3 2 1 1 1 Warning: Null value is eliminated by an aggregate or other SET operation. (1 row(s) affected) Modifying Data SQL’s Data Manipulation Language (DML) provides data retrieval and update capabilities for a relational database system such as SQL Server. In this part of the chapter you will see how to use the Insert, Update, and Delete statements of DML. The Insert statement inserts new rows into tables or views. The Update statement is used to modify column values in existing rows. The Delete statement clears existing data from rows in a table or view. You’ll also see how to use the BULK INSERT statement to load data from a data file into a table and how to commit or roll back database actions using transactions. Insert The INSERT statement is used to insert data into a table or a view. You can insert data into your tables several different ways. You can insert data into a table by simply specifying the table name, the columns into which you are inserting the data, and the actual value of the data to insert. You can insert data into a table by using a SELECT statement inside the INSERT statement to retrieve data from another table and store the results into your table. You can also use the EXECUTE statement inside the INSERT statement to execute a stored procedure and store the results in your table. For the examples in the following sections of this chapter, we will create a table called OrderSum. The code for creating our example table is listed here: CREATE TABLE OrderSum (OrderID INT, CustomerID INT, OrderDate NCHAR(10)) The example table OrderSum has two integer columns, OrderID and CustomerID, and one character column, OrderDate. 66 Microsoft SQL Server 2005 Developer’s Guide INSERT . . . VALUES To simply insert data into a table, you can specify the table name, columns, and values in the INSERT statement. The following example inserts one row of data into the example OrderSum table: INSERT INTO OrderSum (OrderID, CustomerID, OrderDate) VALUES (100, 1, '01/28/2005') The results from the insert are shown here: SELECT * FROM OrderSum OrderID CustomerID OrderDate 100 1 01/28/2005 (1 row(s) affected) When you insert a value into every column of the table, you can omit the list of column names from the INSERT statement, but for clarity and to reduce errors, it is recommended that you include the list of column names. INSERT . . . SELECT Another way to insert data into your tables is to use a nested SELECT statement within the INSERT statement. Using the SELECT statement, you can retrieve data from another table and populate your table with the results. The code for using a nested SELECT statement is shown here: INSERT OrderSum (OrderID, CustomerID, OrderDate) SELECT SalesOrderID, CustomerID, CONVERT(nchar(10), OrderDate, 101) FROM Sales.SalesOrderHeader WHERE SalesOrderID > 75120 As you can see, the SELECT statement is selecting three columns from the Sales .SalesOrderHeader table where the SalesOrderID value is greater than 75120, and the result of the select is inserted into the OrderSum table. The OrderSum table is shown here: Chapter 2: Developing with T-SQL 67 SELECT * FROM OrderSum OrderID CustomerID OrderDate 100 1 01/28/2005 75121 15251 07/31/2004 75122 15868 07/31/2004 75123 18759 07/31/2004 (4 row(s) affected) INSERT . . . TOP Using the TOP keyword, you can specify a certain number or percent of rows to insert into your table. What follows is an example of using the TOP keyword to insert only the top five rows into the OrderSum table from the Sales.SalesOrderHeader table: INSERT TOP (5) INTO OrderSum (OrderID, CustomerID, OrderDate) SELECT SalesOrderID, CustomerID, CONVERT(nchar(10), OrderDate, 101) FROM Sales.SalesOrderHeader The results of the TOP keyword insert are shown here: OrderID CustomerID OrderDate 100 1 01/28/2005 75121 15251 07/31/2004 75122 15868 07/31/2004 75123 18759 07/31/2004 43659 676 07/01/2001 43660 117 07/01/2001 43661 442 07/01/2001 43662 227 07/01/2001 43663 510 07/01/2001 (9 row(s) affected) INSERT . . . EXECUTE The next example shows how to use an EXECUTE expression in the INSERT statement to execute a stored procedure that returns rows to be inserted into 68 Microsoft SQL Server 2005 Developer’s Guide the table. The stored procedure, usp_GetOneSalesOrder, takes one input parameter and retrieves a row from the Sales.SalesOrderHeader table. The code to create the stored procedure is shown here: CREATE PROCEDURE usp_GetOneSalesOrder (@InID int) AS (SELECT SalesOrderID, CustomerID, CONVERT(nchar(10), OrderDate, 101) FROM Sales.SalesOrderHeader WHERE SalesOrderID = @inID) The next code listing shows how to call the stored procedure in the INSERT statement and the results of the insert: INSERT OrderSum (OrderID, CustomerID, OrderDate) EXECUTE usp_GetOneSalesOrder 43670 SELECT * FROM OrderSum OrderID CustomerID OrderDate 100 1 01/28/2005 75121 15251 07/31/2004 75122 15868 07/31/2004 75123 18759 07/31/2004 43659 676 07/01/2001 43660 117 07/01/2001 43661 442 07/01/2001 43662 227 07/01/2001 43663 510 07/01/2001 43670 504 07/01/2001 (10 row(s) affected) Bulk Insert You can use a BULK INSERT statement to load an entire database table or view from a data file. In SQL Server 2005, BULK INSERT has been enhanced to enforce stricter data validation and data checks of data read from a file. Forms of invalid data, such as . Department: 14 Processing Department: 15 Processing Department: 16 62 Microsoft SQL Server 2005 Developer’s Guide SQL Server 2005 supports a number of different cursor types. The most common ones. using INSERT, UPDATE, and DELETE statements is presented later in this chapter. 60 Microsoft SQL Server 2005 Developer’s Guide The following example illustrates using a subquery to retrieve all. Design Engineer 158 3 Research and Development Manager 263 3 Senior Tool Designer 64 Microsoft SQL Server 2005 Developer’s Guide 267 3 Senior Design Engineer 270 3 Design Engineer 5 263 Tool Designer 265

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

TỪ KHÓA LIÊN QUAN