Beruflich Dokumente
Kultur Dokumente
Application conversion
with detailed examples
Whei-Jen Chen
Alain Fisher
Stefan Hummel
Shailendra Kishore
Wei Bin Teah
Ted Wasserman
ibm.com/redbooks
International Technical Support Organization
June 2005
SG24-6672-00
Note: Before using this information and the product it supports, read the information in
“Notices” on page xxi.
This edition applies to DB2 UDB Version 8.2, Microsoft SQL Server 2000, and Windows 2000
Server.
Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
The team that wrote this redbook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Become a published author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Comments welcome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii
Chapter 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 DB2 Universal Database - a high-level overview . . . . . . . . . . . . . . . . . . . . 2
1.2 Project environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 IBM migration offering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.1 DB2 UDB promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.2 DB2 migration services and support . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Chapter 2. Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.1 SQL Server instance definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.2 DB2 UDB instance definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2 Memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 SQL Server memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.2 DB UDB memory allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 SQL Server process model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 DB2 UDB process model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Allocating disk space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.1 SQL Server disk allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.2 DB2 UDB disk allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Transaction logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.1 SQL Server transaction log overview . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5.2 DB2 UDB transaction log overview . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6 Query optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.6.1 SQL Server query optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6.2 DB2 UDB query optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Contents v
4.9.6 @@ROWCOUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.9.7 @@TRANCOUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.10 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.10.1 FOR XML statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4.10.2 OPENXML statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.11 SQL limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server . . . . . . . . . . . 137
6.1 MTK technical overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.1.1 Supported operating system and versions . . . . . . . . . . . . . . . . . . . 139
6.1.2 Hardware requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.1.3 MTK software requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.1.4 Where to install MTK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.2 MTK setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
6.3 Using MTK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.3.1 MTK GUI interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.3.2 Migration tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.3.3 The MTK SQL Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Contents vii
9.3.3 Overcoming inline SQL PL limitations in user defined functions . . . 211
9.3.4 Additional information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
9.4 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
9.4.1 Accessing transition values in triggers . . . . . . . . . . . . . . . . . . . . . . 216
9.4.2 Simple conversion example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
9.4.3 IF UPDATE(column) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
9.4.4 Overcoming inline SQL PL limitations in triggers . . . . . . . . . . . . . . 219
9.4.5 INSTEAD OF triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
9.4.6 Additional information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Contents ix
12.4.4 Large transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
12.4.5 Process tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
12.5 Data access strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
12.5.1 Indexing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
12.5.2 DB2 UDB index expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
12.5.3 Index reorganization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
12.6 Advanced access methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
12.6.1 Materialized query tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
12.6.2 Multidimensional clustering (MDC) tables . . . . . . . . . . . . . . . . . . . 379
12.7 Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
12.7.1 Optimizer analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
12.7.2 Optimizer directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Contents xi
F.2 Database limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Figures xv
13-2 Table definition for Example 13-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
13-3 Data file for Example 13-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
13-4 Development Center debug window . . . . . . . . . . . . . . . . . . . . . . . . . . 409
14-1 MTK - Welcome window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
14-2 MTK - Project Management dialog window . . . . . . . . . . . . . . . . . . . . . 424
14-3 Database conversion process using MTK . . . . . . . . . . . . . . . . . . . . . . 425
14-4 MTK - Specify Source tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
14-5 MTK - Connect to Database dialog window . . . . . . . . . . . . . . . . . . . . . 427
14-6 MTK - Extract dialog window for tables . . . . . . . . . . . . . . . . . . . . . . . . 428
14-7 MTK - Specify Source tab with extracted table script selected . . . . . . 429
14-8 MTK - Convert tab after converting tables . . . . . . . . . . . . . . . . . . . . . . 430
14-9 MTK - Global Type Mapping window . . . . . . . . . . . . . . . . . . . . . . . . . . 431
14-10 MTK - Data Type Editor dialog window . . . . . . . . . . . . . . . . . . . . . . . . 431
14-11 MTK - Global Type Mapping changes . . . . . . . . . . . . . . . . . . . . . . . . . 432
14-12 MTK - Advance Options window (for conversions) . . . . . . . . . . . . . . . 433
14-13 MTK - Refine tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
14-14 MTK - Translator Information message tree (for tables) . . . . . . . . . . . 434
14-15 MTK - Translator Warning message tree (for tables). . . . . . . . . . . . . . 435
14-16 MTK - Translator Warning message tree (for tables). . . . . . . . . . . . . . 435
14-17 MTK - Refine tab including source and target code subtabs, for tables436
14-18 MTK - Refine tab (change recommended object name for indexes) . . 437
14-19 MTK - Refine tab (Translation success ratio). . . . . . . . . . . . . . . . . . . . 438
14-20 MTK - Generate Data Transfer Scripts tab . . . . . . . . . . . . . . . . . . . . . 439
14-21 MTK - Generate Data Transfer Scripts tab (data loading options). . . . 439
14-22 MTK - Generate Data Transfer Scripts tab (LOAD/IMPORT mode) . . 440
14-23 MTK - Generate Data Transfer Scripts tab (file format options) . . . . . 440
14-24 MTK - Deploy to DB2 tab (for tables) . . . . . . . . . . . . . . . . . . . . . . . . . . 442
14-25 MTK - Deploy to DB2 tab (first deployment step) . . . . . . . . . . . . . . . . 443
14-26 MTK - Deploy to DB2 tab (second deployment step). . . . . . . . . . . . . . 443
14-27 MTK - Deploy to DB2 tab (third deployment option) . . . . . . . . . . . . . . 444
14-28 MTK - Deploy to DB2 verification report (for tables) . . . . . . . . . . . . . . 444
14-29 MTK - Extract dialog window for Views . . . . . . . . . . . . . . . . . . . . . . . . 445
14-30 MTK - Convert tab after setting the context (for Views) . . . . . . . . . . . . 446
14-31 MTK - Refine tab after conversion (translation success ratio for Views)447
14-32 MTK - After deploying functions to DB2 UDB . . . . . . . . . . . . . . . . . . . 448
14-33 MTK - Convert tab (choosing a previous conversion activity) . . . . . . . 448
14-34 MTK - Extract dialog window for user defined functions . . . . . . . . . . . 449
14-35 MTK - Set Context window (for functions) . . . . . . . . . . . . . . . . . . . . . . 450
14-36 MTK - Convert context indicator (for functions) . . . . . . . . . . . . . . . . . . 450
14-37 MTK - Refining converted functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
14-38 MTK - Refine tab translation message error (for stored procedures). . 453
14-39 MTK - Refine tab after modifying a SQL Server stored procedure . . . 455
14-40 MTK - Refine tab (for triggers) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Figures xvii
xviii Microsoft SQL Server to IBM DB2 UDB Conversion Guide
Tables
This information was developed for products and services offered in the U.S.A.
IBM may not offer the products, services, or features discussed in this document in other countries. Consult
your local IBM representative for information on the products and services currently available in your area.
Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM
product, program, or service may be used. Any functionally equivalent product, program, or service that
does not infringe any IBM intellectual property right may be used instead. However, it is the user's
responsibility to evaluate and verify the operation of any non-IBM product, program, or service.
IBM may have patents or pending patent applications covering subject matter described in this document.
The furnishing of this document does not give you any license to these patents. You can send license
inquiries, in writing, to:
IBM Director of Licensing, IBM Corporation, North Castle Drive Armonk, NY 10504-1785 U.S.A.
The following paragraph does not apply to the United Kingdom or any other country where such provisions
are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES
THIS PUBLICATION "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer
of express or implied warranties in certain transactions, therefore, this statement may not apply to you.
This information could include technical inaccuracies or typographical errors. Changes are periodically made
to the information herein; these changes will be incorporated in new editions of the publication. IBM may
make improvements and/or changes in the product(s) and/or the program(s) described in this publication at
any time without notice.
Any references in this information to non-IBM Web sites are provided for convenience only and do not in any
manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the
materials for this IBM product and use of those Web sites is at your own risk.
IBM may use or distribute any of the information you supply in any way it believes appropriate without
incurring any obligation to you.
Information concerning non-IBM products was obtained from the suppliers of those products, their published
announcements or other publicly available sources. IBM has not tested those products and cannot confirm
the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on
the capabilities of non-IBM products should be addressed to the suppliers of those products.
This information contains examples of data and reports used in daily business operations. To illustrate them
as completely as possible, the examples include the names of individuals, companies, brands, and products.
All of these names are fictitious and any similarity to the names and addresses used by an actual business
enterprise is entirely coincidental.
COPYRIGHT LICENSE:
This information contains sample application programs in source language, which illustrates programming
techniques on various operating platforms. You may copy, modify, and distribute these sample programs in
any form without payment to IBM, for the purposes of developing, using, marketing or distributing application
programs conforming to the application programming interface for the operating platform for which the
sample programs are written. These examples have not been thoroughly tested under all conditions. IBM,
therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. You may copy,
modify, and distribute these sample programs in any form without payment to IBM for the purposes of
developing, using, marketing, or distributing application programs conforming to IBM's application
programming interfaces.
Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc. in the United States, other countries, or both.
Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the
United States, other countries, or both.
Intel, Intel Inside (logos), and Pentium are trademarks of Intel Corporation in the United States, other
countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other countries.
Linux is a trademark of Linus Torvalds in the United States, other countries, or both.
Other company, product, and service names may be trademarks or service marks of others.
Alain Fisher is a Software Support Specialist for IBM Technical Support and
Remote Operations in the United Kingdom. He has eight years of experience in
the IT field and as a member of the North Region Information Management team
Wei Bin Teah is an IT Specialist for ASEAN and A/NZ Techline for IBM
Malaysia. He holds a bachelor’s degree in Computer Engineering from Purdue
University, West Lafayette, Indiana. His area of expertise is in pre-sales technical
sales support for DB2 information management software. He is an IBM Certified
Advanced Database Administrator, IBM Certified Solution Designer for DB2
Business Intelligence, and IBM Certified Specialist for Grid Computing Technical
Sales.
Ted Wasserman is an IBM employee based out of the IBM Silicon Valley
Laboratory where parts of the DB2 product are developed. Ted works as a
Database Consultant on the DB2 Business Partner Technical Enablement team
where his main responsibility is to provide technical assistance to IBM Business
Partners during migrations to DB2 UDB. Ted has a master’s degree in Computer
Science, as well as a Hon. B.Sc. in Computer Science from Queen’s University
(Kingston, Ontario, Canada).
Acknowledgements
The authors express their deep gratitude for the help received from Jie Zhang
who contributed advice, support, and written content.
Preface xxv
We also thank the following people for their support and contributions to this
book:
Grant Hutchison
IBM DB2 Marketing, Software Group, Toronto Laboratory, Canada
Michael Gao
IBM Software Group, Toronto Laboratory, Canada
Marina Greenstein
Arthur V. Sammartino
IBM Software Migration Project Office, USA
Abdul Al-Azzawe
IBM Software Group, San Francisco, USA
Martin Mezger
Sven-Uwe Kusche
IBM SAP DB2 Center of Expertise, IBM Germany
Eva Billich
IBM Software Group, IBM Germany
Michael Spoden
IBM Global Service, IBM Germany
Sandy Gregus
Michael Logan
Jay Pederson
Sam Qita
IBM DB2 Product Manager, Software Group
Shawn Moe
IBM Software Group, IBM DB2 Migration Toolkit
Emma Jacobs
International Technical Support Organization, San Jose Center
Find out more about the residency program, browse the residency index, and
apply online at:
ibm.com/redbooks/residencies.html
Comments welcome
Your comments are important to us!
Preface xxvii
xxviii Microsoft SQL Server to IBM DB2 UDB Conversion Guide
1
Chapter 1. Introduction
Database management software is now the core of enterprise computing.
Companies need access to a wide range of information such as XML documents,
streaming video, and other rich media types. New ways of working bring new
requirements, including digital rights management. The e-business evolution
makes continuous availability a necessity and is driving the convergence of
transaction, business intelligence, and content management applications as
companies integrate their business operations. DB2 Universal Database (DB2
UDB) for Linux, UNIX, and Windows can help your organization meet these
challenges.
The goal of this book is to provide information that can help SQL Server
customers understand, plan for, and execute a migration to DB2 UDB in the
easiest possible manner.
This book introduces DB2 UDB products and their capabilities, discusses
converting databases and applications, and describes the most important
aspects of converting applications from Microsoft SQL Server to DB2 UDB. It
describes the differences between their architecture, the tools for administration
and development, the data definition language (DDL), the data modeling, SQL
considerations, data conversion, and application conversion.
As the real database leader in several technologies, DB2 UDB provides the
following capabilities:
Integrated support for complex data such as text documents, images, video
and audio clips
Integrated Web access through native support for Java™, Java Database
Connectivity (JDBC), embedded SQL for Java (SQLJ) and Microsoft .NET
Integrated system management tools
Data replication services
High availability disaster recovery (HADR)
At the core of the DB2 portfolio is the DB2 UDB database server. IBM DB2 UDB
Version 8 for Linux, UNIX, and Windows is leading the evolution of the relational
database. DB2 UDB is the database of choice for the development and
deployment of critical solutions such as:
e-business
Business intelligence
Content management
Enterprise resource planning
Customer relationship management
Innovative manageability
DB2 UDB greatly reduces the complexity of data management by simplifying,
automating, or eliminating many tasks traditionally associated with maintaining
Integrated information
DB2 UDB provides a strong foundation of information integration technologies,
including federation, replication, Web services, and XML. With DB2 UDB built-in
capabilities, you can query, update, and replicate data across DB2 UDB and
other data sources. This is a key capability for those transition to DB2 UDB.
Information integration technologies include:
Federated Web services give you the ability to publish and consume data
from various sources, such as another DBMS and XML.
XML productivity tools simplify integrating XML, giving you the ability to store,
retrieve, and decompose XML structures.
Enriched data type support. DB2 provides rich type support for spatial data,
text data, and flat files. Tools that enable these capabilities include the DB2
Spatial Extender, DB2 Net Search Extender, and DB2 Data Links Manager.
Chapter 1. Introduction 3
Dynamic parameter configuration enables you to reconfigure key parameters
without having to bring down the database or instance.
In-place online reorganization.
Online load.
Online storage management.
Mobility on demand feature. DB2 UDB customers can easily extend their
solutions to include mobile data by leveraging the new mobility on demand
capability available with DB2 UDB. The mobility on demand capability, based
on DB2 Everyplace® technology, includes the high-performance, robust DB2
Everyplace database and a powerful synchronization solution for use with an
existing DB2 UDB deployment. This enables wireless and pervasive devices
to occasionally run connected to DB2 UDB applications.
Note: One of the most exciting new features of DB2 UDB Version 8.2 is
support for Common Language Runtime (CLR) procedures. You can code C#
and VB.NET methods in class assemblies and then deploy them as DB2
routines. DB2 CLR routines enable you to code your procedures using your
choice of .NET language.
DB2 UDB offers database solutions that run on all platforms including Windows
servers, AIX, Sun, HP-UX, Linux, AS/400®, OS/390® and z/OS®. Furthermore,
DB2 UDB technologies support both 32-bit and 64-bit environments. The DB2
UDB product family comprises a variety of packages that provide customers
choices based on their business need. The following lists the DB2 UDB product
offerings for Linux, UNIX, and Microsoft Windows:
DB2 UDB Enterprise Server Edition (ESE)
DB2 UDB ESE is designed to meet the database server needs of midsize to
large businesses. ESE’s high scalability, availability, and reliability features
provide customers an ideal database management system for all types of
transactions.
– The Database Partitioning Feature (DPF)
The Database Partitioning Feature is a licensing option that allows ESE
customers to partition a database within a single system or across a
cluster of systems. The DPF capability provides the customer with multiple
benefits including scalability to support very large databases, or complex
workloads and increased parallelism for administration tasks.
DB2 Workgroup Server Edition (WSE)
This Edition is designed for deployment at a departmental level or in a small
business environment with a small number of users. It can be deployed on a
server with up to four CPUs.
DB2 Workgroup Server Unlimited Edition (WSUE)
This product offers a simplified per processor licensing for deployment at a
departmental level or in a small business environment.
Chapter 1. Introduction 5
DB2 Personal Edition (PE)
This flavor of DB2 provides a database management system for a single user
database.
DB2 UDB Developers Edition
This product offers a package for single application developer to design and
build an application.
DB2 UDB Personal Developer’s Edition (PDE)
Similar to the Developer’s edition, this product enables the developer to build
a single user desktop application.
DB2 Express
This is the newest member of DB2 UDB product family. The key features
include simplified deployment, autonomic management capabilities, and
application development support and design for 7/24 operation. This flavor of
DB2 UDB is aimed at Independent Software Vendors (ISV) who want to
integrate DB2 UDB as part of their application with low cost, and have the
capability to expand easily in the future.
IBM also provides federated technologies to extend the functionality of DB2 UDB.
With WebSphere Information Integrator (WebSphere II) you can access objects
in many different databases such as Oracle and SQL Server with a single query.
In addition, DB2 Connect Editions offer you the capability to extend your
enterprise system to access legacy systems.
http://www.ibm.com/software/data/db2/udb/
Another helpful article for this topic is “Which distributed edition of DB2
Version 8 is right for you?” available at:
http://www.ibm.com/developerworks/db2/library/techarticle/0211zikopoulos/021
1zikopoulos.html
Chapter 1. Introduction 7
Workstation PCs
e.g., IBM Thinkpad T40, Pentium® M 1.5 GHz, RAM 1GB
Windows 2000 or XP
DB2 Version 8.2 Enterprise Server Edition with FixPak 8
Microsoft SQL Server 2000
DB2 Migration Toolkit (MTK) Version 1.3
Microsoft SQL Server ODBC Driver Version 03.81.9031
For more information about this promotion, refer to the following Web site:
http://www.ibm.com/software/data/db2/migration
Technical training
IBM offers a workshop designed for ISVs and IBM Business Partners intending to
sell applications on DB2 UDB, and who are new to DB2 UDB. It begins with
building fundamental DB2 UDB skills, where participants learn how to migrate
their existing database to DB2 UDB. You actually bring your own database to the
workshop. In some cases (depending on database complexity), attendees have
been able to completely migrate their database within the five day period. Detail
information for DB2 Enablement workshops can be found at:
http://www.developer.ibm.com/spc/events/db2_en.html
Contacts
IBM services are available for assistance during any part the migration. If you
have a migration project in mind or you want to obtain the latest conversion
information (e.g., porting guides), please contact:
In North America and Latin America
mailto:db2mig@us.ibm.com
In UK, Europe, Middle East and Africa
mailto:emeadbct@uk.ibm.com
Chapter 1. Introduction 9
In Japan, India and Asia Pacific
mailto:dungj@hkl.ibm.com
Chapter 2. Architecture
In this chapter we compare the architectures of the Microsoft SQL Server (SQL
Server) and DB2 Universal Database (DB2 UDB) relational database
management system (RDBMS) products. Understanding these differences will
aid in the transition from SQL Server to DB2 UDB. The architectural differences
and terminologies are compared at a high level. It is not within the scope of this
book to cover in-depth configuration or performance tuning.
The resource settings for each instance, including memory, can be automatically
managed by DB2 UDB or can be user-defined and stored in individual
configuration files, making the way an instance uses memory highly configurable.
The instance runs as a Windows service and single application process
(process) called db2syscs.exe.
An example the DB2 UDB architecture is shown in Figure 2-1 and has the
following components:
One DB2 Administration Server (DAS)
One instance with a database manager
Two databases, database1 and database2
The following allocations for each database:
– Processes
– Log buffers and log files
Computer
DB2 instance
database1 database2
memory
processes processes
Primary
syscatspace log files fact_table
tempspace1
tablespace tablespace
tablespace
Primary
(4 KB page)
userspace1
disk
log files
tablespace syscatspace userspace1
tempspace1 tablespace tablespace
tablespace
The DB2 UDB administration server (DAS) performs a similar function to SQL
Server’s SQL Agent process. The purpose of the DAS is to provide a facility for
job scheduling and management through use of the Tools database allowing DB2
UDB to utilize both DB2 UDB command scripts and operating system scripts.
The DAS also handles remote administration requests from clients.
Chapter 2. Architecture 13
2.2 Memory allocation
Memory allocation is key in achieving optimal performance of a database. It is
important to understand how memory is allocated to an instance in DB2 UDB.
DB2 UDB’s memory model allows for a significant amount of tuning, making it
highly adaptable to many environments. First, we briefly describe about how
SQL Server’s memory is allocated, then describe in more detail how DB2 UDB
allocates memory.
The memory pool size and division continuously change because the regions
within the memory pool are constantly being adjusted to optimize performance.
In addition to SQL Server adjusting memory allocation it is possible for an
administrator to manually allocate memory to the instance. SQL Server is also
able to utilize memory above the 2GB-4GB limit through the use of the Address
Windowing Extensions (AWE) API.
DB2 UDB differs from SQL Server in that many of the memory structures are
allocated at much finer granularities, such as per instance, per database, per
connection, and per agent making DB2 UDB highly configurable for optimum
performance.
Database1
Shared Memory Database2
Shared Memory
The largest component of database global memory is the buffer pools and is
where all index and regular data is handled. A database must have at least one
buffer pool allocated, but can have a number of additional buffer pools depending
on the database workload and page sizes used in the database.
It is possible for DB2 UDB to utilize memory above the 2GB-4GB limit imposed
by 32-bit operating systems through the use of the AWE API.
Like the instance memory, database memory can be configured through the use
of various database configuration parameters. Both the database manager and
database configuration parameters can be set through Control Center or from the
Chapter 2. Architecture 15
command line Processor (CLP). More information about the Control Center and
CLP can be found in Chapter 3, “DB2 UDB administration tools” on page 31.
For all other environments, application shared memory does not exist.
More information about DB2 UDB memory management can be found in the DB2
UDB documentation DB2 Administration Guide: Performance V8, SC09-4821 or
the online Information Center.
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp
2.3 Processes
In this section we describe the differences between process implementation in
SQL Server and DB2 UDB. Both SQL Server and DB2 UDB (Windows version)
use a single process/multi-threaded model.
Table 2-1 shows a list of the DB2 UDB processes running on typical Windows
installation.
Chapter 2. Architecture 17
2.4 Allocating disk space
In this section, we describe the different methods SQL Server and DB2 UDB use
for data storage. You will see that SQL Server and DB2 UDB have very different
approaches to disk space allocation.
SMS table spaces are better suited for database contains many small tables.
These table spaces are also easier to maintain. The space in a SMS table space
is defined as a directory and is not pre allocated. Data can be added as long as
there is enough physical space available. More than one directory can be
assigned to the table space. With SMS table space, data, indexes, and large
objects are all stored together in the same table space.
DMS table spaces are better suited for large database where performance is a
key factor. Containers are added to DMS table spaces in the form of a file or raw
Chapter 2. Architecture 19
2.5.1 SQL Server transaction log overview
SQL Server uses the transaction log of each database to enable database
recovery. The transaction log records the start of each transaction, the changes
to the data, and should the need arise, enough information to undo any
modifications made within each transaction.
SQL Server uses three recovery models for its transaction logging.
Simple
– The simplest to manage.
– Only full database backups are allowed.
– Cannot back up just the changes made since the last full backup.
– Transaction logs will not become full as the log space is reclaimed when
transactions are committed.
– Non-logged operations are allowed.
Full
– Can create complete backups of the database
– Incremental backups of only the changes that have occurred since the last
full backup are possible.
– Allows point in time recovery.
– Space in the transaction log is only reclaimed when a backup of the
transaction log is made.
– Non-logged operations are not allowed.
Bulk-Logged
– Lies between the other two models.
– Incremental backups of the database are possible.
– Bulk copy operations are only minimally logged.
– If a bulk copy operation occurs, point-in-time recovery is not possible.
SQL Server log files are logically divided into “virtual” log files which are the unit
of truncation for the transaction log.
Both circular and archive logging allow non-logged operations to be carried out.
Unlike SQL Server, DB2 UDB is able to take incremental and delta backups for
both logging types. An incremental backup copies all database data that has
changed since the most recent full backup and a delta backup copies all
database data that has changed since the last backup (full, incremental, or
delta).
More information about the logging and recovery options can be found in the
DB2 UDB documentation Data Recovery and High Availability Guide,
SC09-4831.
Chapter 2. Architecture 21
The optimization process is one of the last elements in a chain of events
preceding SQL statement execution. Both DB2 UDB and SQL Server use a cost
based optimizer that determines the cost of a plan based on stored information
including database metadata and statistical parameters.
The first step in optimization is called “Trivial Plan Optimization”. The optimizer
tries to find the most inexpensive access plan based on the SQL statement being
executed. If a simple plan is not found, it then looks at the syntax of the statement
being executed to see if any corrections are necessary. If the optimizer thinks
that the best access plan still has not been found, it examines statistical
information it has on the indexes and starts its cost-based optimization process.
SQL Server’s table and index statistics are created and maintained with the
sp_updatestats system stored procedure and the CREATE STATISTICS T-SQL
command.
DB2 UDB also has a query rewrite feature that will rewrite the original query to
obtain the optimal access plan. Catalog statistics are updated using the runstats
utility. If an application uses static SQL, access plans are generated at bind time
using the statistics in the catalogs and are stored as executable code in the
database. If an application uses dynamic SQL, the access plan is generated at
runtime using current statistics and stored in a dynamic cache. Dynamic caching
allows for quick reuse of optimized code.
DB2 UDB’s cost-based optimizer bases its access plan on such information as:
Query optimization class
Two new features in DB2 UDB V8.2 are automatic statistics profiling and
automatic table statistics collection. Automatic statistics profiling allows DB2
UDB to determine which statistics need to be updated. DB2 UDB will then
automatically run the runstats utility in the background to make sure statistics
are kept up-to-date.
2.7 Parallelism
Both SQL Server and DB2 UDB support parallelism. Parallelism is the ability to
execute an SQL statement, perform I/O, or run certain utilities such as backup,
restore or load across multiple processors.
SQL Server then decides if parallel execution of the query is warranted based on
the server workload and configuration. It can also utilize parallel operations for
backup/restore and parallel load (bulk copy program).
Chapter 2. Architecture 23
2.7.2 DB2 UDB parallelism
DB2 UDB supports two types of parallelism, intra-partition parallelism and
Inter-partition parallelism and within in this DB2 can carry out parallel
operations for the following:
Query Parallelism. There are two types, inter-query parallelism and
intra-query parallelism.
– Inter-Query parallelism, the ability for a database to accept queries from
multiple applications at the same time.
– Intra-Query parallelism, the ability to process multiple parts of a query at
the same time.
Input/Output (I/O)
– Parallel I/O to one or more I/O devices.
Utilities
– Backup, restore, load, and index creation can take advantage of
intra-parallelism.
In addition to the db2diag.log there is an error log for the administration server
(DAS) called the db2dasdiag.log for recording information in relation to operation
of the DAS.
Both of these log files, as well other logs, configuration information, and
environment information can be gathered with the db2support command. This is
similar to SQL Server’s sqldiag command but gathers much more information
and negates the need to gather information for troubleshooting manually. The
db2support command and other problem determination tools are discussed in
Chapter 13, “Testing and troubleshooting” on page 397.
DB2 UDB also has an additional log used for problem determination. The notify
log, also referred to as the Administration Notification log is used to record
significant events that occur. On UNIX and LINUX platforms, the administration
notification log is a text file called instance_name.nfy, typically located in the
$HOME/sqllib/db2dump directory (where $HOME is the home directory of the
instance owner). On Windows, all administration notification messages are
written to the Windows Event Logs.
Chapter 2. Architecture 25
2.9 Security
In this section we describe how security is implemented in SQL Server and DB2
UDB and highlight some of the enhancements made with the introduction of DB2
UDB V8.2.
SQL Server uses “Roles to aid in controlling permissions on the database and
database objects. Roles are equivalent to groups that contain users. They
enable permissions to be granted more easily.
Authentication
The first step in accessing a DB2 UDB database is authentication. Before a user
can access an instance or database they must be authenticated. This is normally
handled by the operating system or another third party security product. DB2
UDB uses parameters in the database manager configuration file to determine
the type of authentication to be used and where users are authenticated.
Authorities
Once a user has been authenticated the next step is for DB2 UDB to evaluate
the authorities and privileges assigned to a user. Authorities are the right
assigned to a user to perform specific administrative, maintenance or utility
operations against databases within an instance. They can be compared to
server and database roles in SQL Server although their implementation is
different. Authorities are divided into the following groups:
SYSADM
This is the highest authority and gives the user full control of the instance. It
also has the authority to grant or revoke DBADM authorities and has control
over database manager resources.
Chapter 2. Architecture 27
SYSCTRL
Applies to operations affecting system resources and includes privileges to
stop an instance or a database, to create, update or drop a database, and to
create or drop a table space.
SYSMAINT
Enables a user to perform maintenance operations on all databases in an
instance. This authority gives the privilege to back up a database or a table
space, update database configuration files and restore an existing database.
The above three authority levels are configurable. For each DB2 UDB instance.
These authorities can only be assigned to groups. The names of the groups that
are assigned these authorities are stored in the database manager configuration
file associated with each instance.
DB2 UDB has further database level authorities and privileges that can be
granted at a database level such as:
DBADM
CREATEDB
BINDADD
CONNECT
LOAD
QUIESCECONNECT
The above database-level privileges and authorities are assigned using SQL
GRANT and REVOKE commands.
Privileges
Privileges convey the right of a user to perform specific operations on database
objects. For example a user may be able to issue an INSERT, UPDATE, or DELETE
against table but not alter the table definition.
Chapter 2. Architecture 29
2.10 High availability
High availability of systems can be achieved with correct planning and
implementation. Both SQL Server and DB2 UDB support high availability
strategies through the use of failover clustering, log shipping, replication and
mirroring.
DB2 UDB mirroring cannot be done at the table space level; it must be performed
outside of the database at the hardware level. DB2 UDB does support mirroring
of the transaction logs. It also supports the increased availability of data by
allowing mirrored copy of data to be split and made available for other types of
processing.
Note: The examples shown in this chapter are based on the SAMPLE
database provided with DB2 UDB.
The following are some of the key tasks that you can be performed using the
Control Center:
Add DB2 UDB systems, federated systems such as DB2 UDB for z/OS and
OS/390 systems, IMSysplexes, instances, databases, and database objects
to the object tree.
Manage database objects. Create, change, and drop databases, table
spaces, tables, views, indexes, triggers, and schemas.
Manage data. Load, import, export, and reorganize data. You can also gather
statistics and run queries.
Perform maintenance operations such as backups and restores.
Configure and tune instances and databases.
Manage database connections, such as DB2 Connect servers and
subsystems.
Manage DB2 UDB for z/OS and OS/390 subsystems.
Manage applications.
Figure 3-1 shows the Control Center. At the top of the window, under the main
menu, you will see the Tools option. This option provides you with access to a
Within Control Center you have the ability to set the configuration options at both
the instance and database level. New in DB2 UDB V8.2 is the ability to set
configuration parameters online. Changes to these online configuration
parameters take effect immediately without the need to stop and re-start the
instance, or deactivate and activate the database. Some SQL Server
configuration options can be managed using Enterprise Manager or the
sp_configure system stored procedure; however, some configuration options
require you to stop and restart the server before the new values take effect.
You can launch the Command Editor from Start Menu → Programs → IBM
DB2 → Command Line Tools → Command Editor or from a command line by
Figure 3-2 shows a Command Editor window. There are three “tabs”, Command,
Query Results, and Access Plan. The Command tab is used for entering DB2
UDB commands and SQL statements. The Query Results tab is where the
results of any queries are displayed. The Access Plan tab show a graphical
representation of the access plan used for an SQL statement.
In addition, Visual Explain displays the statistics that were used at the time of
optimization. You can then compare these statistics to the current catalog
statistics to help you determine whether rebinding the package might improve
performance. You can determine whether or not an index was used to access a
table. If an index was not used, Visual Explain can help you determine which
columns might benefit from being indexed. Visual Explain is particularly useful for
displaying the effects of performance tuning.
Task schedules are managed by a scheduler, included with DB2, while the tasks
are run on one or more systems, called run systems. You define the conditions
for a task to fail or succeed with a success code set. Based on the success or
failure of a task, or group of tasks, you can run additional tasks, disable
scheduled tasks, and other actions. You can also define notifications to send
after a task completes. You can send an e-mail notification to people in the
contacts list, or you can send a notification to the Journal. The Task Center can
be launched either from the Control Center Tools menu or by clicking the Task
Center icon. You can save tasks to Task Center from many of the other GUID
administration Wizards. For example, a backup job created using the backup
3.1.6 Journal
The Journal is similar to the “alerts” funtionality built into the SQL Server Agent
and is used to view historical information about tasks, actions and operations
carried out against a database as well as alerts and messages. You can launch
the Journal from Start Menu → Programs → IBM DB2 → General
Administration Tools → Journal or from a command line by typing db2journal.
You can also launch the Journal from any of the DB2 UDB GUI tools’ Tools menu
or by clicking the Journal icon .
The Journal allows you to view the details and results of any task that has been
executed. Figure 3-6 shows an example of Backup and Reorg tasks that
completed. The Journal can also display information relating to database history,
messages and information from the notification log.
Figure 3-7 shows Health Center with an attention notification against the
USERSPACE1 table space.
You can launch the Development Center from Start Menu → Programs → IBM
DB2 → Development Tools → Development Center or from a command line
by typing db2dc. You can also launch the Development Center from any of the
DB2 UDB GUI tools from the Tools menu or by selecting the Development
Center icon . Development Center (Figure 3-8) can also be launched from
Microsoft Visual Studio.NET and IBM WebSphere Studio Application Developer.
Support for the .NET framework has been extended to include support of CLR
procedures developed with C# and VisualBasic.NET
The Information Center can either be installed either locally or can be accessed
on the Internet. Information Center can be launched from Start Menu →
Programs → IBM DB2 → Information → Information Center or by selecting
the Information Center icon in any of the GUI tools You can launch the
online Information Center from a Web browser using the following URL:
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp
If the Information Center is installed locally, you can type db2ic at a command
line to start the Information Center. You also can access Information Center from
the Help button in any of the GUI tools, this will display help on the particular task
you are carrying out.
You can use the Replication Center to set up the three types of replication: SQL
replication, queue replication, and event publishing. You can specify
unidirectional and bidirectional replication with one or more servers. Use the
Replication Center to:
Create replication control tables.
Register replication sources.
Create subscription sets and add subscription set members to subscription
sets.
Operate the Capture program.
You can launch the Replication Center from Start Menu → Programs → IBM
DB2 → General Administration Tools → Replication Center or from a
command line by typing db2rc. You can also launch the Replication Center from
any of the DB2 UDB GUI tools from the Tools menu or by clicking the Replication
Center icon .
Replication can be used to copy large quantities of data from warehouse sources
into a warehouse target, and then capture any subsequent changes to the
source data. These operations are supported on all of the DB2 UDB workstation
operating environments, DB2 UDB for zSeries®, DB2 for iSeries™, and non-DB2
databases systems such as SQL Server, using WebShpere Information
Integrator. You can also use the Data Warehouse Center to move data into an
online analytical processing (OLAP) database. An expanded functionality version
of DB2 Warehouse Center, called DB2 Warehouse Manager, is available as an
additional cost option. Figure 3-13 shows the Data Warehouse Center with an
SQL Server data source setup. You can launch Data Warehouse Center from
Start Menu → Programs → IBM DB2 → Business Intelligence Tools → Data
Warehouse Manager or from a command line by typing db2dwc. It can also be
launched from any of the DB2 UDB GUI tools’ Tools menu or by clicking the Data
Warehouse Center icon from any of the GUI tools.
Within the Satellite Administration Center, you can create groups, application
versions, satellites, batches, and authentication. Information about the satellite
environment is stored in a central database known as the satellite control
The DB2 Web tool is based on a three-tier architecture. The first tier is the Web
client HTTP browser. The middle tier is an application server that hosts the
business logic such as IBM WebSphere Application Server and set of
applications. This middle tier provides the underlying mechanisms for the
communication (HTTP/HTTPS) with the first tier (Web client browser) and the
third tier (database or transaction server).
The DB2 Web Command Center implements many of the already existing
features of the Command Center but it does not contain the SQL Assist wizard or
Figure 3-16 shows the same script presented in 3.1.2, “Command Editor” on
page 33, being executed in Web Command Center.
The DB2 Web Command Center and Web Health Center are targeted for use
with the HTTP clients (browsers) available on devices such as notebooks as well
as Web-enabled PDAs and Palm devices. For more information about installing
and setting up the Web tools refer to the DB2 UDB documentation Installation
and Configuration Supplement, GC09-4837.
3.2 Wizards
DB2 UDB has a number of graphical wizards designed to guide the user through
administration processes such as creating objects and manipulating data. The
wizards greatly improve productivity, especially when making the transition from
SQL Server to DB2 UDB. Some of the wizards accessible from the Control
Center are:
Create Database with Automatic Maintenance
Create Database
Create Table Space
Create Table
Design Advisor
Configuration Advisor
Backup
Restore Data
Load
Configure Database Logging
Add Partitions Launchpad
Configure Automatic Maintenance
The wizards can be launched from context menus within Control Center or from
the Wizards launchpad as shown in Figure 3-18.
3.3 Advisors
Among the wizards, there are two “advisors” that help with database
configuration and performance, the Configuration Advisor and the Design
Advisor.
Configuration Advisor
Configuration Advisor can be used to help configure instance and database
parameters that improve performance. The advisor asks for information such as:
Amount of memory available to the Database Manager
Type of workload expected
Expected number of transactions
Transaction performance optimization
Whether or not the database is already populated with data
When the Configuration Advisor finishes evaluating the input received, a table
providing the current and new recommended parameter values is displayed.
Figure 3-19 shows the results displayed after running the Configuration Advisor.
Changes can either be applied immediately or scheduled in the Task Center.
Design Advisor
The Design Advisor helps to determine the optimal set of database objects
needed to achieve optimal workload performance. You provide the Design
Advisor with a set of SQL statements that represent the typical workload and
Design Advisor recommends additional indexes, materialized query tables
(MQTs), multidimensional clustering tables, the repartitioning of tables, and the
removal of objects no longer used by the specific workload.
When using the CLP in Interactive mode, you should only type the DB2
command. Figure 3-20 shows the “CONNECT TO <database>” command being
used. To invoke an operating system command in Interactive mode, prefix the OS
command with !, for example db2=>! dir.
In Command Mode, each DB2 command must be prefixed with db2. Figure 3-21
shows the “CONNECT TO <database>” command. Operating system commands
can be issued normally in this mode.
C:\db2 -f clptest.txt
For more information about the CLP, refer to the following article:
http://www.ibm.com/developerWorks/db2/library/techarticle/dm-0503melnyk
/index.html
3.5 Utilities
DB2 UDB has a complete set of maintenance utilities, available through the
command line and graphical interface. In this section, we show the wizards and
the command syntax for each utility.
Inspect
The inspect command inspects table spaces and tables for their architectural
integrity, while the database remains online. Inspection validates table objects
and table space structures. To run inspect, a database connection is required as
are the following authorizations:
SYSADM
DBADM
SYSCTRL
SYSMAINT
CONTROL privilege if single table.
For example, if you wanted to check the integrity of a table space with an ID
number of 4, you would issue the command:
After you run inspect, the db2inspf command should be run to format the
results, as the following example shows:
db2inspf inspect.out inspect.fmt
You can then view the results of the inspect by viewing the inspect.fmt file with a
text viewer. The default location for the output file is the instance profile directory.
The default instance profile directory is C:\Program Files\IBM\SQLLIB\DB2 on
Windows systems. The output file is only available when there is at least one
error.
For a full summary of all of the parameters available for the inspect, refer to
DB2 UDB documentation Command Reference, SC09-4828, for further
information.
DB2DART
The db2dart utility performs a similar task to inspect; however, no database
access is allowed while the tool is running. Some of the parameters that can be
specified with db2dart include:
/DB
– Inspects the entire database. This is the default option if no parameters
are specified.
/T
– Inspects a single table.
/TSF
– Inspects table space files and containers.
/TSC
– Inspects a table space's constructs, but not tables within the table space.
/TS
In addition, db2dart has the ability to perform certain repairs with the following
parameters:
/ETS
– Extends the table limit in a 4 KB table space (DMS only).
/MI
– Marks an index as invalid. The database must be offline when this
parameter is used.
/MT
– Marks a table with a drop-pending state. As with the /MI parameter, the
database must be offline.
/IP
– Initializes the data page of a table as empty. Again the database must be
offline to use this parameter.
For example, the command you would run to inspect the EMPLOYEE tables in the
REDBOOK database would be:
This will generate a plain text file called REBDBOOK.RPT in the SQLLIB\DB2\DART
directory. You do not have to format the output for db2dart.
As with inspect you can find a a full summary of all of the parameters available
with db2dart refer to the DB2 UDB product documentation Command Reference
SC09-4828 for further information.
Example 3-2 shows the results of checking the integrity of a backup image of the
SAMPLE database.
With this tool, it is possible to create a test database where the access plans
created are similar to those that would be used on the production system.
The db2look utility can also be accessed from the Control Center at the database
or the table level by right-clicking the database or table, and selecting Generate
DDL.
Clicking the Show Command button shows the syntax of the command that can
be used in the CLP. In this case, the command would be:
Example 3-3 shows the DDL generated with the db2look execution in
Figure 3-22.
Example 3-3 DDL for the Employee table in the SAMPLE database
-- This CLP file was created using DB2LOOK Version 8.2
-- Timestamp: 02/03/2005 10:56:17 AM
-- Database Name: SAMPLE
-- Database Manager Version: DB2/NT Version 8.2.1
-- Database Codepage: 1252
-- Database Collating Sequence is: UNIQUE
-- Mimic tablespace
------------------------------------------------
-- DDL Statements for table "KITTY "."EMPLOYEE"
------------------------------------------------
COMMIT WORK;
CONNECT RESET;
You can register the DB2 UDB Performance Monitor counters with the db2perfi
command, as shown in Example 3-4:
C:\Program Files\IBM\SQLLIB\BIN>
There are other, third party tools that can be used to aid in performance
monitoring, in addition to Performance Monitor. These tools include:
Kernrate
http://www.microsoft.com/downloads/details.aspx?FamilyID=d6e95259-8d9d-4c22
-89c4-fad382eddcd1&DisplayLang=en
Vtune
http://www.intel.com/software/products/vtune/vpa/
CodeAnalyst
http://www.developwithamd.com/appPartnerProg/codeanalyst/home/index.cfm?act
ion=home
GA Tools Package
http://ausgsa.ibm.com/projects/w/wbiperftools/documentation/tools/ga/packag
es/win32.html
For more information, refer to the redbook DB2 Performance Expert for
Multiplatforms V2, SG24- 6470.
For more information, refer to redbook DB2 REcovery Expert for Multiplatforms,
SG24- 6421.
Using HPU, you can quickly unload your DB2 data into a variety of formats, such
as flat files, tape, or named pipes. Additional features include:
Fast SQL-like SELECT capabilities, such as filtering columns and rows using a
WHERE clause
Unloads a sampling of data (for example, every nth row)
Unloads data from online databases or database backups
Unloads all data or data from selected table spaces
Consider using High Performance Unload if you need to significantly reduce your
unload times or batch processing windows, or if you are performing multiple
unloads against the same table.
DBAs need test data to use when evaluating new software products.
Consider DB2 Table Editor when you need to edit DB2 tables, need easy-to-build
forms capability for end users, or need access to DB2 data.
Consider DB2 Web Query Tool when you need comprehensive query and
comparison capabilities without compromising DB2 security or data integrity,
require thin client access from many different devices on your network, or need
access to DB2 databases across an enterprise.
SQL Server complies with the entry level of ANSI SQL92 standard and with the
Federal Information Processing Standards (FIPS 127-2) as established by the
US National Institute of Standards and Technology (NIST). The commands SET
FIPS_FLAGGER and SET ANSI_DEFAULTS change the level of compliance and
SQL92 behavior.
DB2 UDB complies with the SQL92 standard at the entry-level and includes
additional features from the intermediate and full levels. DB2 UDB also contains
many core features of the SQL99 standard, and many features that are beyond
the SQL99 Core are partially or completely supported. There are no commands
to change the SQL compliance level, but the SQLFLAG option on the PREP
command can be used in embedded SQL applications to check that SQL syntax
conforms to the SQL92 Entry Level syntax.
Differences in implicit type conversions between SQL Server and DB2 UDB can
sometimes result in complications in the translation of DML statements. For
example, the following SQL statements are valid in SQL Server, but not in DB2
UDB:
CREATE TABLE t1 (c1 DATETIME)
CREATE TABLE t2 (c1 VARCHAR(26))
The above INSERT statement is valid in SQL Server because the DATETIME data
type in column c1 is implicitly casted as a VARCHAR. DB2 UDB does not implicitly
convert a TIMESTAMP into a VARCHAR. In order to correctly translate this statement,
the wildcard character (*) must be replaced by the names of all the required
columns, explicitly casting each column to its appropriate data type where
necessary. The INSERT statement above can be converted to DB2 UDB as:
INSERT INTO t2 SELECT TO_CHAR(c1) FROM t1
where the function TO_CHAR() explicitly converts a TIMESTAMP data type into a
character string data type in a specified format. Examples of working with dates
and times are included in 4.3, “Date and time considerations” on page 70.
The direct equivalent of ROWVERSION in DB2 UDB is the TIMESTAMP data type.
However, a better solution is to use an IDENTITY column. Example 4-1 shows
how to define a table with a unique, automatically generated column in DB2 UDB.
There are three additional methods of converting ROWVERSION data types to DB2
UDB (i.e., generating unique values):
GENERATE_UNIQUE()
sequence objects
stored procedures
The DB2 UDB function GENERATE_UNIQUE() in Example 4-2 generates a bit data
character string that is unique between subsequent calls. This allows for an
infinite number of values to be generated while ensuring that all values are
unique throughout the database.
In both SQL Server and DB2 UDB, NULL is interpreted as a synonym for
“unknown”. As such, you cannot write expressions that test for nullability using
the equals (=) operator. For instance, the expression:
SELECT ... WHERE empno = NULL
is not provided for in the ANSI SQL standard and will return an error. Expressions
that test for nullability must use the IS [NOT] NULL clause. The expression above
can correctly be re-written as:
SELECT ... WHERE empno IS NULL
In SQL Server, the ANSI NULL behavior is set by default. However, if the ANSI
NULL option is not set, SQL Server interprets equality and inequality comparisons
involving a variable, such as the expression someColumn = @someVariable, in
some nonstandard way. In particular, when both someColumn and @someVariable
are NULL, the expression will evaluate to true in SQL Server, but will evaluate to
NULL (as per the SQL standard) in DB2 UDB. Additionally, the result of
concatenation and arithmetic operations (e.g., NULL + 3, 'MAR' concat NULL) are
different between SQL Server and DB2 UDB.
The NOT LOGGED option can be specified when declaring LOB columns. It is
mandatory for columns greater than 1 GB in size. Generally, it is recommended
A second option that can be used with LOB columns is COMPACT. Using the
COMPACT option incurs a performance cost; however, it can greatly decrease disk
usage, especially on operating systems that do not support sparse file allocation.
DB2 UDB does not store LOB data in its internal buffer pools. Instead, data is
read directly from the disk each time it is needed. Because of this, it is highly
recommended that LOB data be placed in an SMS table space or a DMS table
space that uses file containers. Both of these types of table spaces will take
advantage of the operating system's file caching, reducing the I/O requirements
and increasing performance.
Users should be aware of the restrictions of the use of LOBs in DB2 UDB. These
restrictions include LOBs not being permitted in:
Indexes
A GROUP BY clause
An ORDER BY clause
The pattern operand in a LIKE predicate, or the search string operand in a
POSSTR function
Full detail about LOBs, including the complete list of limitations, can be found in
the DB2 UDB documentation Application Development Guide: Programming
Server Applications, SC09-4827.
SQL Server has two date and time data types, DATETIME and SMALLDATETIME.
DB2 UDB has three distinct data types for date and time values, DATE, TIME and
TIMESTAMP. Since DB2 UDB uses strong data-typing, implicit conversion between
these data types is not performed.
Both of SQL Server’s types can be mapped to DB2 UDB’s TIMESTAMP data type;
however, this may not be the best solution. If only the date portion of a DATETIME
or SMALLDATETIME column is used by the application, it is more efficient to convert
it to DB2 UDB’s DATE type. Similarly, if only the time portion of a DATETIME or
SMALLDATETIME column is used by the application, it is more efficient to convert
DB2 UDB stores the current date and time in special system registers. The
values retrieved from these registers are based on a reading of the time-of-date
clock when the SQL statement is executed at the application server. These
special registers are accessible using a VALUES or SELECT statement:
VALUES CURRENT TIMESTAMP
VALUES CURRENT DATE
VALUES CURRENT TIME
Or,
SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1
SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1
SELECT CURRENT TIME FROM SYSIBM.SYSDUMMY1
Note: If these special registers are used more than once within a single SQL
statement, or mixed together within a single statement, all values retrieved are
based on a single clock reading.
DB2 UDB supports the functions TO_CHAR() and TO_DATE(), but only one format
is natively supported, as illustrated here:
TO_CHAR (<timestamp_expression>,'YYYY-MM-DD HH24:MI:SS')
TO_DATE (<string_expression>, 'YYYY-MM-DD HH24:MI:SS')
A DB2 UDB user defined function (UDF) is an ideal solution for creating a
customized to_char() or to_date() casting function. The following examples
(Example 4-3 and Example 4-4) show what these functions might look like.
Note that the above functions are different than the built-in to_char() and
to_date() functions, since they offer the ability for customization.
The CHAR() function in DB2 UDB can be used to convert a date or time value to a
character string in a local or specified standard format, such as:
CHAR(CURRENT DATE, ISO);
CHAR(CURRENT DATE, USA);
The following examples show how DB2 UDB dates can be converted into
different formats:
CHAR(CURRENT DATE) => '10/01/2003'
CHAR(CURRENT DATE + 3 DAYS) = '10/04/2003'
CHAR(CURRENT DATE, ISO) = '2003-10-01'
CHAR(CURRENT DATE, EUR) = '01.10.2003'
CHAR(CURRENT DATE, JIS) = '2003-10-01'
CHAR(CURRENT TIME, USA) = '02:21 PM'
CHAR(CURRENT TIME + 2 HOURS, EUR) ='16.21.23'
Note: For more information about the CHAR() function, refer to the DB2 UDB
documentation SQL Reference Volume 1, SC09-4844.
The only arithmetic operations that DB2 UDB supports for date and time values
are addition and subtraction. These operations are supported through the normal
addition and subtraction operators (i.e., + and -). Supplementing these
The following examples show how date arithmetic may be performed in SQL
Server:
DATEADD(day, 1, GETDATE())
DATEADD(month, 1, GETDATE())
DATEADD(year, 1, GETDATE())
DB2 UDB also provides additional convenience functions when working with date
and time values, such as DAYNAME(), MONTHNAME(), and many more.
DB2 UDB has similar functions for extracting components from date and time
values. The equivalent DB2 UDB function to the above SQL Server DATEPART
function is:
DAY(CURRENT DATE)
Table 4-1 shows the mapping of various DATEPART() parameters in SQL Server
to the corresponding functions in DB2 UDB.
Another ideal place for using a UDF is converting the SQL Server
months_between() function:
months_between(sysdate,v_date)
If you use the IBM DB2 Migration Toolkit (MTK) to automate the conversion, MTK
implements the months_between() as a UDF and automatically deploys it into the
target DB2 UDB database. Example 4-5 shows the source code for the
months_between() function:
This function uses the DB2 UDB built-in function TIMESTAMPDIFF(). Details about
TIMESTAMPDIFF() can be found in the DB2 UDB documentation SQL Reference -
Volume 1, SC09-4844.
For more information about manipulating date and time values, refer to the article
DB2 Basics: Fun with Dates and Times on the DB2 developerWorks Web site at:
http://www.ibm.com/developerworks/db2/library/techarticle/0211yip/0211y
ip3.html
In SQL Server, CHAR columns are of variable length whereas in DB2 UDB, they
are of fixed length. The following example illustrates the implications of this.
Suppose the following SQL Server table definition:
CREATE TABLE t (cOne CHAR(1),
cNull CHAR(5),
cNotNull CHAR(5) NOT NULL);
Table 4-2 shows the resulting character string lengths when the values shown
are inserted into the tables defined above.
INSERT INTO t 1 1
VALUES ('x ', 1 5
'x ', 1 5
'x ')
INSERT INTO t 0 1
VALUES (' ', 0 5
' ', 0 5
VALUES (' '
INSERT INTO t 0 1
VALUES (' ', 0 0
' ', 0 5
' ')
The following information should also be kept in mind while working with strings
in DB2 UDB:
Concatenating a NULL value to a string will always result in a NULL value. For
instance, if a variable has not been initialized or contains a NULL value,
concatenation with a character string will evaluate to a NULL value.
The empty string literal (denoted as ‘’ in a SQL expression) is NOT equivalent
to a NULL value.
The empty string literal is NOT equivalent to the single space string (denoted
as ‘ ‘ in a SQL expression).
In DB2 UDB, all database object identifiers (tables, views, columns, etc.) are
stored in the catalog tables in uppercase characters, unless they are explicitly
delimited upon object creation. If a delimited identifier is used to create the
object, the exact case of the identifier is stored in the catalog tables. An identifier,
such as a column name or table name, is treated as case insensitive when used
in an SQL statement unless it is explicitly delimited. For example, assume that
the following statements are issued in DB2 UDB:
CREATE TABLE MyTable (id INTEGER)
CREATE TABLE "YourTable" (id INTEGER)
Two tables, MYTABLE and YourTable exist, and the following two statements are
therefore equivalent:
SELECT * FROM MyTable
SELECT * FROM MYTABLE
However, the second statement below will fail with a SQL0204N error saying it
cannot find an object (table) named YOURTABLE (uppercase):
SELECT * FROM "YourTable" -- executes without error
SELECT * FROM YourTable -- error, table not found
DB2 UDB strings are also case sensitive. For example, the string ‘daTAbase’ is
not the same as the string ‘DATABASE’. Case sensitivity must be taken into
account when comparing strings to ensure consistent results. One strategy for
doing this involves always performing string comparisons using the uppercase or
lowercase representation of the string, via the UPPER() and LOWER() DB2 UDB
functions. Additionally, views constructed over base tables can present the data
in upper or lower case. Application users would then perform all operations
against the views so that case does not become an issue.
For more information about the above case-sensitivity solutions in DB2 UDB,
refer to the article, “Making DB2 Case-Insensitive” on the DB2 developerWorks
Web site:
http://www.ibm.com/developerworks/db2/library/techarticle/0203adamache/
0203adamache.html
Unqualified columns
SQL Server permits the use of an unqualified column wildcard (*) alongside other
elements in the SELECT clause list. DB2 UDB adheres to the SQL standard which
states that a SELECT element that contains an unqualified column wildcard cannot
contain anything else. DB2 UDB requires these elements to be qualified (a
sequence of t1.*, t2.*, ... where t1, t2, ... are the tables in the FROM
clause).
For example, the following query is valid in SQL Server, but not in DB2 UDB:
SELECT e.*, * FROM employee e, jobs j WHERE e.job_id = j.job_id
In DB2 UDB, the FROM clause should always be specified as part of a SELECT
statement. The SYSIBM.SYSDUMMY1 view can be used when a table or view name
is not applicable. This view contains a single row. The above statements can be
translated as:
SELECT (4 * 5) FROM SYSIBM.SYSDUMMY1
Alternatively, the VALUES statement can be used instead of the SELECT statement,
as the following examples show:
VALUES (4 * 5)
VALUES CURRENT TIMESTAMP
Variable assignments
SQL Server syntax for queries involving assignment to variables also differs from
the DB2 UDB syntax. For example, the SQL Server statement:
SELECT @v_max=MAX(c1) FROM table1
Special attention should be given to SELECT INTO statements in DB2 UDB since if
the number of rows returned by the query is greater than one, DB2 UDB will raise
an exception. The default behavior of SQL Server is to use the last row of the
result set. When converting such statements to DB2 UDB, the FETCH FIRST 1
ROW ONLY clause should be used, for example:
SELECT c1 INTO v_c1 FROM table1 FETCH FIRST 1 ROW ONLY
COMPUTE
By including the COMPUTE clause in a SELECT statement, SQL Server generates
totals that appear as additional summary columns at the end of the result set.
When used in conjunction with the BY clause, control-breaks and subtotals are
generated in the result set. For instance, the following SQL Server query
generates an additional summary column for each department consisting of the
sum of employee salaries for that department:
SELECT dept_id, salary
FROM staff order by dept_id COMPUTE sum(salary) BY dept_id
To convert this functionality to DB2 UDB, the UNION ALL of multiple SELECT
statements can be used. The following query shows how the above SQL Server
query can be converted:
SELECT CHAR(dept_id) dept_id, salary FROM staff
UNION ALL
SELECT CHAR(dept_id)||'Sum' dept_id, SUM(salary) salary
FROM staff GROUP BY dept_id
UNION ALL
SELECT 'Sum' dept_id, SUM(salary) salary FROM staff ORDER BY dept_id
The following SQL Server SELECT statement uses column alias names and table
correlation names:
SELECT DepartmentNo = d.deptno,
EmployeeNo = e.empno,
EmployeeName = e.firstnme || ‘ ‘ || lastname
FROM department d INNER JOIN employee e ON d.deptno = e.workdept
DB2 UDB syntax uses the AS keyword to define column aliases and table
correlation names, as shown below:
SELECT d.deptno AS DepartmentNo,
e.empno AS EmployeeNo,
e.firstnme || ‘ ‘ || lastname AS EmployeeName
FROM department AS d INNER JOIN employee AS e ON d.deptno = e.workdept
The second statement is not valid in DB2 UDB because the number of columns
in t1 does not match the number of columns specified in the VALUES clause. DB2
UDB requires that the columns for which the values are intended are explicitly
stated, as shown in the valid translation below:
INSERT INTO t1(c1) VALUES(1)
The following INSERT statement inserts the values (1, column default, NULL) into
a new row, for columns c1, c2, and c3, respectively.
INSERT INTO defaulttab DEFAULT VALUES
To achieve the same functionality in DB2 UDB, the default keyword must be
specified as part of the INSERT statement for each column where the column’s
default value is to be used. Assuming the equivalent table definition in DB2 UDB:
CREATE TABLE defaulttab (
c1 INT IDENTITY GENERATED ALWAYS BY DEFAULT,
c2 VARCHAR(30) NOT NULL
CONSTRAINT default_name DEFAULT ('column default'),
c3 INT)
Alternatively, DB2 UDB tries to use a column’s default value, if one exists, to
supply a value for column not specified in an INSERT statement. For example,
executing the following statement:
INSERT INTO defaulttab(c1, c3) VALUES (default, default)
inserts the values (1, column default, -) into table defaulttab. Notice how the
default value for column c2 is used, even though it was not specified in the INSERT
statement.
This code could be converted to DB2 UDB using two stored procedures in which
one procedure invokes the other, receives the returned result set, then fetches
each row from the returned result set and inserts it into the intended table.
However, this may not be the best solution. In the example above, it is much
simpler to duplicate this behavior using an INSERT statement with a sub-SELECT
statement, like the following:
INSERT INTO table1 SELECT * FROM t1
Since there is no single technique to convert this feature to DB2 UDB, the actual
technique used will depend on how/why this functionality is being used in your
application.
In this example, non-determinism arises when there is more than one matching
row in t2, meaning any of the c1 values of these rows can be selected to do the
update. If there is at most a single matching row in t2, the equivalent DB2 UDB
statement is:
UPDATE t1 SET c1 = (SELECT DISTINCT t2.c1 FROM t2 WHERE t1.c2 = t2.c2)
WHERE EXISTS(SELECT * FROM t2 WHERE t1.c2 = t2.c2)
DB2 UDB also supports retrieving result sets from SQL data-change operations
(INSERT, UPDATE, and DELETE). SELECT and SELECT INTO statements can be used to
retrieve result sets from SQL data-change operations embedded in the FROM
clause of such statements. The following example illustrate this functionality:
SELECT name INTO p_name FROM NEW TABLE
(UPDATE staff SET salary = 10000.0 WHERE id = p_id)
This syntax is more efficient than issuing separate modification and retrieval
statements, thus adopting this syntax is recommended where appropriate.
Note: At the completion of the current unit of work, the NOT LOGGED
INITIALLY attribute is deactivated and all operations that are performed on
the table in subsequent units of work are logged again.
The DB2 UDB syntax for joins is ANSI-style, with the operators:
INNER
LEFT [OUTER]
RIGHT [OUTER]
FULL [OUTER]
Inner joins
The SQL Server syntax for inner joins is similar to the DB2 UDB syntax. The
following example shows what the syntax for an inner join looks like in both SQL
Server and DB2 UDB.
SELECT r.title, a.name
FROM authors a
INNER JOIN redbooks r
ON a.author_id = r.author_id
Outer joins
SQL Server supports the use of proprietary outer join operators (*= and =*) for
backward compatibility. Table 4-3 shows how this proprietary syntax can be
mapped to DB2 UDB supported syntax:
If the ANSI style outer join syntax is already used, it is unlikely that changes will
need to be made during a conversion to DB2 UDB.
Cross join
A cross join (also known as a cartesian product) produces every possible join
permutation of the selected columns. The example in Table 4-4 translates to
“every job is available in all departments”. In DB2 UDB, cross joins are specified
by not including any join conditions in the WHERE clause of the SELECT statement.
Example 4-6 SELECT statements with NULL values at the end of the result set
SELECT city FROM authors ORDER BY city; -- [1]
SELECT city FROM authors ORDER BY city ASC; -- [2]
SELECT COALESCE(city,’’) FROM authors ORDER BY 1 DESC; -- [3]
SELECT COALESCE(city,’’) AS city FROM authors ORDER BY city DESC; -- [4]
Example 4-7 SELECT statements with NULL values at the beginning of the result set
SELECT COALESCE(city,’’) FROM authors ORDER BY 1 ASC; -- [5]
SELECT COALESCE(city,’’) AS city FROM authors ORDER BY city ASC; -- [6]
SELECT city from authors ORDER BY city DESC; -- [7]
Using the above approach, care must be taken because the application is
returned an empty string, not a NULL value. If that must be avoided, the original
column in the SELECT list can be kept and ordered on the COALESCE() form of it, as
the following altered version of query [4] above illustrates:
SELECT city,
COASLESCE(city,’’) AS city_no_nulls
FROM authors
ORDER BY city_no_nulls DESC;
There is no equivalent in DB2 UDB for the TOP n PERCENT clause. To convert this
functionality, customized cursor logic is required.
4.6.9 Cursors
SQL Server supports all ANSI-style cursors: static, dynamic, forward only, and
keyset-driven. This includes support for INSENSITIVE and SCROLL cursor behavior
and for all fetch options (FIRST, LAST, NEXT, PRIOR, RELATIVE, and ABSOLUTE).
Cursor support is available through the following interfaces: ADO, OLE DB,
ODBC, DB-Library, and T-SQL.
DB2 UDB supports static, forward-only, and scrollable cursors. There are two
types of scrollable cursor: static and keyset-driven. The latter provides the ability
to detect or make changes to the underlying data. Application support for static
scrollable cursors is provided through DB2 CLI, ODBC, JDBC, SQLJ, and SQL
PL. Keyset-driven scrollable cursors are supported through DB2 CLI and ODBC.
DB2 UDB has a few additional restrictions on cursor usage than SQL Server. For
example, DB2 UDB’s SQL PL procedural language does not support a cursor
definition that includes more than one table specified in the FROM clause of the
cursor’s SELECT statement (columns to be updated all belong to the same table).
Transaction behavior
The default behavior in SQL Server is to have cursors remain open after a COMMIT
or ROLLBACK statement is issued. This behavior strays from the ANSI SQL
standard, although it is possible to configure SQL Server to use the ANSI SQL
standard behavior of closing open cursors after a COMMIT or ROLLBACK statement
is issued.
DB2 UDB’s default behavior follows the ANSI SQL standard of closing open
cursors whenever a COMMIT or ROLLBACK statement is issued. However, cursors
that are declared with the WITH HOLD option remain open after a COMMIT statement
is issued. In DB2 UDB, all open cursors are closed when a ROLLBACK statement is
issued.
Note: In DB2 UDB, after a COMMIT statement has been issued, a cursor
declared with the WITH HOLD statement remains open and is positioned before
the next logical row of the results table. Additionally, all locks are released
except for locks protecting the current cursor position.
DB2 UDB does not have a similar DEALLOCATE command. Cursors should be
explicitly closed when they are no longer needed, and re-declared if and when
they are needed again.
@@CURSOR_ROWS
In SQL Server, the @@CURSOR_ROWS function returns the number of qualifying rows
from the previous cursor declaration. In DB2 UDB, a local (counter) variable can
be used to store this information after each FETCH operation from the cursor.
Example 4-8 shows how this can be implemented in DB2 UDB. In the example, a
local variable is declared to hold the number of fetched rows [1] and is
incremented each time a new row is fetched [2].
LOOP
FETCH c1 INTO my_ename, my_deptno;
SET v_cursor_rows = v_cursor_rows + 1; -- [2]
...
END LOOP;
OPEN c1;
WHILE 0 = v_sqlstatus DO -- [4]
FETCH c1 INTO ...
...
END WHILE
CLOSE c1;
It should be noted that DB2 UDB throws an exception when a warning or an error
is encountered during processing. In the variable declaration part of the code [1],
a CONTINUE HANDLER is defined for the case that no data is found [2] and another
CONTINUE HANDLER is defined [3] for any other exception that arises. In both CONTINUE
HANDLERs, a flag variable is set, which is checked in the WHILE loop condition [4]. If
the flag is set by one of the CONTINUE HANDLERs, cursor processing (the WHILE loop
processing) ceases.
A second class of SQL functions exist under both platforms, but are named
differently (e.g., CHARINDEX() for SQL Server, which is equivalent to LOCATE() for
DB2 UDB). Converting these functions can be done either by modifying the
actual function calls in the application code, or by creating a sourced user
defined function (UDF). A sourced UDF is defined using a reference to another
Example 4-10 Sourced user defined function called CHARINDEX in DB2 UDB
CREATE FUNCTION CHARINDEX( VARCHAR(4000),
VARCHAR(4000),
INTEGER)
RETURNS INTEGER
SOURCE LOCATE(VARCHAR(4000), VARCHAR(4000), INTEGER)
!
Finally, there are some functions which have no direct equivalent in DB2 UDB
(e.g., DATEADD() for SQL Server). This set of functions can usually be converted
to DB2 UDB using the following techniques:
SQL UDFs
SQL UDFs are the first choice for converting unsupported SQL functions.
Using DB2 UDB's SQL/PL language to create and duplicate SQL Server
function behavior is usually quite successful.
External UDFs
In situations where the desired functionality cannot be accomplished using
SQL PL alone, external UDFs can be used. This involves duplicating the
required behavior in a supported compiled language such as Java or C.
Stored procedures
In some cases where advanced functionality is required, using a stored
procedure instead of a function is the most appropriate method of duplicating
the desired functionality if a table function cannot be used.
In DB2 UDB, each database contains its own metadata in a set of base tables
and views called the catalog. The catalog contains information about the logical
and physical structure of the database objects, object privileges, integrity
information, etc.
The DB2 UDB system catalog views cannot be modified using traditional SQL
statements. They are automatically updated each time a SQL data definition
statement is executed.
Due to the differing product architectures, querying the system for information is
different in SQL Server and DB2 UDB. However, the following section contains
several examples that demonstrate how specific information can be retrieved
from the DB2 UDB system catalog. More detailed information about the system
catalog contents can be found in the DB2 UDB documentation SQL Reference -
Volume 1, SC09-4844.
With the DB2 Command Line Processor (CLP), it is possible to obtain database
and instance metadata as well. The most common commands used for this are:
DB2 GET ...
DB2 LIST ...
To obtain a list of all databases that are cataloged in your environment, the
following command can be entered in a CLP window:
DB2 LIST DATABASE DIRECTORY
RETURN @result
GO
In DB2 UDB, the above functionality can be implement using the DB2 UDB
SNAPSHOT functionality. In Example 4-12, a user defined function and a stored
procedure is provided for this purpose. The procedure requests a database
SNAPSHOT and attempts to access the information returned by the snapshot.
Example 4-12 DB2 UDB function and procedure to check for database existence
CREATE PROCEDURE exists_db ( IN db_name VARCHAR(8),
OUT db_exists INTEGER)
LANGUAGE SQL
READS SQL DATA
BEGIN
DECLARE p_token_string VARCHAR(255);
IF (p_token_string =
'SYSPROC.SNAPSHOT_DATABASE_SNAPSHOT_DATABASE_SQL1611') THEN
SET db_exists = 1;
ELSE
SET db_exists = 0;
END IF;
SET db_exists = (
SELECT COUNT(*)
FROM TABLE(SNAPSHOT_DATABASE(db_name,0)) AS temp_tab);
END
!
RETURN v_rc;
END
!
For more information about the about GET DIAGNOSTICS and SNAPSHOT
commands, refer to the DB2 UDB documentation SQL Reference - Volume 2,
SC09-4845.
Table 4-5 Mapping of SQL Server system tables to DB2 UDB catalog tables
SQL Server system table DB2 catalog table
n/a SYSCAT.BUFFERPOOLS
n/a SYSCAT.TABLESPACES
INFORMATION_SCHEMA.tables SYSCAT.TABLES
INFORMATION_SCHEMA.columns SYSCAT.COLUMNS
INFORMATION_SCHEMA.views SYSCAT.VIEWS
n/a SYSCAT.TRIGGERS
sysindexes SYSCAT.INDEXES
INFORMATION_SCHEMA.routines SYSCAT.SYSPROCEDURES
INFORMATION_SCHEMA.routines SYSCAT.FUNCTIONS
systypes SYSCAT.DATATYPES
sysreferences SYSCAT.REFERENCES
Since both T-SQL and SQL PL are extensions of the same standard, many of the
same features are available in both languages, although, sometimes with
differing syntax. Proprietary extensions are usually what cause difficulty when
converting from T-SQL to SQL PL, since there is not always a direct translation
from one language to the other. Table 4-6 shows an equivalency between T-SQL
and SQL PL language elements and statements.
SELECT @var1=colname FROM table SET var1 = ( SELECT colname FROM table
WHERE… WHERE…);
CONTINUE ITERATE
4.9.2 PRINT
In SQL Server, a PRINT statement is used to display a character string to the
user. It is typically used during development to print debugging statements, but
can also used for logging and as a quick way to notify the user. PRINT is different
A DB2 UDB user defined function called PUT_LINE() that enables file output from
pure SQL is provided as additional material with this redbook. This function has
similar functionality as the PRINT statement. It can be used as a tool for
debugging stored procedures, but also allows any messages to be written to a
specified file for other purposes.
The function code sources and an installation guide can be found on the IBM
Redbooks Internet site. For more details, see Appendix G, “Additional material”
on page 509.
4.9.3 @@ERROR
In T-SQL, the execution of every statement as well as the evaluation of any IF
condition has the effect of setting the @@ERROR system function to a success or
failure value. Execution continues to the next statement. It is the programmer’s
responsibility to check if the execution of any statement failed and to take
appropriate action. Only fatal errors cause the transaction to be rolled back.
In SQL PL, an error encountered during the processing of any SQL statement
will result in an exception being raised. If the exception is not caught by a specific
exception handler, the application is terminated. The error status codes for the
specific error can be retrieved from either of the SQLCODE and SQLSTATE system
variables.
Example 4-15 DB2 UDB stored procedure with a default exception handler
CREATE PROCEDURE proc_name (OUT returncode INTEGER)
LANGUAGE SQL
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
SET returncode = SQLCODE;
This CONTINUE handler catches all exceptions and copies the SQLCODE error value
into a local variable, which is returned to the calling application. The exception
handler should be declared as a CONTINUE handler so that execution continues
with the statement following the statement that raised the exception.
This is not likely what the original programmer intended. When converting
@@ERROR to DB2 UDB, ensure that the original logic is correct and understand how
errors are processed in SQL PL using exception handlers. Refer to Example 4-15
to see how @@ERROR function behavior can be converted to SQL PL using
exception handlers.
There is no direct way to convert this behavior to SQL PL. Rather than simulate
this type of logic in DB2 UDB, we recommend that you re-design the code where
this type of behavior occurs (errors in IF condition evaluation) and adopt an SQL
PL style of error-handling behavior and logic.
4.9.4 RAISERROR
A RAISERROR statement is equivalent to a PRINT statement, followed by an
assignment to @@ERROR. It should be carefully converted to the appropriate SQL
PL statements depending on its context:
A RAISERROR statement that is immediately followed by a RETURN statement
can be converted using the SQL PL command SIGNAL SQLSTATE.
The commonly used sequence RAISERROR; ROLLBACK; RETURN should be
converted as though it were the sequence ROLLBACK; RAISERROR; RETURN, since
the @@ERROR value is reset to zero by the ROLLBACK statement in the former.
Care must be taken when converting the sequence RAISERROR;
RETURN<some_value>. This sequence causes @@ERROR to be reset to zero
before returning from the procedure. When converting to SQL PL, RAISERROR
might simply be ignored is this context. Translating the sequence into SQL
PL’s SIGNAL SQLSTATE will also not work because the procedure will not be
able to return the <some_value>.
4.9.5 @@SQLSTATUS
The T-SQL system function @@SQLSTATUS is similar to @@ERROR except that it is only
relevant for cursor FETCH statements. @@SQLSTATUS indicates if a FETCH operation
was successful, if there was no data, or if there was some other error.
To translate @@SQLSTATUS to SQL PL, the value of SQLCODE can be checked after
each FETCH operation, as shown in Example 4-16:
OPEN cur1;
FETCH cur1
INTO v_id, v_name;
4.9.6 @@ROWCOUNT
The T-SQL system function @@ROWCOUNT indicates the number of rows selected or
affected by the previous statement.
Example 4-17 DB2 UDB logic using the GET DIAGNOSTICS statement
...
DECLARE rc INT DEFAULT 0;
INSERT INTO brands (brand_id, brand_name, brand_desc)
VALUES (1, 'DB2', 'DB2 Information Management');
IF ( rc > 0 )
THEN RETURN 0;
ELSE
RETURN -200;
END IF;
...
4.9.7 @@TRANCOUNT
Transactions are handled differently in SQL Server and DB2 UDB. In SQL
Server, modifications to the database are committed immediately, unless a
transaction is explicitly initiated with a BEGIN TRANSACTION statement.
4.10 XML
Many applications exchange data as XML but store and query data using a
relational database. This presents an added challenge to application developers
and database administrators: how to manage traditional relational data and the
hierarchical data structure presented by XML. Microsoft uses the FOR XML option
as part of the SELECT statement to retrieve results in XML. With the FOR XML
option, application developers can execute SQL queries against existing
relational databases and return results as XML data, rather than as a standard
sets of rows (tables).
SQL/XML, supported by DB2 UDB, is an emerging part of the ANSI and ISO
SQL standard. As such, a database system like DB2 UDB aims to provide
application developers with a set of publishing functions (integrated in SQL) to
receive query results in XML. Using the SQL/XML standard instead of
custom-coded solutions or proprietary vendor extensions greatly simplifies
development, provides for more maintainable code, and allows portability across
database systems.
One approach to convert OPENXML to DB2 UDB is to use the DB2 XML
Extender. The XML Extender provides the capability to shred an XML document
into a set of relational tables. Once the XML document is shredded into relational
tables, standard relational data operations can be performed.
Conversion examples
The FOR XML clause allows three different modes, RAW, AUTO, and EXPLICT, to be
used. This section contains examples to show how the FOR XML clause can be
translated to DB2 UDB in each of the different modes. The examples are based
on the following three table definitions, taken from the DB2 UDB SAMPLE
database. They assume the following DB2 UDB tables also exist in SQL Server,
with the appropriate corresponding data types.
Department Table
CREATE TABLE DEPARTMENT (
DEPTNO CHAR(3) NOT NULL,
DEPTNAME VARCHAR(29) NOT NULL,
MGRNO CHAR(6),
ADMRDEPT CHAR(3) NOT NULL,
LOCATION CHAR(16));
Employee Table
CREATE TABLE EMPLOYEE (
EMPNO CHAR(6) NOT NULL,
FIRSTNME VARCHAR(12) NOT NULL,
MIDINIT CHAR(1) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
Project Table
CREATE TABLE PROJECT (
PROJNO CHAR(6) NOT NULL,
PROJNAME VARCHAR(24) NOT NULL,
DEPTNO CHAR(3) NOT NULL,
RESPEMP CHAR(6) NOT NULL,
PRSTAFF DECIMAL(5,2),
PRSTDATE DATE,
PRENDATE DATE,
MAJPROJ CHAR(6));
Example 4-18 SQL Server query using the FOR XML AUTO clause
SELECT department.deptno,
department.deptname,
employee.empno,
employee.firstnme,
employee.lastname
FROM dbo.department department,
dbo.employee employee
WHERE department.deptno = employee.workdept
ORDER BY department.deptno
FOR XML AUTO
Example 4-19 shows the partial XML output from Example 4-18:
Example 4-19 XML output for query using FOR XML AUTO
<department deptno="A00" deptname="SPIFFY COMPUTER SERVICE DIV.">
<employee empno="000010" firstnme="CHRISTINE" lastname="HAAS"/>
<employee empno="000110" firstnme="VINCENZO" lastname="LUCCHESSI"/>
<employee empno="000120" firstnme="SEAN" lastname="O'CONNELL"/>
</department>
Note how in the SELECT list in Example 4-18, the deptno and deptname labels
identify the department table. Therefore, a <department> element is created and
deptno and deptname are added as its attributes. Next, the empno, firstnme and
lastname column names identify the employee table. An <employee> element is
added as a sub-element of <department>, and the empno, firstnme, and lastname
attributes are added to the <department> element.
The key to performing this type conversion is to produce the same nesting as
SQL Server. When various FOR XML clauses are translated with DB2 SQL/XML
queries, XMLAGG() is used to produce the nesting. In the above example,
Example 4-21 shows a more complex example of using the FOR XML AUTO mode
in SQL Server. In this example, the result set contains all employees in each
department along with all projects assigned to each employee.
Example 4-21 Complex example of SQL Server’s FOR XML AUTO clause
SELECT department.deptno,
department.deptname,
employee.empno,
employee.firstnme,
employee.lastname,
project.projno,
project.projname
FROM dbo.department department,
dbo.employee employee,
dbo.project project
WHERE department.deptno=employee.workdept AND
employee.empno=project.respemp
ORDER BY department.deptno
FOR XML AUTO
Example 4-22 shows the partial XML output from Example 4-21:
In this example, a third table, project, becomes part of the SELECT clause, as
identified by the projno and projname columns in the SELECT list. Therefore, a
<project> element is created and projno and projname attributes are included.
In the previous example, <department> is the top level element. The <employee>
element is nested under <department>. In this example, a new <project>
element is introduced, and is nested under <employee>.
FROM
( SELECT t.deptno, t.deptname, t.empno, t.firstnme, t.lastname,
XMLAGG (XMLELEMENT (name "project",
XMLATTRIBUTES( t.projno AS "projno",
t.projname AS "projname")))
FROM
( SELECT department.deptno,
department.deptname,
employee.empno,
employee.firstnme,
employee.lastname,
project.projno,
A sub-SELECT with XMLAGG() is then constructed on top of that which produces the
inner-most layer child element. In this example, it is the <project> element.
XMLAGG() is always used together with the GROUP BY clause (see Example 4-24).
In fact, this is where the hierarchical structure arises. In the SELECT clause,
XMLAGG() is used to list all <project> elements. The elements specified in the
GROUP BY clause belong to the parent elements of the <project> element. For
example, since deptno, deptname, empno, firstnme, and lastname are part of the
GROUP BY clause, they all belong to the <department> and <employee> elements.
The next step is to navigate through the XML output and build another
sub-SELECT, which is corresponds to the <employee> element (Example 4-25).
Finally the top element, the <department> element is built (not shown).
Another method of converting this example to DB2 UDB involves the use of a
common table expression. Example 4-26 demonstrates how to nest the XMLAGG()
function within a common table expression. The SELECT statement becomes
much easier to construct because it reflects the natural hierarchy of the XML
output.
Example 4-26 Converting FOR XML AUTO query using a common table expression
SELECT xml2clob (
XMLELEMENT (name "department",
XMLATTRIBUTES ( dd.deptno as "deptno",
dd.deptname as "deptname"),
( SELECT XMLAGG (
XMLELEMENT (name "employee",
XMLATTRIBUTES ( ee.empno as "empno",
ee.firstnme as "firstnme",
ee.lastname as "lastname"),
( SELECT XMLAGG (
XMLELEMENT (name "project",
XMLATTRIBUTES (
pp.projno as "projno",
pp.projname as "projname")))
FROM db2admin.project pp
WHERE pp.respemp = ee.empno)))
FROM db2admin.employee ee
WHERE ee.workdept = dd.deptno)))
FROM db2admin.department dd
WHERE dd.deptno IN (SELECT workdept FROM db2admin.employee)
The key to performing this conversion is to understand the XML output produced.
Thus, we use the same techniques covered in the previous examples to produce
a similar result in DB2 UDB with SQL/XML. This result is shown in Example 4-29.
-- Execute an INSERT statement that uses the OPENXML row set provider.
INSERT INTO project (respemp, deptno, projno, projname)
SELECT *
FROM OPENXML (@idoc, '/root/department/employee/project',2)
WITH ( empno char(6) '../@empno',
deptno char(3) '../../@deptno',
projno char(6) '@projno',
projname varchar(24) '@projname')
One way to convert OPENXML to DB2 UDB is to use the DB2 XML Extender. The
DB2 XML Extender offers the capability to shred an XML document into a set of
DB2 relational tables. A similar relational result set as the OPENXML row set can
then be created by SELECTing from the relational tables. The steps for doing this
with the DB2 XML Extender are now presented.
3. Based on the DAD mapping, the XML Extender’s stored procedures can be
invoked to shred the XML document into the DB2 UDB tables. The key when
converting to DB2 UDB is to produce a relational data set that matches the
OPENXML row set. To produce the same relational data set in DB2 UDB, it
must be determined which tables, produced from the XML document
shredding, should be queried. In this case, the following SELECT statement
returns a similar relational set:
SELECT d.deptno, e.empno, projno, projname
FROM department_nn d, employee_nn e, project_nn p
WHERE d.deptno=e.deptno and e.empno=p.empno
4. The PROJECT table in DB2 UDB can be populated by retrieving from the
temporary tables. Thus, the SQL Server INSERT statement can be converted
as:
INSERT INTO project (deptno, respemp, projno, projname)
SELECT *
FROM (
SELECT d.deptno, e.empno, projno, projname
FROM department_nn d, employee_nn e, project_nn p
WHERE d.deptno = e.deptno AND e.empno=p.empno
)
Refer to Appendix F, “SQL limits” on page 505 for a more detailed comparison of
SQL limits in DB2 UDB and SQL Server.
If object identifiers need to be changed to meet DB2 UDB’s SQL limits, changes
must also be reflected in SQL statements and/or scripts where an object is
explicitly referred to by name, such as a user defined function. Objects that are
very infrequently referred to by name from outside the database, such as triggers
or indexes, typically do not require changes external to the database, aside from
the object creation code.
Note: We recommend using the same object identifier names in SQL Server
and DB2 UDB implementations of your application. This makes code
maintenance and design easier.
The full list of DB2 UDB SQL Limits can be found in the DB2 UDB documentation
SQL Reference - Volume 1, SC09-4844.
This chapter introduces the typical conversion process. The planning process, as
well as the available tools and resources are also documented in this chapter.
Detailed conversion information can be found in Chapter 7 through Chapter 10.
The phases of the conversion discussed in this chapter include:
Preparation
This includes project planning, education of database administrators and
developers, and the setup of the environment.
Conversion
This includes the conversion of the database structure, objects, and data, as
well as the applications that connect to the database, and any
maintenance/batch scripts.
Post-Conversion
This includes performance tuning of the converted database and application.
Often, you can retrieve this information by using scripts that query the system
catalog tables, or by using utilities provided by SQL Server. You can also use the
IBM DB2 Migration Toolkit (MTK) to retrieve structural and object information
from a SQL Server database.
Modeling tools
There are a number of modeling tools that can capture the entity-relation (E-R)
descriptions of your database. By capturing this information, the tool can
generate the appropriate DB2 UDB object definition syntax. A few of the common
modeling tools are:
The cost of the new database software and conversion tools should also be
considered when estimating the project cost. MTK is provided free of charge.
Contact your IBM Sales Representative for details about DB2 UDB pricing.
Training costs for DBAs and end-users should also be figured into your
estimation. Finally, hardware procurement must be planned if your existing data
server does not have the capacity to run the existing SQL Server installation,
MTK, and DB2 UDB.
The IBM Software Migration Project Office (SMPO) can also provide porting
estimates. Contact the SMPO at:
http://www.ibm.com/software/solutions/softwaremigration/
A project plan can be as simple as a spreadsheet that lists the main tasks of the
conversion and some of the associated information for each task (start date, end
date, elapsed time, task dependencies, who is assigned, etc.). There are also
project planning tools (such as Microsoft Project, Primavera TeamPlay, and
Primavera Enterprise) that are specifically designed to plan, and track the
progress of the project. These tools let you assign tasks to specific people (or
roles), establish dependencies among the various steps of the port (for instance,
you cannot start testing until you move the database structure and the test data),
and chart the original plan against what actually happens.
To help in the planning process, a porting and migration planning whitepaper has
been developed. It can be downloaded from the following Web site:
http://ww.ibm.com/developerworks/db2/library/techarticle/dm-0311simchuk
By following the steps and suggestions in this whitepaper, you should be able to
develop an accurate project plan.
The fastest way to prepare your technical staff for effectively working with DB2
UDB is through some form of education. For more information about the various
training and certification options available, view the section entitled Convert your
skills on the following Web site:
http://www.ibm.com/software/data/db2/migration/
This Web site provides numerous references to sites within IBM that offer
classroom training, online tutorials, and reference materials.
For more information about these steps and others, refer to the porting zone Web
site at:
http://www.ibm.com/db2/porting/
5.2 Conversion
The steps required to convert a database and application to DB2 UDB are
introduced in this section. The methods employed can vary, but at a minimum,
the following steps are required. This chapter introduces:
Converting the database structure
Converting database objects
Modifying the application
Modifying the database interface
Migrating the data
For more information about these steps and others, refer to the porting zone Web
site at:
http://www.ibm.com/db2/porting/
There are three approaches that are commonly used to move the structure of a
database:
Manual methods: Extract the existing structure, import it to DB2 UDB, and
manually adjust for problems.
Model transport: Generate DB2 UDB DDL from an existing database model.
Conversion tools: Use a tool to extract the database structure, make any
necessary conversions, then load it in a DB2 UDB database.
Manual methods
Many DBMSs come with a utility that extracts the database structure into a text
file. This DDL can then be used to re-create database objects on a new database
server. However, before the extracted DDL likely requires syntax changes before
it can properly execute in DB2 UDB. These syntax changes requires manual
effort.
Besides syntactical differences, there may also be changes needed for data
types and other proprietary features used. It is often easiest to convert a subset
of the source DDL to DB2 UDB, and examine any issues that arise.
Metadata transport
Many databases are designed using modeling tools. These tools allow the
designer to describe the database structure in the form of logical entities and
relationships. The modeling tool can then generate the physical database
structure (DDL) from the model. If the system to be ported is designed (and
maintained) using a modeling tool, converting the database structure to DB2
UDB can be as simple as having the modeling tool and generate DB2 UDB
specific DDL for the model.
Capturing the definition of database objects can often occur at the same time the
database structure is captured, if the objects are written in an SQL-like
procedural language and stored within the database. For those objects written in
higher-level languages (Java, C, etc.), capture and import involves transferring
the source files to the DB2 UDB system, making any necessary syntax changes,
then using a DB2 UDB compatible compiler and binding mechanism.
The conversion of database objects requires testing of the resulting objects. This
implies that test data should be available before to testing. Preliminary data
migration effort is therefore required in order to complete this conversion step.
After the object conversion is completed, some adjustments may still be required.
Issues such as identifier length may need to be addressed. This can be done
manually or using a tool.
Information integration
Will there be other data sources that must be incorporated in the application and
will this occur at the application code level, or at the database level through the
use of federation?
Content management
Will there be a need for complex data objects and/or structures that are more
sophisticated than those usually stored in relational database data types? If so,
what other tools and capabilities are needed and how will they be integrated?
Business intelligence
Is there a need to summarize and analyze the data within the database(s) that
may require the use of OLAP tools and different structures of the data? If so, can
this be done without impact on the production system, or is an additional copy of
key data required which could require replication or other similar processes?
Web integration
If the current GUI is awkward or out-dated, or if a larger population needs access
to the application, is it time to consider using a Web-based front-end to the
application? If so, what development and runtime tools are needed? What
education will be needed for the staff to be able to implement this technology?
Database administration
What backup and recovery plan will be used to ensure the proper level of safety
while still allowing acceptable performance? Is replication needed, and if so, how
will it be implemented? What is the fail over strategy (redundant hardware, hot
nodes that can replace failed ones, other high availability issues)?
Few tools are available to port the actual application code since much of the work
is dependent on vendor-specific issues. These issues include adjustments to
compensate for differing approaches to transaction handling, join syntax, use of
special system tables, and use of internal registers and values. Often, proprietary
functions used in the source DBMS need to be emulated in DB2 UDB, usually by
creating a DB2 UDB user defined function and/or stored procedure with the
same name as the proprietary one being ported. This way, any SQL statements
in the application code that call the proprietary function in question do not need to
be altered. Conversion tools such as MTK are equipped with some of the most
commonly used vendor-specific functions and automatically create them in DB2
UDB during the conversion process.
One issue when porting high-level language (C, C++, Java, COBOL, etc.) code
involves compiler differences. Modifications to the application code may be
required if a different compiler and/or object library are used in the DB2 UDB
environment. This may result from using a different hardware or OS platform. It is
important to fully debug and test such idiosyncrasies before moving a system
into production.
There are certain circumstances where the DB2 UDB-supported driver for an
interface does not implement or support one or more features specified in the
interface standard. In these cases, you must take action to ensure that
application functionality is preserved after the conversion. This usually involves
changing application code to remove references to the unsupported functions
and either replacing them with supported ones, or simulating them by other
means.
DB2 UDB also provides a library of administrative functions which can be used to
develop administrative applications that can administer DB2 UDB instances,
backup and restore databases, import and export data, and perform operational
and monitoring functions. These administrative functions can also be run from
the DB2 UDB Command Line Processor (CLP), Control Center, and DB2 UDB
scripts.
The following sections highlight some of the common interfaces used with DB2
UDB. These interfaces are described more fully in the DB2 UDB documentation
Application Development Guide: Programming Client Applications, SC09-4826.
Microsoft .NET
.NET is Microsoft’s development platform that competes with the Java J2EE
standard. The .NET framework programming model enables developers to build
Web-based applications, smart client applications, and XML Web services
applications, which expose their functionality programmatically over a network
using standard protocols such as SOAP and HTTP. More information about the
.NET Framework program and DB2 UDB’s support for .NET can be found on the
DB2 UDB .NET Program Web page at:
http://www.ibm.com/developerworks/db2/zones/vstudio/
SQLj offers developers a way to write static SQL programs using Java. SQLj
programs generally outperform their JDBC counterparts because the query
access plans of executable statements are optimized before run-time.
More information about the Java supported interfaces can be found at the
following links.
DB2 UDB Java Application Development Web page at:
http://www.ibm.com/developerworks/db2/zones/java/
The article DB2 and Java: The Big Picture posted on the DB2
developerWorks site also provides a good summary about the different Java
options available in DB2 UDB. The article can be viewed at:
http://www.ibm.com/developerworks/db2/zones/java/bigpicture.html
Considering SQLj for Your DB2 V8 Java Applications is a white paper that
addresses access to relational data from Java. This paper can be view at:
http://www.ibm.com/developerworks/db2/library/techarticle/0302tsui/0
302tsui.html
ODBC
Microsoft’s ODBC standard provides a set of APIs for accessing a vendor's
database. Vendors must supply their own driver that implements a subset of the
API for accessing the database. The DB2 UDB CLI driver can be used on its own
to access a DB2 UDB database or as an ODBC driver. DB2 UDB conforms to
most of the Level 3 compliance level for ODBC.
Perl DBI
DBI is an open standard API that provides database access for client
applications written in Perl. DBI defines a set of functions, variables, and
conventions that provide a platform-independent database interface. The latest
http://www.ibm.com/software/data/db2/perl/
Stored procedures
Another popular method of interfacing with the database is through stored
procedures. Stored procedures can be written in DB2 UDB’s SQL procedural
language, or in an external programming language such as a supported
Microsoft CLR language or Java. Restricting database access through stored
procedures offers numerous benefits such as a reduction in network traffic (all
processing takes place on the server), and providing an additional layer of
isolation between the application code and business logic. An excellent reference
for building SQL stored procedures can be found in:
DB2 SQL PL: Essential Guide for DB2 UDB on Linux, UNIX, and Windows,
i5/OS, and z/OS, ISBN 0131477005
DB2 Application Development Guide: Programming Server Applications,
SG09-4827
While it is beyond the scope of this book to offer a full discussion of performance
tuning, a list of performance tuning links and resources can be found at the
following URL:
http://www.ibm.com/developerworks/db2/zones/porting/tuning.html
Methodology
The following steps work well when beginning any type of performance tuning
activity:
Define the objectives: What is wrong or what needs to be accomplished?
Determine the information to analyze.
Determine the monitor(s) to use.
Test and obtain monitor data.
Analyze the information.
Determine the changes required.
Implement changes (one at a time) and go back to step four.
By following this process, and carefully recording all settings, adjustments, and
results, you should be able to achieve incremental improvements in
performance, or determine that you have reached an optimal set of parameters.
It is important to remember that performance tuning is a balancing act. You often
must make trade-offs.
Useful tools
Part of performance tuning involves being able to monitor and measure the items
to be analyzed. Here are some tools that may be helpful:
IBM DB2 Performance Expert:
This IBM tool integrates performance monitoring, reporting, buffer pool
analysis, and a Performance Warehouse function into one tool.
Configuration Advisor:
This GUI wizard provides configuration recommendations based on
user-supplied answers to system environment questions. This wizard can be
accessed through Control Center.
MTK for SQL Server can be downloaded free of charge from the following Web
site:
http://www.ibm.com/software/data/db2/migration/mtk/
MTK can simplify your migration to DB2 Universal Database (UDB) and reduce
time to migrate your database. It provides database administrators (DBAs) and
application programmers with the tools needed to automate previously inefficient
and costly migration tasks. You can eliminate human error, and cut back on
person hours and other resources associated with traditional database migration
by using MTK. MTK converts the following SQL Server source database objects
into the equivalent DB2 UDB database objects:
Data types
Tables
Columns
MTK stores the extracted data on the server where it is installed. Hence, there is
a performance advantage to installing MTK where the source database resides,
since it is faster to unload data locally than across a network.
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 139
6.2 MTK setup
MTK installation is simple and requires minimum preparation. This section
highlights a typical MTK installation. To install MTK:
1. Download the latest IBM DB2 Migration Tool Kit for Windows from the
following Web site:
http://www.ibm.com/software/data/db2/migration/mtk/
At the time of writing, the current the MTK version is 1.3
(db2mtk_V1_3_GA_win.exe.)
2. Launch the MTK installation by double-clicking the downloaded file. This
displays the Welcome screen (Figure 6-1). Click Next on the Welcome
window to proceed with the installation.
4. On the Choose Destination Location Screen, specify the Folder where you
want to install the MTK (Figure 6-3).
Keep the default settings and click Next for this project.
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 141
5. The MTK begins installing all the required files. A progress screen, similar to
to Figure 6-4 is shown to indicate overall installation progress.
The menu bar contains Application, Project, Tools, and Help menus:
Application: Allows you to set-up preferences, such as your favorite text editor.
Project: Start a new project, open or modify an exiting project, import a SQL
source file, or perform backup/restore function.
Tools: Launch the SQL Translator, reports, and the MTK log.
Help: MTK help information
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 143
SQL Server DDL to the equivalent DB2 UDB DDL. Extraction requires a
connection to the source database through ODBC or JDBC. Once the
ODBC/JDBC connection is established to SQL Server, MTK will query the
system catalog tables and extract the object definitions for use in the
conversion process. importing, on the other hand, requires an existing file, or
files, which contain database object DDL. The import task copies the DDL
into the MTK project directory for use in the database structure conversion
process. Using MTK to perform data movement is limited if the importing
option is chosen.
Task 2: Convert
During the Convert task (Figure 6-8), the user can chose to complete several
optional tasks before the actual translation of the source DDL code. These
tasks are:
– Selecting format options for the converted code. This includes keeping the
source code as comments in the converted code; including the DROP
statement before CREATE <object> statements, among others.
– Making changes to the default mapping between a source data type and
its target DB2 UDB data type.
Once the optional tasks are completed. the user can click the Convert button
and the source DDL statement is converted into DB2 UDB compatible DDL.
Each conversion generates two files:
– .db2 file contains all of the source code converted to DB2 UDB code.
Task 3: Refine
During the Refine task (Figure 6-9) the user may:
– Examine the results of the conversion.
– View various types of messages generated by the tool and, if necessary
specify changes to be made to the converted DDL.
If any changes are made to the converted DDL in the Refine step, the user
return to the Convert step to apply the changes.
Other, such as the SQL Translator, Log, and Reports to help you refine the
conversion. After the DDL statements have been refined satisfactorily, the
Generate Data Transfer Scripts step or the Deploy to DB2 step can be
executed.
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 145
Figure 6-9 Refine
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 147
Figure 6-12 Overview of MTK conversion tasks
The SQL Translator can be used for application code translation as well. You can
copy SQL statements from your application, paste it into the editing area of the
window, and click Convert to convert the SQL statements into DB2 UDB
supported syntax. You can then copy the converted code and paste it back to
your application. This makes your conversion effort easier.
Chapter 6. The IBM DB2 Migration Toolkit for SQL Server 149
150 Microsoft SQL Server to IBM DB2 UDB Conversion Guide
7
By default, the database is created in the default database path specified in the
database manager configuration file (DFTDBPATH parameter). In Windows, this
parameter specifies a drive. The following command creates a database called
REDBOOK using all the default options:
CREATE DATABASE redbook
By default, DB2 UDB creates a database with three default table spaces,
SYSCATSPACE, TEMPSPACE1, and USERSPACE1.
DB2 UDB uses four types of table spaces depending on the data be stored:
REGULAR - used for tables, indexes, and system catalog tables.
SYSTEM TEMPORARY - used by the database for sorts, joins and other
operations.
USER TEMPORARY - used for global declared temporary tables.
LARGE - used to store Large Object Data (LOB). This can only be created
using DMS table space.
SQL Server uses a fixed page size of 8KB for all its tables, whereas DB2 UDB
supports multiple page sizes: 4KB (default), 8KB, 16KB, and 32KB. If any row in
a table in the source database has a length greater than 3,985 bytes, table space
with an 8KB page size or larger is required in DB2 UDB.
To create a table space with an 8KB page size, a buffer pool with an 8KB page
size must exist. Example 7-2 shows how a buffer pool and table space using an
8KB page size can be created:
Example 7-2 Creating a buffer pool and table space using an 8KB page size
CREATE BUFFERPOOL bp_8k IMMEDIATE SIZE 250 PAGESIZE 8 K;
7.3 Tables
In SQL Server, the CREATE TABLE statement may include the database name,
owner and table name. The database name is optional and defaults to the
current database name.
In DB2 UDB, a fully qualified table name consists of the schema name and table
name. In the following two examples:
DB2ADMIN.CLASS
ITSO.PRICE
DB2ADMIN and ITSO are schema names, while and CLASS and PRICE are table
names.
If no schema is specified with the CREATE TABLE statement, the default schema is
used (the ID of the user creating the table). Table 7-1 shows an example
comparing the CREATE TABLE syntax between SQL Server and DB2 UDB:
The DB2 UDB CREATE TABLE statement can specify different table spaces for
indexes, tables, and LOBs, if DMS table spaces are used. Example 7-3 illustrates
how to specify the desired table space in the CREATE TABLE statement, using the
IN clause:
Sample statement:
CREATE TABLE itso.mytab1(
Col1 INT,
Col2 CHAR(3))
IN mytbs1
7.3.2 IDENTITY
The SQL Server IDENTITY property can be specified in the table definition to
provide system-generated values in sequence. DB2 UDB also provides an
IDENTITY attribute that can be specified in the table definition. The syntax used in
DB2 UDB differs from SQL Server.
Table 7-2 shows an example comparing the IDENTITY column syntax differences
between SQL Server and DB2 UDB:
Table 7-2 IDENTITY column syntax - compare between SQL Server, DB2 UDB
SQL Server DB2 UDB
For more information about generating unique values in DB2 UDB, refer to the
following article:
http://www.ibm.com/developerworks/db2/library/techarticle/0205pilaka/02
05pilaka2.html
In contrast, DB2 UDB generated columns are defined in a base table where the
stored value is computed using an expression. One or more generated columns
can be added to a table. It is also possible to create non-unique indexes on a
generated column.
Table 7-3 shows the differences in syntax between a SQL Server computed
column and a DB2 UDB generated column.
7.3.4 Constraints
Both SQL Server and DB2 UDB support column constraints on tables.
Unique
There is no difference between unique constraints in SQL Server and DB2 UDB.
No syntax changes are required when converting these type of constraints.
Primary key
The only difference in primary key constraint syntax is that DB2 UDB requires the
NOT NULL keywords to be added. Table 7-4 shows an example highlighting the
difference in syntax between SQL Server and DB2 UDB.
Table 7-4 Primary key syntax - compare between SQL Server and DB2 UDB
SQL Server DB2 UDB
Check constraints
SQL Server and DB2 UDB support column level and table level check
constraints. The syntax for these constraints is very similar. Formatting related
Table 7-5 Check constraint syntax - compare between SQL Server, DB2 UDB
SQL Server DB2 UDB
Referential constraints
SQL Server referential constraints have two possible actions: CASCADE and NO
ACTION (default).
DB2 UDB referential constraint definitions have four possible actions: NO ACTION
(the default), RESTRICT, CASCADE, and SET NULL.
Note: DB2 UDB informational constraints are constraints on tables that are
not enforced by the database manager, but can still be exploited by the
optimizer. The attributes ENFORCED and NOT ENFORCED define whether the
constraint is enforced by the database manager during data modification
operations. NOT ENFORCED should only be specified if the table data is
independently known to conform to the constraint. Furthermore the ENABLE
QUERY OPTIMIZATION and DISABLE QUERY OPTIMIZATION attributes define
whether the constraint can be used for query optimization under appropriate
circumstances.
DB2 UDB creates indexes in a separate data structure that replicates the keys’
values. A B+ tree data structure is used to store indexes. To maintain the cluster
factor of a clustered index or improve it dynamically as data is inserted into the
associated table, DB2 UDB attempts to insert new rows physically close to the
rows with index key values in the same range.
In both SQL Server and DB2 UDB, only one clustered index per table is
permitted. There is a small difference in syntax used to create the indexes. In
DB2 UDB, the CLUSTERED clause proceeds the index definition, as the example in
Table 7-6 shows.
Table 7-6 Clustered index syntax-compare between SQL Server and DB2 UDB
SQL Server DB2 UDB
Table 7-7 Non-clustered index syntax-compare between SQL Server, DB2 UDB
SQL Server DB2 UDB
Note: DB2 UDB indexes can be either type-1 or type-2. A type-1 index is only
used to support DB2 UDB versions prior to V8. The primary advantages of
type-2 indexes are to improve concurrency because the use of next-key
locking is reduced to a minimum and an index can be created on columns that
have a length greater than 255 bytes. A table must have only type-2 indexes
before online table reorg and online table load commands can be issued
against the table. They are also required for multidimensional clustering
(MDC) tables. By default, all new indexes created in DB2 UDB V8 are type-2
indexes, except if a type-1 index already exists on a table.
A DB2 UDB schema is a logical object which can be used to group database
objects together. The naming convention used for all database objects uses both
the schema name and the object name (e.g., SCHEMA.OBJECT). The default
schema for all users is their user name (e.g., JDOE.TABLE). The default schema is
used to resolve all object references where no schema is used (e.g., for user
JDOE, the statement SELECT * FROM EMPLOYEE would be equivalent to the
statement SELECT * FROM JDOE.EMPLOYEE). The default schema can be changed
for the duration of a database connection using the SET SCHEMA command.
You can use the DB2 UDB CREATE SCHEMA statement to create a schema, then
create other objects under it. Example 7-5 shows a simple schema declaration
and use in DB2 UDB.
or
CREATE SCHEMA billing AUTHORIZATION user1;
SET SCHEMA billing;
CREATE TABLE orders (...,...,...)
7.6 Views
There is little difference in view syntax between SQL Server and DB2 UDB.
However, the SELECT statement the view is based on may require some changes.
Table 7-8 shows an example where such a change is necessary. In this example,
SQL Server column aliases must be changed to match DB2 UDB’s column alias
syntax.
Table 7-8 View syntax - compare between SQL Server and DB2 UDB
SQL Server DB2 UDB
7.7 Privileges
SQL Server restricts access to objects and commands based on a user's identity
or group membership. Database permissions can be assigned directly to
Windows NT and Windows 2000 users. The SQL statements GRANT and REVOKE
authorize or prevent users from accessing database objects. Most of SQL
Server’s permissions can be mapped to DB2 UDB privileges.
In DB2 UDB, privileges are stored in the database catalog for each database.
The following types of object privileges exist: database, schema, table space,
table, view, nickname, server, package, index, routine and sequence. The table
space USE privilege controls the table spaces users can create tables in. The
GRANT and REVOKE statements can be executed to assign or to remove privileges
SQL Server has a fixed server role called sysadmin with permissions to perform
any activity. User accounts can become members of the sysadmin role. The
built-in login system administrator (SA) is only provided for backward
compatibility. In DB2 UDB, the System Administration (SYSADM) authority is the
highest level of authority for an instance, and controls all instance objects.
One task involved in converting the database structure involves identifying the
privileges required by each user ID that connects to the database. In SQL
Server, database object privileges are granted to Roles and Users. Privilege
information can be collected from the extracted SQL Server code or from
Enterprise Manager. Table 7-9 shows a simple example outlining the Roles and
Users defined for the REDBOOK database.
Table 7-9 SQL Server user list for the REDBOOK database
Roles Users
Once this list is prepared, the equivalent user IDs and groups can be created on
the target DB2 UDB server, or wherever user authentication takes place.
Table 7-10 shows the object privileges required for the Roles/Users defined in
Table 7-9.
Table 7-10 DB2 UDB object privilege list for the REDBOOK database
Roles/Users Objects Privileges
Based on this information, the equivalent DB2 UDB GRANT statements can be
issued to give the corresponding DB2 UDB user IDs the correct privileges.
GRANT SELECT, INSERT ON TABLE table1 TO librarian
...
GRANT EXECUTE ON PROCEDURE stored_procedure1 TO user1
Launch MTK and create a project called RBOOK. Select the source REDBOOK
database in SQL Server. In the Extract step, extract the database definition
statements from SQL Server and save them to a file.
SETUSER 'dbo'
go
...
In the Convert step, convert the SQL Server database object definitions into
equivalent DB2 UDB object definitions. Save these converted object definitions in
a different file. We recommend that you examine all the error/warning messages
in the generated code and try to correct the errors.
Example 7-8 shows a snippet of the DB2 UDB object definitions generated by
MTK.
...
--|
--| go
--| CREATE CLUSTERED INDEX [PK_author_id]
--| ON [authors] ( [author_id] ASC )
CREATE INDEX PK_author_id ON authors (author_id ASC) CLUSTER!
--|
--| go
--|
--| CREATE NONCLUSTERED INDEX [idx_authors]
--| ON [authors] ( [name] ASC, [firstname] ASC )
Open the converted file, make any necessary corrections and re-save it.
In the Deploy to DB2 step, specify the database named REDBOOK, whether it is a
local or remote database, and the user ID and password required to connect.
Select Launch <DB2_objects_file_name> in the database and click Deploy.
This creates all the converted objects in the DB2 UDB REDBOOK database.
Note: You can use any terminator character at the end of your statement.
This can be specified using -td<terminate_character> flag of CLP or in
the provided text field in Command Editor.
Alternatively, you can create the database using the Create Database wizard in
Control Center.
Alternatively, you can load this script in Command Editor and execute it from
there.
Both source and target systems can be set up to run concurrently, keeping both
data sources synchronized and minimizing downtime while bringing the target
data source to production.
Modern DBMSs provide built-in utilities for moving data. In SQL Server, the Bulk
Copy Program (BCP) and Data Transformation Services (DTS) are two such
utilities for importing and exporting data from SQL Server databases. DB2 UDB
provides the EXPORT utility for extracting data from a DB2 UDB database, and the
LOAD and IMPORT utilities for importing data into a DB2 UDB databases. A
combination of these utilities can be used to complete the data migration.
Data migration is often not trivial because of data type dissimilarities and the
existence of large volumes of data. Other tools, such as MTK, can simplify the
process of data migration. Some of these tools are listed in 8.5, “Using other
tools” on page 182.
Migrate subsets of the production data and perform some tests. These tests
include performing simple comparisons against the source and target databases,
such as row counts. You can also identify if any transformations or adjustments
are required and implement them before migrating the full set of data.
Once you are completely satisfied with the results of your tests, you can migrate
the full set of production data. If you have plans to run both systems in parallel
and keep both systems synchronized or replicated, then WebSphere Information
Integrator Replication Edition can be used.
After moving all the production data from the source to target system, correct any
referential integrity issues, then perform a thorough verification of the migrated
data against the source data for accuracy and completeness. Figure 8-1
illustrates this process.
Test:
Tables Simple comparison of data
Compare row counts
Note anomalies
Make script changes/adjustments
etc.
Views
DB2 UDB
SQL
SQL
Extract + Populate DB2 UDB
UDB
Server
Server
Test:
Extract + Populate Simple comparison of data
Full Compare row counts
Setup replication to
Full Note anomalies
synchonize Fix referential integrity issues
Test with the application
etc.
We assume that you have already created the equivalent REDBOOK database
structure (refer to Chapter 7, “Database structure conversion” on page 151).
Most importantly, you have already decided the data type mappings to use. The
following steps outline how to migrate a subset of data using MTK:
1. Launch MTK.
2. In the project management window, enter a project name. For this example,
enter REDBOOK.
3. Leave the project path and project description as the default values.
4. Choose Microsoft SQL Server as your source database, and 8.2 for Linux,
UNIX, and Windows as your target database, then click OK.
5. At the Extract from source database section in the Specify Source tab, click
Extract.
6. A Connect to Database window appears. Fill in the corresponding
JDBC/ODBC DSN Alias, and the user ID and password. Click OK.
7. Only select the tables in the REDBOOK database shown in Figure 8-2. Name the
generated script redbook.src. Leave all other options as their default values.
8. Click Extract.
9. Select the Convert tab.
10.On the left pane, select redbook.src.
11.Click Global Type Mapping.
12.If your target database is not created to support double-byte character data
(e.g., UTF-8), then the GRAPHIC and VARGRAPHIC data types are unavailable.
For this example, change the data type mapping of NCHAR(I), and
NVARCHAR(I) to VARCHAR(I) instead. Figure 8-3 summarizes this mapping.
13.Leave all other options as their default values, and click Convert.
14.At the Refine tab, some translated information is shown. Error messages are
placed here if MTK could not convert some of the database structure.
15.Click Generate Data Transfer Scripts tab (Figure 8-4).
Note: To help you decide whether to use the IMPORT or LOAD utility in your
environment, refer to DB2 UDB documentation “Appendix B” of Data
Movement Utilities Guide and Reference V8, SC09-4830.
16.Select Use LOAD utility (logging and triggers are deactivated). Try to use
IMPORT after this exercise and compare your results.
17.Click Advanced Options, and enter 20 in the row count field (Figure 8-5).
Note: By indicating the row count value in the Advance Option settings, the
LOAD commands generated will only load the indicated number of records
in every table.
Note: After loading subsets of data, you may want to pursue loading other
subsets and continue assessing them. You can empty the tables quickly using
the LOAD command with the REPLACE option with an empty input file.
Having loaded 20 or less rows of data, referential integrity issues may exist at this
point. Refer to “Deactivating referential integrity checking” on page 175 for
instructions to enable access to the data.
Note: The LOAD utility turns off constraint checking for self-referencing and
dependent tables. This places the tables into check pending state. Once the
data loading completes, MTK turns on the constraint checking for all the tables
for which it was turned off.
For more information about the SET INTEGRITY command, refer to DB2 UDB
documentation: SQL Reference - Volume 2, SC09-4845.
For more information about migrating data using the MTK, refer to the following
article on the developerWorks Web site:
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0411yip/
Example 8-1 Using the BCP utility to export data to a text file
BCP command:
C:\temp>BCP redbook.dbo.authors OUT authorsOUT.del -t , -r \n -e exportERR.txt
In general, the LOAD utility is faster than the IMPORT utility because it writes
formatted pages directly into the database. The LOAD utility validates the
uniqueness of the indexes, but it does not fire triggers or perform referential and
constraint checking.
Example 8-2 shows the DB2 UDB equivalent operation as Example 8-1. The
LOAD command options used in Example 8-2 are:
MODIFIED BY NOCHARDEL: specifies that all bytes found between the column
delimiters to be part of the column's data, without quotes (“ ... ”,“ ... ”).
REPLACE INTO: deletes table contents before inserting data.
Example 8-2 Using the LOAD utility to import data from a text file
db2 => LOAD FROM authorsOUT.del OF DEL MODIFIED BY NOCHARDEL MESSAGES
loadERR.txt REPLACE INTO stefan.authors
Example 8-3 Using the IMPORT utility to import data from a text file
db2 => IMPORT FROM authorsOUT.del OF DEL MODIFIED BY NOCHARDEL REPLACE INTO
stefan.authors
For more information about the LOAD and IMPORT utilities, refer to the DB2 UDB
documentation Data Movement Utilities Guide and Reference, SC09-4830.
WebSphere Information Integrator gives you the option of migrating the data in
stages. Once WebSphere II is set up with nicknames for the SQL Server source
tables, you can access them through DB2 UDB. This permits testing of the
ported application without moving any data to DB2 UDB. Nevertheless, the
ultimate goal is to move the data over to DB2 UDB. After that stage, you can
continue to use WebSphere II to replicate data over to DB2 UDB and
synchronize both databases until production moves over to the new system.
To enable a DB2 UDB database to access the sample REDBOOK database and
tables defined in SQL Server, you need to perform the following steps using
Control Center (CC), or Command Line Processor (CLP). Both methods follow
the same general process.
CC can be used to quickly register SQL Server tables. The Create Nickname
wizard can be launched by expanding the database of interest in CC. Right-click
the Nicknames folder, then select the Create... options, as illustrated in
Figure 8-6.
2. Create a server.
A server represents a specific data source that is accessible through a
wrapper. Each server can have its own set of server options. For example, the
collating sequence of the SQL Server database can be specified. This affects
the sort order used and helps determine if parts of a SQL statement should
be pushed down and handled by the remote data source.
Register a server with the name REDBOOK that resides on SQL Server, using
SQLSERVER as the wrapper name:
CREATE SERVER redbookSRV TYPE MSSQLSERVER VERSION '2000' WRAPPER
"SQLSERVER" OPTIONS( ADD NODE 'sqlserver', DBNAME 'redbook');
For the NODE option, the value used should be the System DSN name for the
SQL Server remote server being accessed. This value is case sensitive.
3. Create a user mapping.
A user mapping provides a mapping from a user ID that accesses the DB2
UDB server to a data source user ID and password that WebSphere II
includes in connections to the remote data source on behalf of the DB2 UDB
user.
The following example shows how to map a DB2 UDB server user ID to a
SQL Server remote server user ID and password:
CREATE USER MAPPING FOR db2user1 SERVER redbooksSRV OPTIONS
(REMOTE_AUTHID 'mssqluser1', REMOTE_PASSWORD 'password')
4. Create nicknames.
A nickname is a local alias of a database object (table, view, etc.) residing in
SQL Server. Queries can reference nicknames as though they are local
database objects in DB2 UDB.
To register a nickname for a SQL Server table named authors, the following
statement can be issued:
CREATE NICKNAME asn.authors FOR redbook.dbo.authors
Table 8-1 Compare between query using nickname in SQL Server, DB2 UDB
SQL Server query DB2 UDB query
Note: Notice how the SQL statement using nicknames accesses the remote
tables residing in SQL Server. The end user/application is unaware that they
represent remote tables.
Figure 8-7 shows how a SQL Server table can be accessed from Control Center
once a nickname for it has been setup.
Figure 8-7 SQL Server tables accessed in Control Center through nicknames
Some of the fundamental terminology of DB2 UDB SQL replication that you need
to understand when using WebSphere II is listed below. For more information,
refer to the DB2 UDB documentation IBM WebSphere Information Integrator
SQL Replication Guide and Reference Version 8.2, SC27-1121.
Subscription Set
A subscription set defines the relationship between the replication source
database and a target database. A subscription set member defines a
relationship between the replication source table and one or more target tables. It
is best to have the target tables and the rest of the database objects pre-created
before setting up replication.
The following steps summarize how to setup SQL replication between SQL
Server and DB2 UDB.
1. Create replication control tables for the CAPTURE program.
2. Enable the source database for replication.
3. Register a replication source.
4. Create replication control tables for the APPLY program.
5. Create a subscription set and a subscription-set member.
6. Create an APPLY password file.
7. Start the APPLY program.
There are also number of third party data migration tools available to assist you
in moving your database, application, and data from an existing DBMS to DB2
UDB.
Ispirer Systems
Ispirer Systems offers SQLWays, a database and data migration tool
Ascential DataStage
The DataStage product family is an extraction, transformation, and loading
(ETL) solution with end-to-end metadata management and data quality
assurance functions.
Data Junction
DataJunction data migration tool provides assistance in moving data from
source database to DB2 UDB. This tool accounts for data type differences,
and can set various filters to dynamically modify target columns during the
conversion process.
To convert SQL scripts from SQL Server to DB2 UDB, the main task comprises
making syntax changes. Refer to Chapter 4, “SQL considerations” on page 65,
which helps you identify SQL syntax differences between SQL Server and DB2
UDB. Also, refer to 10.2, “Converting to DB2 UDB compatible SQL syntax” on
page 226 that explains how to use MTK’s SQL Translator to convert SQL Server
syntax to DB2 UDB-compatible syntax.
In the remainder of this chapter, we discuss how to work with DB2 UDB SQL
scripts.
SQL scripts can also be loaded or created in Task Center and scheduled to run
at a specified time. Figure 8-9 shows how a script can be loaded into the task
center.
Note: In DB2 UDB, only inline SQL can be used for SQL scripting.
For more information about scripting with DB2 UDB, consult the following
articles:
http://www.ibm.com/developerworks/db2/library/techarticle/0203yip/0203yip.html
http://www.ibm.com/developerworks/db2/library/techarticle/0211yip/0211yip.html
http://www.ibm.com/developerworks/db2/library/techarticle/0307fierros/0307fierr
os.html
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0406fierros/index.
html
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0503melnyk/index.h
tml
This chapter also provides examples that can serve as a reference during a
conversion to DB2 UDB. The following database objects are covered:
Temporary tables
Stored procedures
User defined functions
Triggers
DGTTs are created in a USER TEMPORARY table space, which can be optionally
specified using the IN clause of the DECLARE GLOBAL TEMPORARY TABLE statement.
If this clause is not specified, a table space is chosen from the set of existing user
temporary table spaces. The table space chosen must be one for which the user
has USE privilege and must have sufficient space to contain the table. Locking is
NOT performed on DGTTs. Modifications made to temporary tables during a
transaction can optionally be logged or not. Example 9-1 shows a representative
DGTT declaration in DB2 UDB.
In this example, a temporary table called gbl_temp is created with the same
structure as the employee table. When a COMMIT statement is eventually issued,
all rows in the table are deleted automatically. Further, no logging occurs when
DGTTs must always be referenced using the SESSION schema. For instance, the
temporary table created in Example 9-1 needs to be fully qualified with the
SESSION schema when referenced in a SQL statement such as the following:
SELECT * FROM SESSION.gbl_temp
DB2 UDB also supports the use of indexes and statistics collection on DGTTs to
improve performance. Undo logging is also supported to permit ROLLBACKs.
Note: In a default DB2 UDB installation, a user temporary table space is not
created. Any attempt to create a temporary table therefore results in a
SQL0286N error. A user temporary table space of sufficient size must be
created prior to creating the temporary table. The user creating the temporary
table must also have the USE privilege on the table space.
Example 9-3 shows the equivalent temporary table definition in DB2 UDB. The
table is created in a table space called temp4k.
Example 9-5 shows the equivalent temporary table definition in DB2 UDB. The
table is created using a standard DB2 UDB table defined with the ability to turn
logging on and off.
SQL Server extended stored procedures are routines written in a language such
as C. SQL Server automatically loads and execute these routines just like a
regular stored procedure. These procedures run directly in the address space of
SQL Server. The DLLs are created using the Open Data Services (ODS) API.
Important: Running stored procedures in NOT FENCED mode that have not
been adequately tested can compromise integrity of the server. Ensure that
stored procedures are well-tested when defined as NOT FENCED.
In DB2 UDB, once the CREATE PROCEDURE statement is executed, procedural and
SQL statements in the procedure are converted into a native representation
which is stored in the database catalogs. When an SQL procedure is called, the
native representation is loaded from the catalogs and the DB2 UDB engine
executes the procedure.
9.2.4 @@PROCID
The SQL Server function @@PROCID returns the stored procedure identifier (ID) of
the current procedure. Example 9-6 shows a sample stored procedure that uses
a SELECT statement to display the @@PROCID setting from inside the procedure.
Example 9-6 SQL Server stored procedure using the @@PROCID function
CREATE PROCEDURE testprocedure AS
SELECT @@PROCID AS 'ProcID'
GO
EXEC testprocedure
GO
Example 9-7 DB2 UDB external Java UDF simulating the @@PROCID function
CREATE FUNCTION proc_id()
RETURNS INTEGER
LANGUAGE JAVA
PARAMETER STYLE DB2GENERAL
DETERMINISTIC
NOT FENCED
NULL CALL
NO SQL
NO EXTERNAL ACTION
DBINFO
EXTERNAL NAME 'proc_id!proc_id'
!
import COM.ibm.db2.app.*;
Example 9-9 shows the equivalent DB2 UDB SQL PL stored procedure. Notice
how additional exception handlers have been defined in the converted
procedure. This is due to the differences between how errors are handled in
T-SQL and SQL PL. Refer to 4.9, “Transact-SQL to SQL PL translation” on
page 96 for more information.
SELECT ROUND(MAX(price), 4)
INTO v_max_price
FROM redbooks
WHERE brand_id = v_b_id
END
!
Example 9-10 Using default parameter values in SQL Server stored procedures
CREATE PROC get_cheap_books
@cost money = 20.00
AS
SELECT title, price FROM redbooks WHERE price < @cost
GO
In DB2 UDB, default parameter values cannot be specified as part of the CREATE
PROCEDURE statement. There are two options for converting this functionality to
DB2 UDB:
Ensure that all invocations of the procedure (in the application code and other
database objects) always supply the correct default parameter value when
one is required. This solution requires the management of specific default
values outside of the stored procedure logic. That is, all places in the existing
code that invoke the procedure must always specify the correct default value.
When the default value changes, all the corresponding invocation statements
must be updated with the new value.
Ensure that all invocations of the procedure (in the application code and other
database objects) invoke the procedure with a “dummy” value to indicate to
the procedure that a default value defined within the procedure body should
be used instead. Note that for this solution to be successful, a dummy value
must be chosen that is unique from the range of expected real data values.
Further, all invocations of the procedure (in the application code and other
database objects) must be modified to invoke the procedure with the dummy
Example 9-11 DB2 UDB stored procedure simulating default parameter values
CREATE PROCEDURE get_cheap_books (IN v_cost DECIMAL(19,4))
LANGUAGE SQL
READS SQL DATA
DYNAMIC RESULT SETS 1
BEGIN
-- the real default value for the input parameter
-- this variable is used in place of the input parameter
DECLARE default_cost DECIMAL(19,4) DEFAULT 20.00;
-- only change the local default cost variable when the input parameter
-- provided is NOT the dummy value
IF (v_cost != -9999.99) THEN
SET default_cost = v_cost;
END IF;
BEGIN
-- cursor is declared using the locally declared cost value, NOT
-- the input parameter
DECLARE temp_cursor CURSOR WITH HOLD WITH RETURN TO CLIENT
FOR SELECT title, price
FROM redbooks
WHERE price < default_cost;
OPEN temp_cursor;
END;
END
When invoked with the statement above, the dummy value is replaced with the
real default value (20.00) defined in the procedure.
Example 9-12 SQL Server stored procedure using default wildcard parameters
CREATE PROCEDURE get_author_info
@lname varchar(30) = 'K%',
@fname varchar(18) = '%'
AS
SELECT a.name, a.firstname, r.title
FROM authors a INNER JOIN redbooks r ON a.author_id = r.author_id
WHERE a.firstname LIKE @fname AND a.name LIKE @lname
GROUP BY a.name, a.firstname, r.title
GO
Example 9-13 shows the equivalent stored procedure in DB2 UDB. This
procedure uses the dummy value approach to simulate the use of default
parameters. The dummy value used in this example is the character string
‘!!!!!!!!’.
BEGIN
DECLARE temp_cursor CURSOR WITH HOLD WITH RETURN TO CLIENT
FOR SELECT a.name, a.firstname, r.title
FROM authors a INNER JOIN redbooks r ON a.author_id = r.author_id
WHERE a.firstname LIKE fname_default AND a.name LIKE lname_default
GROUP BY a.name, a.firstname, r.title;
OPEN temp_cursor;
END;
END
!
Table 9-1 shows sample invocations of the procedure and the resulting output.
Table 9-1 Different invocation of SQL Server procedure shown in Example 9-14
Condition Invocation Statement Output
Example 9-15 shows the corresponding stored procedure in DB2 UDB. Note, in
this example, the “dummy value” approach described in 9.2.6, “Default parameter
values” on page 193, is used to simulate the use of default parameter values.
The dummy value used for each parameter is the number ‘-9999999’.
Example 9-15 Equivalent DB2 UDB stored procedure simulating default parameters
CREATE PROCEDURE return_params ( IN p1 INTEGER,
IN p2 INTEGER,
IN p3 INTEGER,
OUT p1_out INTEGER,
OUT p2_out INTEGER,
OUT p3_out INTEGER)
LANGUAGE SQL
CONTAINS SQL
BEGIN
-- the real default values for the input parameters
-- these variables are used in place of the input parameters
DECLARE default_p1 INTEGER DEFAULT NULL;
DECLARE default_p2 INTEGER DEFAULT 1;
DECLARE default_p3 INTEGER DEFAULT 2;
-- only change the local default variables when the input parameters
-- provided are NOT the dummy value
IF (p1 != -9999999) THEN
SET default_p1 = p1;
END IF;
Table 9-2 shows the corresponding invocation scenarios of the procedure and
the output.
As is evident from Table 9-2, all parameters must be supplied when invoking a
stored procedure. Default parameter values are specified using the dummy value
expected by the procedure.
To invoke this procedure from CLP, the following command can be issued:
CALL count_books(‘Smith’, ‘John’, ?)
A question mark (?) is used as a placeholder for the output parameter. In DB2
UDB, all parameters must be supplied when invoking a stored procedure.
OPEN @titles_cursor
GO
To invoke the procedure above and receive and manipulate the output cursor, the
following code can be used:
DECLARE @MyCursor CURSOR
EXEC titles_cursor @cur_titles = @MyCursor OUTPUT
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM @MyCursor
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
GO
In DB2 UDB, cursor result sets can also be passed back to the calling
application, although through a different mechanism. To indicate that a cursor is
to be returned to the calling application, the following three steps must be
completed:
1. The DYNAMIC RESULT SETS clause in the CREATE PROCEDURE statement must
specify the number of result sets that will be returned.
2. The cursor must be declared as WITH RETURN.
3. The cursor must be opened before being returned to the client application.
Example 9-18 Equivalent DB2 UDB stored procedure returning a cursor to the caller
CREATE PROCEDURE titles_cursor()
LANGUAGE SQL
READS SQL DATA
DYNAMIC RESULT SETS 1
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE l_error CHAR(5) DEFAULT '00000';
OPEN titles_cursor;
END
!
Example 9-19 shows another DB2 UDB procedure that counts the number of
book titles in the database. This procedure invokes the one in Example 9-18 and
receives the cursor that is passed back to it. Note that Example 9-18 is purely for
illustration purposes in order to demonstrate the use of returning result sets to
the caller. There are more efficient ways to carry out the intended counting
operation.
Example 9-19 A DB2 UDB stored procedure receiving a cursor result set
CREATE PROCEDURE count_titles (OUT p_counter INT)
LANGUAGE SQL
BEGIN
DECLARE v_book_no CHAR(10);
DECLARE v_title VARCHAR(80);
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE v_rs RESULT_SET_LOCATOR VARYING;
CALL titles_cursor();
ASSOCIATE RESULT SET LOCATOR (v_rs)
WITH PROCEDURE titles_cursor;
SET p_counter = 0;
WHILE (SQLSTATE = '00000') DO
SET p_counter = p_counter + 1;
FETCH FROM c_rs INTO v_book_no, v_title;
END WHILE;
END
!
@@ERROR
The @@ERROR function is used to check for a success or failure status after the
evaluation of a statement. Example 9-20 shows a SQL Server stored procedure
that checks the value of @@ERROR after performing an INSERT statement. If an error
value is returned from the INSERT statement, the transaction is rolled back.
Example 9-20 SQL Server stored procedure referencing the @@ERROR function
CREATE PROCEDURE add_brand(
@b_id VARCHAR(6),
@b_name VARCHAR(20),
@b_desc VARCHAR(80))
AS
BEGIN TRAN
INSERT brands(brand_id, brand_name, brand_desc)
VALUES (@b_id, @b_name, @b_desc)
IF (@@ERROR <> 0)
BEGIN
ROLLBACK TRAN
RETURN 1
END
COMMIT TRAN
RETURN 0
GO
Example 9-21 Equivalent DB2 UDB stored procedure with exception handlers
CREATE PROCEDURE add_brand ( v_b_id INTEGER,
v_b_name VARCHAR(20),
v_b_desc VARCHAR(80) )
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE l_error CHAR(5) DEFAULT '00000';
DECLARE v_trancount INTEGER DEFAULT 0;
IF v_trancount = 0 THEN
COMMIT;
END IF;
IF v_trancount = 1 THEN
COMMIT;
END IF;
RETURN 0;
END
!
Notice how COMMIT statements are handled in the code. A local procedure
variable, v_trancount, is used to keep track of the transaction nesting level
(equivalent to @@TRANSCOUNT in T-SQL). A COMMIT statement is only issued when
the value of v_trancount is zero or one. Otherwise, the value of v_trancount is
decremented by one. Although this additional transaction logic is not really
required for this specific procedure, it has been included to demonstrate how
similar transaction control behavior can be simulated in SQL PL.
Example 9-22 SQL Server stored procedure using the RAISERROR statement
CREATE PROCEDURE get_redbook_price (
@b_no char(10),
@book_price money)
AS
DECLARE @b_price money
IF @@ROWCOUNT = 0
BEGIN
RAISERROR ('No redbook with this book number was found.', 16, 1)
RETURN 0
END
RETURN 1
GO
SELECT price
INTO v_price
FROM redbooks r
WHERE r.book_no = v_b_no
FETCH FIRST 1 ROWS ONLY;
SELECT COUNT(*)
INTO l_rowcount
FROM (
SELECT price
FROM redbooks r
WHERE r.book_no = v_b_no) temp_table;
IF l_rowcount = 0 THEN
-- Signalling an exception using RAISERROR and returning a value
-- simultaneously is not possible. The RAISERROR statement is not
-- translated.
RETURN -9;
Notice how the RAISERROR statement is converted in the above stored procedure.
The original T-SQL procedure returned a value as well as signalled an exception.
Since both of these cannot happen simultaneously in SQL PL, an exception is
not raised in the procedure. Rather, the original return value (-9) is returned to
the calling application. However, if the desired effect in the procedure is to raise
an exception, the RETURN statement can be replaced with a SIGNAL SQLSTATE
statement.
SAVE TRAN B
ROLLBACK TRAN B
ROLLBACK TRAN A
Example 9-25 Equivalent DB2 UDB stored procedure with nested savepoints
-- Assumes the use of the following table definition
-- CREATE TABLE nestlog (c1 INTEGER)
CREATE PROCEDURE nested_tran()
ROLLBACK TO SAVEPOINT B;
-- ROLLBACK un-does the entire transaction (i.e. right back to the BEGIN
-- statement)
ROLLBACK;
The values of v_trancount can be written to a file using the user defined function
called put_line(), which enables file output from pure SQL. This function is
provided as additional material with this redbook. This function has similar
functionality as the PRINT statement. It can be used as a tool for debugging
stored procedures, but also allows any messages to be written to a specified file
for other purposes as well. Calls to this function have been left as comments in
the above code.
The function code sources and an installation guide can be found on the IBM
Redbooks Web site. For more details, see Appendix G, “Additional material” on
page 509.
The following articles on the DB2 developerWorks Web site are also helpful for
improving the performance of stored procedures:
http://www.ibm.com/developerworks/db2/library/techarticle/0306arocena/
0306arocena.html
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0501riela
u/
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0406riela
u/index.html
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0409riela
u/index.html
SQL Server UDFs cannot be used to perform a set of actions that modify the
global database state. Further, the SQL statements inside a scalar-valued
function cannot include any non-deterministic system functions.
SQL PL UDFs can be of two different types, scalar functions (return a scalar
value, including NULL) and table functions (return a row or table of data). A scalar
function’s return type is specified in the RETURNS clause of the function. For table
functions, the ROW or TABLE keyword is specified in the RETURNS clause. Functions
can be deterministic or non-deterministic. Only table functions are able to modify
the database.
To execute a table function in DB2 UDB, the TABLE keyword must be specified to
indicate the function returns a table, for example:
SELECT * FROM TABLE(books_by_brand(32)) AS T
To convert a SQL Server UDF using any of the above features, there are three
options:
Attempt to replace the unsupported statement with an equivalent statement or
remove it entirely.
The first option is usually the easiest way to proceed. Sometimes, replacing an
unsupported statement with a similar supported one, or removing it entirely is
possible. For instance, the LOOP and REPEAT statements can usually be
implemented using WHILE statement logic. An explicit CURSOR declaration can
sometimes be replaced with an implicit cursor using the FOR <cursor_name> AS
logic. The SELECT INTO statement can be replaced with a SET statement.
Example 9-30 SQL Server UDF performing SELECT list variable assignment
CREATE FUNCTION avg_price2(@brand_id int)
RETURNS money
AS
BEGIN
DECLARE @avg money
RETURN @avg
END
GO
Example 9-31 shows the corresponding DB2 UDB function. Since the SELECT
INTO statement is not supported in inline SQL PL, the SET statement is used
instead.
Example 9-31 Equivalent DB2 UDB user defined function using a SET statement
CREATE FUNCTION avg_price2 (v_brand_id INTEGER)
RETURNS DECIMAL(19,4)
LANGUAGE SQL
READS SQL DATA
BEGIN ATOMIC
DECLARE v_avg DECIMAL(19,4);
Example 9-32 shows a DB2 UDB table function that invokes a stored procedure
to perform more complicated transaction handling logic using savepoints. The
following table definition is assumed by the stored procedure:
CREATE TABLE result_tab (c1 INTEGER)
Example 9-32 DB2 UDB table function that invokes a stored procedure
-- Assumes the use of the following table definition:
-- CREATE TABLE result_tab (c1 INTEGER)
Note that when invoking stored procedures from UDFs, data access restrictions
must be kept in mind. Both SQL stored procedures and UDFs have several data
access options, specified using the CONTAINS SQL, READS SQL DATA, and MODIFIES
SQL DATA clauses in the procedure/function definition. The CONTAINS SQL option
indicates that only statements that do not read or modify data are allowed. The
READS SQL DATA option indicates that only statements that do not modify SQL
data (INSERT, UPDATE, DELETE) are allowed. The MODIFIES SQL DATA option is the
least restrictive and indicates that any supported SQL statement can be
executed. These access option, presented in increasing order of accessibility,
cannot conflict with each other. That is, UDFs or stored procedures with lower
data access level cannot invoke UDFs or SQL procedures with higher data
access levels. For instance, a SQL UDF defined with the READS SQL DATA cannot
invoke a stored procedure defined with MODIFIES SQL DATA. Similarly, a SQL
stored procedure defined as CONTAINS SQL cannot execute a UDF defined with
READS SQL DATA, and so on.
It is also important to be aware of read and write conflict errors that can occur
when a SQL stored procedure is invoked by a UDF in DB2 UDB. A set of data
integrity rules are enforced when a SQL stored procedure is invoked by a UDF.
Essentially, DB2 UDB does not allow conflicting operations to occur when a
stored procedure invoked by a UDF concurrently accesses the same table. If
such a conflict occurs, an error (SQLCODE -746, SQLSTATE 57053) is returned to the
statement that caused the conflict at runtime. It is therefore important to test all
conditions to ensure that conflict errors do not occur at runtime.
The following articles on the DB2 developerWorks Web site also provide useful
information for user defined function development:
http://www.ibm.com/developerworks/db2/library/techarticle/0309greenste
in/0309stolze.html
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0501nicho
lson/index.html
9.4 Triggers
Triggers are server-side database objects that are commonly used to maintain
complex data integrity rules, implement referential actions such as cascading
DELETEs, and maintain an audit record of changes. Triggers are supported in both
SQL Server and DB2 UDB, albeit with some differences.
SQL Server supports INSERT, UPDATE, and DELETE triggers. A trigger can be
associated with more than one type of INSERT, UPDATE, and DELETE event. Triggers
can be nested up to 32 levels. Direct or indirect recursion is available as a
database configuration option.
Triggers can be activated before or after any table data is affected by the
triggering SQL statement, by specifying the NO CASCADE BEFORE or AFTER clauses
in the CREATE TRIGGER statement. If the NO CASCADE BEFORE clause is specified,
the trigger logic is applied before any changes caused by the triggering SQL
statement are applied to the database. This implies that the triggering SQL
Triggers can also be defined to fire every time a row in the target table is affected
by the triggering SQL statement or fire once for each statement, no matter how
many rows are affected by the triggering statement. Only AFTER triggers can be
defined to fire once for each statement. Multiple triggers can be defined for the
same event. The order in which the triggers are fired is the same as the order
they were created in. The maximum depth of cascading triggers is 16 levels.
Two DB2 UDB triggers are created: a trigger for the INSERT operation, and
another for the UPDATE operation. Separate triggers must be defined for each type
of triggering SQL statement in DB2 UDB.
It is also important to note that in DB2 UDB, a DELETE trigger cannot reference
the NEW_TABLE transition table and an INSERT trigger cannot reference the
OLD_TABLE transition table. To directly convert such functionality, references to
the NEW_TABLE table in a DELETE trigger and the OLD_TABLE table in an INSERT
trigger can be translated into a query that evaluates to an empty table (with the
correct set of columns). In many cases, the statement containing such
references is redundant and the trigger may be simplified by deleting the
statement.
9.4.3 IF UPDATE(column)
The SQL Server IF UPDATE(column) condition can be used within a trigger to test
if a particular column’s value has been modified by the triggering SQL statement.
Example 9-36 shows the equivalent trigger in DB2 UDB using the WHEN clause to
simulate the IF UPDATE(column) function.
Example 9-37 shows another equivalent trigger in DB2 UDB using the UPDATE OF
<column> syntax to simulate the IF UPDATE(column) function.
Example 9-37 Equivalent DB2 UDB trigger using the UPDATE OF <column> clause
CREATE TRIGGER tr_authors2
AFTER UPDATE OF contract ON authors
REFERENCING OLD AS o NEW AS n
FOR EACH ROW
BEGIN ATOMIC
-- executable-statements
END
!
To convert a SQL Server trigger using any of the above features, there are two
options:
Attempt to replace the unsupported statement with an equivalent supported
statement or remove it entirely.
Move the unsupported statement into a new stored procedure and invoke the
stored procedure from the trigger.
The first option is typically the easiest way to proceed. Sometimes, replacing an
unsupported statement with a similar supported one, or removing it entirely is
Example 9-38 shows an example of a SQL Server trigger using an explicit cursor
declaration. This trigger fires after an INSERT operation occurs on the titles
table.
OPEN c1
SET @rownum = 0
WHILE @@FETCH_STATUS = 0
BEGIN
SET @rownum = @rownum + 1
CLOSE c1
DEALLOCATE c1
IF (@rownum > 5)
BEGIN
RAISERROR('Too many authors. Statement will be aborted.', 16, 1)
ROLLBACK TRAN
END
GO
Example 9-39 shows how this trigger can be converted to DB2 UDB by using an
implicit cursor declaration. In this trigger, a NULL indicator variable is used to
detect if the cursor does not return any data. If no data is returned, the v_rowcount
SET v_rowcount = 0;
FOR c1 AS
SELECT name, 1 AS null_ind FROM authors
DO
SET l_name = c1.name;
SET l_name_ind = c1.null_ind;
It is important to be aware of read and write conflict errors that can occur when a
SQL stored procedure is invoked by a trigger in DB2 UDB. A set of data integrity
Example 9-40 shows a SQL Server INSTEAD OF trigger defined on a view. This
trigger fires when an INSERT statement is attempted against the view v_myView.
Instead of inserting into the view, the logic contained in the trigger body is
executed, namely inserting the first two columns of the rows that were attempted
to be inserted into myView into the table myTable. The following view and table
definitions are assumed by this example:
CREATE TABLE myTable (col1 INTEGER, col2 INTEGER)
CREATE VIEW myView AS SELECT col1, col2 FROM myTable
SQL Server INSTEAD OF triggers that are defined on views can be converted as
DB2 UDB INSTEAD OF triggers. SQL Server INSTEAD OF triggers that are defined
on normal tables differ significantly in behavior than DB2 UDB BEFORE and AFTER
triggers and a direct translation to DB2 UDB INSTEAD OF triggers would not be
valid. The reason for this is that the SQL Server INSTEAD OF trigger action is
executed instead of the triggering statement. In DB2 UDB BEFORE and AFTER
triggers, both the triggering action and the triggering statements are executed
eventually. Trigger redesign effort is required to convert this type of functionality
to DB2 UDB if the same behavior is desired.
One method of performing this conversion involves renaming the original table,
then creating a view with the same name as the original table. All the original
statements in the application code referencing the table will now be referencing
the view. The INSTEAD OF trigger can then be converted as a DB2 UDB INSTEAD
OF trigger since it will operate on the newly created view.
The following articles on the DB2 developerWorks Web site also provide useful
information for trigger development:
http://www.ibm.com/developerworks/db2/library/techarticle/0308bhogal/0
308bhogal.html
Since SQL Server and DB2 UDB do not implement the exact same components
of the ANSI SQL standard, some of the SQL statements in the application may
require alternation when converted to DB2 UDB. Any SQL Server proprietary
SQL syntax or functions requires attention when converting to DB2 UDB. Usually
this functionality can be simulated using DB2 UDB compatible features.
Aside from identifying SQL language differences, you also need to understand
the transaction, locking, and isolation level differences between SQL Server and
DB2 UDB. These differences influence how the application should be coded, and
may require you to make some adjustments in order for it to run as expected.
The database interface being used to communicate with SQL Server can usually
also be used with DB2 UDB, except in the case of proprietary interfaces, which
are usually converted to use DB2’s Call Level Interface (CLI) interface. When
converting the application, you must determine which interface to use with DB2
UDB, configure the appropriate interface drivers, and modify the application for
any function call differences, unsupported functions, and data type differences.
When converting a packaged application like SAP or Siebel, there are certain
conversion limitations you must also be aware of and take into account, in order
to maintain support from the vendor.
Enter a SQL Server SQL statement in the white area of SQL Translator window
(see Figure 10-2). Click the Convert button to convert the statement to DB2 UDB
compatible syntax. Note that the MTK has some limitations and cannot convert
all possible SQL Server syntax. Refer to the MTK documentation for more
information about its limitations.
DB2 UDB supports different levels of locking, which are similar to the SQL
Server levels. Table 10-1 shows a comparison of SQL Server and DB2 UDB
locking levels.
Table 10-1 Comparison of SQL Server and DB2 UDB locking levels
SQL Server DB2 UDB
Database Database
Table Table
Row Row
Index Index
Page N/A
Key N/A
In addition, there are intent shared locks, intent exclusive locks, and intent update
locks. SQL Server uses three additional modes of locking: schema stability locks,
schema modification locks, and bulk update locks.
The type of lock chosen depends on the type of SQL statement issued and what
isolation level is set. In general, every SQL statement that modifies data causes
an exclusive lock to be requested on the corresponding database object. Shared
locks are set when an SQL statement reads an object. Update locks are set
when an application is using an UPDATE CURSOR.
DB2 UDB has a complete set of locks to ensure data integrity and provide
access flexibility. Table 10-2 shows a complete list of DB2 UDB locks.
IN Table spaces, The lock owner can read any data in the object,
(Intent None) blocks, including uncommitted data, but cannot update
tables any of it. Other concurrent applications can read
or update the table.
IS Table spaces, The lock owner can read data in the locked table,
(Intent Share) blocks, but cannot update this data. Other applications
tables can read or update the table.
SIX Tables, The lock owner can read and update data. Other
(Share with blocks concurrent applications can read the table.
Intent
Exclusive)
X Rows, The lock owner can both read and update data
(Exclusive) blocks, in the locked object. Only uncommitted read
tables, applications can access the locked object.
buffer pools
Figure 10-4 CONNECT database statement in DB2 UDB with lock mode
10.4.3 Deadlock
Deadlock occurs when two or more transactions wait indefinitely for each others
locks, until an external event causes one or both of them to rollback. DB2 UDB
uses a deadlock detection mechanism to check the system for deadlocks and
selects one of the deadlocked transactions to be rolled back. An error code is
returned to the application in this case.
A deadlock event can be recorded using the deadlock event monitor. This
monitor can capture additional information, such as the SQL statement that was
being executed when the deadlock occurred and what locks were held by the
application that encountered the deadlock. The information captured by the
monitor focuses on the locks involved in the deadlock and the applications that
held them.
More information can also be found in the DB2 UDB documentation Application
Development Guide: Programming Client Applications, SC09-4826.
Table 10-3 shows a comparison of SQL Server and DB2 UDB isolation levels.
Table 10-3 A comparison of SQL Server and DB2 UDB isolation levels
SQL Server DB2 UDB
Example 10-1 Changing the isolation level for an individual SQL statement
SELECT <columns>
FROM <tables> ... WITH {UR|RR|CS|RS}
DB2 Universal Database Version 8.1, 5622-044 (c) Copyright IBM Corp. 1991, 2002
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL Explain Tool
Partition Parallel = No
Intra-Partition Parallel = No
Unlike Repeatable Read, Read Stability does not completely isolate the
application process from the effects of concurrent application processes. At the
RS level, application processes that issue the same query more than once might
see additional rows caused by other application processes adding new
information to the database. These additional rows are called phantom rows.
DB2 UDB provides the DB2 .NET Data Provider, which allows .NET applications
to access DB2 UDB database directly. OLE DB .NET and ODBC .NET data
providers can also be used for .NET applications for direct access, but not as
favorably as with the DB2 .NET data provider because they do not have the
functionality or performance. Visual Basic applications, Visual C++ applications,
and applications that conform to DAO and RDO, can access DB2 UDB using
OLE DB, an ODBC bridge, or the IBM OLE DB Provider for DB2. For Java
applications, JDBC or embedded SQL for Java (SQLJ) can be used.
The main task involved in converting the application interfaces involves ensuring
that all function calls used and supported by the SQL Server interface driver are
supported by the DB2 UDB driver. In most cases, they are and no changes are
required. In some cases an unsupported function call may need to be removed or
simulated using other methods. Any proprietary interfaces used, such as the
DB-Library API, require changes to work with DB2 UDB. Usually, DB2 UDB’s Call
Level Interface (CLI) is used in these situations.
The following sections discuss the different DB2 UDB data providers and key
considerations when using them.
Unmanaged
DB
The OLE DB .NET and ODBC .NET data providers are bridge providers. These
type of providers enable ADO.NET access to any data source using existing data
access technologies such as OLE DB and ODBC. The OLE DB .NET Data
Provider uses the OLE DB provider whereas the ODBC .NET Data Provider uses
the ODBC driver.
SQL Server’s SQL .NET provider and DB2 UDB’s .NET provider are native
providers that communicate with only one specific data source. Native providers
always yield better performance than bridge providers.
If you installed Visual Studio .NET after DB2 UDB, you must register the DB2
Visual Studio add-in before being able to leverage all the integrated features in
the Visual Studio integrated development environment (IDE).
To register the add-in, execute the following command from a command window:
C:\%DB2HOME%\SQLLIB\BIN\db2vsregister.bat
To use the add-in in an existing project, you must add a reference to the data
provider library in your project.
1. Click Project menu → Add Reference
2. At the .NET tab, search for IBM.Data.DB2.dll in the component name column
3. Highlight it and click Select
4. Click OK to add the reference
Replace the following object references in your application code to the DB2 UDB
supported object references:
SqlConnection → DB2Connection
SqlDataReader → DB2DataReader
SqlCommand → DB2Command
SqlDataAdapter → DB2DataAdapter
Also change the data type enumerations to use the DB2 UDB supported types,
for example:
SqlDBType.Int → DB2Type.Int32
SqlDBType.NVarChar → DB2Type.String
SqlDBType.DateTime → DB2Type.DateTime
For a complete list, refer to DB2 .NET Data Provider reference documentation,
available at:
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp?topic=/com.ibm.d
b2.udb.dndp.doc/htm/frlrfIBMDataDB2Hierarchy.htm
Example 10-4 Comparison of typical connection syntax using OLE DB and ODBC
Microsoft OLE DB:
OleDbConnection con = new OleDbConnection(“Provider=SQLOLEDB;
Data source=localhost;Integrated Security=SSPI;Initial Catalog=redbook")
Microsoft ODBC:
OdbcConnection con = new OdbcConnection("Driver={SQL Server};
Server=localhost;Trusted_Connection=yes;Database=redbook");
IBM ODBC:
OdbcConnection con = new
OdbcConnection(“DSN=redbook;UID=userid;PWD=password;”);
For Visual Basic applications, the connection parameters are the same as those
in Example 10-4. These providers do not require many changes in the
application code when converting to DB2 UDB, but you should investigate any
additional usage restrictions they may have. Refer to the DB2 UDB
documentation Application Development Guide: Programming Client
Applications, SC09-4826, for more information.
Example 10-5 Converting an OLE DB connection string from SQL Server to DB2 UDB
Microsoft OLE DB for SQL Server:
“Provider=SQLOLEDB;Data Source=serverName;Initial Catalog=REDBOOK;User
ID=userid;Password=password;”
Table 10-5 provides a mapping between the OLE DB data types used in SQL
Server and DB2 UDB.
ntext
Command ICommandPersist
DataSource IConnectionPoint
IDBAsynchNotify (consumer)
IDBAsynchStatus
IDBConnectionPointContainer
RowSet
IDBDataSourceAdmin
IChapteredRowset
IDBAsynchStatus
IParentRowset
IRowsetChapterMember
IRowsetFind
IRowsetIndex
Session IRowsetView
IAlterIndex
IAlterTable
IIndexDefinition
ITableDefinition
ITableDefinitionWithConstra
View Objects ints
ITransactionObject
ITransactionOptions
IViewChapter
IViewFilter
IViewRowset
IViewSort
Refer to Figure 10-6 for an illustration of the ODBC and CLI environment.
Application Application
Microsoft Microsoft
DB2
ODBC ODBC ODBC/CLI
driver driver driver
A B
DB2
DB2
Client
Server
Gateway DB2
SQL
A Client
Server
SQL DB2
Server Server
Figure 10-6 ODBC Driver Manager environment versus DB2 CLI environment
If the DB2 ODBC driver, going through the ODBC Driver Manager, is used, all the
functions based on ODBC V3.51 are supported. DB2-specific functions cannot
be accessed in an ODBC environment unless an escape clause is used.
Applications using DB2 CLI link directly to the DB2 CLI library. DB2 CLI includes
support for many ODBC and ISO SQL/CLI functions, as well as DB2 specific
functions. Table 10-7 shows a comparison of available functions between the
DB2 ODBC driver and the DB2 CLI driver.
In DB2 UDB Version 8, a new Universal JDBC Driver is provided. This driver is
completely written in Java. The driver library, db2jcc.jar, can be found in the
C:\DB2_HOME\SQLLIB\JAVA directory. This driver supports both JDBC and SQLJ
APIs in a single implementation. Both JDBC and SQLJ can interoperate in the
same application.
If your application is written in Java and uses JDBC to communicate with the
database, it is straightforward to convert to DB2 UDB. The JDBC API is well
defined and is database independent. For instance, the database connection
logic is encapsulated in standard J2EE Data Source objects. The changes
required include the JDBC driver file (which comes with DB2 UDB), the database
connection string, as well as syntax for SQL statements.
Java applications can access DB2 UDB in one of the following ways:
DB2 Server
– Stored procedures (JDBC or SQLJ)
– User-defined functions (JDBC or SQLJ)
Internet browser
– Applets based on JDBC (JDBC)
J2EE Application Servers (such as WebSphere Application Server)
– Java ServerPages (JSPs) (JDBC)
– Servlets (SQLJ or JDBC)
– Enterprise JavaBeans (EJBs) (SQLJ or JDBC)
For more information about Java application development, refer to the DB2 UDB
Java Web site at:
http://www.ibm.com/software/data/db2/udb/ad/v8/java/
Connecting to a database
The CONNECT TO statement, along with the server name, database name, user
name, and password, can be used to connect to a SQL Server database. If the
server name is not specified, the local server is assumed. The password is
optional and the user name can be replaced by $integrated to use Windows
Authentication.
The connection syntax in DB2 UDB is slightly different. The database name
specification is mandatory, but the user name and password are optional.
Example 10-6 shows a comparison between SQL Server and DB2 UDB CONNECT
TO syntax. Depending on the authentication type set at the DB2 UDB instance,
authentication location may vary when the user name is not provided. More
information about this topic can be found in DB2 UDB documentation
Administration Guide: Implementation, SC09-4820.
Example 10-6 CONNECT TO syntax using ESQL/C in SQL Server and DB2 UDB
// SQL Server CONNECT TO Syntax Examples:
EXEC SQL CONNECT TO servername.dbname USER "userid";
EXEC SQL CONNECT TO servername.dbname USER "userid.password";
EXEC SQL CONNECT TO servername.dbname USER $integrated;
More information about the CONNECT TO command can be found in the DB2 UDB
documentation SQL Reference - Volume 2, SC09-4845.
[..]
EXEC SQL INCLUDE SQLCA ;
EXEC SQL BEGIN DECLARE SECTION;
CHAR firstname[20] = {‘\0’);
sqlint32 ret_code = 0;
EXEC SQL END DECLARE SECTION;
[..]
When you pre-compile an E/SQL program, the compiler inserts host language
variable declarations in place of the INCLUDE SQLCA statement. After executing a
SQL statement, the database system fills in both SQLCODE and SQLSTATE with
values.
Note that if SQLCODE has a value that is less than 0, it means that an error has
occurred and the statement has not been processed. If SQLCODE has a value
greater than 0, it means a warning has been issued, but the statement was still
processed.
Example 10-8 shows how to declare SQLCODE and SQLSTATE in a DB2 UDB
ESQL/C application. The following PRECOMPILE command sets the LANGLEVEL
precompile option to SQL92E:
PRECOMPILE applicationCode.sqc BINDFILE LANGLEVEL SQL92E
Example 10-8 Declare SQLCODE and SQLSTATE in ESQL/C code for DB2 UDB
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6]
sqlint32 SQLCODE;
More information about this topic can be found in the DB2 UDB documentation
Application Development Guide: Programming Client Applications, SC09-4826.
source.bnd
BINDV810üa
EXEC SQL CONNECT $8L\l|!$$BDB2INST2CONNE bind
EXEC SQL SELECT CT gAsHRJIUÈ!$"EGIN
DECLARE SECTIONEND
if (oltp && ids) { DECLARE SECTION-
CONNECT TO :H00002
/* don’t trans… */ USER :H00003 USING Package
}
EXEC SQL PREARE
:H000042SELECT COUNT
(*) INTO :H00001 FROM
in database
pre-compile syscat.tables!
while (true) {
EXEC SQL FETCH
static char sqla_program_id[162] 0101010100001010010
1010010101010101001
=
if (sqlca.sqlcode … 0010101010101101010
{ 1010100101010100101
} 0010101101010010101
0,42,68,65,75,65,73,65,67,79,78, 0101010010101010101
source.c a.out
DB2 UDB provides sample build scripts for precompiling, compiling, and linking
programs with ESQL/C. These are located in the C:\DB2_HOME\SQLLIB\SAMPLES\C
directory, along with sample programs.
The DB2 .NET Add-In is part of the DB2 Application Development Client for
Windows, Version 8.1.2 or later. If you installed Visual Studio .NET before you
installed DB2 UDB, the DB2 Development Add-In is registered automatically for
you. If you installed Visual Studio .NET after you installed DB2, or if you modified
your installation, you must manually register the Add-Ins. To register the Add-In
for Visual Studio .NET:
1. Exit Visual Studio .NET.
2. From any command window, run db2vsrgx.bat.
3. Start Visual Studio .NET.
Some of the main integration items in the .NET platform for the Visual Studio
.NET IDE include:
DB2 .NET Managed Provider
The native DB2 .NET managed provider enables application developers to
build and run applications using the standard ADO.NET programming model.
This native provider provides an improved level of support over the ODBC and
OLE DB generic managed providers for DB2 UDB.
Solution Explorer
The DB2 UDB database project templates allows for script-based
Tutorials
There are several tutorial available about DB2 UDB application development with
Visual Studio .NET:
The tutorial ADO.NET data adapters using DB2 UDB V8.2 Procedures walks
you through the steps required to build an ADO.NET application that uses
SQL stored procedures to SELECT, INSERT, UPDATE, and DELETE rows from
database tables and views.
http://www.ibm.com/developerworks/edu/dm-dw-dm-0408alazzawe-i.html
The tutorial DB2 Data Bound ASP.NET Form using VS.NET demonstrates
how to use the DB2 Development Add-In for Visual Studio .NET to build a rich
ASP.NET WebForm in C# using design-time and run-time data binding.
http://www.ibm.com/developerworks/edu/dm-dw-dm-0309alazzawe-i.html
Additional information
For more information and the latest technical articles about DB2 UDB application
development in the Visual Studio .NET environment, refer to the Visual Studio
.NET zone on the DB2 developerWorks Web site at:
http://www.ibm.com/developerWorks/db2/zones/vstudio/
To maintain the support from the vendor, you must meet their prescribed
migration plan and process. In this section, we highlight the conversion approach
for SAP and Siebel environments.
When migrating SAP databases, you must consider SAP's specific tables and
data types, as well as SAP proprietary data compression methods. Therefore,
databases can only be migrated using tools provided by SAP. In SAP
terminology, a migration from Oracle, Informix®, or SQL Server to DB2 UDB is
called a “heterogeneous system copy” or “migration”.
Solutions from SAP are delivered with a broad range of service offerings,
produced by the SAP Active Global Support Organization. One offering in the
context of heterogeneous system copy to DB2 UDB is SAP's OSDB Migration
Check.
Because of the complexity of the SAP ecosystem, we can only provide a brief
overview of the required migration steps.
Migration requirements
For production database migration, SAP requires customers to order the SAP
OSDB Migration Check for the system to be supported. To receive support during
production system migration, this service is mandatory. For Test and
Development Systems, as well as evaluation of Production System Migration,
this service is not needed. The SAP OSDB Migration Check is fee-based and
provides three specific service sessions which are designed to complement the
migration project:
Remote Project Audit session
Analysis session
Verification session
To order the new CD media, customers must change their SAP software license
to DB2 UDB to reflect the new target database platform. With this change,
customers are able to order the new installation kit for their SAP software on DB2
UDB, and a special OS/DB Migration Kit.
To perform a migration, a migration key is needed. This key is provided online via
SAP's support Web site. The key is mandatory for the data export step and data
import step using SAP's tools.
Database independent
format
File
transfer
Disk Disk
Figure 10-9 SAP migration project plan
Since the final result of the migration is used by SAP's end-users, acceptance
from end-users should be ensured by using migration sign-off criteria defined by
them.
10.8.2 Siebel
Siebel solutions are implemented in a layered architecture with database
independence. Thus, the Siebel application layer and business logic layer are not
affected by a database migration. Figure 10-10 shows the typical migration
process of a Siebel System.
For additional information about Siebel migrations, refer to the IBM Redbook
Migrating Siebel Databases from DB2/Oracle for NT to DB2 for OS/390,
SG24-6236.
If you need to migrate to a later version of Siebel, you must upgrade the existing
implementation to that level first.
Ensure that the existing installation has all the required FixPacks applied for the
version of Siebel you want to install. These can be found in Siebel’s Product
Availability Matrix.
Estimate the size of the target database server and the size needed for a data
migration.
Identify personnel with database and Siebel customization skills. Analyze the
amount of customization and plan work-arounds, especially where SQL Server
proprietary features are used.
Once these tasks have been carried out, proceed to plan the data migration in
more detail.
Data migration
Siebel delivers two utilities, Dataexp and Dataimp, which are often used by Siebel
services personnel to move data. The Dataexp utility exports data from tables into
a data file, which is independent of the database implementation. Dataimp takes
this file and loads the contents into a target database.
Having a database independent data file means that Dataimp can be used to
insert the data into a different target. This is exactly how Siebel handles
distributing the seed and base repository data for different operating systems.
The Dataexp and Dataimp utilities are the ideal way of migrating Siebel data with
minimal coding and administration. Running Dataimp for very large volumes is
not advisable due to the slower INSERT process. Alternatively, you can use the
DB2 UDB LOAD utility, which may improve data loading times.
Testing
Once the data migration process is completed, run various tests against the old
system and the new system ensure that they operate the same way and that the
same data is returned for test queries.
Additional information
Additional information about Siebel application and database migration is
available in:
Redbooks:
– Siebel 2000 Database Implementation on OS/390 Using NT Siebel
Servers, SG24-5953
– Migrating Siebel Database from DB2 NT to DB2 S/390®, SG24-6236
Web site:
http://www.ibm.com/software/data/partners/ae1partners/siebel/
DB2 UDB can have multiple instances defined on a machine with just one DB2
UDB installation. Each instance of DB2 UDB maintains its own databases. You
can create or drop instances using only the command line. If you run db2setup to
install DB2 UDB, an instance is created by default. The default instance names
are:
db2inst1 on Linux and UNIX
DB2 on Windows
If you want to add an instance that resides on another DB2 UDB server, then you
need register that instance in similar fashion as a linked server definition in SQL
Server.
The following commands can be used for creating, dropping, and listing
instances:
Windows commands:
x:\DB2ICRT <instance name>
x:\DB2IDROP <instance name>
x:\DB2ILIST
Alternatively, on the Linux and UNIX platforms, simply log into the system using
the instance owner’s user ID, or source the instance's db2profile file, to switch
instances. Logging into the instance owner or sourcing the db2profile file, sets up
all the paths required to access the instance. On Windows, simply set the
environment variable DB2INSTANCE to the desired instance name, or use the
SET DB2INSTANCE command.
In DB2 UDB, the DB2START command starts the current database manager
instance background processes on a single database partition or on all the
database partitions defined in a partitioned database environment. It needs to be
started at the server before you can connect to a database, precompile an
application, or bind a package to a database.
The DB2STOP command terminates the DB2 instance. However, the instance will
not stop unless all active processes are terminated. If you try to issue a DB2STOP
command under this condition, you receive the following error:
02/02/2004 18:02:28 0 0 SQL1025N The database manager was not stopped
because databases are still active.
You can force off or kill all active processes on the instance by using the DB2STOP
FORCE command at the command line.
The QUIESCE command forces all users off the instance, and restricts new users
from connecting to it, to enable system users to perform administrative and
maintenance activities. This is similar to pausing a database in SQL Server, but
in that it does force all users off.
To start, stop, and quiesce an instance using Control Center, refer to Figure 11-1
on page 265, where you can see the corresponding actions after right-clicking an
instance in the object view.
DB2 UDB provides you with three wizards to help you create a database that
includes specific container and table space specifications.
The following list summaries the functionality of the three create database
wizards and their descriptions:
Create Database Wizard (Standard):
This selection helps you create and customize a new database.
Create Database With Automatic Maintenance:
This selection helps you create a new database, configure it for optimal
performance, turn on automatic maintenance, and configure notification by
e-mail or pager if the database needs attention.
Create Database From Backup:
This selection helps you create a new database from a backup image.
In the last step, the wizard provides a Show Command button (see Figure 11-3)
UDB which can be used to display the DB2 command.
Refer to 11.3.3, “Transaction logging” on page 289 for more information about
administering the transaction logs.
If you explicitly activate a database, you also have to explicitly DEACTIVATE the
database to release the memory. Issuing a DB2STOP command, which stops the
instance, will deactivate all databases under that instance, as well as free up
memory held by those databases.
Use the QUIESCE command to immediately force all users off a database and only
allow administrative users to connect.
To configure a database in DB2 UDB using Control Center, expand the object
tree until you see the desired database. Right-click that database, and select
Configure Parameters (Figure 11-6) from the menu.
If you need to configure database parameters from the CLP, or Command Editor,
use the following commands:
As you can see from Figure 11-6, there are a few other options available to
configure database parameters:
Configuration Advisor:
Use this advisor to produce recommended configuration parameter values.
Configure Automatic Maintenance:
Use this wizard to create a strategy for automating database maintenance
activities.
Configure Database Logging:
Refer to 11.3.3, “Transaction logging” on page 289.
To force an application off a database from the Control Center, do the following:
Expand the object tree until you see the Databases folder
Right-click the database you want to drop, and select Application from the
pop-up menu.
Select the application that you want to force off the database.
Click Force.
A table space is a storage structure containing tables, indexes, large objects, and
long data. They enable you to assign the location of database and table data
directly into containers. This enables flexible configuration, which can also result
in improved performance.
When a database is created, three default table spaces are created by default,
as depicted in Figure 11-7. The DBA can customized how these table spaces are
created. The following types of table spaces can be created:
Catalog table space: One catalog table space that contains all of the system
catalog tables for the database. This table space is called SYSCATSPACE, and it
cannot be dropped.
User table space: One or more user table spaces that contain all user-defined
tables. By default, one table space, USERSPACE1, is created.
User temporary table space: One or more temporary table spaces that store
globally declared temporary tables. This type of table space must be
manually created before any global declared temporary tables can be created
System temporary table space: Used by the database manager to perform
operations such as sorts or joins. A database must have at least one system
temporary table space; by default, one system temporary table space called
TEMPSPACE1 is created at database creation time.
DB2 instance
database1
Note: SMS table spaces can take advantage of operating system file system
prefetching and caching.
In an SMS table space, space for tables is allocated dynamically. This expansion
proceeds a single page at a time by default. However, in certain work loads such
as ones with bulk inserts, you can improve performance with the db2empfa tool
which instructs DB2 UDB to expand the table space in groups of pages or
extents. The db2empfa tool is located in the \\SQLLIB\bin directory. When you run
the db2empfa tool, the MULTIPAGE_ALLOC database configuration parameter must
be set to YES.
Important: When you create an SMS table space, you must specify the
number of containers that you want because you cannot add or delete
containers after an SMS table space is created. However, you can always
increase the size of the underlying file system using operating system utilities.
You can only add containers to an SMS table space when you add a new
partition in a partitioned database environment, or perform a redirected
restore from a database backup.
DMS table spaces differ from SMS table spaces in that the entire space is
allocated when the table space is created versus not allocated when needed.
Also, the placement of data can differ between the two types of table spaces. For
example, consider the need for efficient table scans: It is important that the pages
DMS file containers do not take advantage of operating system file system
prefetching and caching.
Recommendations
Unlike SMS table spaces, the containers that make up a DMS table space do
not need to be of equal size. However, we recommend using equal container
sizes.
When working with DMS table spaces, you should consider associating each
container to a different physical disk. This allows for larger table space
capacity and the ability to take advantage of parallel I/O operations.
Although the default table spaces are created upon database creation, you can
specify the types of containers to be used for these table spaces. Table 11-1
shows the configuration recommendations each default table space.
USERSPACE1 Used to store user data tables and Must be DMS if you
indexes. are partitioning data
separate from the
indexes.
Extent size
The extent size of table space determines the number of pages of table data that
will be written to a container before data will be written to the next container.
When selecting an extent size, you should consider:
The size and type of tables in the table space:
Space in DMS table spaces is allocated to a table one extent at a time. As the
table is populated and an extent becomes full, a new extent is allocated.
Space in SMS table spaces is allocated also one extent at a time, but only if
multi page file allocation is enabled.
The type of access to the tables:
If access to the tables includes many queries or transactions that process
large quantities of data, prefetching data from the tables might provide
significant performance benefits.
The minimum number of extents required:
If there is not enough space in the containers for five extents, the table space
is not created.
The basic parameters needed to explicitly create a table space are the table
space name, how it is managed, and the container or containers. Optionally, you
can specify the use of the table space, such as regular or user-temporary. You
Important: An SMS table space is full as soon as any one of its containers is
full. Therefore, it is important to have the same amount of space available to
each container.
In the following example we show how to create a DMS table space using a file
container.
3. Specify the type of table space to create. As shown in Figure 11-9, we have
chosen Regular, because we will be storing table data in this table space.
4. Select the buffer pool the table space will use. In our example we have used a
buffer pool called buffer_pool (Figure 11-10). Buffer pools are covered in
11.4, “Working with buffer pools” on page 294
5. Select the type of table space to create. We create a DMS table space. Click
Next.
6. Specify the container(s) to be used. In Figure 11-11, we use a file as the
container. Click the Add button, browse to the location where the container
will reside, give the container a name and specify the size of the container.
7. Next three screens in the wizard allow you to set setting of the extent size,
prefetch size, disk speed parameters and dropped table recovery. Dropped
table recovery, as the name suggests, allows you to recover a table that has
been dropped. This option can only be enabled on Regular table spaces.
Figure 11-12 shows the wizard summary screen with the options used for
creating the table space. Selecting the Show SQL button shows the syntax of
the command for creating the table space (shown in Example 11-1).
Use the ALTER TABLESPACE statement to modify an existing table space. You can
make the following changes:
Add a container to, or drop a container from, a DMS table space.
Modify the size of a container in a DMS table space.
Modify the following parameters: PREFETCHSIZE, BUFFERPOOL, OVERHEAD, or
TRANSFERRATE.
Modify the file system caching policy for a table space.
When new containers are added to a table space or existing containers are
extended, a rebalance of the table space data might occur.
The high-water mark is the page number of the highest allocated page in the
table space. For example, a table space has 1000 pages and an extent size of
10, resulting in 100 extents. If the forty-second extent is the highest allocated
extent in the table space, that means that the high-water mark is 42 * 10 = 420
pages. This is not the same as used pages, because some of the extents below
the high-water mark might have been freed up such that they are available for
reuse.
Example 11-2 shows the command used to alter the table space when you select
the Show SQL button in the wizard.
Note: Typically, you will not want to set up thresholds for SMS table spaces,
because the space will always be viewed as full, and the Percent Used
indicator will show 100%. This occurs because the space grows with usage. It
is still helpful to monitor SMS spaces because charts will show growth in the
actual size over time and provide you with a helpful view of overall data usage.
Example 11-3 shows detailed output from the list tablespaces show detail
command.
Tablespace ID = 0
Name = SYSCATSPACE
Type = System managed space
Contents = Any data
State = 0x0000
Detailed explanation:
Normal
Total pages = 4338
Useable pages = 4338
Used pages = 4338
Free pages = Not applicable
High water mark (pages) = Not applicable
Page size (bytes) = 4096
Extent size (pages) = 32
Prefetch size (pages) = 16
Number of containers = 1
Tablespace ID = 1
Name = TEMPSPACE1
Type = System managed space
Contents = System Temporary data
State = 0x0000
Detailed explanation:
Normal
Total pages = 1
Useable pages = 1
Used pages = 1
Free pages = Not applicable
High water mark (pages) = Not applicable
Page size (bytes) = 4096
Extent size (pages) = 32
Prefetch size (pages) = 16
Number of containers = 1
Tablespace ID = 2
Name = USERSPACE1
Type = System managed space
Contents = Any data
State = 0x0000
Detailed explanation:
Normal
Total pages = 407
Useable pages = 407
Used pages = 407
Free pages = Not applicable
High water mark (pages) = Not applicable
Tablespace ID = 3
Name = TABLESPACE1
Type = Database managed space
Contents = Any data
State = 0x0000
Detailed explanation:
Normal
Total pages = 6400
Useable pages = 6368
Used pages = 96
Free pages = 6272
High water mark (pages) = 96
Page size (bytes) = 8192
Extent size (pages) = 32
Prefetch size (pages) = 32
Number of containers = 1
Minimum recovery time = 2005-02-07-23.37.05.000000
In Example 11-3, the details for four table spaces are drown starting with the
table space with ID of 0. Three default table spaces are shown. They are
SYSCATSPACE, for system objects, TEMPSPACE1, for temporary system use, and
USERSPACE1, for user data. All three table spaces are defined as SMS.
SYSTOOLSPACE is a table space used for automatic statistics collection.
TABLESPACE1 is DMS and holds table data.
To obtain more information about a table space, you can use the DB2 UDB
command list tablespace containers for tablespace_number show detail.
Example 11-4 shows the command executed against table space 3.
Example 11-4 List table space show detail for a specific table space
C:\>db2 list tablespace containers for 3 show detail
Container ID = 0
Name = C:\DMS_TABLESPACE\tablespace1_cont1
Type = File
Total pages = 6400
Useable pages = 6368
Accessible = Yes
Container ID = 1
Name = C:\DMS_TABLESPACE\tablespace1_cont2
DB2 UDB implements write-ahead logging in which the changed data is always
written to the log files before the change is committed. DB2 UDB logs changes in
its log files, including the old version of the data.
Managing logs
The number of logs to configure, their sizes, and other parameters affecting
logging are set using database configuration parameters. DB2 UDB supports
configurations that can dynamically extend the logs (using secondary logs) when
primary log space is exhausted. DB2 UDB, by default, stores log files in the
directory specified by the database configuration parameter LOGPATH. To change
the log file path, the database configuration parameter NEWLOGPATH parameter
should be updated. Log files can be stored as system files or a raw devices. You
can control the number and size of the DB2 log files with the LOGPRIMARY,
LOGSECOND, and LOGFILSIZ database parameters. Consult the DB2 UDB
documentation Administration Guide: Performance, SC09-4821, for more
information.
The DB2 UDB transaction log is an important part of the backup and recovery
processes. The transaction log records all committed changes to the database
and is used to bring the database to a consistent point after a database crash. It
can also be used to restore a database to a recent state after media failure or
application failure.
A total size of 256 GB is supported for logging. In addition to the primary logs,
DB2 UDB also uses a number (between 0 and 126) of secondary logs as
specified in the LOGSECOND parameter. Secondary logs do not occupy
permanent file space, but are allocated one at a time as needed and deallocated
as not needed. Secondary logs are used when the primary logs fill up and more
log space is required. Infinite active logging is also new in DB2 UDB Version 8.
Its allows an active unit of work to span the primary logs and archive logs
effectively, allowing a transaction to use an infinite number of log files. Without
infinite active log enabled, the log records for a unit of work must fit in the primary
log space. Infinite active logging is enabled by setting the LOGSECOND parameter to
-1. Infinite active logging can be used to support environments with large jobs
that require more log space than you would normally allocate to the primary logs.
The amount of space (in bytes) required for primary and secondary log files is:
(LOGPRIMARY + LOGSECOND) * (LOGFILSIZ + 2) * 4096) + 8192
You should monitor the log storage directory to ensure that you have enough file
system space to hold the logs.
Logging modes
DB2 UDB has two main logging modes: non-recoverable, and recoverable.
The implications of this are that, with the exception of a system crash (where
DB2 UDB will perform crash recovery), you must recover your database from a
full offline backup. You cannot roll forward completed transactions that are
contained in archive logs, but are not part of the offline backup. Circular logging
is used to protect against power failure and by applications to roll back
uncommitted changes if a processing error occurs. When this type of logging is
enabled, both the LOGARCHMETH1 and LOGARCHMETH2 parameters are set to OFF.
Only offline, full database backups can be performed with this type of logging.
The recoverable logging mode is used for databases that require recovery at a
transaction level. With this mode, logs are not reused, but are maintained until
they are archived. This type of logging provides all the protection of circular
logging plus additional recovery benefits. The online and archived logs can be
used, along with the last full backup, to recover a database to the state before a
media or application failure. A database backup can be performed in online or
offline mode and can be a full database backup or a selected table space
backup. Recovery of a database can be performed to the last current committed
transaction before the failure or to some specific point in time using the
ROLLFORWARD utility. Also, with this type of logging, recovery can be performed on
a table space basis. This method of logging is similar to the ‘Full’ logging model
used in SQL Server. There are several options that affect how logs are archived
with this mode. You can archive logs to disk, tape, use tape management
software such as IBM Tivoli® Storage Manager, use third-party APIs, or write
your own user exit routine.
DB2 UDB supports log mirroring which is enabled by setting the MIRRORLOGPATH
database configuration parameter.
3. Choose the location and size or your logs and mirrors (if using mirrors), the
location of your backup, and any performance options. In this example, we are
archiving our logs to the file system (DISK), with a primary archive path of
C:\archive_log, a mirror log path of C:\mirror_logs, a failure archive path of
Example 11-6 shows the commands generated by the wizard. First, we connect
to the database, issue a quiesce to stop activity, and then update the database
configuration to reflect the new archiving method and path. When changing the
type of logging, you must first backup the database. The wizard also
automatically issues the backup command. Note that the database will be taken
offline for the backup, because circular logging does not allow online backups.
Important: In DB2 UDB Version 8.2, DB2 UD no longer requires the use of a
user exit to archive logs. Also, the LOGARCHMETH1 and LOGARCHMETH2
parameters replace the LOGRETAIN and USEREXIT parameters that were used
prior to V8.2. LOGRETAIN and USEREXIT are still available to maintain backward
compatibility.
After converting all database objects, applications and data, you are probably
expected to tune your database to meet your business requirement. Working with
buffer pools is one of the necessary tasks in performance tuning, which is
covered in Chapter 12, “Post-conversion tuning considerations” on page 339. In
this section, we cover are just how to create, drop, list, and alter buffer pools.
Important: The page sizes specified for your table spaces should determine
the page sizes that you choose for your buffer pools. The choice of page size
used for a buffer pool is important, because you cannot alter the page size
after you create a buffer pool. A table space of a chosen page cannot be cre-
ated until at least one buffer pool, using that same page size, exists.
To create a buffer pool, you can use either the CREATE BUFFERPOOL command or
Control Center.
After a buffer pool has been created, it can be associated with one or more table
spaces during creation of table spaces.
If you want to drop a buffer pool, the steps are similar to altering a buffer pool, but
instead select Drop at the pop-up menu.
To create the Tools Catalog using CLP, use the CREATE TOOLS CATALOG command.
Typical usage of the command would be:
You can specify conditional coding by creating task actions. Each task action
consists of a task and the action that should be performed by the task. For
example, task one can have the following task actions:
If task one is successful, task action A enables the schedule for task two.
If task one fails, task action B runs task three.
The following steps guide you through creating a task, scheduling the task and
then viewing historical information about the task execution. In the example we
create a backup job using Task Center. We also demonstrate creating a backup
job using the “Backup Wizard” in Control Center.
1. Launch Task Center from either Control Center or from a Command Line with
the db2tc command. Select the Scheduler System. This is the system where
the task is scheduled on and can be the local system or a remote one.
Right-click in the main window and select New. You can also select Task →
New from the main menu.
2. Enter information such as the name of the task, type of script, a brief
description, and the category of task, as shown in Figure 11-19. The type of
scripts that can be run are DB2 UDB command scripts, operating system
scripts, MVS™ and JCL scripts for running on host database systems such as
z/OS, and Group Tasks, which allows several tasks to be grouped and run
together. Categories of task can also be created. The Run System where the
task will actually run must be specified. The Instance or partition that the
script applies to must also be specified.
3. Input the script for the task. In the example in Figure 11-20, we have specified
a database backup. By clicking the Import button you will be able to import
any scripts previously created or ported from SQL Server.
4. Create success or failure creations for the task. You can define the specific
success code for your task and choose whether DB2 UDB should stop
execution if failure occurs during task execution. In Figure 11-21, we have
chosen to stop execution when failure happens.
5. Schedule the task to run at a specific time, only once or a repeating schedule.
In Figure 11-22, we specified that the backup job should run every two days at
a specific time. If you want to save the task schedule for use with other tasks
7. In addition to the notification setting, you can also specify follow-up actions
under the Task Actions tab, as shown in Figure 11-24. For example, you can
specify a Task Failure condition so if a task fails, you can run another task to
remedy the condition, or to disable another task to avoid unnecessary
damage.
8. You can also specify access privileges to the task (Figure 11-25), such as
read, run, or execute for different users and groups by clicking the Security
tab. The final step is to submit the task to Task Center by clicking OK.
Figure 11-26 Saving and scheduling a task from the Backup Wizard
DB2 UDB, allows backup and restore commands on database and table spaces.
Archived log files can be backed up as ordinary operating system files, and/or
can be included into a backup image during online backup operations. DB2 UDB
has two types of incremental backups - incremental (same as differential in SQL
Server), and delta (changes from last backup of any type). Point in time recovery
is achieved using the rollforward command. DB2 UDB has a recover command
that combines restore and rollforward commands into a single step and uses
the information in the recovery history file to determine which backup images and
log files to use.
Both SQL Server and DB2 UDB are capable of performing backup restore
operations in parallel and API support is also available. The following is a list of
some of the functions that are new to SQL Server administrators are described in
more detail in subsequent sections to help you enhance your database
backup/recovery tasks:
Automatic backup maintenance
Backup using throttling mode
Log file inclusion in backup images
Backup compression
Automatic log file management
Undo management
Note: DB2 UDB now automatically self tunes its backup and restore
operations by choosing the number of buffers, buffer size and parallelism
settings based on the amount of memory available, number of processors
available and database configuration, if those parameters are not supplied
with the backup/restore command.
If you created the database without configuring automatic maintenance, you can
access the Configure Automatic Maintenance wizard either from the Control
Center or the Health Center.
To launch the wizard from the Control Center, expand the object tree until you
see the desired database, then right-click the database and select Configure
Automatic Maintenance.
The automatic database backup feature can be enabled or disabled by using the
AUTO_DB_BACKUP and AUTO_MAINT database configuration parameters.
You can enable throttling when going through the backup wizard (Figure 11-28):
Figure 11-28 Enable throttling and set priority for backup utility
Priority can be any number between 1 and 100, with 1 representing the lowest
priority, and 100 representing the highest priority.
For SQL Server, and DB2 UDB prior to Version 8.2, the backup and restore
utilities requires shipping the backup image and corresponding log files as
The new feature in DB2 UDB Version 8.2 enables you to include the logs as part
of the online backup. This way, you can ship the backup images with the log files
to ensure that the backup will be restorable if archived logs are misplaced. This
feature supports all types of online backups such as database, table space,
incremental, and compressed. To enable it in Control Center, select the option
available in the Options page of the Backup Wizard as shown in Figure 11-29.
Figure 11-29 Include log files in backup images during online backup operation
If this option is enabled, DB2 UDB will truncate the tail log file and close it after
the table space or database is backed up. All logs that are needed to restore the
backup and roll forward to the time corresponding to the end of the backup are
placed in the backup image. DB2 UDB backs up log files in parallel while backing
up the data. If database backup compression is specified, the log files are
compressed as well.
When restoring the database, specify the directory where you want DB2 UDB to
place the log files, as shown in Figure 11-30.
During an automatic incremental restore operation, only the logs included in the
target image of the restore operation are retrieved. Any logs that are included in
intermediate images referenced during the incremental restore process are not
extracted from those backup images. During a manual incremental restore, if you
specify a log target directory when restoring a backup image that includes log
files, the log files in that backup image are restored.
Use this feature by enabling it in the Option tab of the backup wizard, or in the
backup command with the COMPRESS parameter. The restore utility recognizes the
If you use a user-provided compression library, the library is stored in the backup
image by default and is used on restore. The command syntax is:
DB2 BACKUP DATBASE sample COMPRESS COMPRLIB libname COMPROPTS options
DB2 RESTORE DATABASE sample COMPRLIB libname COMPROPTS options
Use of the compression feature may result in additional CPU overhead due to the
compression computations. However, the media I/O time is decreased because
the image size is smaller. The overall backup/restore performance impact
depends on whether CPU or media I/O is a bottleneck of the system.
Note: The LOGRETAIN and USEREXIT are deprecated starting in Db2 UDB
V8.2.
In the PRUNE HISTORY command, the AND DELETE option is used to prune the
unnecessary logs.
>>-PRUNE-------------------------------------------------------->
>--+-HISTORY--timestamp--+-------------------+--+------------+-+-><
| '-WITH FORCE OPTION-' '-AND DELETE-' |
'-LOGFILE PRIOR TO--log-file-name---------------------------'
When the history file entry is removed, the associated log archives are physically
deleted. This option is especially useful for ensuring that archive storage space is
recovered when log archives are no longer needed.
Note: If you are archiving logs via a user exit program or in-house developed
routine, the logs cannot be deleted using this option.
DB2 UDB provides a feature to address this problem: undo management. In DB2
UDB, it is possible to re-create mistakenly dropped tables. If you foresee that
your environment is at risk of users or applications corrupting the data and you
required a solution that fixes the problem to the specific granularity of the
errorneously changed data, consider using undo management.
With the optional DB2 Recovery Expert for Multiplatforms product, you can use
sophisticated undo management features that are based on mining archived and
active logs. DB2 Recovery Expert gives you the option to reverse changes that
were made incorrectly. It also supports autonomic recovery management. Refer
to the product Web site to get the latest information about his product:
http://www.ibm.com/software/data/db2imstools/db2tools/db2re/
11.7.2 HADR
High Availability Disaster Recovery (HADR) is a feature introduced twithDB2
UDB Version 8.2. HADR is a database replication feature that provides a high
availability solution for both partial and complete site failures. HADR protects
against data loss by replicating data changes from a source database, called the
primary, to a target database, called the standby.
DB2 UDB HADR applications can only access the primary database. Updates to
the standby database occur by rolling forward the log data generated on the
The following example shows how HADR can be set up and configured using
Control Center.
Before setting up HADR, you must configure your primary and standby
environments first:
Configure your primary database to use archive logging. This is a
requirement for HADR.
Create and configure a standby instance with TCP/IP communications
enabled. This is a standard database instance created using the db2icrt
command. You do not need to create a database for this instance; one will be
created for you as part of the HADR configuration wizard. We have used the
default instance name (DB2) for our standby server.
4. Identify the backup image to be used and select further backup options. The
wizard will probe the history file for a list of valid backups. As shown in
Figure 11-33, we select the backup taken at 2:34:54 p.m. on 2/04/05. Click
Next.
5. Restore the database on the standby system. Select a database alias and
how the backup image is copied to the standby system for restoration. The
database on the standby system is also cataloged on the primary system. You
will also notice in Figure 11-34 that the HADR wizard process can ship the
primary database backup to the standby server in preparation for restoration.
You can copy it manually. Once you have made your selections, click Next.
6. Optionally specify to copy objects that are stored externally (such as stored
procedures and UDFs) and are not part of the database backup image. This
step enables you to specify the objects to be copied from the primary system
to the standby system (Figure 11-35). Click Next.
8. DB2 UDB provides automatic client re-route so in the event that the primary
database becomes unavailable, client connections are automatically
re-routed to the standby. Figure 11-37, we have accepted the defaults for this
option. Click Next.
9. Specify the synchronization mode for peer state log writing. The
synchronization mode specifies the log write behavior between primary and
standby when they are in peer state (when log pages are being shipped to the
standby). There are three synchronization modes:
– Synchronous (zero data loss): In this mode, DB2 UDB issues gives a
commit acknowledgement to an application only when the transaction is
written to disk on both primary and standby. Therefore, no transaction is
lost, even if both primary and standby failed.
– Near synchronous: In this mode, the log write at the primary and the send
to the standby are performed in parallel. A commit operation succeeds
when the log data is written to disk at the primary and it has been received
by the standby. This is a no transaction loss in single site failure scenario.
– Asynchronous: In this mode, a commit operation succeeds when the log
data is written to disk at the primary and has been sent to the standby. The
log data transmission is guaranteed by TCP/IP sockets. There is no
acknowledgement of log data transmission between primary and standby.
In Figure 11-38, we have selected Near Synchronous which is also the
default. Click Next.
Selecting the Show Command button will show the script used to setup HADR
(Example 11-7). This script can be invoked from the command line to setup
HADR without the use of the GUI.
11.Select Finish from the Summary screen of the wizard. A progress window
opens and indicates successful completion of the HADR setup steps.
After HADR is set up, you can monitor HADR by right-clicking the database from
Control Center and selecting High Availability Disaster Recovery- Manage. In
Figure 11-41, the primary and standby databases are enabled for HADR. You
can also stop HADR or issue a takeover fro this window. Issuing a takeover
means that you switch roles between primary and standby databases.
The index logging provides recoverability but can have performance trade-off. In
the HADR environment we recommend that you set the LOGINDEXBUILD database
configuration parameter to ON to ensure that complete information is logged for
index creation, re-creation, and reorganization.
More information about Index Logging can be found in the DB2 UDB
documentation Data Recovery and High Availability Guide and Reference
SC09-4831.
When MIRRORLOGPATH is first enabled, it is not actually used until the next
database startup. This is similar to the NEWLOGPATH configuration parameter. If
there is an error writing to either the active log path or the mirror log path, the
database will mark the failing path as bad, write a message to the administration
notification log, and write subsequent log records to the remaining good log path
only. DB2 UDB does not attempt to use the bad path again until the current log
file is completed. When DB2 UDB needs to open the next log file, it verifies that
this path is valid, and if so, begins to use it. If not, DB2 UDB does not attempt to
use the path again until the next log file is accessed for the first time. There is no
attempt to synchronize the log paths, but DB2 UDB retains information about
access errors so that the correct paths are used when log files are archived. If a
failure occurs while writing to the remaining good path, the database shuts down.
If you would rather not back up a large database using the DB2 UD backup utility,
you can make copies from a mirrored image by using suspended I/O and the split
mirror function. This approach also eliminates backup operation overhead from
the production machine and is a fast way to clone systems. Use the db2inidb
command in conjunction with the suspend and resume commands to do this.
The SNAPSHOT option specifies that the mirrored database will be initialized as a
clone of the primary database. You can then use this clone for testing or as a
point-in-time failover environment. When you use the STANDBY option, the
database will be placed in rollforward pending state. New logs from the primary
database can be fetched and applied to the standby database. The standby
database can then be used in place of the primary database if it goes down. The
MIRROR option specifies that the mirrored database is to be used as a backup
image which can be used to restore the primary database.
To clone a database using split mirror, perform the following steps in CLPs:
1. Suspend I/O on the primary database:
db2 set write suspend for database
2. Use appropriate operating system-level commands to split the mirror or
mirrors from the primary database.
3. Resume I/O on the primary database:
db2 set write resume for database
4. Catalog the mirrored database on the secondary system.
Consider the following factors, which might indicate that table reorganization is
needed:
A high volume of insert, update, and delete activity on tables accessed by
queries.
Significant changes in the performance of queries that use an index with a
high cluster ratio.
Executing runstats to refresh statistical information does not improve
performance.
The reorgchk command indicates a need to reorganize tables.
The trade-off between the cost of increasing degradation of query
performance and the cost of reorganizing your table, which includes the CPU
time, the elapsed time, and the reduced concurrency resulting from the reorg
utility locking the table until the reorganization is complete.
To reduce the need for reorganizing a table, perform these tasks after you create
the table:
Alter the table and specify a value for the PCTFREE parameter.
Create clustering index with PCTFREE on the index.
Sort the data.
Load the data.
If you the MINPCTUSED parameter is set during index creation, the database server
automatically merges index leaf pages if a key is deleted and the free space is
less than the specified percentage. This process is called online index
defragmentation. To restore index clustering, free space, and reduce leaf levels,
you can use one of the following methods:
Drop and recreate the index.
Use the reorg indexes command to reorganize indexes online. You might
choose this method in a production environment because it allows users to
read from and write to the table while its indexes are being rebuilt.
Use the reorg table command with options that allow you to reorganize both
the table and its indexes offline.
Tip: Creating multidimensional clustering (MDC) tables might reduce the need
to reorganize tables. For MDC tables, clustering is maintained on the columns
provided arguments to the ORGANIZE BY DIMENSIONS clause of the CREATE
TABLE statement. However, reorgchk might recommend reorganization of an
MDC table if it detects that there are too many unused blocks or that blocks
should be compacted.
Table statistics:
Tables defined using the ORGANIZE BY clause and the corresponding dimension
indexes have a '*' suffix to their names. The cardinality of a dimension index
is equal to the Active blocks statistic of the table.
Table reorganization
The reorg table command is used to reorganize a table. It can also reorganize
an index at the same time.
Figure 11-43 shows an example of reorganizing the STATE table in the sample
database. This example illustrates and outlines table reorganization, meaning
that users have access to the table while the utility is running. DB2 UDB does this
by reorganizing data in place, one block at a time. To access the reorg utility in
Control Center, right-click the desired table, and select Reorganize Table. Note
that in the following example, we reorganize the table’s indexes at the same time.
Example 11-9 shows the command used to run reorg from CLP.
Index reorganization
DB2 UDB V8.2, now has the ability to read and update a table and its existing
indexes during an index reorganization. For more information about index
reorganization, see 12.5.1, “Indexing” on page 364.
Automatic reorganization
You can also configure automatic reorganization using the Automatic
Maintenance wizard from Control Center or Health Center. We take a closer look
at Automatic Maintenance in 12.3, “Configuring automatic maintenance” on
page 346.
The DB2 UDB command for generating and updating statistics is runstats. By
contrast SQL Server has two commands, create statistics and update
statistics and a system stored procedure sp_createstats. Like the update
statistics command used in SQL Server, you should run runstats periodically.
The right moment to execute a runstats depends on the dynamic nature of the
data. If the content of a table is changing constantly, you should update the
statistics more often. If table content is static, a single runstats is usually
sufficient.
The runstats command can collect table, column, and index information. This is
reflected in the command syntax shown in Example 11-10.
statistics-options:
[table-object-options] [ALLOW {WRITE | READ} ACCESS]
[table-sampling-options] [profile-options] [UTIL_IMPACT_PRIORITY [priority]]
table-object-options:
[{FOR index-clause | [column-stats-clause] [AND index-clause]}]
table-sampling-options:
[TABLESAMPLE {BERNOULLI | SYSTEM} (numeric-literal)
[REPEATABLE (integer-literal)]]
profile-options:
[{SET PROFILE [NONE | ONLY] | UPDATE PROFILE [ONLY]}]
index-clause:
[[SAMPLED] DETAILED] {INDEXES | INDEX} {ALL | index-name [{,index-name}...]}
columns-stats-clause:
[ON {{ALL | KEY} COLUMNS [AND COLUMNS (column-option [{,column-option}...])] |
COLUMNS (column-option [{,column-option}...])}] [distribution-clause]
column-option:
distribution-clause:
WITH DISTRIBUTION [ON {{ALL | KEY} COLUMNS [AND COLUMNS (dist-column
[{,dist-column}...])] | COLUMNS (dist-column [{,dist-column}...]) }]
[DEFAULT [NUM_FREQVALUES number] [NUM_QUANTILES number]]
dist-column:
{column-name | (column-name [{,column-name}...])} [NUM_FREQVALUES number]
[NUM_QUANTILES number]
The online help of the runstats command demonstrates that there are a lot more
options you can use compared to SQL Server’s update statistics command.
We do not cover all of these in this book. Instead, we compare a few of the more
frequently used options. Table 11-2 provides a basic comparison of update
statistics and runstats commands.
Table 11-2 Basic statistics command - compare between SQL Server, DB2 UDB
SQL Server DB2 UDB
You can also use Control Center to update statistics. Right-click the desired table
and select Run Statistics. As shown in Figure 11-44, you can adjust all the
statistics settings.
The query plans (shown in Figure 11-45) are taken before and after a runstats
run. The query plans are generated with the Visual Explain tool (see 3.1.4,
In this chapter, we highlight the important areas where initial performance tuning
efforts should be directed. Some of the GUI tools that eliminate the guesswork
involved in performance tuning are presented. We also describe some of the
advanced performance-oriented access methods available in DB2 UDB that your
environment may benefit from.
There are three basic areas in DB2 UDB that can be tuned: the instance, the
database, and the SQL queries being submitted. Performance tuning for the
instance is based on optimizing operating system resource usage. Tuning the
database is somewhat based on data layout, table schema, and indexes. Each
database can be tuned to use a specific part of the resources allocated to the
instance. Tuning SQL queries involve writing SQL queries with performance in
mind and taking advantage of advanced database objects that may be used to
improve performance.
Using either the Design Advisor GUI or the command line tool, the Design
Advisor can help simplify database design and workload performance tuning.
While designing your database, use the Design Advisor to generate design
alternatives in a test environment. You can also use it to evaluate indexes, MQTs,
MDC tables, or partitioning strategies that have been generated manually.
After your database is set up, use the Design Advisor to improve the
performance of a particular statement or workload. You can also improve general
database performance, using the performance of a sample workload as a gauge.
When creating a sample workload, we suggest that you create a workload based
on the most frequently executed queries, which you can identify by using the
Activity Monitor.
The detailed information about the Design Advisor can be found in the following
IBM Redbooks:
DB2 UDB Evaluation Guide for Linux and Windows, SG24-6934
DB2 UDB Exploitation of the Windows Environment, SG24-6893
Up and Running with DB2 for Linux, SG24-6899
To invoke this wizard from Control Center, expand the object tree, right-click the
target database and select Configuration Advisor. The wizard collects
information including the percentage of memory dedicated to DB2 UDB, the type
of workload, number of statements per transaction, transaction throughput,
trade-off between recovery and database performance, number of applications,
and isolation level of applications connected to the database. Based on the
supplied answers, the wizard proposes configuration changes and gives the
option to apply the recommendations immediately or to save them as a task in
[...]
Current and Recommended Values for Database Configuration
Note: If you use the ACTIVATE DATABASE command, you must shut down the
database with the DEACTIVATE DATABASE command. The last application that
disconnects from the database does not shut it down.
Proper data organization is also critical for good performance. Data rows and/or
indexes that are physically out of order usually results in additional I/O and thus
additional response time. The reorg utility is used to reorganize table and index
data and should be run whenever a large amount of table data has been
modified. It is also possible to enable automatic maintenance and have the
system run reorg when necessary (see the following section for configuring
automatic maintenance).
For more information about the runstats and reorg utilities, see 11.8, “REORG
and RUNSTATS” on page 330.
Note: The first time the Configure Automatic Maintenance wizard is run, the
Retrieving Policies phase might take some time as new tables are being
created to store control information. If you do not want to wait, you can click
Close because the tables are created in the background. You can then
continue with the wizard when the table creation process has completed.
The automatic database backup feature can be enabled or disabled using the
AUTO_DB_BACKUP and AUTO_MAINT database configuration parameters.
Tables considered for automatic statistics collection are configurable using the
Automatic Maintenance wizard from Control Center or Health Center.
Automation example
The following example uses a wizard available from Control Center to configure
automatic maintenance. You can use this wizard to enable and disable
maintenance.
Finally, the summary window (see Figure 12-4) shows you all the options you
have selected.
Offline database backups and table and index reorganizations are run in the
offline maintenance time period. These features run to completion, even if they
In SMS table spaces, reading and writing data from/to tables is buffered by the
operating system, and space is allocated according to operating system
conventions. When a table is initially created, only one page is allocated to it on
disk. When records are inserted into a table, DB2 UDB extends the table file by
one page at a time by default.
When the workload involves performing many insert operations, extending files
by only one page at a time can be a very expensive operation. To minimize the
overhead of table space extension, multi-page file allocation can be enabled.
With multi-page file allocation enabled for SMS table spaces, disk space is
allocated one extent at a time (contiguous groups of pages defined for the table
space) versus one page at a time.
In Example 12-2 Multi-page is not enabled. This can be changed by running the
db2empfa program on the target database. Since db2empfa connects to the
database in exclusive mode, all other users must be disconnected. After
db2empfa is run against the target database, check the multi-page file allocation
parameter for the status again (see Example 12-3).
For optimal performance, large data volumes and indexes should be placed on
DMS table spaces, which, if possible, reside on separate devices. System
catalogs and system temporary table spaces should generally use SMS table
spaces. The system catalogs contain large objects, which are not cached in DMS
table spaces, but are cached by the operating system when using SMS table
spaces. In an OLTP environment, there is typically no need for creating large
temporary objects or staging areas for data, so an SMS table space is a good
starting point for system temporary table spaces.
Buffer pool
Log buffer
As shown in Figure 12-5, all data modifications are not only written to table space
containers, but are also logged to ensure recoverability. Because every INSERT,
UPDATE, or DELETE operation is recorded in the transaction log, the flushing speed
of the logical log buffer can be crucial for database performance. To understand
the importance of logical log placement, you should keep in mind that the time to
write data to disk depends on the physical data distribution on disk. Many
random read and write operations mean more disk head movements are
required. Flushing (writing) the logical log buffer to disk is a sequential operation
by nature, and should not be interfered with by other read or write requests.
Locating the logical log files on separate physical devices ensures uninterrupted
sequential writes by other processes or queries.
To change the physical location of logical log files, the NEWLOGPATH database
parameter can be modified, as shown in Example 12-4. The log files are
relocated to the new path after the next database activation. It may take some
time to create the log files if they do not already exist.
Example 12-5 shows how to set the initial values of the parameters, assuming a
two processor machine with six disks are available to DB2 UDB.
The following example show how parallel I/O can be enabled for only for two
table spaces: DATASP1 and INDEXSP1:
db2set DB2_PARALLEL_IO="DATASP1,INDEXSP1"
To check the current value for the parameter, the following command can be
issued:
db2set DB2_PARALLEL_IO
The default size of a buffer pool is very small: only 250 pages (~ 1 MB) for
Windows and 1000 pages (~ 4 MB) for Linux and UNIX platforms. The overall
buffer pool size has a great effect on DB2 UDB performance since it can
significantly reduce I/O, which is typically the most expensive operation. We
highly recommend increasing the default buffer pool sizes. However, the total
buffer pool size should not be set too high, because over-allocating memory to
them can also cause problems. To calculate the maximum buffer pool size, all
other DB2 UDB memory related parameters such as the database heap, agent
memory, lock storage, as well as operating system and other application
requirements should be taken into account.
Initially, the total size of buffer pools should be set to approximately 10% to 20%
of available memory. You can always monitor the system later and correct it if
necessary. DB2 UDB allows dynamic modification buffer pool sizes, meaning the
changes take effect immediately, without shutting down the database. The ALTER
BUFFERPOOL statement with the IMMEDIATE option causes the changes to take
effect right away, except when there is not enough reserved space in the
database-shared memory to allocate new space. This feature can be used to
tune database performance according to periodical changes in use, for example,
switching from daytime interactive use to nighttime batch work.
Once the total available size is determined for buffer pools, this area can be
divided into multiple, independent buffer pools to improve utilization. For
example, suppose that a database has many frequently used small tables, which
would normally reside in the buffer pool in their entirety, and thus would be
accessible very fast. Now suppose a query runs against a very large table, using
the same buffer pool as the smaller tables. When this query runs, the pages from
the small, frequently used tables are replaced, making it necessary to re-read
them when they are needed again. If the smaller tables had used their own buffer
pool, the situation described above where a large query would cause their pages
to be replaced could be avoided.
You can create additional buffer pools for caching user data and leave the
IBMDEFAULTBP buffer pool for exclusive used by system catalogs. Creating an
extra buffer pool for system temporary data also can be valuable for system
performance, especially in an OLTP environment where temporary objects are
Example 12-6 shows how to create buffer pools. It assumes that an additional
table space called DATASPACE is already created for storing data and indexes and
that there is enough memory in the system. This example can be used as a
starting point for buffer pool configuration for a system with a minimum of 2 GB
RAM.
The results:
BPNAME NPAGES PAGESIZE TBSPACE
-------------------- ----------- ----------- --------------------
IBMDEFAULTBP 16384 4096 SYSCATSPACE
IBMDEFAULTBP 16384 4096 USERSPACE1
DATA_BP 65536 4096 DATASPACE
TEMP_BP 16384 4096 TEMPSPACE1
More work
for page cleaner
With 32-bit versions of DB2 UDB, there is a limit on the total size of the buffer
pools that can be defined regardless of the available real memory. No such
restrictions apply to 64-bit versions of DB2 UDB, where real memory limits that
cause operating system paging are most likely a problem with large buffer pools.
An advantage of a single buffer pool is that it exploits the efficient page
management algorithm of DB2 UDB, which maintains a high buffer hit ratio by
keeping the most active and important pages, such as index pages, in memory
while migrating less frequently used pages to disk. Having one buffer pool also
requires no tuning after its size is chosen. The main disadvantage of a single
buffer pool is that it will not be able to distinguish between high priority access
data with potentially low access rates when there are lower priority table spaces
with higher activity rates using the same buffer pool.
The advantages and disadvantages of multiple buffer pools are the opposite of
those for a single buffer pool. Multiple buffer pools offer greater flexibility for
prioritizing the I/O performance of different table spaces, but require constant
monitoring and tuning to keep them performing optimally.
In summary:
Larger buffer pools are better.
Set CHNGPGS_THRESH lower if victim/dirty page steals occur.
Set NUM_IOCLEANERS to one or two more than the number of physical drives.
A single transaction must be able fit into the available log space. If it does not fit,
the transaction is rolled back by the system (SQL0964C The transaction log for
the database is full). To process transactions that modify a large number of
rows, adequate log space is needed.
The total log space available for transactions can be calculated by multiplying the
size of one log file (database parameter LOGFILSIZ) by the number of log files
(database parameter LOGPRIMARY).
From a performance perspective, it is better to have a larger log file size because
of the high cost of switching from one log to another when a log file fills up. When
log archiving is enabled, the log size also indicates the amount of data for
archiving. In this case, a larger log file size is not necessarily better, since a
larger log file size may increase the chance of failure, or cause a delay in
archiving or log shipping scenarios. The log size and the number of logs should
be balanced.
Locking is the mechanism that the database manager uses to control concurrent
access to data in the database by multiple applications.
Each database manages its own list of locks (a structure stored in memory,
which contains the locks held by all applications concurrently connected to the
database).
The size of the lock list is controlled by the LOCKLIST database parameter. The
default amount of memory for LOCKLIST is 50 pages (200 KB) for Windows and
100 pages (400 KB) for Linux and UNIX. On 32-bit platforms, each lock requires
36 or 72 bytes of the lock list, depending on whether other locks are held on the
object or not. Using the default installation parameter values, a maximum of 5688
(Windows) or 11377 (Linux and UNIX) locks can be allocated as shown in
Figure 12-7.
Figure 12-7 Maximum number of locks available for default settings on Linux
When the maximum number of locks has been reached, the database manager
replaces existing row level locks with table locks (lock escalation). This operation
reduces the amount of lock space needed since a transaction will only hold a
single lock on the entire table instead of many locks on every row. Lock
escalation has a negative performance impact because it reduces concurrency
The lock escalation can also be forced by the MAXLOCKS database parameter,
which defines a limit for the maximum percentage of the lock storage held by one
application. In Linux and UNIX, the default value for this parameter is 10, while on
Windows, it is 22. Thus, if one application requests more than 10% of the total
locks space (LOCKLIST), a lock escalation occurs for that application. For
example, inserting 1137 rows of data into a table on Linux within one transaction,
using the default parameter values, results in lock escalation because the
transaction requests 1138 locks (one per each inserted row plus one internal
lock). This requires at least 1138*36 = 40968 bytes of storage - more than 10% of
global lock memory defined by the LOCKLIST parameter default value.
Initial values for LOCKLIST and MAXLOCKS should be based on the maximum
number of applications and average number of locks requested by a transaction
(for OLTP systems, assume a minimum of 512 locks for every application). When
setting MAXLOCKS, you should take into account lock-intensive batch processes
that run during daytime hours. To check the current usage of locks, a lock
snapshot can be taken, as illustrated in Example 12-8.
The snapshot collects the required locking information at the time the command
is issued. Figure 12-8 shows the lock snapshot output. In this example, during
the time the snapshot was taken, there were two applications connected to the
database SAMPLE, and a total of 1151 locks were acquired.
database section
Database Lock Snapshot
Application handle = 21
Application ID = *LOCAL.db2inst1.0C3B32182808
Sequence number = 0001
Application name = db2bp
CONNECT Authorization ID = DB2INST1
Application status = UOW Waiting Total number of
application section
List Of Locks
Lock Name = 0x00030005001052540000000052
locks description
To check for lock escalations, examine the db2diag.log file. A lock escalation
message looks similar to Example 12-9.
A snapshot for applications can be used to check the log space occupied by
transactions. Before taking an application snapshot, the Unit Of Work monitor
must be switched on, as shown in Example 12-10.
At the time the above snapshot was issued, only three transactions were running
in the system. The first transaction used 478 bytes of log space, the second
21324 bytes, and the third 110865 bytes. An application snapshot only provides
the counter values at the moment the command was executed. To obtain more
valuable information about the use of log space by transactions, take multiple
snapshots over time and compare the resulting outputs.
Example 12-11 shows how to obtain information about log I/O activity.
For convenience, the snapshot output can be re-directed to a file, and analyzed
using the Linux grep/egrep tool. In Example 12-11, 630 pages were written at the
time the snapshot was taken, which is about 630 / (23 + 2 + 1) = 25 pages per
transaction. It is not possible to tell what the average size of a transaction is
looking strictly at the value Log pages written, because the basic DB2 UDB
read or write unit is one page (4 KB). Issuing only one small INSERT forces a flush
of 4 KB from the log buffer to the disk. The partially filled log page remains in the
log buffer and can be re-written to disk more than once, until it becomes full. This
behavior guarantees that log files are contiguous.
When setting the value for the log buffer, also consider the ratio between log
pages read and log pages written. The ideal situation is to have zero log pages
read with a large number of log pages written. When there are too many log
pages read, it usually suggests that a bigger LOGBUFSZ parameter value can
improve performance.
If you do not want to set this parameter to automatic, the value of this parameter
must be equal to or greater than the sum of the connected applications, plus the
number of these same applications that might be concurrently in the process of
completing a two-phase commit or rollback. Then, add to this sum the anticipated
number of in-doubt transactions that might exist at any one time.
The value of MAXAGENTS should be at least the sum of the values for MAXAPPLS in
each database allowing concurrent access.
Second, the volumes of data collected and stored by an enterprise today are
growing at an exponential rate. Therefore, there is typically much more data on
disk than can be buffered in memory for fast access. It is very important,
therefore, for the database engine to provide techniques that enable the data to
be accessed quickly and efficiently.
In this section, we describe the types of access methods that DB2 UDB offers to
retrieve data efficiently. The topic is discussed from a logical (software) view,
rather than a physical data storage and access view. Therefore, issues regarding
how to organize physical disks is not included. However, you should note that this
is also a very important consideration for enabling faster data access and
improving performance.
12.5.1 Indexing
Indexes provide an efficient way to access data. Instead of scanning the entire
table to locate particular data elements, an index contains a reference to the data
page where the desired data can be found. That is, the index points directly to the
appropriate data page without the requirement to access each page to determine
whether the desired data is located there. DB2 UDB implements its indexing
using B+ tree data structures.
An index name must be unique for a database and the identifier (the name of the
index) must not be longer than 128 characters. Special characters are not
allowed in the index name.
DB2 UDB allows index columns up to a width of 1024 characters. Keep in mind
that it is not always ideal to index very large columns. The larger the indexed
column, the less performance since the size of the index tree also grows larger,
thus requiring more time to process the additional data.
Clustered index
For performance reasons, you might to decide to create a clustered index. The
syntax to create a clustered index is shown in Example 12-14.
DB2 UDB clustered indexes attempt to insert new rows physically close to the
rows for which the key values in the index are in the same range. Therefore, the
PCTFREE option becomes important, because it influences the future quality of a
clustered index when new rows are inserted (see the following section entitled
PCTFREE).
If the quality of a clustered index becomes poor, you may have to reorganize the
index.
Where,
r = number of rows in table
i = key size (bytes)
When the index is being created, it consumes temporary disk space. You should
estimate how much temporary space is required. You can estimate this using the
following formula:
3.2 * r * (i + 9)
Where,
r = number of rows in tables
i = key size (bytes)
For these equations, unique indexes are assumed. If the calculation is made with
indexes that allow duplicates, you must add 5 bytes per row. In addition, if the
index allows NULL values, add one byte per row for the NULL indicator.
Be aware that these equations only provide estimates. Therefore, you should
validate the estimates before using them.
Figure 12-9 Estimating the index size using the Create Index wizard
Include option
DB2 UDB allows key-only reads. These are reads from the database where all
the columns in the SELECT list are available through an index. Key-only reads are
very efficient because there is no need to access data pages.
DB2 UDB allows you to include additional columns (see the general syntax in
Example 12-16) into a unique index. These index entries that are placed at the
leaf level only.
Example 12-16 Index with INCLUDE expansion
CREATE UNIQUE INDEX idx_name ON tab_name (col_list)
INCLUDE (col_list)
The creation of an INCLUDE index is shown in Example 12-17. First, a table with
two columns is created. col1 is unique and used for query qualification, but col2
is added to the index for a key-only read. Rows are then inserted into the table.
Examine the INCLUDE index in Example 12-18. The column list of the CREATE
INDEX statement does not include the column col2 any longer, but it appears at
the end of the statement in the INCLUDE clause. Figure 12-11 on page 370
illustrates the logical structure of the INCLUDE index.
Notice that the included column is stored at leaf level only, and it is still possible
to use this index for queries by the unique col1 column. If a query selects both
columns col1 and col2, it is possible to get a key-only select without reading
data pages. Using this technique can increase performance and save index disk
capacity.
MINPCTUSED parameter
An index tree is as dynamic as the data changes in the corresponding table.
Index leaf pages are split into two new leaf pages if the original leaf page
becomes full. If a leaf page becomes empty because rows have been deleted
from the corresponding table, DB2 UDB attempts to combine two leaf pages
together into one.
When you create an index in DB2 UDB, you must specify what sort sequence the
index should support. If you do not specify the sorting sequence, the index
follows ascending sorting. When ALLOW REVERSE SCANS is specified in the CREATE
INDEX statement (see Example 12-20), the index also can be used for inverse
sorting. The default is DISALLOW REVERSE SCANS. We recommend using the ALLOW
REVERSE SCANS option since it adds negligible overhead.
COLLECT
DB2 UDB allows the collection of statistical information of an index during
creation time, as demonstrated in Example 12-21. Therefore, there is no need to
execute a runstats command after the index has been created.
During an online index reorganization, the entire index object (that is, all indexes
on the table) is rebuilt. A “shadow copy” of the index object is made, leaving the
original indexes and the table available for read and write access. Any
concurrent transactions that update the table are logged. After the logged table
changes have been forward-fitted and the new index (the shadow copy) is ready,
the new index is made available. While the new index is being made available, all
access to the table is prohibited. The time required to swap files is quite small,
typically on the order of seconds. The default behavior of the reorg indexes
command is ALLOW NO ACCESS, which places an exclusive lock on the table during
the reorganization process, but you can also specify ALLOW READ ACCESS or ALLOW
WRITE ACCESS to permit other transactions to read from or update the table.
Dynamic SQL: The optimizer will choose an MQT instead of using the base
tables only for dynamic SQL statements. Static SQL statements will never be
candidates for re-routing to an MQT.
Optimization class: DB2 UDB supports different levels of optimization
aggressiveness. To let the optimizer consider an MQT for a query, the
database configuration parameter DFT_QUERYOPT must be set to 2, 5, 7, or 9.
You can also adjust the query optimizer class on a connection basis, as
shown in Example 12-25.
Example 12-25 Changing the query optimization level for a single session
SET CURRENT QUERY OPTIMIZATION [2, 5, 7, 9]
MQT routing
Figure 12-12 shows the decision tree used by the optimizer for choosing to use
an MQT. The first prerequisite for using an MQT (other than those previously
mentioned) is that the optimizer is able to rewrite the query. Query rewriting
means that the optimizer has recognized other possible query paths to choose.
Even if the optimizer has rewritten the query, it still might not use the MQT if the
costs for the query with the MQT is higher than with the base tables.
No Yes
Rewrite?
No
MQT Cheaper?
Yes
MQT
Base Tables
Example 12-26 shows the use of an MQT and how it is chosen by the optimizer.
You should compare the Optimized Statement section and the costs of the query
plans (“The db2exfmt utility” on page 387 describes the db2exfmt utility).
Although the first query must run against the base table (no MQT had been
created), the second query (with a FROM clause that specifies the data is to be
read from the base table) is rerouted to the MQT. The costs estimated by the
optimizer are lower when fetching data from the MQT than those of the base
table. The two costs we refer to are in a large, bold (in red if viewed online) font to
make it easy to compare the costs.
db2exfmt -d ifmx2db2 -1
Access Plan:
-----------
Total Cost: 18997.2
# runstats omitted
db2exfmt -d ifmx2db2 -1
Optimized Statement:
-------------------
SELECT Q1.COL1 AS "COL1", Q1.TWICE_COL1, Q1.THE_COUNT
FROM DB2INST2.MQT AS Q1
Access Plan:
-----------
MQT refreshing
When accelerating queries by using MQTs, there are trade-offs. The first is the
space required for the MQT, but another issue is the timeliness of the data. Every
time a row is inserted into one of the base tables referenced by the MQT, the
data in the MQT cannot be refreshed, so it becomes old. Therefore, whether or
not the use an MQT is acceptable depends on your business requirements.
There are advanced methods to keep an MQT’s data up to date, such as using
incremental refreshes. To avoid a full refresh of an MQT, the refresh table
command can be issued, as shown in Example 12-27.
DB2 UDB also tries to refresh only the differences between base tables and the
MQT. Refer to the DB2 UDB documentation SQL Reference - Volume 2,
SC09-4845, for further information.
When deciding whether or not to create a materialized query table, consider the
following questions:
Will the MQT significantly increase performance?
Will many queries benefit? Will the most frequent or most critical or most
expensive and long running queries benefit?
Will the MQT offer resource savings: communication, I/O, and CPU?
Is the loss of disk space that will contain the MQT and its indexes a
worthwhile trade for the performance that will be gained?
What is the cost of updating or refreshing the MQT?
What are the patterns for accessing groups of tables for aggregation and for
grouping requirements?
How current does the data in the MQT need to be? Does it need to be
up-to-the-minute?
For MQTs that are maintained in real time, will automatic updates be too
slow?
Keep in mind that the DB2 Design Advisor can take the guesswork out of
deciding what MQTs to create. It can recommend MQTs, as well as other
database objects based on a given workload. See 12.2.1, “Design Advisor” on
page 341 for more information.
The table data is physically separated into blocks differentiated by the columns
defined in the ORGANZED BY clause, called dimensions. The data blocks
(dimensions) are indexed using a block index. Every time a query uses one of
these dimensions in its WHERE clause (filter), the data can be retrieved using a
block index.
If you create an MDC table, pay attention to the extent sizes that are defined for
a table space. The size of an extent equals the size of an MDC block. There
should be enough space within one extent/block to accept all rows that should
belong to that block. If a block becomes full, additional blocks are allocated on
disk. Ideally, the goal is to have only one block per data group, but having small
number of blocks per group is acceptable. You should be careful about the
number of dimensions specified. As guideline, the more dimensions, the smaller
the blocks are.
Block Index
year dimension
{
2002 2003 2004
SELECT *
FROM sales
WHERE year = 2004;
Result of query =
{
E
region dimension
Block Index
SELECT *
S FROM sales
WHERE year = 2002
W AND
region = ’N’;
Result of query =
Figure 12-13 Visualization of a two-dimensional MDC table
In Example 12-30, the syntax of a sample MDC table is shown. This table
separates data into different blocks, by year, quarter, and region. The advantage
of creating data blocks is that if the query retrieves data using a filter on the
dimensioned columns, a whole block of data can be read from disk. Contrary to a
clustered index (see “Clustered index” on page 365), the need for disk-spread
does not occur if the blocks are big enough.
12.7 Optimizer
The optimizer is the part of a database engine responsible for creating query
plans. A query plan describes how to obtain the data from disk, what indexes can
SQL Query
Check
Sematics
Rewite
Query
Pushdown Query
Analysis Graph
Model
Optimize
Access Plan
Generate
Executable
code
Execute Plan
Explain Executable
Tables Plan
Preparations
In DB2 UDB, before any kind of optimizer analysis can be performed, a special
set of tables (explain tables) need to be created in each database. You can find
the file that holds the necessary DDL to create the explain tables in C:\Program
Files\IBM\SQLLIB\MISC\explain.ddl (see Example 12-31). After the tables have
been created, you can proceed with your optimizer analysis.
Visual Explain
Visual Explain can be launched from Control Center. This tool allows you to
analyze an SQL statement. To use Visual Explain, follow these steps:
1. Open Visual Explain. Launch Control Center, right-click the target database,
and select Explain SQL from the menu, as shown in Figure 12-6.
3. After you enter the SQL statement and set the parameters, click OK to
retrieve the graphical presentation of the query plan, shown in Figure 12-8.
The colored boxes represent each step in the data access path that the
optimizer has chosen.
Before you can begin analyzing an SQL statement with the db2exfmt utility, you
have to create the explain plan first. This is done by appending the clause
EXPLAIN PLAN FOR before the actual SQL statement.
The explain file that is generated by the db2exfmt utility contains very detailed
information about the steps chosen by the optimizer. Most sections of the output
are self-explanatory, especially if you are already familiar with terms such as
costs or join methods.
Example 12-32 demonstrates the process used to create an explain file with the
db2exfmt utility.
Since the query plans of packages are created at compile time, there is the
potential risk of reduced performance if the query plan becomes outdated. The
db2expln utility is able to analyze the query plan of a package. It also reports the
isolation level or optimization class of a package.
Example 12-33 ESQL/C sample application with a single, static SQL statement
#include <stdio.h>
#include <string.h>
#include <sql.h>
int main ()
rowcount = 0;
strcpy (dbname, "sample");
strcpy (uid, "db2admin");
strcpy (pwd, "db2udb");
EXEC SQL
SELECT COUNT (*) INTO :rowcount FROM syscat.tables;
After the application has been precompiled, compiled, and bound to the
database, you can run the db2expln utility, as shown in Example 12-34.
DB2 Universal Database Version 8.1, 5622-044 (c) Copyright IBM Corp. 1991, 2002
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL Explain Tool
Partition Parallel = No
Intra-Partition Parallel = No
SQL Statement:
End of section
| | Predicate Aggregation
| | | Column Function(s)
End of section
Example 12-35 Setting the query optimization level for the whole database
UPDATE DATABASE CFG FOR <dbname> USING DFT_QUERYOPT = [0, 1, 2, 3, 5, 7, 9]
Some queries might benefit from a higher level of optimization. The optimization
level can also be set on a session basis with the SET CURRENT QUERY
OPTIMIZATION command, as shown in Example 12-36.
Example 12-36 Setting the query optimization level for a single session
SET CURRENT QUERY OPTIMIZATION = [0, 1, 2, 3, 5, 7, 9]
The higher the value of the optimization class, the more aggressive the optimizer
becomes.
Note: The SET CURRENT QUERY OPTIMIZATION statement only affects dynamic
SQL statements. To specify an optimization level for static SQL statements,
you must specify it at bind time using the QUERYOPT parameter in the BIND
statement.
The following optimizer classes (levels) can be specified for query compilation:
Optimization class 0
This optimization class has the following characteristics:
Minimal optimization.
Non-uniform distribution statistics are not considered by the optimizer.
Only basic query rewrite rules are applied.
Greedy join enumeration occurs.
Only nested loop join and index scan access methods are enabled.
List prefetch and index ANDing are not used in generated access methods.
This class should only be used in circumstances that require the lowest possible
query compilation overhead. Query optimization class 0 is appropriate for an
application that consists entirely of very simple dynamic SQL statements that
access well-indexed tables.
Optimization class 1
This optimization class has the following characteristics:
Non-uniform distribution statistics are not considered by the optimizer.
Only a subset of the query rewrite rules are applied.
Greedy join enumeration occurs.
List prefetch and index ANDing are not used in generated access methods
although index ANDing is still used when working with the semi-joins used in
star-joins.
Optimization class 1 is similar to class 0 except that merge scan joins and table
scans are also available.
Optimization class 2
This class directs the optimizer to use a degree of optimization significantly
higher than class 1, while keeping the compilation cost significantly lower than
classes 3 and above for complex queries. This optimization class has the
following characteristics:
All available statistics, including both frequency and quantile non-uniform
distribution statistics, are used.
All query rewrite rules are applied, including routing queries to materialized
query tables, except computationally intensive rules that are applicable only
in very rare cases.
Greedy join enumeration is used.
A wide range of access methods are considered, including list prefetch and
materialized query table routing.
The star-join strategy is considered, if applicable.
Optimization class 3
This class requests a moderate amount of optimization. This class comes closest
to matching the query optimization characteristics of DB2 for MVS/ESA™,
OS/390, or z/OS. This optimization class has the following characteristics:
Non-uniform distribution statistics, which track frequently occurring values,
are used if available.
Most query rewrite rules are applied, including subquery-to-join
transformations.
Dynamic programming join enumeration, as follows:
– Limited use of composite inner tables
– Limited use of Cartesian products for star schemas involving lookup tables
A wide range of access methods are considered, including list prefetch, index
ANDing, and star joins.
This class is suitable for a wide range of applications. This class improves
access plans for queries with four or more joins. However, the optimizer might
fail to consider a better plan that might be chosen with the default optimization
class.
Optimization class 5
This class directs the optimizer to use a significant amount of optimization to
generate an access plan. This optimization class has the following
characteristics:
All available statistics are used, including both frequency and quantile
distribution statistics.
All of the query rewrite rules are applied, including the routing of queries to
materialized query tables, except for those computationally intensive rules
that are applicable only in very rare cases.
Dynamic programming join enumeration, as follows:
– Limited use of composite inner tables
– Limited use of Cartesian products for star schemas involving lookup tables
A wide range of access methods are considered, including list prefetch, index
ANDing, and materialized query table routing.
When the optimizer detects that the additional resources and processing time
are not warranted for complex dynamic SQL queries, optimization is reduced.
When the query optimizer reduces the amount of query optimization, it continues
to apply all the query rewrite rules that would normally be applied. However, it
does use the Greedy join enumeration method and reduces the number of
access plan combinations that are considered.
Optimization class 7
This class directs the optimizer to use a significant amount of optimization to
generate an access plan. It is the same as query optimization class 5 except that
it does not reduce the amount of query optimization for complex dynamic SQL
queries.
Optimization class 9
This class directs the optimizer to use all available optimization techniques.
These include:
All available statistics
All query rewrite rules
All possibilities for join enumerations, including Cartesian products and
unlimited composite inners
All access methods
This class can greatly expand the number of possible access plans that are
considered by the optimizer. You might use this class to find out whether more
comprehensive optimization would generate a better access plan for very
complex and very long-running queries that use large tables. Use Explain and
performance measurements to verify that a better plan has actually been found.
Figure 12-20 shows the impact on the resulting query plan using different
optimization classes for a sample query.
Optimization class 5
Notice how the plan produced with an optimization class of 5 performs better
than the plan produced with an optimization class of 0 (37.51 timerons versus
48.75 timerons).
Scope
State the purpose of the plan, possibly identifying the level of the plan (master,
etc.). This is essentially the executive summary part of the plan. You may want to
include any references to other plans, documents, or items that contain
information relevant to this project and process. If preferable, you can create a
references section to contain all reference documents.
Identify the scope of the plan in relation to the software project plan that it relates
to. Other items may include resource and budget constraints, scope of the testing
effort, how testing relates to other evaluation activities (analysis and reviews), the
process to be used for change control and communication, and coordination of
key activities.
As this is the executive summary, keep information brief and to the point.
This section is a technical description of the software, and can be oriented to the
level of the test plan. For higher levels, it may be by application or functional area,
for lower levels it may be by program, unit, module, or build.
Features to be tested
This is a listing of what is to be tested from the user’s viewpoint of what the
system does. This is not a technical description of the software, but a user’s view
of the functions. Users do not understand technical software terminology. They
understand functions and processes as they relate to their jobs.
Set the level of risk for each feature. Use a simple rating scale such as high,
medium, and low (H, M, L). These types of levels are understandable to a user.
You should be prepared to discuss why a particular level was chosen.
Test strategy
This is your overall test strategy for this test plan. It should be appropriate to the
plan and should be in agreement with plans affecting application and database
parts. Overall rules and processes should be identified:
Are any special tools to be used and what are they?
Will the tool require special training?
What metrics will be collected?
Which level is each metric to be collected at?
How is configuration management to be handled?
How many different configurations will be tested?
Which combinations of hardware, software, and other vendor packages are
used?
What levels of regression testing will be done and how much at each test
level?
Specify what constitutes stoppage for a test or series of tests, and what is the
acceptable level of defects that will allow the testing to proceed past the defects.
Testing after a truly fatal error will generate conditions that may be identified as
defects, but are in fact ghost errors caused by the earlier defects that were
ignored.
Test deliverable
What is to be delivered as part of this plan?
Test plan document
Test cases
Test design specification
Tools and their outputs
Error logs and execution logs
Problem reports and corrective actions
One thing that is not a test deliverable is the software itself, which is listed under
test items, and is delivered by development.
Environmental needs
Are there any special requirements for this test plan such as:
Special hardware such as simulators, static generators, etc.
How will test data be provided? Are there special collection requirements or
specific ranges of data that must be provided?
How much testing will be done on each component of a multi-part feature?
Special power requirements
Specific versions of other supporting software
Responsibilities
Who is in charge? This issue includes all areas of the plan. Here are some
examples:
Setting risks
Selecting features to be tested and not tested
Setting overall strategy for this level of plan
Ensuring all required elements are in place for testing
Providing for resolution of scheduling conflicts, especially if testing is done on
the production system
Who provides the required training?
Functional testing
Functional testing is a set of tests in which new and existing functionality of the
system is tested after migration. Functional testing includes all components of
the RDBMS system, networking, and application components. The objective of
functional testing is to verify that each component of the system functions as it
did before migrating, and to verify that new functions are working properly.
Performance testing
Performance testing of a target database compares the performance of various
SQL statements in the target database with the statements’ performance in the
source database. Before migrating, you should understand the performance
profile of the application under the source database. Specifically, you should
understand the calls the application makes to the database engine.
Acceptance testing
Acceptance tests are carried out by the end users of the migrated system. Users
are asked to simply explore the system, test usability, and system features, and
give direct feedback. Acceptance tests are usually the last step before going into
production with the new system.
It is always best to couple all test dates directly to their associated migration
activity dates. This prevents the test team from being perceived as the cause of a
delay. For example, if system testing is to begin after delivery of the final build,
then system testing begins the day after delivery. If the delivery is late, system
Figure 13-1 shows the test phases during a typical migration project. The test
cases, and all the following tasks, must be completed for all test phases.
The time exposure of tests depends on the availability of an existing test plan and
already prepared test items. The effort also depend on the degree of changes
made during the application and database migration.
Note: Testing can occupy between 50% and 70% of the total migration effort.
SQL3148W A row from the input file was not inserted into the table. SQLCODE
"-545" was returned.
SQL0545N The requested operation is not allowed because a row does not satisfy
the check constraint "ITSO.TABLE01.SQL030812222227680". SQLSTATE=23513
SQL3185W The previous error occurred while processing data from row "2" of the
input file.
SQL3117W The field value in row "3" and column "1" cannot be converted to a
SMALLINT value. A null was loaded.
SQL3125W The character data in row "4" and column "2" was truncated because
the data is longer than the target database column.
SQL3110N The utility has completed processing. "4" rows were read from the
input file.
SQL3149N "4" rows were processed from the input file. "3" rows were
successfully inserted into the table. "1" rows were rejected.
As shown in above example, during the import process, one record from the
input file was rejected, and three were inserted into the database. To understand
the nature of the warnings, look at the data source file and the table definition
(db2look command). For Example 13-1, the table definition is presented in
Figure 13-2, and the data file in Figure 13-3.
1,"abc",1
2,"abc",4
32768,"abc",2
4,"abcd",3
The first row from the input file (Figure 13-3) was inserted without any warnings.
The second row was rejected because it violated check constraints (warnings
SQL3148W, SQL0545N, SQL3185W). A value of 32768 from the third row was
changed to null because it was out of the SMALLINT data type range (warning
SQL3117W) and string abcd from the last row was truncated to abc because it was
longer than the associated column definition (warning SQL3125W).
The LOAD utility generates messages in a similar format, but since it is designed
for speed, it bypasses the SQL engine, and inserts data directly into table spaces
without performing constraint checking. Inserting the same table01.unl file
(Figure 13-3) into table01 (Figure 13-2) with the LOAD utility generates messages
without SQL3148W, SQL0545N, SQL3185W warnings, as shown in Example 13-2.
[..]
SQL3125W The character data in row "4" and column "2" was truncated because
the data is longer than the target database column.
[..]
Number of rows read = 4
Number of rows skipped = 0
Number of rows loaded = 4
Number of rows rejected = 0
Number of rows deleted = 0
Number of rows committed = 4
A table that has been created with constraints is left in a check pending state.
Accessing the table with SQL queries generates a warning:
SQL0668N Operation not allowed for reason code "1" on table
"<TABLE_NAME>". SQLSTATE=57016.
The SET INTEGRITY statement should be used to move a table into a usable
state. Example 13-3 shows a sample invocation of this command. All rows that
violate constraints are moved to exception table table01_e.
SQL3602W Check data processing found constraint violations and moved them to
exception tables. SQLSTATE=01603
The SET INTEGRITY statement has many options such as only checking the
integrity of new data, turning integrity checking off, or specifying exception tables
with additional diagnostic information.
For small tables (less that 50,000 rows), you can write a script that compares
data byte-by-byte. The script can extract sorted rows from SQL Server and DB2
UDB to files in the same ASCII format. The files can be binary compared to
determine if they are the same.
For each table, you should count the number of rows and store the information in
the CK_ROW_COUNT table. The following INSERT statement can be used for that
purpose:
INSERT INTO dbo.CK_ROW_COUNT SELECT 'tablename', COUNT(*), 'MSSQL',
getdate() FROM dbo.tablename
The table CK_ROW_COUNTS and its data can be manually migrated to the target DB2
UDB database. Example 13-5 shows the DB2 UDB syntax to the table.
On the DB2 UDB system, you should submit the equivalent INSERT statement:
INSERT INTO ck_row_count SELECT 'TAB_NAME', count(*), 'DB2', CURRENT
TIMESTAMP FROM tab_name
After performing the steps described above, both CK_ROW_COUNT tables should
contain information about the number of rows counted on SQL Server and DB2
UDB respectively. Example 13-6 shows what the table content should look like.
Having the information about the number of rows in a table is very convenient,
because with a single query such as:
SELECT tab_name FROM (SELECT DISTINCT tab_name, num_rows FROM ck_row_count)
AS t_temp GROUP BY t_temp.tab_name HAVING(COUNT(*) > 1)
you can get the table names that contain a different number of rows in the source
and target database.
Within the Development Center there are options to run selected procedures in
debug mode (see Figure 13-4).
You should document all test conditions and result, such as what operations were
performed, which application screens were opened, what input data was used for
testing, and what the result was. For larger projects, creating documentation can
become overwhelming. Typically, specialized software is used for these cases.
Together with various functional testing, the application should also be checked
for performance. Since there are many architectural differences between SQL
Server and DB2 UDB, some SQL statements might require further optimization.
Before going into production, the migrated database should be verified under
high volume and loads that emulate the production environment. this can help
determine if further application or database tuning is necessary. The stress load
can also reveal other hidden problems, such as locking issues, which can
typically only be observed in a production environment.
IBM Rational Suite TestStudio is a set of tools for testers and developers. It
automates regression, functionality, and performance testing, and provides
background runtime analysis for increased product reliability. IBM Rational Suite
TestStudio also includes tools for control, management, and reporting of all test
activities, defect, and change tracking, software configuration management, and
requirements management. IBM Rational Suite TestStudio addresses everything
from test process standardization to results analysis, requirement determination
to impact analysis, and test automation to defect tracking and reporting.
For more information about testing products, go to the IBM Rational Web site at:
http://www.ibm.com/software/rational
13.4 Troubleshooting
The first step of problem determination is to know what information and tools are
available to you. Whenever DB2 UDB performs an operation, there is a return
code associated with that operation. The return code is displayed to the user in
the form of an informational or error message. These messages are logged in
diagnostic files, depending on the diagnostic level set in the DB2 UDB
The following actions should be taken when experiencing a DB2 UDB related
problem:
Check related messages
Explain error codes
Check documentation
Search through available Internet resources
Review APARs for current FixPak level
Use available tools to narrow the problem
Ask IBM for support
DB2 UDB also provides detailed information for each message. The full error
message describes the nature of the problem in detail and the potential user
responses. To display the DB2 UDB return code full message, you can use the
DB2 UDB command: db2 ? error-code. See Example 13-7.
Explanation:
[...]
You can find full information about the DB2 message format, and a listing of all
the messages in the DB2 UDB documentation Messages Reference, Volumes 1
and 2, GC09-4840-00, and GC09-4841-00.
Windows:
<INSTALL PATH>\<DB2INSTANCE>
Where, <INSTALL PATH> is the directory where DB2 UDB is installed, and
<DB2INSTANCE> is the name of DB2 UDB instance.
$HOME/sqllib/db2dump
Most of the time, the default value is sufficient for problem determination. In some
cases, especially on development or test systems you can set the parameter to 4
and collect all informational messages. However, be aware that depending on the
activity, this may cause performance issues due to the large amount of data
recorded into the file. Setting DIAGLEVEL to 4 may also make the file very large
and harder to read.
The above example shows a administration warning about lock escalation (326
row locks were successfully replaced by one table lock) on table
SYSTOOLS.STMG_OBJECT.
Other components such as the Health Monitor, the Capture and Apply programs,
and user applications using the db2AdminMsgWrite API function can also write to
the notify logs.
Trap files
Whenever a DB2 UDB process receives a signal or exception (raised by the
operating system as a result of a system event) that is recognized by the DB2
UDB signal handler, a trap file is generated in the DB2 UDB diagnostic directory.
The files are created use the following naming convention:
Windows:
DBpppttt.TRP
– ppp : The process ID (PID)
– ttt : The thread ID (TID)
Example: DB123654.TRP
Depending on the signal received or the exception raised, the existence of these
files can indicate different extremes of consequences. These consequences can
range from the generation of a simple stack trace for additional diagnostics, to a
Dump files
When DB2 UDB determines that internal information needs to be collected, it will
often create binary dump files in the diagnostic path. These files are generated in
the following format:
Windows:
pppttt.nnn or lpppttt.nnn (for lock list dump)
– ppp: The process ID (PID)
– ttt: The thread ID (TID)
– nnn: The node where the problem occurred
– Example: 123654.000
Example: 123456.000
Messages files
Some DB2 UDB utilities like BIND, LOAD, EXPORT and IMPORT provide an option to
write messages file to a user-defined location. These files contain useful
information to report the progress, success, or failure of the utility that was run.
Maintenance version
The db2level utility can be used to check the current version of DB2 UDB. As
shown in Example 13-9, the utility returns information about the installed
maintenance updates (FixPaks), the length of word used by the instance (32-bit
or 64-bit), the build date, and other code identifiers. We recommend to
periodically check if the newest available FixPaks are installed. DB2 UDB
maintenance updates are freely available at:
ftp://ftp.software.ibm.com/ps/products/db2/fixes
db2support utility
The db2support utility is designed to automatically collect all DB2 UDB and
system diagnostic data. This program generates information about a DB2 UDB
server, including information about its configuration and system environment.
In one simple step, the tool can gather database manager snapshots,
configuration files, trap and dump files, and operating system parameters, which
should make the problem determination quicker. Below is sample command
syntax to run the utility:
db2support . -d sample -c
The dot represents the current directory where the output file is to be stored.
Other parameters are optional. the -d and -c flags instruct the utility to connect
Note: To obtain the most complete output from the db2support command we
recommend that you run the command as the instance owner.
DB2DIAG utility
DB2 UDB has a new message format in V8.2 and the db2diag utility is a tool
used for filtering and formatting db2diag.log files. It’s a command line tool that
has a range of options designed to assist with problem determination using the
db2diag.log.
For example, the following command syntax will return severe issues reported
against the REDBOOK database:
There are many command options available for db2diag. Listed below are some
common ones useful for obtaining help and viewing examples.
db2diag -help
– provides a short description of the options
db2diag -h brief
– provides a description of all the options without examples
db2diag -h notes
– provides usage notes and restrictions
db2diag -h examples
– provides a small set of examples to get started
db2diag -h tutorial
– provides examples for all available options
db2diag -h all
– provides the most complete list of options
DB2PD utility
A new utility that can be used to retrieve statistics from a running DB2 UDB
instance or database is db2pd.
The tool can provide a wide range of information useful for troubleshooting and
problem determination, performance improvements, and application
development design, including:
Figure 13-10 shows how you can use the tool to return information about the
buffer pools.
BufferPools:
First Active Pool ID 1
Max Bufferpool ID 2
Max Bufferpool ID on Disk 2
Num Bufferpools 6
To search for related problems in the DB2 Knowledge Base, use keywords that
may include the command that was run, the symptoms, and tokens from the
diagnostics messages. The Knowledge Base offers an option to search through
DB2 UDB documentation, TechNotes, and DB2 UDB defects (APARs).
Authorized Program Analysis Reports (APARs) are defects in the DB2 UDB code
that require a fix. APARs have unique identifiers and are always specific to a
particular version, but may affect multiple products in the DB2 family on multiple
platforms. Fixes for APARs are provided through DB2 UDB FixPaks.
On the DB2 Support Site there is also the possibility to search for closed, open,
and HIPER APARs. A closed APAR indicates a resolution for the problem has
been verified and included in FixPaks. Open APARs represent DB2 UDB defects
that are currently being worked on or are waiting to be included in the next
available FixPak. HIPER APARs (High-Impact or PERvasive) are critical
problems that should be reviewed to assess the potential impact of staying at a
particular FixPak level.
The DB2 Technical Support site offers e-mail notification of critical or pervasive
DB2 UDB customer support issues including HIPER APARs and FixPak alerts.
To subscribe to it, follow the DB2 Alert link on the Technical Support main page.
The guidelines and reference materials (which you may need when calling IBM
support) as well as the telephone numbers are available in the IBM Software
Support Guide at:
http://techsupport.services.ibm.com/guides/handbook.html
While no scenario can prepare you for all the issues you may potentially
encounter during a conversion, we show you how to manage different classes of
problems. Therefore, your goal in following along with this scenario should be to
understand the reasoning of each step in the process and the lessons being
taught.
On the source system (Boron), the following products are installed and the
system is configured as follows:
Windows 2000 Server with Service Pack 4.
Microsoft SQL Server with Service Pack 3a.
– REDBOOK database
The REDBOOK database is a small database containing tables, views,
stored procedures, triggers, and user defined functions. Although it is
small, the objects in it include features that are not directly portable to DB2
UDB, thus making the conversion non-trivial. A script to create the REDBOOK
database is available for download. Refer to Appendix G, “Additional
material” on page 509 for more information.
DB2 UDB Application Development Client Version 8.1 with FixPak 8.
MTK Version 1.3.
A Data Source Name (DSN) called SQLServer has been created for SQL
Server.
When accessing SQL Server databases from MTK, an ODBC DSN must be
created. This can be configured from the ODBC Data Source Administrator
panel in Windows 2000, available from Start → Settings → Administrative
Tools → Data Sources (ODBC).
A DB2 UDB database, also called REDBOOK, residing on the target system
(Lead) has been cataloged. This task can be performed once the REDBOOK
database is created on Lead.
On the target system (Lead), the following products are installed and the system
is configured as follows:
Windows 2000 Server with Service Pack 4
DB2 UDB Enterprise Server Edition Version 8.1 with FixPak 8
A DB2 UDB database called REDBOOK has been created using the command:
CREATE DATABASE redbook
A progress window is displayed while MTK loads. If you select the Don’t
show again checkbox on the welcome window, the next time MTK starts, the
toolkit is launched immediately.
2. Setup a new project in MTK. In the Project management dialog window
(Figure 14-2), under the New Project tab, enter the project information
contained in Table 14-1 and click OK. This creates a folder with the given
project name under the C:\MTK\projects folder.
DB2 UDB Platform and Version 8.2 for Linux, UNIX and Windows
Each tab represents a phase of the MTK conversion process. The database
conversion process using MTK is summarized in Figure 14-3.
Connect to
Import Extract
source
Convert
Refine
Generate data
Transfer
Script
DB2 Database
Deploy
HTML Report
MTK can either convert a database from an existing SQL Server script or
connect to a SQL Server database to extract the information it needs. By using
the Extract option, it is possible to:
Extract only a subset of objects into their own file (e.g., extract tables into their
own file, extract triggers into their own file, etc.).
Automatically resolve dependencies (e.g., when extracting a procedure, also
extract all the dependent objects, such as tables).
Extract each stored procedure and trigger into its own file.
In this scenario, we extract the information directly from the SQL Server REDBOOK
database.
Note: We could have also used the REDBOOK database creation script provided
as the input conversion source (Import option). However, during your
conversion, you will likely be extracting the information directly from a SQL
Server database.
Click Extract to begin the extraction process. MTK begins extracting the table
and index object definitions from the SQL Server REDBOOK database. Note that
this step may take a while if the source database has a lot of objects.
Figure 14-7 MTK - Specify Source tab with extracted table script selected
Take a moment to familiarize yourself with the contents of the script by clicking
the View button.
14.3.2 Convert
Customize how MTK converts the extracted object definitions to DB2 UDB.
1. Select the Convert tab (Figure 14-8). On the left window pane, a listing of all
source (input) files is provided. In the middle pane, the Convert button
initiates the conversion process of the currently selected source file. The right
pane contains the translated (output) files. Click the View Output File button
in the right pane to view the contents of the converted file. The contents of the
file open in a text editor. Ensure you close the file in the text editor before
proceeding to the next step.
2. Click the Global Type Mapping button to open the Global Type Mapping
window (Figure 14-9). In this window, you can specify the data type mapping
you want to use between SQL Server data types and DB2 UDB data types. In
the Target type column, you can modify a target DB2 UDB data type by
clicking the icon next to the type. Alternatively, you can also click in the
target cell you want to change. A Data Type Editor dialog window is displayed
(Figure 14-10) where you can specify the target DB2 UDB data type and its
attributes. Click Apply to apply your changes. In this example, we change all
GRAPHIC and VARGRAPHIC data types to CHAR and VARCHAR, respectively.
Clicking the Restore Previous Mapping button re-loads the mapping used in
the last conversion activity.
4. Click the Advanced Options button. This opens the Advanced Options
window (Figure 14-12) where advanced conversion properties can be set,
such as how the original source code should be copied into the converted file.
In this scenario, we use all the default options. Click OK to go back to the
Convert tab.
5. In the Convert tab, click Convert to begin converting the SQL Server table
and index definitions to DB2 UDB. When the conversion completes, MTK
automatically advances to the Refine tab.
14.3.3 Refine
During the refine stage, you have the opportunity to review any errors, warnings
and other information about the conversion.
1. The Refine tab (Figure 14-13) has two main panels. The left panel contains a
messages tree. The right panel contains additional information about the
currently selected node in the messages tree. When the root tree node is
selected, a summary of the errors encountered is displayed (listed in order of
decreasing importance). This summary information is typically used to target
the most critical conversion issues first.
This general message displays the version of MTK being used: 041209.0332.
The version number you see may vary from this. No action is required here.
3. Expand the Translator Warning message tree as shown in Figure 14-15.
On the right pane of the Refine tab, you should see two sub-tabs, as shown in
Figure 14-17.
Click the source SQL Server file tab (Source file: tables.src) to see the
problematic code and the target DB2 UDB file tab (DB2 file: tables.db2) to
see how MTK converted it.
This is another common warning message encountered in conversions to
DB2 UDB. Index names in DB2 UDB are limited to 18 characters, so MTK
suggests a new name for the index. The original index name was
idx_redbooks_author_id. MTK renamed this index to idx_redbooks_auth1.
Usually, the renaming of indexes is harmless since applications generally do
not need to reference indexes by name. However, you might have a standard
naming convention for object names and the name suggested by MTK might
be inappropriate. You are able to provide a different name for the converted
index by completing the following steps:
– Click the Go to Source Object button in the top right pane of the Refine
tab. This displays a panel where you can see both the original object name
and the renamed DB2 UDB object.
– Click the icon beside the recommended DB2 UDB object name and a
dialog window appears where you are able to provide a new name for the
object.
– In this example, we rename the new DB2 UDB index redbook_aid and
click Apply to apply the new name. Notice how the
idx_redbooks_author_id index is now mapped to redbook_aid
(Figure 14-18).
– Return to the previous view of the Refine tab by selecting the Messages
tab in the tab panel underneath the messages tree. You can always return
to the current screen (Figure 14-18) by selecting the SQL Server tab.
5. Now that you are back at the Messages tab, notice how the index name in the
DB2 file has not changed. For the change to take effect, we must convert the
file again by:
a. Selecting the Convert tab at the top of the MTK window
b. Clicking the Convert button
Clicking the Convert button causes the original source file to be re-read and
applies any changes defined to generate a new tables.db2 file. The previous
tables.db2 file is discarded and a new tables.db2 file is generated. If you
expand the Messages tree once more, the warning message (Msg Number
20) about object name change for this particular index no longer exists.
6. Follow the procedure described above to rename any other objects in which
the MTK-generated name is not preferred.
7. Ensure all other messages are read and cleared if possible. Expand the
messages tree again, as shown in Figure 14-19.
Observe that the script translation ratio is 100%. That is, 100% of the
statements in the input script are accounted for and converted without error.
Warnings are not critical and are not included in the calculation.
A common misconception is that data is moved from SQL Server to DB2 UDB in
this step. In fact, no data is actually moved in this step - only the scripts to
perform the data extraction and deployment are generated.
This step creates scripts that can be used by the current machine or on another
machine to perform the deployment. This is very useful when the volume of the
data precludes using the machine where MTK is installed.
Figure 14-21 MTK - Generate Data Transfer Scripts tab (data loading options)
IMPORT IMPORT reads data from a flat file and generates corresponding INSERT
statements. COMMIT statement frequency can be reduced to improve
performance. Because INSERT statements are used, constraints are
enforced and triggers are activated. IMPORT performance is largely
determined by buffer pool size and placement of database logs (logging is
performed).
LOAD LOAD reads data from a flat file and loads data at the data page level.
Because SQL statements are not used, triggers are not activated as rows
are loaded. Performance of this method is largely determined by the utility
heap size. Logging is not performed.
Recommendation: In most cases, you should use the LOAD utility since you
need to move the data from SQL Server “as-is”, without activating triggers.
Figure 14-22 MTK - Generate Data Transfer Scripts tab (LOAD/IMPORT mode)
File format
MTK supports both ASCII delimited (DEL) and ASCII positional (ASC) files, as
shown in Figure 14-23. In most cases, the default (ASC) works fine. The DEL
format requires less disk space for extracted flat files, but you may encounter
difficulty selecting a unique column delimiter depending on the data.
Figure 14-23 MTK - Generate Data Transfer Scripts tab (file format options)
Advanced options
Depending on the data loading method selected (LOAD or IMPORT), you will see a
different menu for advanced options. The options you select may affect the
overall performance of the data migration process. For more details about the
LOAD and IMPORT commands, consult the DB2 Information Center at:
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp
Navigate to:
3. Create the DB2 UDB database objects and load the data. At the bottom of the
Deploy to DB2 tab, three deployment steps (check boxes) are available:
– Launch tables.db2 in the database
– Extract and store data on this system
– Load data to target database using general scripts
We do NOT recommend performing all three steps at the same time. Rather,
each step should be performed independently. This gives you the most
control and avoids trying to do too much in one step:
a. Start with the first step (Figure 14-25) and click Deploy. This creates the
tables and indexes in the DB2 UDB database and gives you a chance to
review the verification report (Figure 14-25).
b. When you are comfortable with the results of the previous step, select only
the second step (Figure 14-26). Click Deploy once again to perform data
extraction to flat files.
d. Later, once any functions, triggers, and stored procedures are converted,
you can repeat step a) for each of those scripts. You do not have to
perform steps b) or c) again.
When deployment completes (after clicking the Deploy button), a Verification
Report (Figure 14-28) is displayed to show you the results. Confirm that
everything deployed properly. If there are no errors in the verification report,
you have successfully converted your tables and indexes to DB2 UDB.
3. After completing this step, two files should appear in the MTK project,
tables.src and views.src.
4. Click the Convert tab.
To convert these Views, we must first set the context of the conversion
because the Views reference some of the tables defined in tables.src. Click
the Context button. Understanding how context affects a conversion is critical
to effective use of MTK.
5. In the Set Context window, indicate to MTK that your conversion has
dependencies on the tables.src file by selecting the tables.src file and
Figure 14-30 MTK - Convert tab after setting the context (for Views)
7. Select the Refine tab. Expand the messages tree as shown in Figure 14-31:
Observe that the script translation ratio is 100%. That is, 100% of the
statements in the views.src script are accounted for and translated without
error. Warnings are not critical and are not included in the calculation. Since
there are no Translator Error messages listed in the Messages tree, we can
be confident that MTK will convert all the Views to DB2 UDB.
8. Deploy the Views to DB2 UDB.
Click the Deploy to DB2 tab. From the Conversion name pull-down list, select
views. Ensure that the target database name indicates REDBOOK. Leave the
default values for all other settings. Ensure the (re)create database option is
NOT selected. Click Deploy.
After deployment completes, you should see that all views have been created in
the DB2 UDB REDBOOK database.
Note: You may need to click the Refresh button on your Web browser to see
the latest Validation Report.
Although it does not appear on the screen, the tables.db2 file remains in your
project folder. It is simply not listed as part of the current conversion activity. If
you need to refer to previous conversions, select tables from the pull-down list in
Previous Conversions sub-panel (Figure 14-33) and click Reload. You can switch
back and forth between conversions without having to re-convert.
Figure 14-34 MTK - Extract dialog window for user defined functions
This warning message is being reported since the SQL Server USE redbook
and SET USER ‘dbo’ statements have no equivalents in DB2 UDB. However,
MTK has determined that these statements are not required in the conversion
and that their behavior is accounted for in the converted code. In the absense
of any other significant warning messages, the functions need no further
refinements and can now be deployed to DB2 UDB.
7. Deploy the functions to DB2 UDB.
Click the Deploy to DB2 tab. From the Conversion name pull-down list, select
functions. Ensure that the target database name indicates REDBOOK. Leave
the default values for all other settings. Ensure the (re)create database
option is NOT selected. Click Deploy.
After deployment completes, you should see that all functions have been
successfully created in the DB2 UDB REDBOOK database.
Note: You may need to click the Refresh button on your Web browser to see
the latest Validation Report.
OPEN temp_cursor;
END!
If you were to deploy/run this script (procedures.db2) in its current state, the
procedure avg_dept_salary() would create successfully, but without the
COMPUTE functionality expected.
Manual intervention is needed here in order to preserve this functionality.
Make a note to manually add this functionality back later.
5. Return to the Convert tab. In the left pane, select the procedures.src file.
Click the View Source File button on the bottom left hand corner. Locate the
code for avg_dept_salary() and change the SELECT statement from:
SELECT dept_id, basic_salary
FROM employee ORDER BY dept_id COMPUTE AVG(basic_salary) BY dept_id
to:
SELECT dept_id, basic_salary
FROM employee ORDER BY dept_id
Save and close the text editor (closing the text editor is important to avoid
confusion later if you open the file again). We have effectively changed the
input file to the MTK. Click the Convert button once again. The translator
re-reads the input file and generates a new procedures.db2 file. On the Refine
tab, notice that the conversion of avg_dept_salary() has now completed
without errors or warnings. Msg Number: 5 no longer appears (Figure 14-39).
Just remember to manually edit the DB2 UDB script later on to compensate
Figure 14-39 MTK - Refine tab after modifying a SQL Server stored procedure
After deployment completes, check to make sure that all stored procedures are
successfully created in DB2 UDB.
The other Translation Warning messages also involve the various cursor
statements in the SQL Server trigger code. Since explicit cursor declarations
or statements involving cursors are not supported in DB2 UDB triggers, this
trigger should be manually converted to DB2 UDB. Possible options for
converting this include:
– Replacing the explicit cursor declaration and statements with implicit
cursor operations
– Moving the cursor logic into a stored procedure, then invoking the stored
procedure from the trigger
– Performing the cursor logic in the application code, instead of in a trigger.
This would involve additional manual conversion effort.
For a more complete discussion about conversion possibilities, refer to 9.4.4,
“Overcoming inline SQL PL limitations in triggers” on page 219. For now,
DB2 UDB does not support combined UPDATE/DELELE triggers, but multiple
triggers can be defined on the same table. Therefore, the single SQL Server
UPDATE/DELELE trigger is converted as two separate triggers - one for UPDATE
and one for DELELE. MTK simply converts the trigger twice and renames the
triggers slightly. The original SQL Server trigger:
CREATE TRIGGER chg_redbooks
ON redbooks
FOR UPDATE, DELETE
AS
INSERT INTO redbooks_history(
book_id,
book_no,
title,
brand_id,
price,
notes,
author_id,
release_date,
change_date,
timestamp)
After deployment completes, check to make sure that all triggers are successfully
created in DB2 UDB. Also remember to manually convert the problematic trigger
at a later time and re-add it to the deployment script.
You can download the application code for this sample application. Refer to
Appendix G, “Additional material” on page 509 for download instructions.
The other window is the Author search screen where a user can search for IBM
Redbooks contained in a database. This window is shown in Figure 14-44.
The application uses the Microsoft Sql .NET data provider to interface natively
with SQL Server. A change in the interface driver to the IBM DB2 .NET data
provider is used in this scenario in order to allow the application to work with a
DB2 UDB database with minimal changes.
The application can switch between a SQL Server and DB2 UDB database by
setting a boolean variable in the application code:
// Switch between SQL Server: false, and DB2 UDB: true
static public Boolean usingDB2UDB = true;
The code to interface with SQL Server using the Sql .NET data provider and with
DB2 UDB using the DB2.NET data provider is included in the application code for
comparison purposes.
14.5.2 Set up Visual Studio .NET for the DB2 UDB environment
To use the DB2 .NET Provider, you must register the IBM DB2 Development
Add-In, if you installed Visual Studio .NET after installing DB2 UDB.
Install the add-in by executing the following command from a command window:
C:\%DB2HOME%\SQLLIB\BIN\db2vsregister.bat
DB2 UDB
private IBM.Data.DB2.DB2Connection db2Connection1;
[...]
this.db2Connection1.Open();
pubIdCommand.Connection = this.db2Connection1;
m_objPubId = pubIdCommand.ExecuteScalar();
this.db2Connection1.Close();
In this sample program, there are no specific data type enumerations to change.
In your environment, you may need to change data type enumerations, for
example:
SqlDBType.Int → DB2Type.Int32
SqlDBType.NVarChar → DB2Type.String
SqlDBType.DateTime → DB2Type.DateTime
SQL Server:
m_strConnectString = "SERVER=" + this.TBServerName.Text
+";UID=" + this.TBUserName.Text + ";PWD=" + this.TBPassword.Text
+";DATABASE=redbook;Trusted_Connection=Yes";
DB2 UDB
m_strConnectString = "DATABASE=REDBOOK;" + "Connect Timeout=30;"
+"user Id=" + this.TBUserName.Text + ";PWD=" + this.TBPassword.Text;
14.5.6 Summary
This section discussed the steps involved in converting a simple application from
SQL Server and the Microsoft-specific SqlClient data provider to DB2 UDB and
the IBM DB2 .NET data provider.
SQL languages differences must also be identified in any SQL statements in the
application code. For this sample application, no changes are required.
Filegroup Tablespace
Table A-2 shows the terminology mapping of logical objects between DB2 UDB
and MS SQL Server.
Server Server
Instance Instance
Database Database
Table A-3 shows the terminology mapping of database objects between DB2
UDB and MS SQL Server.
Schema Schema
Table Table
View View
Index Index
Table A-4 shows the terminology mapping of administration and usage between
DB2 UDB and MS SQL Server.
Table B-1 SQL Server data types mapped to DB2 data types
SQL Server DB2 UDB data type Range of values
data type
BIGINT BIGINT
FLOAT(p) FLOAT(p)
REAL REAL
SMALLMONEY NUMERIC(10,4)
MONEY NUMERIC(19,4)
B.3 SQL data types for the DB2 .NET Data Provider
Table B-3 shows the mappings between the SQL data types and the .NET
Framework data types.
Note: For Java applications connected from a DB2 UDB Version 8.x client to a
DB2 UDB Version 7.x server, when the getObject() method is used to
retrieve a BIGINT value, a java.math.BigDecimal object is returned.
Differences between SQL Server functions and DB2 UDB functions are
highlighted in bold.
If you do not intend to use MTK, you can deploy this functions manually using the
following steps:
1. Install MTK
This step is necessary because the function sources are packed with the
installation files.
2. Issue the following commands from the DB2 Command Line Processor after
connecting to the database where you want the functions to be installed:
db2 CALL SQLJ.INSTALL_JAR('C:\MTK\ms7udfs.jar' , 'myproc_jar')
db2 -td! -vf C:\MTK\mtkms7.udf
This assumes that MTK is installed in C:\MTK. The default schema of the
provided functions is MS7.
3. Test the installation with the command
db2 VALUES ms7.version()
The output should looks similar to the following:
1
--------------------------------------------------------------
SQL UDF Version: 041209.0332, Java UDF Version: 041201.0314
1 record(s) selected.
% MOD Modulus
Table C-2 Character and string function mapping for SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
ASCII ASCII Returns the ASCII code value of the left most
character of a character expression
Table C-3 Boolean function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table C-4 Date and time function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
DATEADD see 4.1, “SQL Returns a new date time value based on adding an
standard interval to the specified date
compliance”
on page 66
DATEPART see 4.1, “SQL Returns an integer representing the specified date
standard part of the specified date
compliance”
on page 66
GETDATE see 4.1, “SQL Returns the current system date and time in the
standard SQL Server standard internal format for date and
compliance” time values
on page 66
Metadata about the database itself can be obtained from the database
configuration of the database manager configuration. For more information refer
to the DB2 UDB documentation Command Reference, SC09-4828.
Table C-5 Metadata function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table C-6 Aggregate function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
SUM SUM Returns the sum of all the values, or only the
DISTINCT values, in the expression
Table C-7 System function mapping Sfrom SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table C-8 Security function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table C-9 Miscellaneous function mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Differences between SQL Server functions and DB2 UDB functions are
highlighted in bold.
Table D-1 Arithmetic operator mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
+ + Addition
- - Substraction
* * Multiplication
/ / Division
% MOD Modulus
Table D-2 Assignment operator mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table D-3 String concatenation operator mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
Table D-4 Comparison operator mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
= = Is equal to
!= != Is not equal to
Table D-5 Logical operator mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
All these functions are implemented using external Java user defined function.
Below is the Java code for the bitand() function:
import java.sql.*;
import java.lang.*;
import COM.ibm.db2.app.*;
Compile this Java code to a .class file then generate a JAR file. Copy this JAR file
into the \sqllib\function\jar directory. This is the directory where DB2 UDB
expects to find the external Java function byte code. The function must then be
registered in the database. The following command can be issued from the CLP
to register the above bitand() function in the database:
Table D-6 lists the bitwise operation functions of SQL Server mapped to the
functions provided in Appendix G, “Additional material” on page 509.
Table D-6 Bitwise operators mapping from SQL Server to DB2 UDB
SQL Server DB2 UDB Notes
| rb.bitor() Bitwise OR
List source for sp_helptext procname Use Get Source function of the
stored DB2 Stored Procedure Builder
procedures
Export a text file BCP table_name OUT filename EXPORT TO filename OF type
from a table ... SELECT ...
Load a text file BCP table_name IN filename LOAD FROM filename OF type
into a table ... INSERT INTO tablename
or
IMPORT FROM filename OF type
INSERT INTO tablename
Table F-1 SQL Server 2000 and DB2 UDB V8.2 identifier limits
Item SQL Server 2000 DB2 UDB V8.2
Table F-2 SQL Server 2000 and DB2 UDB V8.2 database limits
Item SQL Server 2000 DB2 UDB V8.2
Select the Additional materials and open the directory that corresponds with
the redbook form number, SG246672.
The publications listed in this section are considered particularly suitable for a
more detailed discussion of the topics covered in this redbook.
IBM Redbooks
For information on ordering these publications, see “How to get IBM Redbooks”
on page 515. Note that some of the documents referenced here may be available
in softcopy only.
Oracle to DB2 UDB Conversion Guide, SG24-7048
Database Transition: Informix Dynamic Server to DB2 Universal Database,
SG24-6367
MySQL to DB2 UDB Conversion Guide, SG24-7093
Other publications
These publications are also relevant as further information sources:
IBM DB2 UDB Command Reference V8, SC09-4828
IBM DB2 UDB What’s New V8, SC09-4848
IBM DB2 UDB Administration Guide: Planning V8, SC09-4822
IBM DB2 UDB Administration Guide: Implementation V8, SC09-4820
IBM DB2 UDB Administration Guide: Performance V8, SC09-4821
IBM DB2 UDB Data Movement Utilities Guide and Reference V8, SC09-4830
IBM DB2 UDB Data Recovery and High Availability Guide and Reference V8,
SC09-4831
DB2 Universal Database Federated Systems Guide, Version 8 Release 1,
GC27-1224
IBM DB2 UDB Guide to GUI Tools for Administration and Development,
SC09-4851
IBM DB2 UDB SQL Reference, Volume 1, V8, SC09-4844
IBM DB2 UDB SQL Reference, Volume 2, V8, SC09-4845
IBM DB2 UDB System Monitor Guide and Reference V8, SC09-4847
Online resources
These Web sites and URLs are also relevant as further information sources:
DB2
Database and Data Management
http://www.ibm.com/software/data/
http://www.ibm.com/software/data/highlights/db2tco.html
DB2 Universal Database
http://www.ibm.com/software/data/db2/udb/
http://www.ibm.com/db2/v8
DB2 developerWork
http://www.ibm.com/developeworks/db2/
DB2 Universal Database V8 Application Development
http://www.ibm.com/software/data/db2/udb/ad
Microsoft
Microsoft SQL Server home page
http://www.microsoft.com/sql/
Microsoft MSDN library
http://msdn.microsoft.com/sql/
A B
backup 3, 15–16, 20–21, 23–24, 36–37, 49, 54,
acceptance testing 402
57–58, 63, 127, 129, 134, 143, 263, 268, 289–290,
access methods 339, 364, 372, 391, 393
346–349, 402, 413, 467, 503
access plan 21–23, 33–35, 377, 393–394
BACKUP DATABASE 294, 467, 503
activate database 270, 345
backup image 21, 58, 268, 306, 308, 315, 329, 347
Active Directory 29
BCP 54, 170, 175, 177, 467, 503
Active Directory Helper 17
buffer pool tuning 355
ActiveX 129, 238–239, 242
buffer pools 13, 15, 70, 231, 263, 270, 279, 294,
Activity Monitor 50, 341
345, 355–357, 418
ADO 4, 88, 129, 238–240, 242
Bulk Copy Program 23, 170, 467
agents 16–17, 363, 419
Business Intelligence xxv, 1–2, 4, 45–46, 126
AIX 2, 5, 120, 139, 313
Business intelligence 4, 127
ALTER BUFFERPOOL 295, 355–356
business logic layer 260
Alter Table Space 283
ALTER TABLESPACE 19, 60, 276, 282, 284, 356
Analyze 4, 54, 127, 132, 256, 261, 383, 387–388 C
APAR 420 C 4–5, 39, 53, 120, 126, 130, 139, 153, 176, 189,
API 14–15, 27, 130, 189, 238, 249, 306, 326, 415 281, 388, 412, 423, 482
application xxiii–xxv, 1, 4–6, 12, 14, 16–17, 22, 24, C++ 189
26, 32, 34, 39–40, 46–47, 51, 63–64, 70–71, 78, 87, calculate index sizes 366
119–122, 124–132, 135, 137, 143, 149, 177, 182, CHNGPGS_THRESH 344, 356–358
185, 190, 193, 200, 208, 215, 224, 226, 266, 272, circular 289
290–291, 339–340, 344–345, 397, 399, 401–402, circular logging 20, 291–293
422, 461–462, 478, 482, 491, 503 client 21, 26–27, 40, 47, 64, 120, 129–130, 139,
application layer 260 189–190, 234, 242, 252, 320–321, 324, 326,
architecture profiling 120 373–374, 382, 409, 411, 422, 480
archival 289 clone 329
archive logging 20–21, 292, 307, 314, 347 CLP 16, 52–53, 59, 93, 129, 162, 167, 178, 190,
ARCHRETRYDELAY 312 199, 265, 267, 270, 272
assessment 9, 120, 122 CLR 4–5, 39, 189, 209
ATTACH 265, 502 clustered index 158, 163–164, 331, 365, 380
authentication 26–27, 29, 46, 250, 410, 503 COBOL 128, 130, 189
authorities 26–29, 123 Columns 4, 35, 63, 69–70, 76, 137, 158, 182, 209,
Authorized Program Analysis Reports 420 216, 218, 222, 261, 331, 335–336, 340, 364–366,
AUTOCONFIGURE 344–345 488, 494, 506
Index 519
Include option 368–369 125–126, 128, 130–131, 189–192, 251
incremental backup 20–21, 305 leaf page 370
index expansions 368 Linux xxv, 1–2, 5, 25, 62–63, 120, 131, 139, 171,
index leaf pages 331, 366, 370–371 264, 266, 342, 355, 358–359, 413, 415–416, 419,
Index placement 365 424
index rebuild 229 LIST APPLICATIONS 414, 503
index reorganization 334, 372 LIST DB DIRECTORY 502
index space estimates 366 LIST TABLES 53, 502
index tree 364, 367, 370 LIST TABLESPACE CONTAINERS 288, 502
Indexes 4, 18–19, 21–22, 28, 32, 35, 51, 62, 70, LIST TABLESPACES 53, 286, 502
117, 120–121, 125, 138, 152, 154, 158, 176, 187, LOAD 4, 23–24, 28, 32, 49, 54, 138, 146–147,
231–232, 254, 273, 275, 284, 340–341, 346, 352, 169–170, 173–174, 232, 289, 299, 354, 402,
489, 506 404–405, 467, 503
indexes and constraints 371 Load 23, 54, 146, 173–174, 330, 402, 404, 410
indexing columns 364 load stress testing 402
indexing strategies 364 Local System account 29
Information integration 3, 126–127 Lock escalation 229, 233, 359–361, 414
installation 9, 12, 17, 29, 41, 48, 99, 139–142, 187, lock escalation 233, 359–361
253, 258, 261–262, 264, 297, 340, 359, 411, 416, lock snapshot 360–361
482 LOCK TABLE 232–233
instance 4, 12–17, 24–29, 33, 38, 48, 50, 56, 69, Locking 15, 123, 158, 186–187, 226, 229–231,
77–78, 92, 121, 123–124, 187, 190, 212, 214, 330, 359–360, 410
249–250, 258, 264–267, 340, 350, 361, 363, 402, LOCKLIST 359–360
413–415, 466, 501–502 lock-placement 229
integration testing 402 log mirroring 30, 292, 327
interface 53, 124, 128–132, 142, 226, 285–286, log space 20, 290, 307, 347–348, 358, 362
326, 411, 461–462 LOGBUFSZ 362–363
Inter-partition parallelism 24 LOGFILSIZ 290–291, 293–294, 358–359
inter-query parallelism 24 logical log 290, 353, 361
Intra-partition parallelism 24 logical log buffer 353, 361
intra-query parallelism 24 logins 26
Isolation Levels 234–235, 345 LOGPRIMARY 290–291, 293–294, 358–359
Isolation Levels - changing 235 LOGRETAIN 294, 311–312
isql 52, 467, 502 LSA 29
J M
Java 2, 4, 32, 39, 64, 91, 120, 126, 128–131, 138, Managing logs 290
189, 191, 209, 238–239, 249, 255, 402, 478–479, Manual methods 125
482 MAXLOCKS 360
JDBC 2, 4, 17, 88, 130, 138, 144, 162–163, 171, MDC 4, 233, 331, 341, 379–381
238–239, 249, 478 dimensions 331, 379
extent sizes 379
syntax 379–380
K two-dimensional 379
Kilobytes 277
Memory Visualizer 3
messages files 412, 416
L metadata 45–46, 58, 91, 93, 138, 143, 182, 267,
language xxiii, 1, 4–5, 65, 68, 72, 75, 120, 122, 481, 488
Index 521
privileges 26–29, 91, 160–161, 264, 304, 410 Snapshots 94, 133, 285, 362, 417
problem determination 25, 410–411, 413, 416–417 sourced function 209
sp_addalias 503
sp_addgroup 503
Q sp_addlogin 503
query plan analysis 387
sp_adduser 503
Queue replication 43, 328
sp_changegroup 503
QUIESCE 266
sp_configure 33, 501–502
sp_dropalias 503
R sp_droplogin 503
raw device 18, 273, 466 sp_dropuser 503
Read Stability 234, 237, 345 sp_help 502
recovery 2–3, 8, 20–21, 30, 50, 62, 127, 134, 263, sp_helpdb 502
281, 288–289, 342, 345, 351–352, 467 sp_helpdevice 502
recovery model 20–21 sp_helpgroup 503
Redbooks Web site 509, 515 sp_helptext 502
Contact us xxvii sp_helpuser 503
Referential Integrity 63, 114, 169–170, 174 sp_password 503
Refine 134, 143, 145–146, 172, 433 sp_spaceused 502
REORG INDEXES 331, 372 sp_updatestats 22, 54
REORGCHK 54, 330–332, 348 sp_who 503
Repeatable Read 234, 236–238, 345 Specify Source 143, 171, 425, 429
replication 2–3, 30, 43–45, 127, 134, 170, 177, split image 315
180–181, 313, 328, 411 split mirror function 329
Reports 62, 138, 143, 145, 175, 400, 420 split mirror image 329
requirements 1, 3, 5, 70, 123, 139, 146, 186, 226, SQL xxiii, xxv, 1–2, 4–5, 7–9, 11–16, 20, 22–26,
257, 267, 296, 328, 355, 366, 373, 378, 400, 410, 28–29, 32–37, 39, 41, 45, 47, 51–52, 54, 58, 62–63,
511 65–68, 119–122, 125–126, 128–131, 133, 137–139,
resouce 122 142–144, 148–149, 151–153, 169–170, 172,
RESTORE DATABASE 311, 323, 467, 503 185–188, 226, 263, 266–267, 340–341, 351, 368,
reverse-engineer 122 402, 405–406, 408, 411, 422, 424, 465–467, 471,
Role 26, 161, 493 476, 478, 481–484, 495–498, 501, 505–506
RUNSTATS 22–23, 54, 134, 263, 308, 330, 332, SQL Agent 17
336, 346–348, 365, 371, 467 SQL function 209
runstats 23, 330, 335–336, 346–348, 377 SQL PL 96, 99–101, 138, 189–190, 192, 199, 250,
408
SQL replication 43, 181, 328
S
scalar function 209, 494 SQL Server xxiii, 7–8, 11–12, 14, 16–18, 20–24,
scripts 13, 36, 117, 119, 121, 129, 138, 143, 26–28, 30–33, 35, 40, 42–43, 45, 49, 52, 54, 58, 61,
145–146, 169, 172, 174, 252, 254, 297, 299–300, 121–122, 137, 144
403, 406, 429 SQL standards 79, 125
Sequences 69, 138 SQL Translator 143, 145, 148–149, 182, 226
SET INTEGRITY 175, 406 SQL*Plus 138
severity indicator 411 sqladhlp.exe 17
showserver 502 sqlagent.exe 17
SMPO 122 SQLCODE 89, 99, 101, 214, 222, 251, 404, 411
SMS 18–19, 70, 273–275, 351, 412, 466 sqldiag 24–25
snapshot 93, 285, 329–330, 360–363 SQLj 2, 88, 130, 239, 249, 482
SQLJ programming interfaces 4
Index 523
X
XML 1, 3–4, 6, 39, 64, 103–106, 129