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

Microsoft SQL Server 2000 Programming by Example phần 9 doc

71 426 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 71
Dung lượng 1,1 MB

Nội dung

Chapter 13. Maintaining Data Consistency: Transactions and Locks 553 Start a transaction BEGIN TRAN Get OBJECT_ID to interpret the sp_lock output SELECT OBJECT_ID('Products') as 'Products' Get current locks to use as a baseline to further executions of sp_lock PRINT 'Initial lock status'+ CHAR(10) EXEC sp_lock SELECT ProductID, ProductName, UnitPrice FROM Products (SERIALIZABLE) WHERE CategoryID = 3 PRINT CHAR(10) + 'lock status after SELECT'+ CHAR(10) EXEC sp_lock ROLLBACK TRAN Products 117575457 (1 row(s) affected) Initial lock status spid dbid ObjId IndId Type Resource Mode Status 51 4 0 0 DB S GRANT 52 4 0 0 DB S GRANT 53 10 0 0 DB S GRANT 54 7 0 0 DB S GRANT 55 6 0 0 DB S GRANT 56 10 0 0 DB S GRANT 57 6 0 0 DB S GRANT 58 6 0 0 DB S GRANT 61 6 0 0 DB S GRANT 61 1 85575343 0 TAB IS GRANT 62 6 0 0 DB S GRANT 63 6 0 0 DB S GRANT 68 6 0 0 DB S GRANT 69 6 0 0 DB S GRANT ProductID ProductName UnitPrice 16 Pavlova 17.4500 19 Teatime Chocolate Biscuits 9.2000 Microsoft SQL Server 2000 Programming by Example 554 20 Sir Rodney's Marmalade 81.0000 21 Sir Rodney's Scones 10.0000 25 NuNuCa Nuß-Nougat-Creme 14.0000 26 Gumbär Gummibärchen 31.2300 27 Schoggi Schokolade 43.9000 47 Zaanse koeken 9.5000 48 Chocolade 12.7500 49 Maxilaku 20.0000 50 Valkoinen suklaa 16.2500 62 Tarte au sucre 49.3000 68 Scottish Longbreads 12.5000 (13 row(s) affected) lock status after SELECT spid dbid ObjId IndId Type Resource Mode Status 51 4 0 0 DB S GRANT 52 4 0 0 DB S GRANT 53 10 0 0 DB S GRANT 54 7 0 0 DB S GRANT 55 6 0 0 DB S GRANT 56 10 0 0 DB S GRANT 57 6 0 0 DB S GRANT 58 6 0 0 DB S GRANT 61 6 0 0 DB S GRANT 61 6 117575457 0 TAB IS GRANT 61 6 117575457 1 KEY (310027bf2c96) S GRANT 61 6 117575457 1 KEY (3200c9109984) S GRANT 61 6 117575457 1 KEY (3e0071af4fce) S GRANT 61 6 117575457 1 KEY (2f008b9fea26) S GRANT 61 6 117575457 2 KEY (1700bde729cb) RangeS-S GRANT 61 6 117575457 2 KEY (1e00ebf74a93) RangeS-S GRANT 61 6 117575457 1 KEY (1b007df0a359) S GRANT 61 6 117575457 1 KEY (14002be0c001) S GRANT 61 6 117575457 1 PAG 1:276 IS GRANT 61 6 117575457 2 PAG 1:277 IS GRANT 61 6 117575457 2 KEY (32001d9803ec) RangeS-S GRANT 61 6 117575457 2 KEY (4100e7a8a604) RangeS-S GRANT 61 6 117575457 2 KEY (3400b1b8c55c) RangeS-S GRANT 61 6 117575457 2 KEY (35005f17704e) RangeS-S GRANT 61 6 117575457 1 PAG 1:360 IS GRANT 61 1 85575343 0 TAB IS GRANT 61 6 117575457 1 KEY (440089efcdca) S GRANT 61 6 117575457 1 KEY (300042d8902e) S GRANT 61 6 117575457 2 KEY (1c00603f4339) RangeS-S GRANT 61 6 117575457 2 KEY (1d008e90f62b) RangeS-S GRANT 61 6 117575457 2 KEY (160004dffe56) RangeS-S GRANT 61 6 117575457 2 KEY (1300ea704b44) RangeS-S GRANT 61 6 117575457 2 KEY (1800d8809573) RangeS-S GRANT 61 6 117575457 1 KEY (15004e877cb9) S GRANT 61 6 117575457 1 KEY (130092d8179c) S GRANT 61 6 117575457 1 KEY (10007c77a28e) S GRANT 61 6 117575457 1 KEY (1900f638aaf3) S GRANT 61 6 117575457 1 KEY (1a0018971fe1) S GRANT 61 6 117575457 2 KEY (3300d4df79e4) RangeS-S GRANT 61 6 117575457 2 KEY (0f006da996c9) RangeS-S GRANT 61 6 117575457 2 KEY (47001fe82400) RangeS-S GRANT 62 6 0 0 DB S GRANT Chapter 13. Maintaining Data Consistency: Transactions and Locks 555 63 6 0 0 DB S GRANT 68 6 0 0 DB S GRANT 69 6 0 0 DB S GRANT A Serious Problem to Avoid: Deadlocks Imagine that your database application has two users: Paul and Mary. Paul starts a transaction and modifies some attributes of the Acme Ltd. customer. Later, inside the same transaction, Paul tries to modify this customer's payments. However, Paul cannot modify these payments because Mary holds an exclusive lock on these payment records. Paul must wait for these records to be unlocked before completing the transaction. Mary is modifying customers' payments, and that's why this information is locked. Inside the same transaction, Mary tries to modify some data about the Acme Ltd. customer. At this moment, Paul, who modified this record just a few minutes ago, locks this information. Mary cannot update this information because Paul is holding an exclusive lock on it, so Mary must wait for this resource to be unlocked before proceeding with her transaction. However, Paul cannot continue with his transaction because he's waiting for Mary to unlock the information he needs to update. This situation of mutual blockings is called deadlock. If SQL Server detects this situation, it decides which process has a bigger execution cost, and selects this process as a winner. After the winner is selected, SQL Server notifies the other processes waiting in this deadlock situation with error 1205, telling them that they have been selected as victims in a deadlock situation. If the processes involved in a deadlock situation are blocking one another in a circular reference, SQL Server selects which process can be selected to break the deadlock with the least overall cost, and notifies this process with error 1205. Note You can propose your specific session as a potential deadlock victim by using the statement SET DEADLOCK_PRIORITY LOW. Two processes can create a deadlock situation when they access resources in opposite orders and try to convert a shared lock into an exclusive lock at the same time. Figure 13.8 illustrates this scenario: Figure 13.8. A typical deadlock situation. Microsoft SQL Server 2000 Programming by Example 556 1. Connection A starts a transaction and reads the UnitPrice column from the Product 37. This connection uses the HOLDLOCK locking hint to maintain the shared lock on the row corresponding to Product 37. 2. Connection B starts a transaction and reads the average UnitPrice from the Order Details table for Product 37. This connection uses the HOLDLOCK locking hint to maintain the shared lock on the Order Details rows from Product 37. 3. Connection A tries to update the Order Details table to reset the unit price of Product 37 to the value stored in the Products table. To execute this statement, Connection A needs an exclusive lock on the affected rows, but this exclusive lock must wait because Connection B holds a shared lock on the same rows. 4. Connection B tries to update Product 37 in the Products table with the average unit price retrieved from the Order Details table. Connection B requests an exclusive lock on Product 37, but this lock must wait because Connection A holds a shared lock on it. 5. SQL Server detects this deadlock situation, selects Connection B as victim of this situation, and sends message 1205 to Connection B. Resources locked by Connection B are unlocked. Chapter 13. Maintaining Data Consistency: Transactions and Locks 557 6. After Connection B has been selected as a victim and its locks have been released, Connection A can continue its operation. Another typical case is when two transactions want to convert an existing shared lock on a common locked resource into an exclusive lock. To prevent this situation, you should use the UPDLOCK locking hint in transactions in which you read data with intentions of updating it later in the same transaction. Caution When a transaction is selected as a victim in a deadlock situation, the process is cancelled and changes applied are rolled back. However, the calling application could usually resend the transaction and, hopefully, the previous locks have disappeared. Avoiding deadlock is not always possible; however, you can help to reduce deadlocks by following these guidelines: • Keep transactions as short as possible. • Avoid user interaction inside transactions. In other words, start a transaction only when required and release it as soon as possible. • Always access resources in the same order and check for potential circular references. • Use the READ COMMITTED isolation level if possible, because it produces fewer locks than higher isolation levels. Try to avoid SERIALIZABLE as much as possible. • If an application uses several connections, bind them to share the same locking space. You can execute the stored procedure sp_bindsession to keep more than one session in the same transaction. What's Next? Transactions and locks are key aspects to provide the adequate concurrency to your database application in a multiuser environment. However, they are restricted, as covered in this chapter, to a single-server operation. The following two chapters focus on the multiserver environment from two different perspectives: • Chapter 14 shows how to transfer data to and from SQL Server databases stored in the same or different servers. Data Transformation Services (DTS) is a feature-rich application which, integrated in SQL Server or as a standalone subsystem, transfers data between hetero-geneous systems, including all the required transformations. • Chapter 15 discusses the multiserver environment and the implications of the distributed transactions. In Chapter 15, you learn how to use linked servers to maintain data in multiple servers, as an alternative to DTS and Replication. Chapter 14. Transferring Data to and from SQL Server 559 Chapter 14. Transferring Data to and from SQL Server In a standard business environment, it is quite common to have different system platforms, different operating systems, heterogeneous networks, and different database systems. Linking existing data from different sources is a convenient way to work with heterogeneous data to gain data consistency through the company without creating any data redundancy. However, in some cases, you might need to transfer data from one system to another. Importing and exporting data is a common task for a database administrator, and it is not our intention to cover this subject in detail. However, as a database programmer, you should know the basics of importing and exporting data, and this chapter will teach you how to solve this problem. This chapter teaches you the following: • Why you need to transfer and transform data • SQL Server 2000 tools for transferring data • How to use the BULK INSERT statement • How to use the bcp command-line utility • How to use the Copy Database Wizard The Need for Transferring Data If your company has a single database, in a single server, and you never need to receive data from other systems or send data to other servers, you could skip this chapter. Many systems receive their data through direct user input. However, there are some cases where transferring data is important: • You want to migrate to a new system and you want to populate the new database with data coming from your old system. • Your accounting system works in a mainframe and you do not want to change this system. However, it would be useful to have some accounting information in the SQL Server Sales database. In this case, you must periodically refresh this information from the mainframe. • The post office changes the national postal code information and they distribute this new information as a CSV file. You need to import this file into your system to update the Customer Management application. • The Inland Revenue changes their requirements and now the annual accounts must be sent in a different format. You must create the process of exporting data in exactly the way they require. • You create a testing server in your network and you want to have the same databases as in your production server to test a new indexing strategy. • Your sales managers visit customers, and they want to have a copy of the Sales System database in their laptops so they can look at sales figures when they are at the customer site. • Your corporation has many different companies in different countries, and you want to receive periodic financial information from them. Every one of these companies uses a different system, and the only way to receive data is by text files, so you can import them easily. • You have a Documents database and you receive many documents from different sources. You want to import them into the Documents database efficiently. • You are running a Geographical Information System and your field teams send you files every week with their field measurements. You need to integrate this new data with your existing GIS database. • You just finished a new Agricultural Census in your county and you want to compare this new data with the latest census's data. The old data is in a different system and you want to import the old data to consolidate both databases. • Your remote offices need to produce reports about their local sales figures. They complain because they need to access your central mainframe to produce these reports, but the mainframe connection is not always available. You decide that a good solution is to have a local database with local data to produce reports locally. You need to refresh these local databases periodically to have their data synchronized with the central database. • Your network administrators are concerned about a potential bottleneck on your central database system. A feasible solution is to install departmental servers with replicated data. In this way, users Microsoft SQL Server 2000 Programming by Example 560 can receive data from a local server, in the same network segment, without traversing the entire network to arrive to the data center. SQL Server 2000 provides different tools to transfer data from any source to any destination. Depending on your specific requirements, one tool can be more appropriate than another. You will learn about the SQL Server 2000 tools used to transfer data in the next section of this chapter. In other cases, the problem is not only transferring data, but also modifying data from the source database to meet the requirements of the destination database system. Some examples are as follows: • You have a relational database and you need to create a data warehouse database with a different database schema; in this case, it could be a star schema. • Your legacy system in the USA stores dates in a different format (mmddyyyy) from the legacy system you have in France (ddmmyyyy). You want to make sure you can import dates correctly to your central server in Indonesia, which uses the ISO/ODBC standard format (yyyy-mm-dd). • After a company merge, you need to consolidate data from two different systems. In one system, the codes used in lookup tables are different from the codes used in the other system. In the Spanish system, end users can be S (solteros), C (casados), D (divorciados o separados), V (viudos). In the British system, end users can be S (single), M (married), D (divorced), W (widow or widower). You need to agree about new codes and transform the old ones. • You just bought a bank in Morocco, and you see that their database system identifies customer accounts by their full name, including title. You want to provide a new account identification number and store title, family name, and first name in separate fields. • You work in an international project and you need to integrate data in different currencies. Your system selects Euro as the standard internal currency and you must transform all quantities into Euros and store the exchange rate applied to every amount in a different field. • You created a weather database to help on global weather forecasts. This system receives continuous information from weather systems around the world, each one using different units for temperature, rainfall, pressure, and so on. You must convert the data to uniform units to be able to produce consistent results. Data Transformation Services 2000 can help you create complex packages that transfer and transform the data to meet the requirements of the destination database. Tools for Transferring Data Using SQL Server 2000 SQL Server 2000 offers many different choices to transfer data. Every tool has advantages and disadvantages. You can use the following examples as guidelines to select the data distribution tool to use: • Using distributed queries, you can directly access data from different servers. Chapter 15, "Working with Heterogeneous Environments: Setting Up Linked Servers," covers distributed queries in detail. • You can use replication to copy data from one server to another, on demand or at regular intervals. If you need to distribute data to mobile users, and they need to modify data locally, merge replication is an excellent solution. Transactional replication is a very efficient mechanism to distribute data changes to remote servers, if the latency inherent to replication is acceptable in your case. Replication is not covered in this book. Books Online contains a full section about replication, with comprehensive information about how replication works. • You can back up a database in a SQL Server 7.0 or 2000 server and restore it in another SQL Server 2000 server. If you restore a SQL Server 7.0 database into SQL Server 2000, the restore process modifies the database internal physical structure to adapt it to the new SQL Server 2000 physical structure. Restoring SQL Server 2000 databases into SQL Server 7.0 is not supported. Backup is not covered in this book because it is an administrative task. Books Online contains the "Backing Up and Restoring Databases" section, where you can find more information about this topic. Note Chapter 14. Transferring Data to and from SQL Server 561 Contrary to what happened with SQL Server 7.0, in SQL Server 2000 you can restore databases from servers with different collations, because every database has its own collation, independent from the server default collation. • You can detach a database from a server running SQL Server 7.0 or 2000, copy the database files to another server, and attach them to the destination server. This procedure is more efficient than using backup and restore. After you attach a SQL Server 7.0 database into SQL Server 2000, it is converted to the new database structure. Attaching SQL Server 2000 databases into SQL Server 7.0 is not supported. Look in Books Online for information on how to use the stored procedures sp_detach_db and sp_attach_db. Note In SQL Server 2000, you can attach databases that have been detached from servers with different collations, because every database has its own collation, independent from the server default collation. • You can convert SQL Server 6.5 databases into SQL Server 2000 running the SQL Server Upgrade Wizard. Look in Books Online for information on "Upgrading Databases from SQL Server 6.5 (Upgrade Wizard)." • Data Transformation Services (DTS) is a flexible and powerful tool that you can use to import and export data, and transform the data as well. You will learn how to import and export data using DTS later in this chapter, in the "Using Data Transformation Services" section. • Use the bcp command-line utility to import and export data to and from SQL Server 2000. You learn how to use bcp in the next section of this chapter. • Use the new BULK INSERT statement in a batch or stored procedure to import data from a file into a SQL Server 2000 table. The next section of this chapter covers this tool in detail. • You can use the ODBC bulk copy application programming interface (API), as the bcp utility does, using any programming language to create your own transferring application. To get more information about this interesting programming solution, search in Books Online for the section "How to Bulk Copy with the SQL Server ODBC Driver (ODBC)." • You can write an application using the SQL-DMO library, and use the Transfer and Transfer2 objects'properties and methods to transfer data and schema between SQL Server 2000 or SQL Server 7.0 servers. Search in Books Online for the "Transfer Object" topic. The BULK INSERT Statement and bcp You can use the bcp command-line utility to export a table or the result of a query to an external data file. You can copy this file over the network or the Internet, or use any media to send it to its destination. It also can be used to import the data file into a single table. You can use the bcp native mode to export data to and from SQL Server databases. If you export data from SQL Server in native mode, you cannot import that data into any other database system than SQL Server. However, using character-based files provides better flexibility, because the data can be exported to any database system that supports importing from text files. Tip Microsoft SQL Server 2000 Programming by Example 562 Using bcp in native mode, between SQL Server databases, is more efficient than using character mode. To use bcp, you must open a command prompt window and execute this utility from there. If you want to import data from a data file into SQL Server, using Transact-SQL language, you can use the new BULK INSERT statement. This method of importing data is highly efficient, and you should use it to perform simple import operations of big data files. Using bcp and BULK INSERT is faster than inserting the same information, row by row, either manually or from a client application. By default, constraints and triggers are ignored when importing data using bcp or BULK INSERT, providing a faster inserting operation. However, you should check the data to guarantee that it complies with the existing constraints. Tip If you define triggers in your tables to maintain denormalized data in other tables, you should create a stored procedure with similar functionality to apply to the imported data after the bulk operation terminates. In this case, it is better if the stored procedure executes both operations in sequence: the import process and the post-import maintenance operations. In the next section, you will see how to enable or disable constraint checking and trigger execution during the bulk copy operations. If your destination database uses a full recovery model, the import operation must be fully logged, and you will be potentially running out of space in the transaction log. The fastest way to import data into SQL Server is by executing a minimally logged bulk-copy operation, which can be performed if all these conditions are met: • The database recovery model is set to simple or bulk-logged. • The destination table is not replicated. • The destination table does not have any triggers. • The destination table is empty or does not have any indexes. • You run the bulk copy operation, specifying the TABLOCK hint. If the destination of the bulk copy operation does not meet any of these conditions, the operation will be logged. Tip [...]... SQL Server 2000 to Microsoft Access 2000 Note To execute the examples from the next three sections, you must have two instances of SQL Server 2000 installed, or access to two different severs in your network with SQL Server 2000 installed 5 79 Transfer Objects Between Two SQL Server 2000 Databases In this section, you learn how to transfer database objects from the Northwind database in a SQL Server 2000. .. SQL Server 2000' s msdb database, SQL Server 2000 Meta Data Services, a COM structured file, or as a Visual Basic file It is not the purpose of this book to cover in detail this important tool How ever, we want to show you how to perform two common tasks, step by step: • • How to transfer database objects between two Microsoft SQL Server 2000 databases How to export tables and views from Microsoft SQL. .. 12 171100 d:\winnt\media\tada.wav 13 135876 d:\winnt\media\The Microsoft Sound.wav 14 95 708 d:\winnt\media\Utopia Asterisk.WAV 15 4616 d:\winnt\media\Utopia Close.WAV 16 5824 d:\winnt\media\Utopia Critical Stop.WAV 578 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 99 46 24 596 13026 1 492 2 3462 2 692 1 499 0 10760 13084 98 330 5120 15372 86 798 156760 344108 486188 d:\winnt\media\Utopia Default.WAV d:\winnt\media\Utopia... to connect to the data source In this case, we accept the following default settings: • • • • Microsoft SQL OLE DB provider for SQL Server Server SQLBE\Inst3, which is the named instance Inst3 in the SQLBE server DTS uses Windows Authentication Mode to connect to the SQLBE\Inst3 server You could select SQL Server authentication instead, and, in that case, you must supply a valid username and password... settings: Figure 14.3 You can select the destination where the data will be sent 581 • • • • Microsoft SQL OLE DB Provider for SQL Server Server SQLBE\Inst2, which is the named instance Inst2 in the SQLBE server DTS will use Windows Authentication mode to connect to the SQLBE\Inst3 server You could select SQL Server Authentication instead and, in that case, you must supply a valid username and password... Execution— This option causes a SQL Server Agent job to be executed automatically, according to the required schedule Save DTS Package— Use this option to store the package so you can modify it later You can store the package in the MSDB database in SQL Server, in the SQL Server Meta Data Services, in a COM structured storage file, or as a Visual Basic File Select SQL Server as the storage location... prompt, using the osql utility to connect to SQL Server and execute the CREATE TABLE statement Listing 14.6 shows the execution of both osql and bcp Listing 14.6 Create the NewRegions Table and Import the region.txt File C:\TEMP>osql -S YourServer\YourInstance -E -d Northwind -Q "CREATE TABLE NewRegions (ID int, Name nchar(50))" C:\TEMP>bcp northwind.dbo.NewRegions in region.txt - S YourServer\YourInstance... database in a different SQL Server 2000 instance in the same server Note that this example works as well between two different servers To perform this task, you will use the DTS Import/Export Wizard To start the wizard, you can run Enterprise Manager, open the Tools menu, select Wizards, Data Transformation Services, and DTS Export Wizard A different way to start the wizard is by choosing All Tasks,... destination table Using the bcp Command-Line Utility The bcp command-line utility copies data from SQL Server to an external data file and imports data from an external data file into SQL Server Note The bcp utility uses the ODBC Bulk Copy Application Programming Interface (API) It is compatible with any version of SQL Server To test the bcp utility, open a command prompt window and execute bcp /?, as in Listing... files or documents into SQL Server Using Data Transformation Services Data Transformation Services is a powerful tool introduced with SQL Server 7.0 It is a versatile tool that enables developers to design packages that transfer and transform the data efficiently between two data sources Using Data Transformation Services, you can • • • • • • • Select any data source, not necessarily SQL Server, if . region.txt -S YourServerYourInstance -T -c Microsoft SQL Server 2000 Programming by Example 566 SQLState = S0002, NativeError = 208 Error = [Microsoft] [ODBC SQL Server Driver] [SQL Server] Invalid. can back up a database in a SQL Server 7.0 or 2000 server and restore it in another SQL Server 2000 server. If you restore a SQL Server 7.0 database into SQL Server 2000, the restore process modifies. 0 0 DB S GRANT 69 6 0 0 DB S GRANT ProductID ProductName UnitPrice 16 Pavlova 17.4500 19 Teatime Chocolate Biscuits 9 .2000 Microsoft SQL Server 2000 Programming by Example 554 20

Ngày đăng: 08/08/2014, 22:20

TỪ KHÓA LIÊN QUAN