SQL Server 2000 Stored Procedures XML Programming 2nd Edition Dejan Sunderic instant download
SQL Server 2000 Stored Procedures XML Programming 2nd Edition Dejan Sunderic instant download
https://ebookname.com/product/sql-server-2000-stored-procedures-
xml-programming-2nd-edition-dejan-sunderic/
https://ebookname.com/product/programming-oracle-triggers-and-
stored-procedures-3rd-edition-feng-yuan/
https://ebookname.com/product/pro-sql-server-2008-xml-1st-
edition-michael-coles-auth/
https://ebookname.com/product/professional-sql-server-2000-data-
warehousing-with-analysis-services-1st-edition-chris-graves/
https://ebookname.com/product/dealing-with-d4-deviations-1st-
edition-john-cox/
Settlement and Urbanization in Early Islamic Palestine
7th 11th Centuries Texts and Archaeology Contrasted 1st
Edition Hagit Nol
https://ebookname.com/product/settlement-and-urbanization-in-
early-islamic-palestine-7th-11th-centuries-texts-and-archaeology-
contrasted-1st-edition-hagit-nol/
https://ebookname.com/product/stress-relief-for-teachers-the-
coping-triangle-1st-edition-claire-hayes/
https://ebookname.com/product/obstetrics-evidence-based-
guidelines-1st-edition-carp-howard/
https://ebookname.com/product/patch-clamping-an-introductory-
guide-to-patch-clamp-electrophysiology-1st-edition-areles-
molleman/
https://ebookname.com/product/religious-idiom-and-the-african-
american-novel-1952-1998-1st-edition-tuire-valkeakari/
The McGraw Hill Guide Writing for College Writing for
Life 2nd edition Roen
https://ebookname.com/product/the-mcgraw-hill-guide-writing-for-
college-writing-for-life-2nd-edition-roen/
SQL Server 2000
TM
Stored Procedure
& XML Programming
Second Edition
Dejan Šunderic
McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City Milan
New Delhi San Juan Seoul Singapore Sydney Toronto
McGraw-Hill/Osborne
2100 Powell Street, 10th Floor
Emeryville, California 94608
U.S.A.
To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact
McGraw-Hill/Osborne at the above address. For information on translations or book distributors
outside the U.S.A., please see the International Contact Information page immediately following the
index of this book.
SQL Server™ 2000 Stored Procedure & XML Programming, Second Edition
Copyright © 2003 by The McGraw-Hill Companies. All rights reserved. Printed in the United States
of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be
reproduced or distributed in any form or by any means, or stored in a database or retrieval system,
without the prior written permission of publisher, with the exception that the program listings may be
entered, stored, and executed in a computer system, but they may not be reproduced for publication.
1234567890 CUS CUS 019876543
ISBN 0-07-222896-2
Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable. However, because of the possibility
of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the
accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained
from the use of such information.
Writing a book isn’t easy, but living with someone
who is writing a book can be, at times, even harder.
I would like to thank my family for their patience,
understanding, and inspiration.
Acknowledgments
I wish to thank all the people who helped to make this book a reality,
in particular:
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
v
This page intentionally left blank
Contents
Chapter 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What You Will Find in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Stored Procedure Programming Requirements . . . . . . . . . . . . . . . . . . . 5
XML Programming Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Sample Database and Other Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Sample Database Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Purpose and Design of the Sample Database . . . . . . . . . . . . . . . . . . . . 9
Database Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
vii
viii SQL Server 2000 Stored Procedure & XML Programming
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Unicode Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Date and Time Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Integer Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Approximate Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Exact Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Monetary Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Binary Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Special Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Table Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Flow-Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Statement Blocks: Begin…End . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Conditional Execution: The If Statement . . . . . . . . . . . . . . . . . . . . . . . 112
Looping: The While Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Unconditional Execution: The GoTo Statement . . . . . . . . . . . . . . . . . . . . 119
Scheduled Execution: The WaitFor Statement . . . . . . . . . . . . . . . . . . . . 121
Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Transact-SQL Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Cursor-Related Statements and Functions . . . . . . . . . . . . . . . . . . . . . . 126
Problems with Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The Justified Uses of Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
DiffGrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Using DiffGrams to Insert Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Using DiffGrams to Update Data . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Using DiffGrams to Delete Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Processing Multiple Records Using DiffGrams . . . . . . . . . . . . . . . . . . . . 634
DiffGrams Behind the Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Executing DiffGrams Programmatically Using SqlXmlCommand . . . . . . . . . . . 636
Executing DiffGrams Using URLs . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Generating DiffGrams After DataSet Change . . . . . . . . . . . . . . . . . . . . 637
Debugging DiffGrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
SQLXML BulkLoad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Executing SQLXML BulkLoad from a .NET Application . . . . . . . . . . . . . . . . 639
Error Log File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Executing BulkLoad from DTS (Using VBScript) . . . . . . . . . . . . . . . . . . . 641
Schema Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
BulkLoad Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Table Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Using SQLXML BulkLoad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Mapping Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
SOAP Messages and XML Web Services Architecture . . . . . . . . . . . . . . . . . 647
Using SQLXML to Create XML Web Services . . . . . . . . . . . . . . . . . . . . . 648
Creating .NET SOAP Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Appendix T-SQL and XML Data Types in SQL Server 2000 . . . . . . . . . . . . . . 659
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
CHAPTER
Introduction
1
IN THIS CHAPTER:
Who Should Read This Book
What You Will Find in This Book
Requirements
Sample Database and Other Resources
1
2 SQL Server 2000 Stored Procedure & XML Programming
When I began working with SQL Server, reference materials relating to the
development and deployment of stored procedures were rare and not particularly
helpful. These materials described basic concepts, but the examples presented were
often trivial and not complex enough to be applied to real-world situations in which
aspects such as error handling, debugging, naming conventions, and interfaces to other
applications are critical. As the legions of application developers and development
DBAs migrate from Microsoft Access to SQL Server, and as SQL Server becomes
the leading database for mission-critical application development, the need for more
advanced work on SQL Server stored procedures becomes even more critical.
Chapter 1: Introduction 3
Requirements
To make full use of this book, you will need access to a server running one of the
following versions of SQL Server 2000 or SQL Server 2000 (64-bit):
Although MSDE is compatible with all other versions of SQL Server 2000 and
thus makes an excellent development tool in a stand-alone environment, the absence
of administrative tools such as Enterprise Manager and Query Analyzer means that
some of the information you find in this book will not be usable right away. I recommend
that you obtain some other version (such as Developer Edition or Evaluation Edition),
or at least buy a Server/Per-Seat Client Access License (CAL) that will allow you to
use administrative tools against MSDE.
XML for SQL Server Web release (SQLXML) I recommend that you
download and install at least SQLXML 3.0, Service Pack 1.
Microsoft SOAP Toolkit Download version 2, Service Pack 2 or newer.
Microsoft XML Core Services (MSXML) Use version 4, Service Pack 1 or
newer. Earlier versions were called Microsoft XML Parser.
Internet Information Services (IIS) Use version 5 or newer.
Internet Explorer Use version 5 or newer.
Several tools for source code management and database deployment Set
of T-SQL, VBScript and .NET tools for generating, managing, and deploying
code of database objects.
Sample SQLXML code Visual Studio .NET sample projects for demonstrating
use of SQLXML managed classes.
Chapter 1: Introduction 7
The subject of the Asset sample database created for this book is an asset
management system within a fictional organization. Although the database is
based on real-world experience within financial institutions, it is also applicable
in many other environments.
The main purpose of the database is to track assets. Assets are defined as equipment,
and all variations in their content, attributes, and shape are recorded as values of
properties. The Inventory table tracks location, status, leasing information, and who
is currently using each asset. To transfer an asset from one location to another, to
assign assets to a different owner or department, to request maintenance, or to request
upgrades or new assets, users of the database use orders and order items. Activities
performed to complete the order are recorded in the charge log and interdepartment
invoices are generated. There are lookup tables used to track provinces, lease
frequencies, statuses, and other details.
1. Visit www.trigonblue.com/sqlxml
2. Click the Download Sample DB link.
3. Click the Asset sample database link to start the download. When prompted,
opt to save the file to disk. Remember the location to which you saved the file.
4. Unzip the contents of the Zip file into the Data folder of the machine on
which SQL Server is installed (usually \Program Files\Microsoft SQL
Server\MSSQL\Data).
8 SQL Server 2000 Stored Procedure & XML Programming
5. Make sure that SQL Server is running. If necessary, run SQL Server Service
Manager from Programs | MS SQL Server or use the system tray icon. If
necessary, start the SQL Server service.
Silverbell
SILVERBELL TREE
(Mohrodendron Carolinum)
Sycamore
SYCAMORE
(Platanus Occidentalis)
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com