Professional SQL Server ™ 2005 CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types Derek Comingore Douglas Hinson... Library of Congress Cataloging-in-Publ
Trang 2Professional SQL Server ™ 2005
CLR Programming with Stored Procedures, Functions, Triggers, Aggregates, and Types
Derek Comingore Douglas Hinson
Trang 3Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-470-05403-1
ISBN-10: 0-470-05403-4
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1MA/RX/RR/QW/IN
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA
01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355,
or online at http://www.wiley.com/go/permissions
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY:THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTA-TIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR
A PARTICULAR PURPOSE NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING,
OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS
A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDA-TIONS IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ
For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S at (800) 762-2974, outside the U.S at (317) 572-3993 or fax (317) 572-4002
Library of Congress Cataloging-in-Publication Data
Comingore, Derek,
1978-Professional SQL server 2005 CLR programming : with stored procedures, functions, triggers, aggregates, and types / Derek Comingore and Douglas Hinson
p cm
Includes indexes
ISBN-13: 978-0-470-05403-1 (paper/website)
ISBN-10: 0-470-05403-4 (paper/website)
1 SQL (Computer program language) 2 Database management Computer programs 3 Microsoft NET Framework I Hinson, Douglas, 1968- II Title
QA76.73.S67C655 2007
005.75’85 dc22
2006031028
Trademarks:Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade-marks or registered tradetrade-marks of John Wiley & Sons, Inc and/or its affiliates, in the United States and other countries, and may not be used without written permission SQL Server is a trademark of Microsoft Corporation in the United States and/or other countries All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book
Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available
in electronic books
Trang 4To my son, Derek Steven Comingore, and the next generation
of programmers
— Derek Comingore
To Misty, Mariah, and Kyle, for your love, support, and patience
—Douglas Hinson
Trang 5About the Authors
Derek Comingoreis an independent consultant, trainer, and speaker specializing in SQL Server and NET technologies He has recently started two ventures, an online Microsoft community for colleagues to learn from one another, located at www.RedmondSociety.com, and a second for SQL Server consulting at
www.SQLServerDBAs.com Derek holds the MCAD/MCDBA Microsoft certifications, the Carnegie Mellon Personal Software Process (PSP) credential, and an AAS/BS in Computer Information Systems from University of North Dakota Derek’s blog is located at http://RedmondSociety.com/blogs/Derek
Douglas Hinsonhas worked as an independent software and database consultant in the logistics and financial industries, with an extensive SQL Server background He has coauthored several Wrox books,
including Professional SQL Server 2005 Integration Services.
Trang 6types, user-defined (UDTs)
described, 4, 97
managed code
creating, altering, and dropping, 101
described, 97–99
Parse()method, 100
SqlUserDefinedTypeattribute, 101
ToString()method, 100
methods, required, 45–46
X and Y coordinates, accepting to store in SQL Server,
245–246
U
UDAs (user-defined aggregates)
altering, 96
creating, 96
described, 4, 93
managed code, 93–96
removing, 97
SqlUserDefinedAggregateattribute (Format), 96
UDF (user-defined function)
adding, 34–35
COM data type, handling, 197–199
forward-only, read-only row navigation example, 161
method name, 38
ParseRowfunction, adding to, 251
processing
forward-only, read-only row navigation example, 161
as supported object, 4
SQL CLR, 32–33
stored procedures versus, 26–27
UDTs (user-defined types)
described, 4, 97
managed code
creating, altering, and dropping, 101
described, 97–99
Parse()method, 100
SqlUserDefinedTypeattribute, 101
ToString()method, 100
methods, required, 45–46
X and Y coordinates, accepting to store in SQL Server,
245–246
unrecoverable exceptions, 257
unsafe assemblies, testing BCL, 183
unsafe permission level, 296
UNSAFEpermission set
assemblies
backing up, 321
blocking from server, 308–309
described, 9
listing, 190
OLE assembly, marking, 197
unwound calling stack, 268–269
UPDATEstatements
forward-only, read-only row navigation example, 154
logic dependent on results of, 106
updated columns, information about, 86
updating data or schema, T-SQL definition, 27
URL, Yahoo! stock retrieval service, 191–193 user errors, 257
User Interfacepermission, 296
user-defined aggregates See UDAs user-defined function See UDF user-defined types See UDTs users, controlling access See permissions
usingstatement, 35
V
values name, reading registry key, 180 NULL
data type classes, 46–48 database testing, 177–178, 181–182 syntax, 104
T-SQL, 122–123 UDFs, 199 type CTS framework, 4–5 reference type versus, 44 variable
cursors, designing around, 346–347 declaration and scope, 104–106 scoping within error-catching blocks, 270
Vbfiles, 320 VB.NET barrier of entry, perspective, 12 compiling NET DLL, 24–25 files, supported, 320 Vbprojfiles, 320 verifying
file system changes, 206 Web services, 222 VES execution environment for CIL code, 5 view, system
CPU usage, 324 creating with SQL Server metadata, 304 stored information, 29
sys.assemblies, 29–30 T-SQL performance, 161 Visual Studio
CLR routine, creating auto-deployment process, 37 creating class, 34–35 deploying class, 36–37 described, 33–34 conventions assemblies, 38 class and structure functions or methods, 38–39 classes, 38
structures, 38 debugging with SQL Server Profiler, 328–329 deployment, CLR Security, 318–319 trigger, generated T-SQL, 91 Visual Studio 2005 SQL CLR support hardware, 14–15
memory, 14
408
types, user-defined (UDTs)
Trang 7Web Accesspermission, 296 Web forms, 246–248 Web services, consuming calling and passing to T-SQL procedure, 218–220 described, 213
design, 214 procedure code to implement, 217–218 publishing to location where can be found and consumed, 215–216
reference, adding and naming, 216–217 review, 214
scheduling, 223 serialization assembly, 221 testing, 220, 221–222 verifying, 222 weekends, 354, 377–378 WHERE clause, 252–253 Windows (Microsoft), 14 WinForms and SQL CLR UDTs (user-defined types) accepting WinForm input to save, 245–246 point UDT, creating, 241–242
Windows form to display, 242–245 WITH PERMISSION SETargument, 296–297
worms, detecting See server processes, auditing
X
X and Y coordinates, accepting to store in SQL Server, 245–246
X509 Storepermission, 296
content, INSERT or UPDATE, 208–210 design, 207
DOM, 290 existence, discerning, 208 problem, 207
submission, 207 testing solution, 210–212 validating, 212–213 validation, importance of, 207 xmsp_regreadprocedure, 181–182 xp_fileexist, 175
xp_regreadextended procedure, 179 XPs (Extended Stored Procedures) console applications, 238–240 described, 2
SQL CLR versus, 10
Y
Y and X coordinates, accepting to store in SQL Server, 245–246
Yahoo! stock retrieval service, 191–193
Z
zero, divide-by-error design-time checking, 274 NET structured error-handling, 269 SQL CLR error-handling, 270–271
409