Programming Microsoft SQL Server 2000 with Microsoft Visual Basic .NET [1st edition] 0735615357, 9780735615359

Find out the fastest ways to transform data into potent business solutions with this definitive guide for developers. Yo

367 63 8MB

English Pages 530 Year 2002

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Programming Microsoft SQL Server 2000 with Microsoft Visual Basic .NET [1st edition]
 0735615357, 9780735615359

  • Commentary
  • yes
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Program m ing Microsoft ® SQL Server™ 2000 wit h Micr osoft Visual Basic® .NET Foreword Acknow ledgm ent s I nt roduct ion Who’s t he Book For? What ’s Special About This Book? How’s t he Book Organized? Syst em Requirem ent s Sam ple Files Support 1. Get t ing St art ed w it h Visual Basic .NET for SQL Serv er 2000 Visual St udio .NET, t he Visual Basic .NET I DE An Overv iew of ADO.NET Capabilit ies A St art er ADO.NET Sam ple Using Query Analyzer 2. Tables and Dat a Types Chapt er Resources Dat a Types for Tables Scr ipt ing Tables 3. Pr ogram m ing Dat a Access wit h T- SQL I nt r oduct ion t o Dat a Access wit h T- SQL Aggregat ing and Grouping Rows Processing Dat es Joins and Subquer ies 4. Pr ogram m ing Views and St ored Procedures I nt r oduct ion t o Views Creat ing and Using Views Views for Rem ot e and Het erogeneous Sources I nt r oduct ion t o St ored Procedur es Creat ing and Using St or ed Procedures Processing St or ed Procedur e Out put s I nsert ing, Updat ing, and Delet ing Rows Program m ing Condit ional Result Set s 5. Pr ogram m ing User - Defined Funct ions and Tr iggers I nt r oduct ion t o User - Defined Funct ions Creat ing and I nv ok ing Scalar UDFs Creat ing and I nv ok ing Table- Valued UDFs I nt r oduct ion t o Tr iggers Creat ing and Managing Tr iggers 6. SQL Serv er 2000 XML Funct ionalit y Overv iew of XML Support XML Form at s and Schem as URL Access t o SQL Serv er Tem plat e Access t o SQL Server 7. SQL Serv er 2000 Secur it y

Overv iew of SQL Serv er Secur it y I nt r oduct ion t o Special Securit y I ssues Sam ples for Logins and Users Sam ples for Assigning Perm issions 8. Overv iew of t he .NET Fram ework An I nt r oduct ion t o t he .NET Fram ework An Overv iew of ASP.NET XML Web Ser v ices 9. Cr eat ing Windows Applicat ions Get t ing St art ed w it h Windows For m s Creat ing and Using Class References I nher it ing Classes Program m ing Event s Except ion Handling for Run- Tim e Err ors 10. Progr am m ing Windows Solut ions w it h ADO. NET An Overv iew of ADO.NET Obj ect s Making Connect ions Wor k ing w it h Com m and and Dat aReader Obj ect s Dat aAdapt ers , Dat a Set s, Form s, and Form Cont rols Modify ing, I nsert ing, and Delet ing Rows 11. Progr am m ing ASP.NET Solut ions Rev iew of ASP.NET Design I ssues Creat ing and Running ASP.NET Solut ions Session St at e Managem ent Dat a on Web Pages Validat ing t he Dat a on a Web Page 12. Managing XML wit h Visual Basic .NET SQL Ser ver Web Releases Overv iew of XML Technologies Generat ing XML Docum ent s wit h t he .NET Fram ework Dy nam ically Set t ing an XML Result Set The I nt erplay Bet w een XML and Dat a Set s Creat ing HTML Pages wit h XSLT 13. Cr eat ing Solut ions wit h XML Web Ser v ices Overv iew of Web ser v ices A Web Serv ice t o Ret ur n a Com put ed Result A Web Serv ice t o Ret ur n Values from Tables The SQL Ser ver 2000 Web Ser v ices Toolk it Mor e on Populat ing Cont rols wit h Web Ser vices About t he Aut hor

For e w or d During m y five years at Micr osoft , I ’v e been helping developers underst and t echnologies such as Microsoft Visual St udio, Microsoft SQL Ser ver, and Micr osoft Office Developer. Dur ing t he past t wo years, I hav e w orked on t he Microsoft Office XP Visual Basic Language Refer ence, and now, t he MSDN Office Dev eloper Cent er. I n t he m ont hly colum n on MSDN, Office Talk, I have wr it t en art icles t o help Office developers underst and t he .NET plat for m and how it affect s t heir curr ent and fut ure developm ent effort s. As I wr it e t his for ew ord t o Rick Dobson’s book on program m ing Microsoft SQL Ser ver solut ions wit h Microsoft Visual Basic .NET, I t hink back t o m y own experiences dev eloping soft war e applicat ions w it h Visual Basic. My first experience wit h Visual Basic was lear ning t he language using v ersion 3.0. I rem em ber pick ing up m y first Visual Basic beginner’s book and being excit ed as I dev eloped m y first few “ Hello, Wor ld” applicat ions. I couldn’t believ e how quick and easy it was t o dev elop soft ware applicat ions t hat operat ed sim ilar ly t o ot her popular sharewar e program s of t hat t im e. Howev er, dur ing t hat t im e I also discov er ed som e of t he short com ings of Visual Basic as an ent erpr ise- lev el developm ent language. I t was t hen t hat I t ur ned m y at t ent ion t o C+ + . I r em em ber being v ery frust rat ed at t r y ing t o lear n t he language, t ry ing t o underst and concept s such as point ers, m em ory allocat ion, and t rue obj ect - or ient ed program m ing. I t ook classes on C+ + at t he local universit y , but I got even m or e fr ust rat ed hav ing t o wait m ont hs unt il I was t aught how t o cr eat e t he sim plest Micr osoft Windows form , som et hing I did in j ust a couple of m inut es using Visual Basic. I n m y frust rat ion, I gave up t ry ing t o lear n C+ + and hav e been using Visual Basic t o dev elop soft ware applicat ions ever since. As each new v ersion of Visual Basic was r eleased, I readied m yself t o learn new soft war e developm ent t echnologies. First it was Act iveX cont rol developm ent . Then it was calling t he Windows API . Next it was DHTML Applicat ions. Then it was dat abase dev elopm ent using Micr osoft SQL Ser ver. I t alw ays seem ed as t hough I had t o lear n a new language and a new developm ent paradigm for ev er y new t echnology t hat cam e along. I kept t hink ing t hat t her e had t o be an easier and m or e unified appr oach. Well, now w e’ve r eached t he adv ent of t he Micr osoft .NET plat form , and wit h it , a revolut ion in t he Visual Basic language, Microsoft Visual Basic .NET. I believ e t hat Visual Basic .NET will pr ov ide soft ware dev elopers wit h new opport unit ies for quickly and easily designing int egrat ed soft war e applicat ions t hat connect businesses and indiv iduals anyt im e, anyw her e, and on v irt ually any soft ware dev ice. Wit h advances in t he Visual Basic .NET language, Visual Basic . NET dev elopers will finally be on a par w it h t heir C+ + and C# count erpart s, part icipat ing in m any high- end dev elopm ent pr oj ect s. Wit h Visual St udio .NET feat ur es such as cross- language debugging, along w it h Visual Basic .NET conform ance t o t he com m on t ype syst em and t he com m on language r unt im e, organizat ions can drive down t heir developm ent cost s by t apping int o t he w ide range of sk ills t hat Visual Basic .NET dev eloper s now possess. Tr ue obj ect - orient ed pr ogram m ing is now available in Visual Basic .NET, including feat ur es such as inher it ance and m et hod ov er loading. I t ’s now sim pler t o call t he Windows API by using t he .NET Fram ew or k Class Libraries. Web applicat ion dev elopm ent is now as easy as dev eloping Windows form s–based applicat ions. Dat abase applicat ion developm ent is m ade easier by unit ing disparat e dat a obj ect libraries such as DAO, RDO, OLE DB, and ADO under ADO. NET, ut ilizing t he power of XML t o consum e and t ransm it r elat ional dat a ov er com put er net w orks. And a new t echnology, XML Web serv ices, allows Visual Basic .NET developers t o host t heir soft ware applicat ions’ logic ov er t he Web. Addit ionally , a big issue for

soft war e developers t oday is t hat of soft war e applicat ion deploym ent and versioning. I f y ou don’t agree, j ust ask any soft ware dev eloper about “ DLL hell,” and y ou’re bound t o get an earful. For m any .NET applicat ions, t he .NET plat form feat ur es “ copy and past e” or XCOPY deploym ent . ( Users sim ply copy y our applicat ion files from t he source m edia t o any single direct ory and r un t he applicat ion. ) And because .NET no longer r elies on t he r egist ry , virt ually all DLL com pat ibilit y issues go away. Wit h t his book, Rick aim s t o give you t he sk ills y ou need t o pr ogram SQL Serv er solut ions wit h Visual Basic .NET. I k now y ou w ill find Rick’s book helpful. Rick brings his exper ience t o bear from t hree prev ious books: Pr ogram m ing Micr osoft Access Version 2002 ( Microsoft Press, 2001) , Pr ogram m ing Micr osoft Access 2000 ( Micr osoft Press, 1999) , and Pr ofessional SQL Serv er Developm ent wit h Access 2000 ( Wr ox Pr ess I nc., 2000) . Rick also br ings his exper ience of leading a successful nat ionwide sem inar t our. Mor e im port ant , I know y ou w ill enj oy Rick ’s book because of his deep int erest in Visual Basic .NET and SQL Ser ver, and in helping y ou, t he pr ofessional dev eloper, underst and and apply t hese t echnologies in y our daily soft ware applicat ion dev elopm ent proj ect s. Paul Cornell MSDN Office Dev eloper Cent er ht t p: / / m sdn.m icr osoft .com / officeMicrosoft Corporat ion February 2002

Ack now le dgm e nt s This sect ion offers m e a chance t o say t hank you t o all w ho helped m ak e t his book possible. I wish t o offer special recognit ion t o five support r esources. First , t he folks at Microsoft Press have been fant ast ic. Dave Clar k, an acquisit ions edit or , select ed m e t o w rit e t he book j ust m ont hs aft er I com plet ed anot her book for Microsoft Press. Dick Br ow n, m y pr oj ect edit or, st aunchly st ood up for his percept ion of how t o m ake t he book ’s organizat ion and cont ent clear t o y ou wit hout being pet t y or bor ing t o m e. Dick also light ened m y load subst ant ially by show ing a real k nack for edit ing m y t ext w it hout dist ort ing t he or iginal int ent . When Dick was especially busy, he handed off som e of his load t o Jean Ross, who also did an adm irable j ob. Ot hers at Micr osoft Press who cont r ibut ed t o m y w ellbeing in one way or anot her include Aar on Lavin and Anne Ham ilt on. Second, I had excellent wor k ing r elat ions wit h sev eral professionals w it hin Micr osoft . Paul Cor nell, a widely k now n t echnical edit or at Micr osoft , w as kind enough t o share his insight s on how t o pr esent .NET concept s com pellingly. I want t o t hank Paul especially for writ ing t he For eword t o t his book. Kart hik Rav indran serv ed as t he MSXML Bet a Pr oduct Lead Engineer at Microsoft Pr oduct Support Serv ices dur ing t he t im e t hat I wrot e t his book. He provided valuable t echnical cont ent about t he SQL Serv er 2000 Web releases. Ot her Microsoft represent at iv es pr ov iding m oral and t echnical support for t his book include Richard Waym ir e and Jan Shanahan. Third, I want t o express m y appreciat ion t o t he m any r eaders, sem inar at t endees, and sit e v isit ors w ho t ook t he t im e t o t ell m e what I did right or wrong for t hem , and also t o t hose w ho shared t heir t echnical support quest ions wit h m e. I t is t hr ough t his k ind of feedback t hat I am able t o k now w hat ’s im port ant t o pract icing developers. I encourage y ou t o visit m y m ain Web sit e ( ht t p: / / www .pr ogram m ingm saccess.com ) and sign t he guest book. The ent ry for m includes space for you t o leave your evaluat ion of t his book or y our quest ion about a t opic cov er ed in t he book . I prom ise t o do m y best t o r eply per sonally. I n any ev ent , I definit ely r ead all m essages and use t hem so t hat I can serv e y ou bet t er w it h fut ur e edit ions of t his, and ot her, books. Fourt h, I want t o t ell t he w orld how grat eful I am t o m y w ife, Virginia. Wit hout Virginia’s warm support , lov e, and care, t his book w ould be less professional. She reliev es m e of near ly ev er y r esponsibilit y around t he house w hen I undert ak e a book proj ect . I n addit ion, she offers st rat egic advice on t he issues t o address and t heir st yle of cov erage. When I r un out of t im e, she ev en pit ches in w it h t he proofreading. Fift h, it is im port ant for m e t o give praise and glory t o m y Lord and Sav ior, Jesus Chr ist , w ho I believe gave m e t he st rengt h and wisdom t o w r it e t his book. I n addit ion, He gav e m e healt h dur ing t he long gest at ion per iod t hat result ed in t he birt h of t his book . I t is m y prayer t hat t he book pr ov e t o be a blessing t o y ou.

I nt r odu ct ion Any one w ho buys a book —or considers buy ing it —want s t o k now w ho t he book is for , w hat set s it apart fr om ot hers lik e it , and how t he book is organized. This int r oduct ion cov ers t hose t hr ee quest ions, and it also discusses syst em requir em ent s, sam ple files, and support . •





First , w h o is t h e b ook for ? Ther e ar e at least t wo answers t o t his quest ion. One answ er is t hat t he book t arget s professional dev elopers ( and ot hers aspir ing t o be pr ofessional dev elopers) . The second group t he book addr esses is t hose who want t o build full- feat ured, secure SQL Serv er solut ions wit h Visual Basic .NET. Se con d, w h a t ’s sp ecia l a bou t t h e b ook ? I hope you com e t o believ e t hat t he m ost im port ant answer t o t his quest ion is t hat t he book consider ed qualit y and dept h of coverage m or e im port ant t han r ushing t o m arket . The book w ill arr iv e on bookshelv es m or e t han t hr ee m ont hs aft er t he official release of t he . NET Fram ew ork. I t is m y w ish t hat you der iv e value from t he ext ra t im e t ak en t o develop t he m any code sam ples and t he in- dept h discussions of advanced t opics, such as class inher it ance, ASP.NET, and XML Web serv ices. Th ird , h ow is t h e b ook org an iz e d? The short answer is t hat t her e ar e t wo m ain sect ions. One sect ion int roduces SQL Ser ver concept s as it dem onst rat es T- SQL ( Tr ansact SQL) pr ogram m ing t echniques. Aft er conv ey ing SQL Ser ver basic building blocks in t he first part , t he second part rev eals how t o put t hose part s t oget her w it h Visual Basic .NET and relat ed t echnologies int o SQL Ser ver solut ions for handling com m on dat abase chores.

The t hr ee support it em s include a br ief descr ipt ion of t he book’s com panion CD and how t o use it , Micr osoft Pr ess Support I nfor m at ion for t his book, and a sum m ary of syst em and soft war e requirem ent s for t he sam ple code pr esent ed in t he book.

W ho’s t h e Book For ? This book t arget s pr ofessional Visual Basic and Visual Basic for Applicat ions dev elopers. From m y sem inar t ours and Web sit es ( ht t p: / / www .pr ogram m ingm saccess.com and ht t p: / / www.cabinc.net ) , I know t hat t hese professionals ar e dr iven by a passion t o deliv er solut ions t o t heir client s t hrough applying t he m ost innov at iv e t echnologies t heir client s w ill accept . I n- house dev elopers are t he go- t o persons for get t ing result s fast —part icular ly for cust om in- house sy st em s and dat abases. I ndependent developers specialize in serv ing niche sit uat ions t hat can include under - ser ved business needs and w or k ov erflows. I n bot h cases, t hese pr ofessionals need t raining m at erials t hat addr ess pract ical business requir em ent s w hile showcasing innov at ive t echnologies wit hout wast ing t heir t im e. This book st rives t o ser ve t his broad need in t w o specific areas. This book is for developers look ing for code sam ples and st ep- by - st ep inst ruct ions for building SQL Ser ver 2000 solut ions wit h Visual Basic .NET. The book focuses on t he int egrat ion of SQL Serv er 2000 w it h .NET t echnologies t apped v ia Visual Basic .NET. I t is m y fir m belief t hat y ou cannot creat e gr eat SQL Ser v er solut ions in any program m ing language w it hout k nowing SQL Server. Therefore, t his book

goes beyond t radit ional coverage of SQL Serv er for Visual Basic dev elopers. You’ll lear n T- SQL pr ogram m ing t echniques for dat a access, dat a m anipulat ion, and dat a definit ion. A whole chapt er equips you t o secur e your SQL Ser ver solut ions. I n addit ion, t her e’s plent y of cont ent in t his book on Visual Basic .NET and relat ed t echnologies, such as ADO.NET, ASP.NET, XML ( Ex t ensible Mark up Language) , and XML Web serv ices. The present at ion of t hese t echnologies dem onst rat es coding t echniques and ex plor es concept s t hat equip y ou t o build bet t er solut ions wit h SQL Ser ver 2000 dat abases. I n addit ion, t he book highlight s innovat ions int r oduced t hrough t he Web releases for SQL Serv er 2000 t hat int egrat e SQL Ser ver 2000 t ight ly w it h Visual Basic .NET. This isn’t a book about XML, but t hr ee of t he book ’s 13 chapt ers focus in w hole or in part on XML. Therefore, t hose seek ing pract ical dem onst rat ions of how t o use XML w it h SQL Ser ver and Visual Basic .NET w ill deriv e value fr om t his book . I f you hav e look ed at any of t he com put er m agazines ov er t he past couple of y ears, you k now t hat XML is com ing t o a solut ion near you. However, t he rapid pace of XML innovat ion m ay have dissuaded som e fr om j um ping on t he bandwagon w hile t hey wait t o see w hat ’s going t o last and what ’s j ust a fad. I n t he book’s t hr ee chapt ers on XML t echnology, you’ll learn about XML docum ent s, fragm ent s, and for m at t ing as w ell as r elat ed t echnologies, such as XPat h ( XML Pat h Language) quer ies, XSLT ( Ext ensible St y lesheet Language Transform at ion) , and WSDL ( Web Ser vices Descript ion Language) .

W ha t ’s Spe cia l Abou t Th is Book ? Ther e ar e sev eral feat ur es t hat m ake t his book st and apart from t he flood of books on .NET. One of t he m ost im port ant of t hese is t hat t his book didn’t rush t o m ark et but rat her shipped m ont hs aft er t he r elease of t he .NET Fram ew ork . This allowed m e enough t im e t o filt er, exam ine, and uncov er w hat w er e t he m ost useful and innovat iv e feat ur es for Visual Basic .NET developers building SQL Ser ver solut ions. For ex am ple, t he book includes a whole chapt er on creat ing solut ions wit h XML Web serv ices. That chapt er includes t wo m aj or sect ions on t he SQL Ser ver 2000 Web Serv ices Toolk it , w hich didn’t ship unt il t he day of t he .NET Fram ew ork release. The .NET Fram ew ork cont ent is at a professional lev el, but it isn’t j ust for t echies. This book doesn’t assum e any pr ior know ledge of t he .NET Fr am ew ork . I t does assum e t hat you get paid for building solut ions program m at ically and t hat at least som e of t hose solut ions are for SQL Serv er dat abases. Ther efor e, t he book explains basic .NET concept s and dem onst rat es how t o achiev e pract ical result s wit h t hose concept s t hr ough a huge collect ion of .NET code sam ples. This book is about building solut ions for SQL Ser ver 2000. I include coverage of t he m any special feat ur es t hat t ie Visual Basic .NET and SQL Serv er 2000 closely t o one anot her. Alt hough t here is coverage of general .NET dat abase t echniques, t his book div es deeply int o T- SQL pr ogr am m ing t echniques so t hat y ou can creat e your own cust om dat abase obj ect s, such as t ables, st or ed pr ocedur es, views, t riggers, and user - defined funct ions. I n addit ion, t her e is separat e cov erage of t he XML feat ures released w it h SQL Serv er 2000 as well as separat e coverage of t he XML feat ures in t he first t hr ee Web releases t hat shipped for SQL Ser ver 2000. There ar e num er ous code sam ples t hroughout t he book. These will equip you t o build solut ions w it h Visual Basic .NET, T- SQL, and com binat ions of t he t wo. Finally, t his book is special because of t he unique exper iences of it s aut hor, Rick Dobson. I have t rained professional dev elopers in Aust ralia, England, Canada, and t hr oughout t he Unit ed St at es. This is m y fourt h book in four years, and you can find m y art icles in popular publicat ions and Web sit es, such as SQL Serv er Magazine and MSDN Online. As a Webm ast er , m y m ain sit e

( ht t p: / / www .pr ogram m ingm saccess.com ) serv es hundr eds of t housands of sessions t o dev elopers each y ear. I const ant ly ex am ine t heir v iewing habit s at t he sit e t o det erm ine w hat int erest s t hem . I n addit ion, m y sit e feat ures scores of answers t o t echnical support quest ions subm it t ed by professional developers. My goal in offer ing answers t o t hese quest ions is t o st ay in t ouch w it h pract icing dev elopers wor ldw ide so t hat m y new book s address t he needs of pract icing, professional dev elopers.

H ow ’s t h e Book Or ga n ized? Ther e ar e t wo m ain part s t o t his book t ied t oget her by an int roduct ory part . Part I I , t he first m ain part , dwells on SQL Ser ver t echniques. Part I I I builds on t he SQL Ser ver background as it lays a firm foundat ion in .NET t echniques for Visual Basic .NET dev elopers. Part I , t he int r oduct ory part , dem onst rat es way s t o use SQL Ser ver and Visual Basic .NET t oget her.

Pa r t I , I nt r odu ct ion Part I , w hich includes only Chapt er 1, has t hree m ain goals. First , it acquaint s y ou wit h t he basics of Visual Basic .NET w it hin Visual St udio .NET. You can t hink of Visual Basic .NET as a m aj or upgrade t o t he Visual Basic 5 or 6 t hat you are probably using cur rent ly . This first sect ion int r oduces som e concept s t hat you w ill find useful as y ou init ially learn t he landscape of Visual Basic .NET. The second goal of Chapt er 1 is t o int r oduce ADO.NET. I f y ou t hink of Visual Basic .NET as a m aj or upgrade t o Visual Basic 6, ADO. NET is m or e lik e a m aj or overhaul of ADO. I n t wo sect ions, you get an int roduct ion t o ADO.NET classes—part icular ly as t hey relat e t o SQL Serv er—and y ou get a chance t o see a couple of beginner sam ples of how t o creat e SQL Serv er solut ions w it h Visual Basic .NET and ADO.NET. The t hird goal of t he int roduct ory part is t o ex pose y ou t o Query Analy zer . This is a SQL Ser ver client t ool t hat ships w it h all com m ercial edit ions of SQL Ser ver 2000. You can t hink of it as an I DE for T- SQL code. Most of t he book’s first par t relies heav ily on T- SQL, and t her efor e hav ing a conv enient env ir onm ent for debugging and running T- SQL code is helpful. The final sect ion of Chapt er 1 addresses t his goal.

Pa r t I I , SQL Se r v e r Part I I consist s of six r elat iv ely short chapt ers t hat focus subst ant ially on program m ing SQL Serv er 2000 w it h T- SQL. Chapt er 2 and Chapt er 3 int r oduce TSQL and SQL Serv er dat a t ypes. I f y ou ar e going t o program SQL Ser ver and creat e efficient , fast solut ions, y ou m ust lear n SQL Ser ver dat a t ypes, which is one of t he m ain point s conveyed by Chapt er 2. Many r eaders w ill grav it at e t o Chapt er 3 because it int roduces cor e T- SQL pr ogram m ing t echniques for dat a access. You’ll apply t he t echniques covered in t his chapt er oft en as y ou select subset s of r ows and colum ns in dat a sources, group and aggregat e row s from a t able, pr ocess dat es, and j oin dat a fr om t w o or m or e t ables. Chapt er 3 also considers special dat a access t opics, such as out er j oins, self j oins and subquer ies. The next pair of chapt er s in Part I I , Chapt er 4 and Chapt er 5, t ak e a look at program m ing dat abase obj ect s t hat you w ill use for dat a access and dat a m anipulat ion, such as v iews, st ored procedures, user - defined funct ions, and t riggers. These dat abase obj ect s are im port ant for m any reasons, but one of t he m ost im port ant is t hat t hey bundle T- SQL st at em ent s for t heir easy r euse. I t is

widely k now n t hat t he best code is t he code t hat you don’t hav e t o wr it e. Howev er, if y ou do have t o w rit e code, y ou should definit ely wr it e it j ust onc, and t hen reuse it w henev er you need it s funct ionalit y. St or ed pr ocedur es are part icular ly desirable dat abase obj ect s because t hey save com piled T- SQL st at em ent s t hat can deliv er significant speed advant ages ov er r esubm it t ing t he sam e T- SQL st at em ent for com pilat ion each t im e y ou want t o perform a dat a access or dat a m anipulat ion t ask. Chapt er 4 and Chapt er 5 ar e also im port ant because t hey conv ey T- SQL sy nt ax for using param et ers and condit ional logic t hat support dy nam ic r un- t im e behav ior and user int eract iv it y. One of t he m ost im port ant feat ur es of SQL Ser ver 2000 is it s XML funct ionalit y . Because XML as a t opic is changing so rapidly, Micr osoft adopt ed a st rat egy of upgrading t he SQL Serv er 2000 XML funct ionalit y t hrough Web releases. Alt hough t hose w it h SQL Serv er 2000 can dow nload t he Web releases w it hout charge from t he Microsoft Web sit e, t he Web r eleases ar e fully support ed. Chapt er 6 int r oduces core XML funct ionalit y int roduced w it h SQL Ser ver 2000 as w ell as funct ionalit y fr om t he fir st t wo Web r eleases. I n part icular, y ou can learn in t his chapt er about I I S v irt ual direct ories as well as form at s for XML docum ent s and schem as. You also learn about t em plat es in virt ual dir ect or ies t hat facilit at e dat a access and dat a m anipulat ion t asks over t he Web. Chapt er 7 closes out t he SQL Ser ver part of t he book w it h an in- dept h look at program m ing SQL Serv er secur it y . I n t hese t im es, secur it y has grown int o a m onum ent al t opic, and t his chapt er can k eep y ou out of t rouble by blocking hackers from get t ing int o or corr upt ing y our dat abase. You learn such t opics as how t o creat e and m anage differ ent t y pes of login and user account s and how t o cont r ol t he perm issions available t o indiv idual account s as well as gr oups of account s. By learning how t o script account s and perm issions w it h T- SQL, y ou sim plify r ev ising and updat ing secur it y as condit ions change ( for exam ple, w hen users leav e t he com pany or w hen new , sensit iv e dat a get s added t o a t able) .

Pa r t I I I , .N ET Chapt er 8 st art s t he .NET part of t he book w it h a r ev iew of select ed .NET t opics t hat ar e cover ed in t he init ial look Chapt er 1 offer ed at t he .NET Fram ewor k. This chapt er pr ov ides an ov erv iew of t he archit ect ur e for .NET solut ions, and it dr ills down on t wo t opics: ASP.NET and XML Web ser vices. The general purpose of t his chapt er is t he sam e as Chapt er 1, w hich is t o int r oduce concept s. The em phasis in Chapt er 8 isn’t how y ou do som et hing, but rat her w hat are t he m aj or t echnologies enabling y ou t o do som et hing. Chapt er 1 and Chapt er 8 are bot h relat iv ely short chapt ers, but you m ay find t hem invaluable if y ou are t he k ind of person w ho benefit s from high- lev el ov er views of a collect ion of t opics. Chapt er 9 st art s wit h a close exam inat ion of how t o use Windows Form s wit h Visual Basic .NET. I t t hen shift s it s focus t o a review of t radit ional class processing concept s via Visual Basic .NET as an int r oduct ion t o class inherit ance, a new obj ect - or ient ed feat ure t hat m ak es it s fir st appearance in Visual Basic w it h Visual Basic .NET. Next t he t r eat m ent of classes progr esses t o t he handling of built - in ev ent s as w ell as t he raising of cust om ev ent s. Finally t he chapt er closes wit h an exam inat ion of t he new except ion handling t echniques for pr ocessing r unt im e errors. Chapt er 10 is a how - t o guide for solut ions t o t y pical pr oblem s w it h ADO.NET. Befor e launching int o it s progr ession of sam ples show ing how t o perform all k inds of t asks, t he chapt er st art s wit h an ov er v iew of t he ADO.NET obj ect m odel t hat covers t he m ain obj ect s along w it h select ed propert ies and m et hods for each obj ect . The how - t o guide focuses on dat a access t asks, such as select ing rows and colum ns fr om SQL Ser ver dat abase obj ect s, as well as dat a m anipulat ion t asks, such as insert ing, updat ing, and delet ing rows in a t able. Work ing t hr ough

t he sam ples in t he how - t o guide offers a hands- on feel for using t he Syst em .Dat a.SqlClient nam espace elem ent s t o per form t ypical t asks. Chapt er 11 swit ches t he focus t o t he Web by addressing t he cr eat ion and use of ASP.NET solut ions. This chapt er st art s by int r oducing basic elem ent s t hat you need t o k now in order t o use ASP.NET t o cr eat e great Web solut ions wit h Visual Basic .NET. These include lear ning w hat happens as a page does a r ound- t r ip from a browser t o a Web serv er and back t o t he br owser—part icularly for dat a associat ed wit h t he page. Ot her pr elim inary t opics t hat equip y ou for building professional Web solut ions include running t he sam e page in m ult iple br owser t ypes and sniffing t he browser for cases in w hich y ou want t o send a page opt im ized for a specific kind of browser t ype. Managem ent of session st at e is a m aj or t opic in t he chapt er , and y ou lear n how t o use enhancem ent s t o Session variables for Web farm s as well as t he new v iew st at e var iables, a non- ser ver based t echnique for m anaging st at e in ASP.NET solut ions. The last t wo sect ions in t he chapt er deal w it h ADO.NET t opics in ASP.NET solut ions and t he new aut om at ic dat a validat ion feat ur es built r ight int o ASP.NET. The last t wo chapt ers in t he book explor e how XML int erplays wit h Visual St udio .NET and SQL Ser ver 2000. For exam ple, Chapt er 12 exam ines special t ools in Visual St udio .NET t o facilit at e t he design and edit ing of XML docum ent s and schem as. I n addit ion, y ou learn how t o designat e XPat h queries t hat accept runt im e input for r et ur ning SQL Ser ver result set s inside Visual Basic .NET program s. The chapt er dem onst rat es t echniques for processing t he XML docum ent associat ed wit h all ADO. NET dat a set obj ect s. I n t he chapt er’s last sect ion, I present a couple of code sam ples t hat illust rat e how t o pr ogram st at ic HTML pages based on XML docum ent s wit h XSLT. Ch a pt e r 1 3 dr ills dow n on XML Web serv ices by dem onst rat ing several differ ent approaches for cr eat ing Web serv ices as well as consum ing XML out put from Web serv ices. Web serv ices behave som ewhat like COM obj ect s in t hat y ou can set up serv er applicat ions for client applicat ions. The ser ver applicat ions expose m et hods t o w hich t he client applicat ions can pass param et ers. XML com es int o play wit h Web serv ices in a couple of areas. First , Web ser vices repr esent t heir input s and out put s v ia WSDL, an XML- based language t hat form ally describes an XML Web serv ice. Second, Web ser vices ret urn dat a t o t heir client s as XML docum ent s or docum ent fragm ent s.

Syst e m Re qu ir e m en t s The requir em ent s for t his book var y by chapt er. I developed and t est ed all sam ples t hroughout t his book on a com put er equipped wit h Windows 2000 Ser ver, SQL Serv er Ent erprise Edit ion, and t he Ent erprise Dev eloper Edit ion of Visual St udio .NET, w hich includes Visual Basic .NET. To use t his book, y ou’ll need t o have Visual Basic .NET or Visual St udio .NET inst alled on your com put er . ( See Chapt er 1 for m or e inform at ion on v ersions of Visual Basic .NET and Visual St udio .NET.) I n addit ion, y ou’ll need SQL Serv er 2000, and for som e of t he chapt ers, you’ll need SQL Serv er 2000 updat ed wit h Web r eleases 1, 2, and 3. Chapt er 6 giv es t he URLs for downloading Web r eleases 1 and 2. Chapt er 12 giv es t wo different URLs for downloading Web Release 3—one w it h t he SQL Serv er 2000 Web Ser v ices Toolk it and t he ot her wit hout it . For select ed chapt ers, y ou can run t he sam ples wit h less soft ware or different operat ing syst em s t han t he one t hat I used. For exam ple, chapt ers 2 t hrough 5 will run on any operat ing syst em t hat support s a com m ercial version of SQL Ser ver 2000, such as Windows 98 or a m or e recent Windows operat ing syst em . Chapt er 7 r equires an operat ing syst em t hat support s Windows NT secur it y , such as Windows 2000 or Windows XP Professional. Chapt er 6, Chapt er 11, and

Ch a pt e r 1 3 r equir e Microsoft I nt ernet I nform at ion Serv ices ( I I S) . I n addit ion, Chapt er 6 r equires t he inst allat ion of Web r eleases 1 and 2. For Chapt er 11, your syst em needs t o m eet t he m inim um requirem ent s for ASP.NET. ( See a not e in t he “ How Does ASP.NET Relat e t o ASP?” sect ion of Chapt er 8.) Several of t he sam ples in Ch ap t er 1 3 require Web Release 3 and it s associat ed SQL Ser ver 2000 Web Serv ices Toolkit .

Sa m ple File s Sam ple files for t his book can be found at t he Microsoft Press Web sit e, at ht t p: / / www .m icrosoft .com / m spress/ books/ 5792.asp. Click ing t he Com panion Cont ent link t ak es you t o a page fr om w hich y ou can dow nload t he sam ples. Supplem ent al cont ent files for t his book can also be found on t he book’s com panion CD. To access t hose files, insert t he com panion CD int o y our com put er’s CD- ROM dr ive and m ake a select ion fr om t he m enu t hat appears. I f t he Aut oRun feat ure isn’t enabled on y our sy st em ( if a m enu doesn’t appear when you insert t he disc in y our com put er ’s CD- ROM drive) , r un St art CD.ex e in t he r oot folder of t he com panion CD. I nst alling t he sam ple files on y our hard disk requir es approx im at ely 15.3 MB of disk space. I f y ou have t rouble r unning any of t hese files, r efer t o t he t ext in t he book t hat describes t hese pr ogr am s. Aside from t he sam ple files t hat t his book discusses, t he book ’s supplem ent al cont ent includes a st and- alone eBook inst allat ion t hat w ill allow y ou t o access an elect ronic v ersion of t he pr int book direct ly from your deskt op.

Su ppor t Ev er y effort has been m ade t o ensure t he accur acy of t his book and t he cont ent s of t he com panion CD. Microsoft Press pr ov ides cor rect ions for books t hr ough t he World Wide Web at t he follow ing address: ht t p: / / www .m icrosoft .com / m spress/ support To connect dir ect ly t o t he Micr osoft Pr ess Know ledge Base and ent er a query regarding a quest ion or an issue t hat you m ay have, go t o: ht t p: / / www .m icrosoft .com / m spress/ support / search.asp I f you have com m ent s, quest ions, or ideas r egarding t his book or t he com panion cont ent , or quest ions t hat are not answered by query ing t he Know ledge Base, please send t hem t o Microsoft Pr ess via e- m ail t o: m spinput @m icrosoft .com Or v ia post al m ail t o: Micr osoft Pr ess At t n: Pr ogram m ing Micr osoft SQL Ser ver 2000 w it h Microsoft Visual Basic .NET Edit or One Microsoft Way Redm ond, WA 98052- 6399 Please not e t hat product support is not offer ed t hr ough t he above m ail address. For product support inform at ion, please visit t he Microsoft Support Web sit e at : ht t p: / / support .m icr osoft .com

Cha pt e r 1 . Ge t t ing St a r t e d w it h Visua l Ba sic .N ET for SQL Se r v e r 2 0 0 0 This book aim s t o giv e professional dev elopers t he background t hat t hey need t o program SQL Ser ver applicat ions w it h Micr osoft Visual Basic .NET. This ov erall goal im plies t hree guidelines: •





First , t he book t arget s pract icing dev elopers. I n m y exper ience, t hese ar e busy pr ofessionals who need t he det ails fast . These indiv iduals alr eady know how t o build applicat ions. They buy a book t o lear n how t o build t hose applicat ions wit h a specific set of t ools. Second, t he book is about building applicat ions for SQL Ser ver 2000. This focus j ust ifies in- dept h coverage of SQL Serv er program m ing t opics—in part icular, T- SQL, Micr osoft ’s ext ension of t he St ruct ured Query Language ( SQL) . Third, t he book illust rat es how t o pr ogr am in Visual Basic .NET, but w it h part icular em phasis on dat abase issues for SQL Ser ver 2000. Special at t ent ion goes t o relat ed .NET t echnologies, such as t he .NET Fram ew ork, ADO.NET, ASP.NET, and XML Web ser vices.

My goal in t his chapt er is t o equip y ou concept ually for t he r est of t he book . Ther efor e, t his chapt er includes m at er ial t hat acquaint s y ou w it h applicat ion dev elopm ent t echniques and t opics for SQL Serv er 2000 and Visual Basic .NET. The discussion of t he sam ples in t his chapt er generally aim s t o convey broad approaches inst ead of how t o r un t he sam ple. All t he r em aining chapt ers except for Chapt er 8, anot her concept ual chapt er , hav e sam ples w it h inst ruct ions aim ed at professional dev elopers. I believe t hat t he overw helm ing m aj or it y of pr ofessional Visual Basic developers hav e no hands- on fam iliarit y wit h Visual Basic .NET and it s relat ed t echnologies. I f you already knew Visual Basic .NET, it wouldn’t m ake any sense t o buy a book describing how t o use it . This chapt er t herefore focuses on how t o get st art ed wit h Visual Basic .NET and one of it s cor e r elat ed t echnologies for t hose building SQL Ser ver applicat ions—ADO. NET. I also believ e t hat m ost Visual Basic dev elopers don’t have an int im at e know ledge of SQL Serv er —especially for creat ing user - defined obj ect s, such as t ables, v iews, and st or ed procedur es. This capabilit y can em pow er you t o build m or e pow erful and m or e secure applicat ions. As y ou lear n about dat abase obj ect s and how t o creat e t hem in Chapt er 2 t hr ough Chapt er 7, reflect back on t he Visual Basic .NET cov erage in t his chapt er and how t o m ar ry dat abase creat ion t echniques and Visual Basic .NET dev elopm ent t echniques. One of t he best t ools t o build dat abase obj ect s is SQL Ser ver 2000 Query Analyzer . This chapt er’s closing sect ion conveys t he basics of Query Analyzer t hat you need t o follow t he sam ples in Chapt er 2 t hrough Chapt er 7.

Visua l St u dio .N ET, t h e V isu a l Ba sic .N ET I D E Visual St udio .NET is t he new m ult ilanguage int egrat ed developm ent env ir onm ent ( I DE) for Visual Basic, C# , C+ + , and JScr ipt developers. I f you are dev eloping solut ions for Visual Basic .NET, I definit ely r ecom m end t hat you use Visual St udio .NET as y our dev elopm ent envir onm ent . This sect ion dem onst rat es how t o get st art ed using Visual St udio .NET for dev eloping solut ions wit h Visual Basic .NET.

Visual Basic .NET is available as part of Visual St udio .NET in four edit ions: • • • •

Professional Ent erpr ise Dev eloper Ent erpr ise Archit ect Academ ic

All four edit ions of Visual St udio .NET include Visual Basic .NET, Micr osoft Visual C# .NET, Microsoft Visual C+ + .NET, and support for ot her languages. I n addit ion, Microsoft offer s Visual Basic .NET St andard, w hich doesn’t include Visual C# .NET or Visual C+ + .NET. Because t his book t arget s professional Visual Basic dev elopers creat ing SQL Ser ver applicat ions, it uses t he Ent erpr ise Developer Edit ion of Visual St udio .NET. You m ay not ice som e differ ences if y ou’r e using anot her edit ion. Visual St udio .NET can be inst alled on com put ers r unning one of five operat ing syst em s: Windows 2000, Windows NT, Window s XP, Windows ME, and Windows 98. Not all t he .NET Fram ework feat ures are av ailable for each operat ing syst em . For exam ple, Windows 98, Windows Me, and Windows NT don’t support dev eloping ASP.NET Web applicat ions or XML Web serv ices applicat ions. The sam ples for t his book ar e t est ed on a com put er running Windows 2000 Ser ver, which does support all .NET Fram ew or k feat ur es.

St a r t ing V isu a l St udio .N ET To open Visual St udio .NET, click t he St art but t on on t he Windows t ask bar, choose Program s, and t hen choose Micr osoft Visual St udio .NET. Visual St udio displays it s int egrat ed dev elopm ent env ironm ent , including t he St art Page ( unless you prev iously configur ed Visual St udio t o open different ly ) . Fr om t he St art Page, you can configure Visual St udio t o w ork according t o y our dev elopm ent preferences, and y ou can st art new solut ions as well as open ex ist ing pr oj ect s.

Con figu r ing V isu a l St udio .N ET f or Visua l Ba sic .N ET Use t he links on t he left side of t he St art Page t o begin configur ing Visual St udio .NET for developing solut ions in Visual Basic .NET. Click t he My Pr ofile link t o open a pane in w hich you can specify an overall profile as w ell as indiv idually indicat e y our preferences for Keyboard Schem e, Window Lay out , and Help Filt er. You also can designat e t he init ial page t hat Visual Basic .NET displays. When y ou are beginning, it m ay be part icular ly conv enient t o choose Show St art Page. As a Visual Basic dev eloper who has work ed w it h Visual Basic 6, y ou m ight feel m ost fam iliar w it h a lay out t hat reflect s y our pr ior developm ent env ir onm ent . Figure 11 shows t hese My Pr ofile select ions. Figu re 1 - 1 . M y Profile se le ct ion s for st a r t in g V isu al St u dio .N ET for a V isu a l Ba sic de velop e r.

Usin g t h e St a r t Pa ge Aft er set t ing your pr ofile, you can r et urn t o t he init ial St art Page pane by choosing t he Get St art ed link from t he m enu on t he left border. I f you had creat ed pr ev ious solut ions, t he last four m odified proj ect s would appear on t he Proj ect s t ab of t he St art Page. The t ab shows pr oj ect nam es along w it h dat e last m odified. I f a pr oj ect y ou want t o v iew doesn’t appear on t he list , you can click t he Open Proj ect link t o display t he Open Proj ect dialog box and t hen navigat e t o a direct ory cont aining t he pr ev iously cr eat ed solut ion. Select t he proj ect ’s folder t hat y ou want t o open in t he I DE, and double- click t he solut ion file ( .sln) for t he proj ect . The next sect ion illust rat es t his pr ocess in t he cont ext of a sam ple proj ect . To cr eat e a new solut ion, click t he New Pr oj ect link t o open t he New Pr oj ect dialog box . I f y ou saved prefer ences such as t hose show n in Figure 1- 1, t he dialog w ill aut om at ically select Visual Basic Proj ect s in t he Proj ect Types pane of t he New Proj ect dialog box. On t he r ight , y ou can select a t em plat e for launching a proj ect . Table 1- 1 shows t he pr oj ect t em plat e nam es along w it h a br ief descript ion av ailable from t he Ent erpr ise Dev eloper Edit ion of Visual St udio .NET. Choosing a t em plat e ( by clicking OK aft er select ing a t em plat e) opens a proj ect ready for creat ing t he t ype of solut ion t hat y ou want t o dev elop. When Visual St udio .NET sav es t he t em plat e t o st art a new proj ect , it specifies eit her a file folder or a Web sit e for t he t em plat e’s files; y ou can overr ide t he default nam es for t he file folder and Web sit e.

N ot e Not all t he pr oj ect t em plat e t ypes in Table 1- 1 are available wit h t he non- Ent er prise ( or St andard) edit ions of Visual St udio .NET. I n addit ion t o t he em pt y proj ect s, t he St andar d edit ions m ak e available t he Windows Applicat ion, ASP.NET Web Applicat ion, ASP.NET Web Serv ice, and Console Applicat ion t em plat es. 7DEOH9LVXDO%DVLF1(73URMHFW7HPSODWH7\SHV 7HPSODWH1DPH

&UHDWHV$

Windows Applicat ion

Windows applicat ion w it h a form

Class Library

Windows applicat ion suit able for a library of classes wit hout a for m

Windows Cont r ol Librar y

Proj ect for dev eloping cust om reusable form cont r ols for Windows applicat ions

ASP.NET Web Applicat ion

Web applicat ion on a Web server

ASP.NET Web Ser vice

XML Web serv ice on a Web serv er

Web Cont r ol Librar y Proj ect for dev eloping cust om reusable cont r ols for Web applicat ions Console Applicat ion Com m and line applicat ion t hat operat es in an MS- DOS–st y le window ( t he Console) Windows Serv ice

Windows serv ice, form erly NT serv ice, applicat ion t hat runs in t he background w it hout it s ow n cust om user int erface

Em pt y Pr oj ect

Local pr oj ect wit h no cust om st yle

Em pt y Web Pr oj ect Web proj ect wit h no cust om st yle New Pr oj ect I n Blank proj ect in an ex ist ing folder Ex ist ing Folder Ther e ar e t wo m ain cat egor ies of t em plat es: Web pr oj ect s and local proj ect s. Web proj ect s perm it a browser t o serv e as t he client for a proj ect . Web pr oj ect s are opt im ized for form processing on t he Web ser v er . Local pr oj ect s offer cust om for m user int erfaces wit h t he capabilit y of processing on a local w orkst at ion. Local proj ect s can pr ov ide richer env ir onm ent s m or e conduciv e t o client - side program m ing, but local proj ect s don’t offer t he wide accessibilit y of solut ions running fr om a Web ser ver.

Cr e a t in g a n d Run n in g a Con sole Applica t ion When y ou select a Console Applicat ion t em plat e and click OK t o launch a new proj ect , Visual St udio .NET r esponds by opening a pr oj ect w it h a blank m odule. I n addit ion t o t he Module window, Visual St udio displays Solut ion Explorer and t he Propert ies w indow. You can ent er code dir ect ly int o t he Module w indow, which appears as a t ab t hat y ou can select alt er nat ely w it h t he St art Page. Figur e 1- 2 shows a code sam ple in t he Main subrout ine t hat prom pt s for a first and second nam e before com bining t hem and display ing t hem in t he Console ( t he com put er’s m onit or ) . The code is also available as MyNam eI sFrom Console in t he Chapt er 1 folder on t he com panion CD for t his book . Alt hough Visual Basic dev elopers didn’t prev iously have Console applicat ions rout inely available, t his sam ple should be

very easy t o follow. The final t w o lines present an inst ruct ion and cause t he window t o r em ain open unt il t he user responds t o t he inst ruct ion. This allows t he user t o v iew t he full nam e in t he Console w indow. Figu r e 1 - 2 . A Con sole a p plicat ion for displayin g a fu ll n a m e b a se d on u se r inpu t for fir st a n d se con d n am e s.

To t he r ight of t he Module w indow are t w o ot her windows. The t op one of t hese is Solut ion Explorer. I t shows t he file st r uct ure for t he solut ion. Solut ion Explor er indicat es in it s first line t hat t he solut ion consist s of j ust one proj ect . Below t hat line appears t he nam e of t he proj ect , MyNam eI sFrom Console. Wit hin t he proj ect are t hree ent ries: one each for t he Refer ences, Assem blyI nfo.v b, and Module1.vb elem ent s wit hin t he solut ion’s proj ect . By default , t he Propert ies window is below Solut ion Explorer. I n t he Full Pat h pr opert y t ext box is an excerpt show ing t he pat h t o Module1.vb on m y com put er . When y ou click t he proj ect nam e in Solut ion Explor er, t he Pr oj ect Folder t ext box in t he Propert ies window displays t he pat h of t he dir ect ory holding t he solut ion’s files. I t is t his direct ory t hat you copy t o deploy your solut ion on anot her com put er w it h t he .NET Fram ew or k inst alled. The solut ion w on’t run wit hout t he com m on language runt im e on t he com put er t o which you copy t he direct ory cont aining t he .NET Fram ew or k solut ion. See Chapt er 8 for m or e det ailed cov erage of t he .NET Fram ew or k, including t he runt im e and dist ribut ing .NET Fram ework solut ions as assem blies of files in folders. You can t est r un t he applicat ion by choosing St art from t he Debug m enu, or by pressing F5. This opens t he Console w indow w it h a prom pt t o ent er a first nam e. Aft er y ou close y our applicat ion and sav e any changes t o it , y our solut ion appears

on t he St art Page for r ecent solut ions. I f you st art Visual St udio .NET and t he solut ion y ou want t o open doesn’t appear on t he Proj ect s t ab of t he St art Page, you can also open t he solut ion by click ing Open Pr oj ect . I n t he Open Pr oj ect dialog box , choose t he file w it h t he .sln ext ension and t he solut ion’s nam e ( My Nam eI sFrom Console) . A solut ion can cont ain j ust one .sln file, but it can cont ain m ult iple pr oj ect s. You also can r un t he solut ion and open t he Console w indow dir ect ly from Windows Explor er w it hout using Visual St udio .NET. Open t he bin subdirect ory wit hin t he dir ect ory cont aining t he assem bly folder for t he solut ion. Then doubleclick t he My Nam eI sFr om Console.ex e file. This opens t he Console w indow w it h t he prom pt for a first nam e.

An Ove r view of AD O.N ET Ca pa bilit ie s ADO.NET encapsulat es t he dat a access and dat a m anipulat ion for t he .NET Fram ew ork . This sect ion giv es y ou an overv iew of t he t opic t hat equips you for a st art er sam ple in t he next sect ion. Microsoft chose t he nam e ADO.NET for t he .NET Fr am ew ork dat a access com ponent t o indicat e it s associat ion w it h t he ear lier ADO t echnology for dat a access. While t her e ar e som e sim ilarit ies in sy nt ax bet ween ADO.NET and ADO ( part icular ly for connect ion st r ings) , m any will find t he differ ences ar e m or e obv ious t han t he sim ilarit ies. These differences subst ant ially upgrade ADO.NET over ADO in t w o k ey ar eas—scalabilit y and XML ( Ext ensible Mark up Language) int er operabilit y. As a result , y ou w ill be able t o creat e dat abase applicat ions w it h ADO.NET t hat serv e m or e users and shar e m ore dat a t han y ou did w it h ADO. See Chapt er 10 for a m or e int ensive exam inat ion of ADO.NET. Chapt er 12 explicit ly explores int eroperabilit y bet ween ADO. NET and XML.

.N ET D a t a Pr ovide r Type s Your .NET Fram ework solut ions requir e .NET dat a providers t o connect t o dat a sources. These pr ov iders are differ ent fr om t hose used wit h ADO, but t her e are dist inct sim ilar it ies in som e of t he ways y ou use t hem . Wit h .NET dat a prov iders, your solut ions can connect , r ead, and ex ecut e com m ands against dat a sources. The .NET prov iders also offer select ed ot her funct ions, such as t he m anagem ent of input and out put par am et ers, securit y, t ransact ions, and dat abase ser ver errors. Visual St udio .NET ships wit h t wo .NET dat a providers—t he SQL Serv er .NET dat a prov ider and t he OLE DB .NET dat a pr ov ider. I n addit ion, y ou can dow nload an ODBC .NET dat a prov ider fr om t he Micr osoft MSDN dow nload sit e ( ht t p: / / m sdn.m icr osoft .com / downloads/ default .asp) .

N ot e As I writ e t his chapt er , t he ODBC .NET dat a pr ov ider j ust becam e av ailable wit h t he r ollout of t he shipping version of Visual St udio .NET. You can download it fr om ht t p: / / m sdn.m icrosoft .com / downloads/ default .asp?url= / downloads/ s am ple.asp?url= / m sdn- files/ 027/ 001/ 668/ m sdncom posit edoc.x m l. The URLs for resour ces som et im es change. You can alway s sear ch for t he ODBC .NET dat a pr ovider at t he MSDN download sit e t o obt ain it s curr ent download locat ion.

The t hr ee pr ov iders t ak en t oget her offer fast , highly focused access t o select ed dat a sources as well as general access t o a w ide range of possible dat a sources. The SQL Ser ver .NET dat a provider is opt im ized for SQL Ser ver 7.0 and SQL Ser ver 2000. This dat a prov ider connect s dir ect ly t o a SQL Serv er inst ance. The OLE DB .NET dat a prov ider connect s t o OLE DB dat a sources t hr ough t w o int erm ediat e lay ers—t he OLE DB Serv ice Com ponent and t he classic OLE DB prov ider int roduced along w it h ADO. The OLE DB Serv ice Com ponent m anages connect ion pooling and t ransact ion serv ices. The classic OLE DB prov ider , in t urn, dir ect ly connect s t o a dat abase serv er . Micr osoft explicit ly t est ed t he OLE DB .NET dat a provider w it h SQL Ser ver, Or acle, and Jet 4.0 dat abases. Use t he OLE DB .NET dat a provider t o connect t o t he SQL Serv er 6.5 v ersion and earlier ones. This pr ov ider is also good for connect ing t o y our Micr osoft Access solut ions based on t he Jet 4.0 engine. The OLE DB .NET dat a prov ider definit ely doesn’t wor k w it h t he OLE DB prov ider for ODBC dat a sources ( MSDASQL) . Because t he .NET OLE DB dat a pr ovider doesn’t connect t o ODBC dat a sources, y ou requir e t he ODBC .NET dat a prov ider for connect ing t o ODBC dat a sources fr om y our .NET Fr am ew ork solut ions. Ther e ar e four m ain .NET dat a pr ov ider classes for int eract ing w it h a rem ot e dat a source. The nam es of t hese classes change slight ly for each t ype of pr ov ider, but each .NET dat a prov ider has t he sam e four kinds of classes. The nam es for t he SQL Ser ver .NET dat a prov ider classes for int eract ing wit h SQL Server inst ances are SqlConnect ion, SqlCom m and, SqlDat aReader, and SqlDat aAdapt er. You can use t he SqlDat aReader class for read- only applicat ions fr om a SQL Serv er dat a source. Two especially convenient ways t o display r esult s w it h a SqlDat aReader class are in a m essage box or t he Visual St udio .NET Out put w indow. The SqlDat aAdapt er class act s as a bridge bet w een a r em ot e SQL Ser ver dat a source and a Dat aSet class inst ance inside a Visual Basic .NET solut ion. A dat a set in a Visual St udio solut ion is a fift h t y pe of ADO.NET class. A dat a set can cont ain m ult iple t ables. A sixt h ADO.NET class is t he Dat aView class, w hich act s lik e a v iew based on a t able w it hin a Dat aSet obj ect . Windows For m s in Visual Basic .NET applicat ions can bind only t o t ables w it hin a Dat aSet obj ect and Dat aView obj ect s. I exam ine t he Dat aSet obj ect lat er in t his sect ion. Chapt er 10 includes a syst em at ic sum m ary of all six ADO.NET classes t hat r ev iews select ed propert ies and m et hods of each class. The ov er view of ADO.NET classes in Chapt er 10 is support ed by num erous code sam ples t hat illust rat e how t o m anipulat e inst ances of t he classes program m at ically .

N ot e I n or der t o use abbreviat ed nam es, such as t hose list ed in t his sect ion for t he SQL Serv er .NET dat a pr ovider class inst ances, y our applicat ion needs a refer ence t o t he SqlClient nam espace. You can cr eat e such a reference wit h an I m port s Sy st em .Dat a.SqlClient st at em ent j ust befor e a Module declarat ion. SqlCon n e ct ion Cla ss An inst ance of t he SqlConnect ion class can int erface direct ly w it h a SQL Serv er dat a source. Use a const ruct or st at em ent t o inst ant iat e a SqlConnect ion obj ect from t he SqlConnect ion class. The const ruct or st at em ent is a new t ype of sy - nt ax for .NET Fram ew or k solut ions. This t ype of st at em ent perm it s y ou t o declare, inst ant iat e, and pass st art up param et ers t o an obj ect based on a class. Wit h t he SqlConnect ion const ruct or st at em ent , y ou can specify a connect ion st r ing as an argum ent for t he const r uct or st at em ent . Alt er nat iv ely, you can assign t he

connect ion st r ing t o t he SqlConnect ion obj ect aft er it s inst ant iat ion w it h a propert y assignm ent st at em ent for t he Connect ionSt ring propert y. The follow ing line shows t he synt ax t o inst ant iat e a new SqlConnect ion obj ect , My SQLCnn1, wit h a connect ion st r ing designat ing int egrat ed secur it y t o t he m ydb dat abase on t he m yserv er inst ance of SQL Serv er. You don’t hav e t o explicit ly indicat e a prov ider because t he const ruct or st at em ent r ev eals t he t ype of prov ider t hr ough it s r efer ence t o t he SqlConnect ion class. Dim MySQLCnn1 As New _ SqlConnection(“Integrated Security=SSPI;" & _ "Data Source=myserver;Initial Catalog=mydb") Aft er inst ant iat ing a SqlConnect ion obj ect , you need t o invoke it s Open m et hod befor e t he obj ect can link anot her obj ect based on one of t he ot her SQL Ser ver .NET dat a provider classes, such as SqlCom m and, SqlDat aAdapt er, or SqlDat aReader , t o a SQL Serv er inst ance. I nv oke t he Close m et hod t o recover t he resources for a SqlConnect ion obj ect w hen your solut ion no longer needs it . The Close m et hod rolls back any pending t ransact ions and releases t he connect ion t o t he connect ion pool. The Dispose m et hod is also available for r em ov ing connect ions, but it inv okes t he Close m et hod and perform s ot her .NET adm inist rat iv e funct ions. Microsoft r ecom m ends t he Close m et hod for r em oving a connect ion. Unclosed connect ions ar en’t r et urned t o t he connect ion pool.

SqlCom m a n d a n d SqlD a t a Re a de r Cla sse s One w ay t o put a connect ion t o use is t o em ploy it along w it h t he SqlCom m and and SqlDat aReader obj ect s. A SqlDat aReader obj ect can m aint ain an open for ward- only, read- only connect ion w it h a SQL Ser ver dat abase. While t he SqlDat aReader using a SqlConnect ion obj ect is open, you cannot use t he SqlConnect ion obj ect for any ot her purpose except t o close t he connect ion. Closing a SqlDat aReader obj ect releases it s associat ed SqlConnect ion obj ect for ot her uses. The SqlDat aReader class doesn’t have a const ruct or st at em ent . You declare t he SqlDat aReader obj ect w it h a Dim st at em ent and assign a result set from a SqlCom m and obj ect t o a SqlDat aReader wit h t he Ex ecut eReader m et hod of t he SqlCom m and obj ect . Finally, invoke t he SqlDat aReader obj ect Read m et hod t o open a r ow fr om t he result set in t he SqlDat aReader . The SqlCom m and obj ect can ser ve m ult iple funct ions, including processing a TSQL st at em ent against a connect ion. When used in t his fashion, t he SqlCom m and can t ake t wo argum ent s. The first can be a T- SQL dat a access st at em ent , such as SELECT * FROM MyTable . The second SqlCom m and argum ent designat es t he source connect ion for t he T- SQL st at em ent . For exam ple, y ou can use t he nam e of a SqlConnect ion obj ect , such as My SQLCnn1. Figur e 1- 3 shows t he rout e fr om a SQL Serv er dat a source t o a SqlDat aReader obj ect . Alt hough t he SqlConnect ion and SqlCom m and obj ect s support t wo- way int eract ion w it h a dat a source, t he SqlDat aReader obj ect allows read- only access t o t he result set from t he T- SQL st at em ent ser v ing as an argum ent for a SqlCom m and const r uct or. Because a SqlDat aReader obj ect cannot specify it s ow n dat a source, a SqlDat aReader obj ect m ust link t o a SqlConnect ion obj ect t hr ough an int erm ediat e SqlCom m and obj ect . Figu re 1 - 3 . A sch e m a t ic illu st ra t ing t h e r ou t e b y w h ich a SqlDa t a Rea de r ob j e ct re t u r n s va lu e s t o a n a pplicat ion .

The SqlCom m and obj ect can do m ore t han prov ide a result set t o t he SqlDat aReader obj ect . The discussion of t he SqlDat aReader obj ect described t he use of t he SqlCom m and obj ect Execut eReader m et hod. Thr ee r elat ed m et hods highlight som e cont rast ing SqlCom m and obj ect funct ionalit y. •





I nvok e t he Execut eNonQuery m et hod t o perfor m t wo t ypes of act ions. First , use t his m et hod t o perfor m dat a definit ion t asks, such as cr eat ing st ored procedures and v iews. Second, t he Ex ecut eNonQuery m et hod can enable dat a m anipulat ion t asks, such as insert s, updat es, and delet es. Next , y ou can apply t he Ex ecut eScalar m et hod t o a SqlCom m and obj ect when y ou want t o r et ur n a single v alue fr om a SELECT st at em ent . The m et hod ret ur ns t he first colum n from t he first r ow of a r esult set . I f you program t his cell t o be an aggregat e value such as a count or sum , y ou can r eadily ext ract t hat single value w it h t he Ex ecut eScalar m et hod. Finally, t he Execut eXMLReader m et hod opens a T- SQL source st at em ent wit h a FOR XML clause int o an XMLReader obj ect . Chapt er 6 cont ains ext ensive cov erage of t he FOR XML clause. Obj ect s for dealing w it h XML will be cov ered in Chapt er 12.

Th e SqlD a t a Ada pt e r Cla ss a n d t h e D a t a Se t Cla ss You use obj ect s based on t he SqlDat aAdapt er class in com binat ion w it h obj ect s based on t he Dat aSet class. A Dat aSet obj ect , which is an inst ance of t he Dat aSet class, represent s an in- m em ory cache of dat a ret r iev ed from a dat abase. The Dat aSet obj ect offers a disconnect ed dat a source as opposed t o t he alw aysconnect ed dat a source for SqlDat aReader obj ect s. As a consequence, using t he SqlDat aAdapt er and Dat aSet obj ect s inst ead of a SqlDat aReader obj ect im prov es applicat ion scalabilit y. This scalabilit y im prov em ent result s because t he Dat aSet doesn’t persist a connect ion t o it s underly ing dat a source ov er t he w hole of it s lifet im e as does t he SqlDat aReader obj ect . While t he SqlDat aReader isn’t as scalable as t he SqlDat aAdapt er / Dat aSet com binat ion, t he SqlDat aReader can prov ide fast er per form ance from a rem ot e dat a source because it deliv ers dat a in t he st yle of a forward- only, r ead- only cursor—t he classic fir ehose deliv er y m odel.

N ot e The t er m firehose refers t o t he fact t hat dat a gushes out of a forward- only, read- only cursor. The SqlDat aAdapt er and Dat aSet obj ect s com bine t o enable bot h dat a access and dat a m anipulat ion capabilit ies. This is im port ant because SqlDat aReader obj ect s prov ide st rict ly dat a access capabilit ies ( t hat is, you cannot perform updat e,

insert , or delet e t asks w it h a SqlDat aReader obj ect ) . Use t he SqlDat aAdapt er Fill m et hod t o populat e a Dat aSet obj ect wit h values from a SQL Ser ver dat a source. Because a single Dat aSet obj ect can work w it h m ult iple SqlDat aAdapt er and OLE DB Dat aAdapt er obj ect s, y ou can populat e a single Dat aSet obj ect w it h het er ogeneous dat a sources fr om m ult iple dat abase serv ers. For exam ple, y ou can populat e a single dat a set wit h t ables, v iews, or st or ed procedures from t wo different SQL Serv er inst ances or from Access and Oracle dat a sources in addit ion t o a SQL Server dat a source. Furt herm or e, you can j oin all t he dat a sources wit hin a Dat aSet obj ect on fields w it h com m on dat a t ypes. Use t he SqlDat aAdapt er Updat e m et hod t o t ransfer changes from a Dat aSet obj ect t o it s under ly ing dat a sources. When users per form insert , updat e, and delet e operat ions against t he cont ent s of a Dat aSet obj ect , t hose m odificat ions don’t t ransfer t o t he dat a sources for t he Dat aSet obj ect unt il your applicat ion inv okes t he Updat e m et hod for a SqlDat aAdapt er obj ect underly ing t he dat a source. Despit e it s nam e, t he Updat e m et hod can process all t hree t ypes of dat a m anipulat ion operat ions. How ev er , y ou need a cust om SqlCom m and obj ect t o accom m odat e each t ype of dat a m anipulat ion t ask. Therefore, a SqlDat aAdapt er can r elat e t o a r em ot e dat a source t hr ough m or e t han a single SqlCom m and obj ect . Bet ween t he t im e y ou populat e t he Dat aSet obj ect and t he t im e your applicat ion inv ok es t he SqlDat aAdapt er Updat e m et hod, it ’s possible for t he underly ing dat a source on a SQL Ser ver inst ance t o change. Any changes can cause ex cept ions because t he or iginal values in a dat a set can be differ ent fr om t he cur rent values in t he SQL Server dat a source. The SqlDat aAdapt er has ev ent s and propert ies t o help m anage except ions t hat can occur dur ing an updat e process. Figure 1- 4 pr esent s a schem at ic diagram sum m ar izing how SqlDat aAdapt er and Dat aSet obj ect s exchange dat a wit h an under ly ing dat a source. By cont rast ing t his diagram w it h t he one in Figure 1- 3, you can easily spot an im port ant differ ence bet w een t he SqlDat aReader and a Dat aSet obj ect supplied by a SqlDat aAdapt er obj ect . The capabilit y of per form ing dat a m anipulat ion w it h t he Dat aSet obj ect is a cr it ical feat ur e t hat m eans m any applicat ions w ill rely on a Dat aSet obj ect inst ead of a SqlDat aReader obj ect . Figu r e 1 - 4 . A sch em a t ic illu st r at in g t h e rou t e by w h ich SqlD a t aAd a pt er a n d D a t a Set obj e ct s e xch a n g e va lu es w it h a SQL Se rv e r da t a sou r ce .

The Dat aSet obj ect offers an obj ect m odel for m anaging t he indiv idual elem ent s wit hin it . The Dat aSet obj ect consist s of a Dat aTable collect ion ( along wit h ot her elem ent s) . This collect ion can cont ain one or m ore t ables. You can creat e t hese t ables w it h t he SqlDat aAdapt er Fill m et hod w hen y ou init ially populat e a Dat aSet obj ect from a SQL Serv er dat a source. The Select Com m and pr opert y , which is a T- SQL st at em ent or a st or ed procedure, for a SqlDat aAdapt er obj ect can serv e as t he basis of a t able in t he Dat aTable collect ion for a Dat aSet obj ect . You can use m ult iple SqlDat aAdapt er obj ect s t o add m or e t han one t able t o a Dat aSet obj ect . Each t able has a r ich obj ect m odel t hat perm it s t he designat ion of pr im ary keys and for eign k eys as well as const raint s t o m anage dat a int egrit y w it hin a t able. One very pract ical use for t he Dat aTable collect ion and t he obj ect m odel for indiv idual t ables is t hat you w ill use it t o nav igat e am ong t he values wit hin a Dat aSet obj ect .

N ot e I n addit ion t o referencing t he colum n values of r ows wit hin an indiv idual Dat aTable in a Dat aSet obj ect , y ou can reference t he schem a of Dat aTable obj ect s wit hin a Dat aSet obj ect . This is par t icular ly convenient when y ou want t o cr eat e a t able t hat you w ant t o populat e wit h dat a fr om an XML docum ent . The Dat aSet obj ect support s four k ey m et hods for exchanging it s dat a wit h XML docum ent s. Two of t he m et hods are used for w rit ing XML docum ent s based on a Dat aSet obj ect , and t wo ar e for r eading XML docum ent s int o a Dat aSet obj ect . Wit hin each pair , one m et hod focuses j ust on t ransferr ing schem a infor m at ion and t he ot her focuses on t ransferr ing dat a as well as schem a inform at ion.

A St a r t e r AD O.N ET Sa m ple This sect ion pr esent s a st art er sam ple t o illust rat e som e of t he concept s described in t he preceding sect ion. Don’t w or ry about follow ing t he det ails of t he exam ple. I nst ead, pay at t ent ion t o how easy it is t o get st art ed w it h ADO.NET. This sect ion r einforces t he present at ion of basic ADO.NET concept s described in t he preceding sect ion w it h sim ple dr ag- and- dr op t echniques and a lit t le code included t o t ie obj ect s t oget her or enable select ed funct ionalit y. See Chapt er 10 for a collect ion of code sam ples t hat illust rat e how t o pr ogram ADO.NET obj ect s when y ou r equir e cust om ized solut ions not r eadily available fr om t he graphical dev elopm ent env ironm ent . Most pr ofessional developers get called on t o do t he hard work t hat goes beyond dragging and dropping obj ect s. Aft er all, if it were easy, t hey wouldn’t need you. How ev er, it is nice t o st art out by seeing how easy it is t o cr eat e a sim ple solut ion m ost ly by dragging and dr opping.

N ot e For t hose who want t he sam ple fr om t his sect ion as a point of depart ur e, it is av ailable on t he book’s CD as t he GraphicalDat aBind solut ion. Addin g a SqlD a t a Ada pt e r , SqlConn e ct ion, a n d D a t a Se t

You can drag a SqlDat aAdapt er obj ect t o a for m j ust lik e a t ext box or a com bo box in Visual Basic 6. Ther e is ev en a w izard t o help y ou configur e t he SqlDat aAdapt er obj ect . Figur e 1- 5 shows t he opening screen im m ediat ely aft er dragging a SqlDat aAdapt er obj ect t o t he st art up blank form , Form 1, for a Windows applicat ion. You can use t his w izard t o specify t wo ADO.NET obj ect s. Figu re 1 - 5 . Th e D a t a Ad a pt er Con figu ra t ion W iz a rd e n a ble s you t o gr ap hically con fig u r e a Sq lD at a Ada pt e r ob j ect a n d it s re la t ed Sq lConn ect ion ob j e ct for u se w it h a W ind ow s for m .

First , you can designat e a SQL Serv er dat abase t o w hich t o connect ; t his creat es a SqlConnect ion obj ect . This w izard offers sev eral rout es for specify ing a dat abase connect ion. For exam ple, y ou can pick a prev iously creat ed connect ion, or you can creat e a new collect ion fr om t he Dat a Link Propert ies dialog box. This dialog box let s y ou specify t he com m on connect ion st ring argum ent s, such as a dat abase serv er , a t ype of aut hent icat ion, and a dat abase nam e. I n t his st art er sam ple, I used t he default connect ion t o t he Nort hw ind dat abase. Second, y ou can specify a dat a source w it hin a dat abase connect ion using a SQL st ring or a st or ed pr ocedur e. For t his st art er applicat ion, I used SELECT CategoryID, CategoryName FROM Categories as t he SQL st ring sour ce for t he SqlDat aAdapt er obj ect . Alt hough a graphical designer is available for building query st at em ent s, y ou will be sev erely ham per ed as a SQL Ser ver dev eloper if you don’t lear n T- SQL, t he dialect of SQL t hat SQL Ser ver support s. I n addit ion, you w ill find a grasp of T- SQL im port ant for craft ing t he st at em ent s for t he SqlCom m and obj ect s t hat enable you t o build solut ions t hat updat e a SQL Ser ver dat a source from a Windows applicat ion.

Aft er y ou finish configur ing t he Dat a Adapt er Configurat ion Wizard, t he com ponent t ray w ill open below your blank for m . The t ray w ill hold t he t w o obj ect s t hat t he w izard creat ed—a SqlDat aAdapt er obj ect and a SqlConnect ion obj ect . Because a SqlDat aAdapt er obj ect is m er ely a br idge bet w een a r em ot e dat a source and a dat a set in a Windows applicat ion, y ou w ill need t o creat e a dat a set . Then your SqlDat aAdapt er obj ect can fill t he dat a set w it h dat a fr om t he rem ot e dat a source specified by y our r eplies t o t he Dat a Adapt er Configurat ion Wizard. I m m ediat ely aft er a SqlDat aAdapt er obj ect is cr eat ed, t hr ee link s are displayed near t he bot t om of t he Propert ies w indow for t he obj ect . One of t hese link s r eads Generat e Dat aset . Clicking t he link opens t he Generat e Dat aset dialog box, in which you designat e an ex ist ing dat a set or specify t he nam e for a new one. Figur e 1- 6 shows t he specificat ion of a new dat a set nam ed DsCat egories for t he SqlDat aAdapt er creat ed wit h t he Dat a Adapt er Configurat ion Wizard. When you click OK w it hin t he Generat e Dat aset dialog box port ray ed in Figur e 1- 6, Visual Basic .NET adds a new obj ect nam ed DsCat egories1 t o t he t ray below t he form . I n addit ion, Visual Basic .NET adds an XML schem a nam ed DsCat egories.xsd t o t he solut ion t hat describes t he dat a set . You can view t he schem a for t he dat a set graphically or as XML code by double- click ing t he file’s nam e in Solut ion Explor er . The schem a’s graphical view is int eract iv e so t hat you can change t he dat a t ype specificat ion for colum ns and m ak e ot her design changes t o t he Cat egor ies t able. The Propert ies w indow for t he DsCat egor ies.xsd shows t he nam e of t he t able specificat ion as Cat egor ies. At t his point , you have com plet ed t he cr eat ion of t he DsCat egories dat a set , which cont ains a Dat aTable nam ed Cat egories. Figu r e 1 - 6 . You n e ed t o ad d a da t a set b efore you ca n u se a SqlDa t a Ada pt e r. You ca n ad d t h e da t a set a s sim p ly a s g ivin g it a n a m e in t h e Ge n e ra t e D at a set dia log b ox .

N ot e Alt hough t he Generat e Dat aset dialog box shows t he dat a set nam e as DsCat egories, Visual Basic .NET assigns DsCat egories1 as t he dat a set nam e in t he t ray below For m 1. Aft er adding a SqlDat aAdapt er obj ect and a Dat aSet obj ect t o an applicat ion, y ou can pr ev iew t he dat a t hat t he SqlDat aAdapt er will bring t o t he applicat ion. Click ing t he Prev iew Dat a link in t he Propert ies window for a SqlDat aAdapt er obj ect opens t he Dat a Adapt er Pr ev iew dialog box. Click t he Fill Dat aSet but t on t o display t he dat a in t he dialog box . Because of t he SQL st at em ent used when configur ing our SqlDat aAdapt er obj ect , t he but t on populat es t he form wit h a t able t hat shows t he Cat egor yI D and Cat egory Nam e colum n values from t he Nort hw ind dat abase. Don’t confuse click ing t he but t on on t he form w it h populat ing t he dat a set for use w it h a Windows form . Filling t he Cat egories dat a t able in t he DsCat egories dat a set wit h dat a values fr om a SQL Ser ver inst ance and display ing t he values on a Window s form r equires t w o m or e st eps. First y ou need t o invoke t he Fill m et hod for t he SqlDat aAdapt er obj ect . Second you need t o bind form cont r ols, such as t ex t box es, t o colum ns in t he local Cat egor ies dat a t able.

Fillin g a D a t a Se t a n d Bin din g Con t r ols t o I t A logical place t o fill a dat a set for use w it h a form is t he for m Load ev ent procedur e. A single line of code in t he t em plat e will fill t he dat a set . Run t he line of code from t he form Load event t o m ake t he cont ent s for t he dat a set available as soon as t he form opens. The follow ing code segm ent illust rat es t he synt ax for inv ok ing t he SqlDat aAdapt er Fill m et hod t o populat e a dat a set . The ev ent procedur e is for Form 1, which is t he default st ar t up obj ect for a Windows applicat ion. The Fill m et hod t akes t w o argum ent s in t his sit uat ion. First you specify t he dat a set nam e. Second y ou designat e t he Dat aTable nam e wit hin t he dat a set . You m ust nam e a Dat aTable obj ect because one dat a set can hold m ult iple Dat aTable obj ect s. Leav ing out t he Dat aTable nam e w ill cause an er ror. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DsCategories1, “Categories”) End Sub Aft er y ou fill t he dat a set , you can bind it t o cont rols on a form . For exam ple, I added t wo t ext box es t o Form 1 for t he st art er ADO.NET applicat ion. You can do t his w it h t he Toolbox j ust as in pr ior Visual Basic versions. What ’s new is t hat t here is now a Dat aBindings propert y . You can graphically bind t he Tex t propert y for a t ext box cont r ol t o a colum n in t he Cat egories dat a t able. Figur e 1- 7 shows how t o bind t he Text pr opert y for Text Box1 t o t he Cat egor yI D colum n in t he Cat egor ies dat a t able. The Form 1.vb Design t ab shows Text Box1 select ed on Form 1. The Propert ies window r ev eals t he assignm ent of t he Cat egory I D colum n t o Text Box1. Select ing a colum n fr om t he Cat egor ies dat a t able com plet es t he t ask. I follow ed t he sam e pr ocess for Text Box2, but I select ed Cat egory Nam e inst ead. I f you r un Form 1 by pr essing t he F5 key, you see t he form w it h t w o t ext box es show ing t he Cat egoryI D and Cat egory Nam e colum n values for t he first row from t he Cat egor ies dat a t able. While it is nice t o see dat a in t he t ext boxes, applicat ions t ypically seek t o allow users t o at least browse t hrough dat a. To

enable browsing, y ou need cont rols t hat let a user nav igat e t hrough t he rows of t he Cat egor ies dat a t able. Figu r e 1 - 7 . Use t h e D at a Bin din gs pr op e rt y t o bin d t h e Te x t p roper t y of a t ex t box con t rol t o a colu m n in a D at a Ta ble ob j ect .

N a v iga t in g Th r ou gh Row s A row of but t on cont r ols can pr ov ide t he basis for a nav igat ion bar. All we need are Text propert y set t ings indicat ing t he nav igat ion each but t on prov ides and ev ent procedures for t he Click ev ent of each but t on t hat nav igat es t hr ough t he rows in t he Cat egor ies dat a t able. I added four but t on cont r ols t o Form 1 wit h ev ent procedures t o cont rol nav igat ion in r esponse t o click ev ent s. For exam ple, Figur e 1- 8 shows t he t ext box es aft er t he but t on cont r ol on t he far r ight has been click ed. Not ice t hat t he last row ( for Cat egoryI D 8 in t he Cat egor ies dat a t able) shows in t he t op t ext box . Figu r e 1 - 8 . For m 1 in t h e st a rt e r AD O.N ET sam ple aft e r t h e last - row bu t t on ( > | ) h a s b e en clicke d displa ys colu m n valu e s fr om t h e cor re spon d in g r ow in it s t e xt b ox con t r ols.

The follow ing set of Click ev ent pr ocedur es for But t on1 t hr ough But t on4 shows how easy it is t o cont r ol nav igat ion. The but t ons from left t o r ight navigat e t o t he first row, t he pr ev ious r ow, t he next row, and t he last row. The procedur es updat e t he Posit ion propert y of t he BindingCont ext pr opert y on t he for m for t he Cat egor ies Dat aTable in t he DsCat egor ies1 dat a set . This m anipulat ion, in t ur n, affect s all t ext box cont r ols bound t o t he Cat egories dat a t able. Chapt er 10 dr ills down m or e deeply int o t he obj ect m odel suppor t ing t hese m anipulat ions. The im port ant point t o not ice her e is t hat t he code doesn’t hav e t o handle m ov ing past t he beginning or ending r ow because ADO.NET is sm art about recognizing eit her end of a r owset , such as t he Cat egories dat a t able. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

’Move to the first row. Me.BindingContext(DsCategories1, “Categories”).Position _ = Me.BindingContext(DsCategories1, “Categories”). _ Position.MinValue End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ’Move to the previous row. Me.BindingContext(DsCategories1, “Categories”).Position _ -= 1 End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click ’Move to the next row. Me.BindingContext(DsCategories1, “Categories”).Position _ += 1 End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click

’Move to the last row. Me.BindingContext(DsCategories1, “Categories”).Position _ = Me.BindingContext(DsCategories1, “Categories”). _ Position.MaxValue End Sub

Usin g Qu e r y Ana lyzer Query Analyzer is y our friend for debugging T- SQL st at em ent s. Because T- SQL is so im port ant t o SQL Ser ver developm ent , m ast ering t his t ool can be par t of what m akes y ou int o a gr eat SQL Ser ver dev eloper .

W ha t ’s Qu e r y An a lyze r For ? Query Analyzer is one of t he client t ools t hat ships wit h SQL Serv er 2000. This is anot her way of saying t hat Quer y Analyzer isn’t part of t he dat abase serv er. You are aut hor ized t o use Query Analyzer, and t he ot her client t ools, by t he allocat ion of a Client Access License t o y our workst at ion. Alt hough t he client t ools don’t ship wit h MSDE 2000 ( Microsoft SQL Serv er 2000 Deskt op Engine) , t hey ar e available wit h any r egular v ersion of SQL Serv er 2000, such as t he Ent erprise, St andard, Dev eloper , and Personal edit ions. I t hink of Query Analyzer as sort of an I DE for running T- SQL st at em ent s. This client t ool is a r eal help for anyone program m ing solut ions for SQL Serv er. Quer y Analyzer w ill help y ou t o easily and quick ly debug y our T- SQL code. Alt hough y ou can pr ogram and debug T- SQL dir ect ly w it h Visual Basic .NET and ADO.NET, Query Analyzer prov ides a m uch r icher env ironm ent t hat m akes y our T- SQL coding go m uch fast er. Ev en if an applicat ion calls for r unning T- SQL inside of a Visual Basic .NET applicat ion, I oft en find it convenient t o debug t he st at em ent in Query Analyzer befor e insert ing t he T- SQL code int o m y Visual Basic .NET applicat ion. Ther e ar e at least five r easons t o becom e com fort able w it h T- SQL, and using Query Analyzer m ay be one of t he best ways t o do t hat . •



• •



You can build richer query st at em ent s t hat r et ur n precisely t he dat a y ou want wit hout hav ing t o resort t o a graphical quer y builder . I ndeed, som e query operat ions, such as t hose per form ed by t he UNI ON funct ion, cannot be r epresent ed by graphical query designers. You can cr eat e dat a m anipulat ion st at em ent s for updat ing, insert ing, and delet ing r ows. Graphical query builders aren’t always effect ive at creat ing t hese st at em ent s. You can program secur it y t opics, such as creat ing SQL Ser ver logins and cont r olling access t o dat abase obj ect s and serv er adm inist rat ion funct ions. You can program t he cr eat ion of dat abases and t he obj ect s wit hin t hem , such as t ables, st or ed procedures, and user- defined funct ions. Several chapt ers w it hin t his book include script s t o creat e dat abases and populat e t hose dat abases wit h obj ect s aut om at ically. You can t ake advant age of program m ing feat ur es, such as I F…ELSE st at em ent s, local var iables, param et ers, and ret ur n values t o build flex ibilit y and user int er act ivit y int o y our applicat ions.

Many T- SQL sam ples ar e especially designed for use wit h Quer y Analyzer . For exam ple, t hese sam ples set t he dat abase cont ex t for T- SQL code w it h a USE

st at em ent . This st at em ent explicit ly t arget s Quer y Analyzer and doesn’t run fr om m ost ot her SQL Ser ver client s, such as Visual Basic .NET. Books Online, t he SQL Ser ver Help syst em , follows t his convent ion wit h it s sam ples. Therefor e, a basic fam iliar it y w it h Quer y Analyzer w ill help y ou t o t ake adv ant age of t he r ich collect ion of sam ples in Books Online. I n addit ion, t he T- SQL sam ples in t his book follow t he sam e convent ion. Ther efor e, t his sect ion gives y ou a br ief int r oduct ion t o Quer y Analyzer. You will hav e am ple opport unit y t o r einforce and ext end t he underst anding t his sect ion conveys wit h t he T- SQL sam ples t hr oughout t he balance of t his book. I n fact , t he com m ent ary for t hese sam ples som et im es describes how t o r un code in Quer y Analyzer.

M a k ing a Conn e ct ion w it h Qu e r y An a lyz e r To st art Query Analyzer , click t he St art but t on on t he Windows t askbar; choose Program s, t hen Micr osoft SQL Serv er, and t hen Query Analyzer. When y ou st art Query Analyzer t his way , y ou w ill be gr eet ed w it h t he Connect To SQL Ser ver dialog box . Recall t hat Query Analyzer is a client t ool. Therefore, y ou can use it wit h any SQL Serv er inst ance t hat y ou can connect t o and for w hich y ou hav e access perm ission. I f y ou are connect ing t o t he local inst ance of SQL Serv er on your com put er for which y ou are t he adm inist rat or, y ou can designat e t he SQL Ser ver as “ ( local) ” and choose Windows Aut hent icat ion. ( See Figur e 1- 9.) The set t ings in Figur e 1- 9 ar e suit able for connect ing t o SQL Ser ver w it h any Windows login. Click OK t o com plet e t he connect ion t o t he serv er. Figu r e 1 - 9 . Th e con n e ct ion se t t in gs for logg in g in t o t h e loca l in st an ce of SQL Se rve r w it h W indow s a u t h e n t ica t ion .

Query Analyzer offers t he norm al flex ibilit y in how y ou connect t o a SQL Serv er inst ance. As I alr eady not ed, y ou can connect wit h any Windows login t hat a SQL Ser ver inst ance recognizes. I n addit ion, y ou can use SQL Serv er aut hent icat ion. I f you select SQL Server Aut hent icat ion rat her t han Windows Aut hent icat ion in t he Connect To SQL Serv er dialog box , Query Analy zer enables t he Login Nam e and Password t ext box es so t hat y ou can specify a SQL Ser ver login and password. I n addit ion, you can connect t o any ot her SQL Ser ver inst ance besides t he local default one. I f y ou k now t he nam e of t he inst ance t o w hich y ou want t o connect , t ype t he nam e in t he SQL Ser ver com bo box in t he Connect To SQL Serv er dialog box. Ot herw ise, click t he br owse but t on ( …) next t o t he com bo box . This opens a dialog box t hat list s SQL Ser ver inst ances cur r ent ly act ive on t he net w ork t o which your w orkst at ion connect s. Select an inst ance nam e t o specify a connect ion t o t hat serv er.

See Chapt er 7 for m or e about SQL Serv er secur it y and logging in t o SQL Ser ver inst ances w it h differ ent t ypes of logins. Unt il Chapt er 7, one safe appr oach t o running t he sam ples is t o connect as a m em ber of t he sysadm in ser ver role, such as t he SQL Serv er adm inist rat or. Mem bers of t he sysadm in serv er r ole hav e unr est rict ed perm ission on a SQL Server inst ance. Chapt er 7 giv es guidelines and procedur es for r est rict ing t he perm issions for an applicat ion’s users.

Ru nn in g, Sa vin g, a n d Ope nin g T- SQL Scr ipt s When Quer y Analyzer opens as descr ibed in t he preceding sect ion, it will connect a user t o t he default dat abase for t he login t hat t he user specified in t he Connect To SQL Serv er dialog box . The default is t he m ast er dat abase unless a dat abase adm inist rat or changed t he st andard default dat abase specificat ion w hen adding a new login. Because m ost user - defined quer ies don’t int er r ogat e t he m ast er dat abase, w hich is a sy st em dat abase, y ou w ill usually want t o change t he dat abase cont ext befor e w r it ing any SQL query st at em ent s. You can em ploy t he USE st at em ent for t his. Just follow USE wit h t he nam e of t he dat abase for w hich you want t o wr it e a query . The follow ing st at em ent dir ect s Query Analyzer t o run quer y st at em ent s against t he pubs dat abase ( unt il anot her USE st at em ent or som e ot her specific inst ruct ion t o use anot her dat abase) . The pubs dat abase is one of t he sam ple dat abases t hat is inst alled aut om at ically wit h SQL Ser ver 2000. USE pubs Figur e 1- 10 shows t his sim ple SELECT st at em ent for t he aut hors t able in t he pubs dat abase: SELECT au_fname, au_lname, state FROM authors WHERE contract = 1 The SELECT st at em ent appears aft er t he USE st at em ent in t he Edit or pane, which is w here y ou t ype T- SQL st at em ent s in Query Analyzer. The st at em ent select s t hr ee colum n values from t he aut hors t able if a row has a cont ract colum n value equal t o 1. You can see t he r esult set fr om t he query st at em ent in t he Result s pane t hat appears below t he Edit or pane, as show n in Figure 1- 10. Query Analyzer aut om at ically displays t he Result s pane w hen you r un a query , but you can also show and hide it by pr essing Ct r l+ R. By default , Quer y Analy zer displays t he r esult set in t he Result s pane w it hin a spreadsheet lik e gr id. At t he bot t om of t he Result s pane are a Grids t ab and a Messages t ab. You can click t he Messages t ab t o see general feedback from SQL Ser ver about how a query st at em ent operat ed. For exam ple, t he Messages t ab for t he quer y in Figur e 1- 10 says, “ ( 19 r ow ( s) affect ed) ” , which corr esponds t o t he num ber of rows t he query st at em ent r et ur ns. Warnings and err or feedback fr om a SQL Ser ver inst ance appear in t he Messages pane. You also can choose t o display t he result set in t he Result s pane as t ext in colum ns. I n t hat case, t her e is only a Result s t ab at t he bot t om of t he Result s pane, and bot h t he r esult set and m essages are display ed in t he pane. To specify whet her y ou want t o set t he r esult set in a gr id or in t ex t , choose Opt ions fr om t he Tools m enu, t hen choose t he Result s t ab, and t hen use t he com bo box at t he right of t he Opt ions dialog box t o specify Result s To Text , Result s To Gr ids, or Result s To File. Figu re 1 - 1 0 . A q u e ry st a t e m e n t for t h e pub s d at a ba se a n d it s r esu lt se t r u n from Qu e ry An a lyz er .

Aft er creat ing a T- SQL script , you can save it so t hat y ou or ot hers can reopen it and use it again lat er. Most of t he sam ple files for Chapt er 2 t hrough Chapt er 7 are saved scr ipt s wit h t he .sql ext ension. To save a script file for t he fir st t im e or resav e an ex ist ing script file w it h a new nam e, choose Sav e As from t he File m enu, nav igat e t o a desir ed folder w it h t he Sav e Quer y dialog box , ent er a filenam e, and click Sav e. These st eps will sav e t he cur r ent scr ipt in t he designat ed folder w it h t he filenam e t hat y ou specify w it h t he .sql ext ension. For exam ple, I follow ed t hese st eps t o sav e t he script shown in Figur e 1- 10 t o m y com put er. I saved t he file as Aut horsQuery .sql in t he Chapt er01 folder of t he SQL Ser ver Dev elopm ent Wit h VBDot Net dir ect ory on m y C dr iv e. Ther e ar e sev eral ways t o open a script file. For exam ple, im m ediat ely aft er connect ing t o a SQL Ser ver inst ance for a new Query Analyzer session, y ou can choose Open from t he File m enu, navigat e in t he Open Query File dialog box t o t he folder wit h t he script file ( .sql) , highlight t he filenam e, and click Open. These st eps open an Edit or pane in Query Analyzer w it h t he sav ed script file. Figure 111 shows t he opened script file sav ed in t he pr eceding paragraph in an Edit or pane. Not ice t hat t he t it le bar for t he pane includes t he pat h along w it h t he filenam e and ext ension. The Obj ect Br owser w ill also script obj ect s for y ou. To aut om at ically cr eat e a script for an obj ect , r ight - click an obj ect such as t he Cat egories t able, and choose Script Obj ect To New Window As and t hen t he Creat e com m and. This feat ur e allows you t o see t he T- SQL scr ipt behind y our favorit e obj ect s t o lear n how t o

m ake m ore obj ect s lik e t hem or t o help you change t heir design t o m eet expanded obj ect ives. As y ou build up your collect ion of dat abases and t he obj ect s wit hin t hem , y ou m ight st art t o find special value in t he Obj ect Search com ponent w it hin Query Analyzer. You can open t he Obj ect Search dialog box by pr essing t he F4 key or by choosing Obj ect Search from t he Tools m enu and t hen Open. You can open m ult iple Obj ect Search dialog box es at t he sam e t im e. The dialog box let s you search for any obj ect or subset of obj ect s, such as views or st ored pr ocedur es, by nam e or ev en a part of a nam e. Figur e 1- 13 shows an excerpt fr om t he result s in a search for any t ype of dat abase obj ect t hat begins w it h Cat eg in any dat abase on t he curr ent ly connect ed SQL Serv er inst ance. As you can see, obj ect s beginning w it h Cat eg for t heir nam e ar e very popular in t he Nort hw ind dat abase. ( Ot her dat abases out side t he excerpt show n also hav e obj ect s beginning wit h Cat eg.) Figu r e 1 - 1 1 . An op en e d T- SQL scr ipt fr om a sa v ed .sq l file . Th e p at h an d file n a m e in t h e t it le ba r in dica t e t h e sou rce of a .sq l file.

Se le ct e d Ot h e r Topics Ther e’s lot s m or e t o Quer y Analyzer, but t he pr eceding int r oduct ion equips you for t he w ays in which t his book exploit s t he t ool. I n t his sect ion, I br iefly highlight a couple of m y fav orit e ot her uses for Quer y Analyzer . The Obj ect Br owser is a conv enient t ool for explor ing t he dat abases and t he obj ect s wit hin t hem on a connect ed SQL Serv er inst ance wit h a t r ee- t y pe int erface. You can use t his Query Analyzer com ponent t o ex am ine t he dat abase obj ect s wit hin a dat abase. You can show or hide t he Obj ect Br owser by pressing t he F8 k ey or by choosing Obj ect Br owser from t he Tools m enu and t hen t he Show / Hide com m and. Figur e 1- 12 shows t he Obj ect Browser w indow expanded t o display t he colum n nam es and dat a t ype specificat ions for t he Cat egor ies t able ( dbo.Cat egories) in t he Nort hw ind dat abase. I oft en find it conv enient t o drill down int o a dat abase design and check t he spelling of colum n nam es. Being able t o quick ly look up t he dat a t ype for a colum n in a t able is part icularly convenient when y ou ar e declaring a search param et er for a co- lum n in a t able; use t he wrong dat a t ype, and y ou m ay not get a m at ch, even w it h t he right value. Figu r e 1 - 1 2 . Th e Obj e ct Br ow ser ope n e d t o sh ow t h e n a m es an d da t a t ype s for t h e colu m n s in t h e N or t h w in d d at a ba se .

Figu re 1 - 1 3 . You ca n u se t h e Obj e ct Sea rch d ia log in Qu e r y An a lyz e r t o se a rch for obj e ct s by n a m e ( or e ve n pa rt of a n am e ) .

Cha pt e r 2 . Ta ble s a nd D a t a Ty pe s This chapt er t arget s t he design and program m ing of SQL Serv er t ables wit h TSQL ( Transact St r uct ured Query Language) . SQL Serv er dat abase adm inist rat ors and dev elopers use T- SQL for program m ing dat abase adm inist rat ion and dat a access. By dat a access, I m ean select ing r ecords from a dat abase. T- SQL is generally com pat ible wit h t he SQL- 92 st andard endorsed by ANSI ( Am erican Nat ional St andards I nst it ut e) and I SO ( I nt er nat ional St andards Organizat ion) . Howev er, Micr osoft opt im ized and st ream lined T- SQL for use wit h SQL Ser ver. Any dev eloper w ho want s t o use Visual Basic .NET t o build cust om SQL Ser ver solut ions will be sever ely handicapped w it hout a good grasp of SQL Serv er dat a t ypes and t ables, as well as T- SQL. Several subsequent chapt ers in t his part of t he book w ill explor e select ed ot her dat abase obj ect s, such as views, st ored procedur es, and user - defined funct ions, from design and im plem ent at ion perspect ives w it h T- SQL. The next part of t he book builds on t his foundat ion as it dem onst rat es how t o cr eat e cust om SQL Ser ver solut ions w it h Visual Basic .NET. This chapt er begins w it h an ex plorat ion of SQL Ser ver dat a t ypes. Next it prov ides an overv iew of differ ent t ypes of t ables. A ser ies of T- SQL sam ples illust rat es core t able design issues and solut ions. These sam ples int roduce y ou t o program m ing t echniques for SQL Serv er t ables. By underst anding how t o script dat abase obj ect s, such as t ables, y ou can r eadily duplicat e t hose dat abase obj ect s across m ult iple serv ers. For exam ple, a Visual Basic dev eloper can build a solut ion on one serv er and t hen readily t ranspor t t he obj ect s for use on anot her serv er —j ust by running t he script s for t he obj ect s. You can also adapt t he script from one obj ect as a st art ing point for ot her, sim ilar , obj ect s. A clear underst anding of t able script ing t echniques w ill help you t o aut om at e t able design. This frees r esources for focusing on t he needs of client s for y our dat abases.

Cha pt er Re sou r ces Ther e ar e t wo k ey r esources for t his chapt er . Fir st , a SQL Serv er dat abase nam ed Chapt er 02 illust rat es m any of t he design concept s used t hroughout t his chapt er. Second, a collect ion of T- SQL sam ple script s illust rat es coding t echniques for creat ing t ables and wor king w it h t he r esources wit hin a t able.

Th e Ch a pt e r ’s T- SQL Sa m ple Scr ipt s The T- SQL sam ple collect ion for t his chapt er illust rat es k ey design and im plem ent at ion issues for script ing SQL Serv er dat abase obj ect s. All t he sam ple script s t hat y ou see in t his chapt er ar e av ailable on t he book ’s com panion CD. The sam ples ar e all sav ed wit h t he .sql ext ension, so you can open and r un each of t hem from Query Analyzer . As you learned in Chapt er 1, Query Analy zer is a graphical t ool t hat ships wit h Micr osoft SQL Ser ver 2000. As y ou read and run t he sam ple script s, you m ight find it helpful t o lear n m ore about t he st r uct ure of t he Chapt er02 dat abase by browsing it w it h SQL Serv er Ent erprise Manager , which also was discussed in Chapt er 1.

Th e Ch a pt e r ’s Sa m ple D a t a ba se

The script in t his sect ion cr eat es a new v ersion of t he Chapt er02 dat abase. Subsequent T- SQL code sam ples w ill cr eat e addit ional t ables in t he dat abase and dem onst rat e t echniques for w or k ing w it h t ables. Prepar e t o creat e t he Chapt er02 dat abase by st art ing Query Analyzer and connect ing t o t he SQL Ser ver inst ance y ou are using. Log in as sa or wit h a user I D t hat belongs t o t he sysadm in fix ed serv er r ole. This book dr ills dow n on secur it y explicit ly in Chapt er 7, wher e y ou will learn how t o fine- t une dat abase and user securit y set t ings. When users connect t o a SQL Serv er dat abase t hrough your Visual Basic .NET applicat ions, t hey m ust ident ify t hem selv es t hr ough t he secur it y account s discussed in Chapt er 7. Unt il t hat chapt er, using a login t hat belongs t o sysadm in w ill w ork for all sam ples. Copy or t ype t he follow ing T- SQL scr ipt int o t he Edit or pane in Query Analyzer, and press F5 t o run t he script t o cr eat e t he dat abase. Alt ernat iv ely , y ou can open t he script direct ly fr om Query Analy zer : choose Open fr om t he File m enu, and t hen nav igat e t o t he locat ion of t he script . Not ice t hat t he first com m ent in t he sam ple is “ Cr eat eSam pleDB” —t he nam e of t he sam ple file. I use t his convent ion for all t he sam ples in t he book t o m ak e it easier for y ou t o locat e and open t hem from Quer y Analyzer.

At t a ching a D a t a ba se t o a N e w SQL Se r ve r I nst a nce I r egular ly r ead on t he SQL Ser ver newsgroups of folks asking how t o at t ach a dat abase t o a server . These dev eloper s want t o t ake a dat abase and it s obj ect s developed on one server and r un t hem on anot her server. Their need can be as sim ple as copying a dat abase applicat ion t hey are dev eloping on t heir deskt op t o t heir lapt op so t hey can wor k on it while away from t he office. Alt er nat ively, t hey m ay want t o copy a dat abase fr om headquar t ers or one branch office t o one or m ore ot her br anch offices. Alt hough t here ar e wizards for t his kind of t hing, it is nice t o know how t o pr ogr am t he adm inist rat ion of t his k ind of t ask for y our own cust om solut ions. This capabilit y liberat es you fr om t he canned wizar d solut ion and gives y ou m ore flexibilit y in how y ou work wit h SQL Serv er. At it s m ost elem ent ar y level, t his can be as sim ple as at t aching a pair of dat abase files t o a new server inst ance. I n t he cont ex t of t his chapt er , a com plet ed v ersion of t he Chapt er 02 dat abase is on t he book’s CD. Therefore, you m ight care t o copy a version t o anot her inst ance of SQL Ser ver besides t he one y ou use t o t est t he sam ples for t his chapt er. The inst ance can be on anot her com put er or t he sam e com put er . St art t o m igr at e t he Chapt er02 dat abase by copy ing t he Chapt er 02_dat .m df and Chapt er 02_log.ldf files fr om t he CD t o t he Dat a folder for t he SQL Serv er inst ance t o which you want t o at t ach t he com plet ed dat abase. Aft er clear ing t he r ead- only at t r ibut e set t ings for t he files, y ou can r un t he following scr ipt from Query Analyzer . The script at t aches t he chapt er’s t wo dat abase files t o t he default inst ance of t he SQL Serv er t o which Quer y Analyzer connect s. By changing MSSQL t o MSSQL$MYOTHERI NSTANCE, you can at t ach t he dat abase files t o a SQL Server inst ance nam ed

MYOTHERI NSTANCE. You m ust copy your dat abase files t o t he Dat a pat h for t he SQL Server inst ance in t he sp_at t ach_db st at em ent before r unning t he scr ipt . --AttachSampleDB --Run the script from the master database. USE master --Update the paths for the data and log files so they --are appropriate for your computer. EXEC sp_attach_db @dbname = N’Chapter02’, @filename1 = N’c:\Program Files\Microsoft SQL Server\MSSQL\Data\Cha pter02_dat.mdf’, @filename2 = N’c:\Program Files\Microsoft SQL Server\MSSQL\Data\Cha pter02_log.ldf’ The init ial USE st at em ent in t he script specifies t he source dat abase so t hat t he sam ple r uns from t he SQL Ser ver m ast er dat abase. Next t he script rem ov es any prior v ersion of t he Chapt er02 dat abase on t he serv er. This ensures t hat you can always cr eat e a new copy of t he dat abase. Aft er r em ov ing any pr ior v ersion, t he code invokes t he CREATE DATABASE st at em ent . This st at em ent assigns t he logical filenam es Chapt er02_dat and Chapt er02_log t o t he dat a and log files for t he dat abase. Alt hough your SQL Serv er dat abases can hav e m ore files, t hese t wo are necessary for populat ing a dat abase and per form ing backup operat ions. Updat e t he operat ing syst em file pat hs so t hat t hey are appr opr iat e for your com put ing set up. --CreateSampleDB --Execute statements from the master database. USE master GO --Drop any prior version of Chapter02 database. IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME = N’Chapter02’) DROP DATABASE Chapter02 GO --Create new version of Chapter02 database. CREATE DATABASE Chapter02 ON (NAME = Chapter02_dat, FILENAME = ’c:\program files\microsoft sql server\mssql\data\Chapter02_dat.m df’, SIZE = 1) LOG ON (NAME = Chapter02_log, FILENAME = ’c:\program files\microsoft sql server\mssql\data\Chapter02_log.l df’, SIZE = 1, MAXSIZE = 5) GO

D a t a Types for Ta bles Tables are t he building blocks for SQL Serv er applicat ions because t hey st or e t he dat a for t he ent it ies t hat an applicat ion m odels. Lik ew ise, colum ns are t he building blocks of t ables because t ables st ore t heir dat a as colum n values. SQL Ser ver applicat ions can oft en have t ables w it h num er ous rows, so it is im port ant t o specify t he dat a t ype for colum ns t o ensur e t hat t hey use t he m inim um am ount of st orage. When y ou specify t he dat a t ype, you ar e indicat ing t he k ind of dat a t hat t he colum n is going t o cont ain. Making t hese assignm ent s cor rect ly speeds t he perform ance of y our SQL Ser ver applicat ions w hile also conser ving st orage space. I n addit ion, t he v alidit y of y our dat abase m odel for a r eal- w or ld syst em can depend on t he use of pr oper dat a t y pes. I n m any circum st ances, y our applicat ions can denot e dat a wit h one of t he dat a t ypes built int o SQL Ser ver—t he syst em dat a t y pes. When your applicat ion needs m or e definit ion t han t hese syst em dat a t ypes allow nat iv ely, you can creat e userdefined dat a t y pes t hat refine t he syst em dat a t ypes. Howev er, y our abilit y t o fashion valuable user - defined dat a t ypes depends on your grasp of t he syst em dat a t ypes. I f you ar e fam iliar wit h dat a t ypes, y ou m ay want t o skip t his sect ion and refer t o it as needed. But if you are new t o SQL Serv er program m ing or need a refr esher on dat a t ypes, read on.

Syst e m D a t a Type s I t is useful t o t hink about t he syst em dat a t ypes in six gr oups. I n addit ion t o t he six hom ogeneous cat egor ies, t her e is a collect ion of special, or m iscellaneous, syst em dat a t ypes. The six hom ogeneous gr oups of dat a t ypes pert ain t o: • • • • • •

Charact er dat a Unicode dat a Num er ic dat a Monet ary dat a Dat e and Tim e dat a Binary dat a

Ch a r act e r D a t a Charact er dat a consist s of alphanum eric charact er sequences. Ther efor e, y ou can represent any com binat ion of num bers and w or ds wit h charact er dat a, such as “ 123 Mulber ry Lane” , “ $1,000,000” , “ Your nam e goes her e: ” or “ Rick Dobson” . SQL Ser ver has t hree charact er dat a t ypes: char, v archar, and t ext . The follow ing t able br iefly sum m ar izes t hem . 'DWD

'DWD7\SH'HVFULSWLRQ

7\SH 1DPH char

For fixed- lengt h charact er dat a up t o 8000 charact ers. Use char ( n) t o specify, wit h n as t he num ber of charact ers. The st orage size is n byt es. Appr opriat e w hen all t he colum n values are t he sam e lengt h ( or w hen t his is v er y near ly t r ue) .

varchar

For var iable- lengt h char act er dat a up t o 8000 charact ers. Use v archar( n) t o specify, w it h n as t he m axim um num ber of charact ers.

The st orage size for any varchar colum n value is t he act ual size, w her e 1 byt e equals 1 charact er . Appr opr iat e when t her e is subst ant ial v ariabilit y in lengt h bet ween colum n values. For var iable- lengt h char act er dat a t hat can grow t o 2 31 - 1 ( 2,147,483,647) charact ers in t he SQL Serv er inst ance’s code page for m at . Alt hough som e of t hese code pages perm it double- byt e form at for r epresent ing charact ers, t he lengt h of a t ext dat a t ype colum n value is st ill t he num ber of charact ers, w her e 1 charact er equals 1 byt e. SQL Ser ver support s im plicit and explicit conv er sion bet ween dat a t ypes. SQL Ser ver handles im plicit conv ersions aut om at ically ; you use t he CAST and CONVERT funct ions t o convert bet ween t ypes explicit ly . The CONVERT funct ion is a propr iet ary ext ension of t he CAST funct ion t hat offers ext ra conv ersion capabilit y not available from CAST, which is SQL- 92 com pliant . See t he “ CAST and CONVERT” t opic in SQL Ser ver Books Online for m ore det ail on conversion bet ween SQL Ser ver dat a t ypes. I m plicit conv ersions don’t depend on t he t ransform at ion of a value by t he CONVERT or CAST funct ion. I m plicit conv ersion also applies t o t he conv ersion of a result fr om com bining or com par ing t w o or m or e values wit h different dat a t ypes. A var iet y of Books Online t opics clar ify im plicit conversion, including t he “ CAST and CONVERT” t opic. For exam ple, see t he “ Dat a Type Conversion,” “ Dat a Types and Table St ruct ures,” and “ Dat a Type Pr ecedence” Books Online t opics. You can use t he Search t ab in Books Online t o search for t hese t opics. The Book s Online search engine w ill oft en r et urn m ult iple t opics for any search st r ing, ev en w hen you specify a precise search t opic t it le. Scan t he list of t it les ret urned by t he search engine for t he ex act one y ou seek. t ext

Un icode D a t a Unicode is a 16- bit char act er encoding st andard. SQL Serv er dat a t ypes for Unicode cor respond t o SQL charact er dat a t ypes—nchar , nvarchar , and nt ext for fix ed- lengt h, var iable- lengt h, and v er y long Unicode dat a. One k ey dist inct ion is t hat t he Unicode form at for t r anslat ing bit s t o charact ers r elies on a single st andard t ranslat ion t able t hat uses 2 byt es per charact er . The charact er dat a for m at s use a collect ion of differ ent code pages m ost of which assign 1 byt e per charact er . This dist inct ion gives Unicode form at t he capacit y t o r epr esent m or e t han 65,000 charact ers, while non- Unicode char act er dat a t ypically repr esent s only 256 charact ers at a t im e ( or per code page) . The Unicode codes t hat hav e been assigned r epresent charact ers in m ost of t he wr it t en languages of t he wor ld. Charact er dat a uses syst em - lev el t ables called code pages t o det erm ine how t o t ranslat e bit s t o charact ers. Different count ries can r ely on differ ent code pages t o represent t heir charact er set . For applicat ions t hat r un in m any different count ries, it can be challenging t o find a single code page w it h valid and consist ent bit - t o- charact er t ranslat ions for all languages. Using Unicode dat a resolv es t his pr oblem because it s code page accom m odat es 2 16 charact ers. The price for t his easier cross- count ry applicabilit y is t hat each charact er has a size of 2 by t es inst ead of t he 1 byt e per charact er. As a r esult , t he m ax im um num ber of charact ers for Unicode dat a t ypes is half t hat of cor responding charact er dat a t ypes. The follow ing t able sum m ar izes t he t hr ee Unicode dat a t ypes. These dat a t ypes align w it h t he charact er dat a t ypes, but t hey have differ ent lengt h and applicabilit y. 'DWD7\SH

'DWD7\SH'HVFULSWLRQ

1DPH nchar

For fixed- lengt h charact er dat a up t o 4000 char act ers in lengt h w it h a Unicode dat a form at . Use nchar ( n) t o specify, w it h n as t he num ber of

charact ers. The st orage size in byt es equals t wice t he num ber of charact ers. Corresponds t o t he char dat a t ype in t erm s of applicabilit y except for it s broader usefulness for repr esent ing charact ers from m ult iple languages. nvarchar For var iable- lengt h char act er dat a up t o 4000 charact ers in lengt h. Use nvarchar ( n) t o specify, wit h n as t he m ax im um num ber of charact ers. The st orage size for any nvarchar colum n value is t he act ual size, wher e 2 byt es equal 1 charact er. Corr esponds t o t he varchar dat a t ype in t erm s of applicabilit y except for it s broader usefulness for represent ing charact ers fr om m ult iple languages. nt ext

For var iable- lengt h char act er dat a t hat can grow t o 2 30 - 1 ( 1,073,741,823) charact ers in t he Unicode code page form at . Cor r esponds t o t he t ext dat a t ype in t erm s of applicabilit y except for it s broader usefulness for r epresent ing charact ers from m ult iple languages.

N ot e I n t he case of colum n dat a t y pe specificat ions, precede t he charact er dat a t y pe nam e w it h an n t o denot e t he m at ching Unicode dat a t ype nam e. Repr esent charact er const ant s in SQL Server wit h single- quot at ion m ark delim it ers. Use a leading N t o represent a Unicode const ant . For exam ple, a charact er const ant appears as ‘my character constant’ . However, t he m at ching Unicode equiv alent appear s as N’my Unicode constant’ . N u m e r ic D a t a Num er ic dat a consist s of num bers only. You can per form arit hm et ic operat ions on num er ic dat a, and y ou can com pare num er ic values along a num eric scale, w hich can differ fr om com par isons based on collat ions for charact er dat a and Unicode dat a. SQL Serv er has t hree general cat egor ies for num er ic dat a: int eger dat a, decim al dat a, and approxim at e dat a. Wit hin each of t hese cat egor ies, t her e are one or m or e specific dat a t ypes. Bey ond t hat , t he num eric dat a cat egor ies denot e different classes of num bers or ways of r epresent ing num bers. I n t ege r Da t a I nt eger dat a t ypes denot e values t hat SQL Ser v er r epr esent s exclusively as whole num bers. The int eger dat a t ypes include t iny int , sm allint , int , and bigint . The dat a t ypes differ pr im ar ily in t he m agnit ude of t he num ber t hat t hey can r epr esent , but t he t iny int dat a t ype differs in t hat it cannot represent negat iv e values as can t he ot hers. I nt eger dat a t ypes, part icular ly int , are com m only used along w it h t he I DENTI TY propert y t o specify aut om at ically incr em ent ing colum n v alues t hat serv e as t he pr im ar y k ey for a t able. The next t able list s t he int eger dat a t ypes along wit h br ief sum m ar ies of t heir capabilit ies. Your applicat ions should generally use t he sm allest dat a t ype possible. However, use a dat a t ype w it h sufficient range for y our needs because SQL Ser ver rej ect s colum n values out side t he lim it s for a dat a t ype. Calculat ions, such as aggregat ions in views, w ork different ly for t iny int and sm allint v alues. I n t hese cases, SQL Serv er aut om at ically prom ot es t he r et ur n value t o t he int value

range. Therefor e, t he sum of a set of t iny int colum n values can exceed 255, but no indiv idual t iny int colum n value can ex ceed 255. The t iny int / sm allint prom ot ion policy doesn’t apply t o calculat ions based on int colum n values; SQL Ser ver doesn’t aut om at ically pr om ot e a r et ur n value out side t he int lim it s—even if t he result is wit hin t he bigint lim it s. I nst ead, SQL Ser ver ret ur ns an er ror. I n addit ion, t he bigint dat a t ype doesn’t work w it h all funct ions t hat t he ot her int eger dat a t y pes can use, and t her e are special funct ions for select ed t asks, such as count ing inst ances and ret ur ning r ows affect ed by quer ies, in which t he quant it ies exceed t he int r ange t o fall in t he bigint range. See t he “ Using bigint Dat a” t opic in Books Online for m ore det ail on t he special rest r ict ions t hat apply t o t he bigint dat a t ype. 'DWD7\SH

'DWD7\SH'HVFULSWLRQ

1DPH t iny int

For values in t he range 0 t hr ough 255. Each t iny int colum n value is 1 byt e long.

sm allint

For values from - 2 15 ( - 32,768) t hrough 2 15 - 1 ( 32,767) . Each sm allint colum n value consum es 2 byt es of st orage.

int

For values from - 2 31 ( - 2,147,483,648) t hr ough 2 31 - 1 ( 2,147,483,647) . Each int colum n value r equires 4 byt es of st orage.

bigint

For values from - 2 63 ( - 9,223,372,036,854,775,808) t hr ough 2 63 - 1 ( 9,223,372,036,854,775,807) . Each bigint colum n value requir es 8 byt es of st orage.

De cim a l D a t a The decim al dat a cat egory is a single num er ic cat egory w it h t w o equivalent SQL Ser ver dat a t ypes: num er ic and decim al. You can use t hem int erchangeably, but decim al is pr obably t he m or e com m on dat a t ype nam e. Like t he int eger dat a t ypes, t he decim al dat a t ypes pr ecisely r epr esent values. How ev er , decim al dat a t ypes differ from int eger dat a t y pes in t hree ways. First , decim al dat a t ype values allow for places aft er t he decim al. ( Recall t hat int eger dat a t ypes rest r ict you t o whole num bers.) Second, decim al dat a t ype specificat ions per m it a v ar iable precision ( or t ot al num ber of digit s) . The t ot al num ber of digit s, w hich can range from 1 t hr ough 38, includes digit s t o t he right and left of t he decim al point . Third, you can designat e a decim al dat a t y pe for a colum n w it h var iable scale ( or digit s t o t he r ight of t he decim al point ) .

N ot e The decim al dat a t y pe in SQL Serv er 2000 and t he Decim al dat a t ype in Visual Basic .NET aren’t t he sam e. The Decim al dat a t ype in Visual Basic can represent num ber s wit h values fr om 1 t hr ough 28 digit s t o t he right and left of t he decim al point . This dist inct ion ( 1 t hrough 28 v s. 1 t hr ough 38) is im port ant . Unless pr oper pr ecaut ions ar e t ak en, you can encount er over flow err or s as y ou ext ract colum n v alues wit h a decim al dat a t ype from a SQL Serv er t able int o your Visual Basic .NET applicat ion. I f you know t he num ber s in t he SQL Server t able exceed t he v alues t hat Visual Basic .NET can represent wit h it s Decim al dat a t ype, consider represent ing t he SQL Serv er decim al dat a t y pe v alues wit h anot her dat a

t y pe in Visual Basic .NET, such as Double, which has a range fr om - 1.79E + 308 t hr ough 1.79E + 308. Designat e a decim al cat egory value w it h decim al( p,s) or num eric( p,s) . The p value r epresent s t he pr ecision; t he s value denot es t he scale. The pr ecision m ust be less t han or equal t o 38 but great er t han or equal t o t he scale. The scale m ust be less t han or equal t o t he pr ecision, but t he scale has t o be gr eat er t han or equal t o 0. The m ax im um dat a range for decim al t ype values is fr om - 10 38 + 1 t hr ough 10 38 - 1. This r ange subst ant ially ex ceeds t he lim it s of any int eger dat a t ype. The sam e holds t r ue for t he t w o m onet ar y dat a t ypes t hat SQL Ser ver offers. ( We’ll rev iew t hese short ly.)

N ot e Colum ns w it h t he decim al dat a t ype specificat ion can also serve as an aut o- increm ent ing pr im ary k ey w hen you assign an I DENTI TY pr opert y t o t he colum n. Set t he scale t o 0 for t his applicat ion of t he dat a t y pe. The lengt h in byt es for t he decim al dat a t ype specificat ion depends on t he precision. The follow ing t able sum m arizes t he relat ionship bet ween st or age requir em ent s and pr ecision for decim al dat a t ypes. 3UHFLVLRQ

6WRUDJH%\WHV

1–9

5

10–19

9

20–28

13

29–38

17

App roxim a t e D at a All t he prior num er ic dat a t ypes pr ecisely r epr esent ed dat a values. This avoids rounding err or . The t wo approx im at e dat a t ypes allow you t o represent dat a values wit hout perfect precision ( but ext rem ely close t o t he exact value) . I n exchange for r educed precision r equirem ent , t he approx im at e dat a t ypes offer a m uch w ider range t han t he pr ev ious dat a t ypes. When y ou need t o repr esent num bers bey ond t he range of t he preceding num er ic cat egory dat a t ypes, t he approx im at e dat a t y pes offer a v iable alt er nat iv e ( for exam ple, in engineer ing applicat ions w or king wit h v ery large or sm all values) . Approxim at e dat a t ypes also enable your applicat ions t o use less st orage space when r educed precision is accept able for y our needs. The t wo SQL Ser ver approx im at e dat a t ypes ar e r eal and float . The real dat a t ype offers t he sm aller range and precision, but it r equires j ust 4 byt es per dat a value. I t s range ext ends from - 3.40E + 38 t hr ough 3.40E + 38. The float dat a t ype ext ends from - 1.79E + 308 t hr ough 1.79E + 308, but each float dat a t y pe value requir es 8 byt es of st or age. Therefore, t he float dat a t ype offers increased range and precision r elat ive t o t he r eal dat a t ype, but float dat a t ype v alues consum e 4 m or e byt es per colum n value. Bot h dat a t ypes follow t he I EEE ( I nst it ut e of Elect r ical and Elect ronic Engineers) 754 specificat ion for approx im at e dat a t y pes. SQL Ser ver uses t he round up m ode, which is one of four r ounding m odes in t he 754 specificat ion. M on et a ry D a t a SQL Ser ver has t wo dat a t ypes for r epr esent ing m onet ary dat a. Bot h are accurat e t o t he near est t en- t housandt h of a m onet ary unit . The sm allm oney dat a t ype has

a range fr om - 214,748.3648 t hr ough 214,748.3647. SQL Serv er r equires 4 byt es of st orage for each value w it h t his dat a t ype. The m oney dat a t ype has a range t hat st art s at - 922,337,203,685,477.5808 and runs t hrough 922,337,203,685,477.5807. This dat a t ype consum es 8 byt es of st orage for each colum n value. Wit h eit her dat a t ype, y ou can use a cur r ency sym bol, such as $, and a decim al point w hen input t ing values, but you shouldn’t input values w it h com m as. I n ot her w ords, use $1234.5678 inst ead of $1,234.5678. As y ou can see, t he t w o m onet ary dat a t ypes ar e t w o possible variat ions of t he decim al dat a t ype in t erm s of it s pr ecision and r ange. For exam ple, you can represent sm allm oney dat a t ypes wit h decim al( 10,4) . The m oney dat a t ype has decim al( 19,4) . When you need t o represent m onet ary dat a wit h ot her form at s, use alt er nat iv e decim al specificat ions, such as decim al( 19,2) or decim al( 38,2) . Da t e an d Tim e D a t a SQL Ser ver has t wo dat a t ypes for int er nally r epresent ing dat e and t im e v alues. These dat a t ypes differ in precision as w ell as range. Befor e div ing int o t he det ails of each dat a t ype, not e t hat SQL Ser ver dat a t y pes for dat e and t im e v alues always cont ain bot h a dat e and a t im e value. I n addit ion, while SQL Ser ver uses one of t w o int er nal form at s for st or ing dat e and t im e values, it displays dat e and t im e values as st r ings. I n addit ion, you will frequent ly input a new dat e or t im e colum n value as a st ring. When designat ing a dat e or a t im e value w it h a st ring, you can designat e j ust t he dat e, j ust t he t im e, or bot h t he dat e and t he t im e. The sm alldat et im e dat a t y pe has t he short er range of t he t wo dat a t ypes for dat es and t im es. This dat a t ype includes dat es from January 1, 1900, t hr ough June 6, 2079. Wit hin any given day, sm alldat et im e dat a t y pe values r epresent t im e from 12: 00 A.M. ( m idnight ) t hr ough 11: 59 P.M., t o t he near est m inut e. The sm alldat et im e dat a t y pe rounds down t o t he nearest m inut e for all values of 29.998 seconds or less. Conv ersely , it r ounds up t o t he near est m inut e for all values of 29.999 seconds or m or e. You can designat e a dat et im e value wit h a charact er st ring t o t he near est one- t housandt h of a second, such as 'January 1, 1900 12: 00: 29.998', for im plicit conv ersion as input t o colum ns wit h a sm alldat et im e dat a t ype specificat ion. Each sm alldat et im e colum n v alue r equires 4 byt es of st orage—t w o for t he dat e and t wo for t he t im e. The ot her dat a t ype for dat e and t im e values is dat et im e. Values in dat et im e for m at can range from January 1, 1753, t hr ough Decem ber 31, 9999. As wit h t he sm alldat et im e dat a t y pe, t he dat et im e dat a r epr esent s t im e from m idnight . Howev er, t he pr ecision is t o t he nearest 3.33 m illiseconds. Therefore, y ou can represent t he first t im e value aft er m idnight as '00: 00: 00: 003'. SQL Serv er rounds dat et im e values int ernally t o t he near est m illisecond wit hin it s pr ecision. For exam ple, t im e values t o t he near est m illisecond pr ogr ess from '00: 00: 00: 000' t o '00: 00: 00: 003' t o '00: 00: 00: 007'. The dat et im e dat a t ype specificat ion consum es 8 byt es of st orage—4 byt es for t he dat e and 4 byt es for t he t im e. Bin a r y D at a Binary dat a represent s dat a in it s nat iv e binar y form at . For exam ple, a GUI D, or globally unique ident ifier, appears as a 16- byt e binary dat a st ream . SQL Serv er represent s each byt e w it h t wo hex adecim al num bers. The decim al num ber 17, for exam ple, appears as 11 in hexadecim al form at , which cor responds t o 00010001 as a byt e. Hexadecim al for m at t ing uses t he let t ers A t hr ough F t o denot e t he decim al v alues 10 t hrough 15. Therefor e, t he hexadecim al num ber 9F t ranslat es t o 159 in decim al for m at , or 10011111 as t he bit s for a byt e. SQL Ser ver fr equent ly denot es hex adecim al v alues for input and display w it h a leading 0x ; t hat is, a zer o follow ed by a lowercase x. Of course, t he int er nal represent at ion cont ains j ust t he binary represent at ion for dat a.

Ther e ar e t hr ee dat a t y pes for binar y dat a in SQL Ser ver. When you ar e w or king wit h dat a st r ings of 8 KB or less, use eit her binary or varbinary. For longer binar y dat a st ream s, such as Word docum ent s or Excel w orksheet s in Office 97 or Office 2000, use t he im age dat a t ype. The follow ing t able sum m arizes t he t hr ee binary dat a t ypes. 'DWD7\SH

'DWD7\SH'HVFULSWLRQ

1DPH binary

For fixed- lengt h binar y dat a up t o 8000 byt es in lengt h. Use binary ( n) t o specify, w it h n as t he num ber of byt es. The st orage size is n byt es. Appr opriat e w hen all t he colum n values are t he sam e lengt h.

varbinary For var iable- lengt h binary dat a up t o 8000 byt es in lengt h. Use varbinary ( n) t o specify, wit h n as t he m ax im um num ber of byt es. The st orage size for any var binary colum n value is t he act ual size of a bit st ream in byt es. Appr opriat e when not all colum n values ar e t he sam e lengt h. im age

For var iable- lengt h char act er dat a t hat can grow t o 2 31 - 1 ( 2,147,483,647) byt es. Use t his dat a t ype w hen your binary dat a ex ceeds 8 KB for any colum n values. Alt hough t he dat a t ype’s nam e is im age, it accom m odat es any binar y dat a, including bit m ap or GI F im age files as well as Word .doc files.

Spe cia l Syst e m D a t a Type s Four rem aining syst em dat a t ypes com plet e t he set available for specify ing colum ns in a t able: t im est am p, bit , uniqueident ifier , and sql_var iant . These dat a t ypes don’t fit int o any one cat egor y. This sect ion addr esses each of t he dat a t ypes indiv idually. The t im est am p dat a t ype is a binary v ariable t hat t racks t he lat est addit ion or revision of a r ow t hroughout a dat abase. I t is a sequent ial num ber—som ew hat lik e an aut onum ber in Access or an int eger wit h an I DENTI TY pr opert y set t ing in SQL Ser ver. How ev er, it pert ains t o an ent ire dat abase inst ead of a single t able wit hin a dat abase. Whenever a user adds a new r ow or r ev ises a v alue in a row of a t able wit h a t im est am p colum n, t he t im est am p colum n value incr eases by 1. SQL Ser ver repr esent s t his t im est am p v alue as an 8- byt e binary value. I f t he largest t im est am p value t hr oughout any row in any t able of a dat abase is 0x13579BDF, t he next t im est am p value w ill be 0x13579BE0.

N ot e Colum ns declared wit h a t im est am p dat a t y pe don’t cont ain dat et im e or sm alldat et im e values. Micr osoft announced it s int ent ion t o reference t he t im est am p dat a t y pe as t he rowv er sion dat a t ype in fut ur e SQL Ser v er ver sions. The bit dat a t ype is for r epr esent ing Tr ue/ False or Yes/ No dat a. I n SQL Ser ver, a bit dat a t ype w it h t he v alue 1 is equivalent t o True or Yes. The bit value 0 corr esponds t o False or No. You can, opt ionally, m ak e a bit dat a t ype nullable so t hat it can hav e t he value 0, 1, or NULL. Values in bit form at consum e 1 bit , and SQL Ser ver packs bit dat a values 8 bit s t o t he byt e t o conser ve space. Ther efor e, 1 t hrough 8 bit dat a t ype colum ns in a row requir e 1 byt e of st orage. The nint h t hr ough t he sixt eent h bit dat a t ype colum ns add a second byt e of st orage for each row. The uniqueident ifier dat a t ype specifies a 16- by t e GUI D. Since a GUI D is unique in space and t im e, t he uniqueident ifier is a candidat e for ident ify ing r ow s across

m ult iple inst allat ions of SQL Ser ver, such as by st at e in t he Unit ed St at es or by count ry. How ev er , because of it s size, using a uniqueident ifier can slow an applicat ion and consum e st orage dispr oport ionat ely. The sever it y of t his uniqueident ifier w eak ness escalat es wit h t he num ber of rows in a t able. Consider using an int ( or ev en a bigint ) colum n w it h an I DENTI TY propert y along wit h a second colum n t o denot e place. This alt ernat iv e approach t o uniquely ident ify ing records at m ult iple locat ions can m ake an applicat ion r un fast er and consum e less st orage. Also, t he uniqueident ifier doesn’t w or k well for t he full range of SQL Ser ver funct ions; see t he “ uniqueident ifier” and “ Using uniqueident ifier Dat a” t opics in Books Online for m ore det ail. A sql_var iant dat a t ype specificat ion enables a colum n in a t able t o accept a m ix ed collect ion of dat a values based on any ot her syst em dat a t ype ex cept t ext , nt ext , t im est am p, im age, and sql_var iant . All ot her syst em dat a t ypes requir e t he values in a colum n t o be of one dat a t ype. You can’t ent er charact er dat a int o a colum n wit h an int dat a t ype. Wit h a sql_var iant dat a t ype specificat ion, a single colum n can cont ain char, int , and dat et im e dat a values all in a single colum n. The sql_var iant dat a t ype nam e der iv es it s nam e because of it s sim ilarit y t o t he Visual Basic Var iant dat a t ype. The m ix ed dat a t ype values in a sql_var iant colum n can cause it s values t o behave different ly w hen you’re com par ing sql_v ariant values w it h values of anot her dat a t ype or w hen y ou’r e sort ing a t able by t he values in a sql_var iant colum n. See t he “ Using sql_v ariant Dat a” t opic in Books Online for m or e det ails on t his t opic. 'DWD7\SH1DPH

'DWD7\SH'HVFULSWLRQ

t im est am p

The t im est am p dat a t ype has a binary ( 8) dat a form at unless you m ake it nullable. A nullable t im est am p dat a t ype has a varbinary ( 8) dat a form at . SQL Serv er aut om at ically generat es t im est am p values; your applicat ion or your users hav e no need t o populat e a t im est am p colum n.

bit

Pr im arily for m odeling at t ribut es t hat can hav e one of t w o st at es. Howev er, t he dat a t ype also does perm it NULL v alues. SQL Ser ver opt im izes st orage of bit dat a t ype values so t hat t he first 8 t ake up t o 1 byt e, t he next 8 a second byt e, and so on.

uniqueident ifier A 16- byt e binary num ber t hat r epresent s it s value as 32 hexadecim al charact ers. The form at for display ing t he hexadecim al charact ers is xx xxx xxx - x xxx - x xxx - xxxx xxxx xxx xxx xx. Use t he NewI D funct ion t o generat e a new uniqueident ifier. You can specify a uniqueident ifier value eit her wit h a charact er st r ing r epr esent ing t he 32 hex adecim al charact ers or w it h a binary num ber. Howev er, always use t he NewI D funct ion w hen y ou m ust ensur e t he uniqueness of t he uniqueident ifier value. sql_var iant

For st or ing m ult iple ot her k inds of dat a t ype values in a single colum n. This SQL Serv er dat a t ype bears a r esem blance t o t he Visual Basic Var iant dat a t ype.

N ot e Two addit ional SQL Server t ypes do not r epresent individual num bers or st r ing values. These ar e t he t able and cursor t y pes. The t able ty pe can represent a whole r esult set of values, such as a t able r et urned by a user- defined funct ion. Chapt er 5 dem onst rat es t he use of t his SQL Serv er t ype. The SQL Server cur sor t ype refers t o server- side cur sors. Many

dev eloper s prefer t o avoid t his t ype because it can degr ade dat abase perfor m ance. Use r - D e f ine d D a t a Type s User- defined dat a t ypes enable you t o define cust om dat a t ypes based on syst em dat a t ype and nullabilit y . You and your dev eloper t eam can t hen apply t hese user - defined dat a t ypes in m ult iple t ables t hroughout a dat abase. The scope of a user - defined dat a t ype is t he cur rent dat abase, but y ou can copy t he script defining a user - defined dat abase t o ot her dat abases. Use t he sp_addt ype and sp_dropt y pe syst em st or ed procedur es t o add and drop user - defined dat a t ypes t o and from a dat abase. When you add a user - defined dat a t ype w it h sp_addt ype, specify it s nam e, base dat a t ype, and nullabilit y by posit ion or w it h param et er nam e assignm ent s. Aft er creat ing a user - defined dat a t ype, you can assign it t o t able colum ns t hroughout a dat abase. I f y ou add a user - defined dat a t ype t o t he m odel dat abase, every new user - defined dat abase will hav e t he user - defined dat a t ype. This is because SQL Serv er uses t he m odel dat abase as a st art ing point for all user- defined dat abases. The sp_dropt y pe syst em st ored procedur e can generally r em ove a user- defined dat a t ype from a dat abase, but t his syst em st ored pr ocedur e w on’t succeed if any t ables ex ist wit h colum ns defined by t he user - defined dat a t ype.

N ot e Sy st em st ored procedures, such as sp_addt ype and sp_dropt ype, gain focus as a general t opic in Chapt er 4. The sp_addt ype syst em st ored pr ocedur e allows you t o specify a user - defined dat a t ype in t erm s of it s base dat a t ype. For ex am ple, y ou can designat e a post al code dat a t ype w it h a char( 5) or a char ( 9) base dat a t ype. How ever, users can st ill ent er values ot her t han num bers in t he post al code charact er fields. To specify const raint s t hat apply t o user - defined dat a t ypes, designat e a SQL Ser ver Rule obj ect and bind t he rule t o t he user - defined dat a t ype; use t he sp_bindr ule syst em st ored procedur e t o bind a r ule. Then dev elopers can specify new t ables wit h colum ns specified by user - defined dat a t y pes t hat conv ey t he r ule. This approach for apply ing r ules bound t o user - defined dat a t ypes works for t he creat ion of new t ables but not for t he m odificat ion of ex ist ing colum ns in ex ist ing t ables. For im plem ent at ion det ails of user - defined dat a t ypes, see t he Books Online t opics for sp_addt y pe, sp_dr opt ype, and sp_bindr ule.

N ot e Exam ine t he pubs sam ple dat abase in Ent er prise Manager for several ex am ples of how t o apply user- defined dat a t y pes in a dat abase.

Scr ipt in g Ta ble s The “ Dat a Types for Tables” sect ion earlier in t his chapt er described t he m ost fundam ent al elem ent s of a t able. How ev er , it didn’t dem onst rat e how t o apply t hose elem ent s t o t he const r uct ion of a t able. This sect ion int r oduces T- SQL st at em ent s and synt ax rules for cr eat ing t ables. The sect ion also exam ines issues

relat ing t o t he processing of select ed dat a t ypes and t he m odify ing of a t able’s design.

Cr e a t in g a Ta ble You use t he CREATE TABLE st at em ent t o creat e a new t able. Before inv ok ing t he st at em ent , y ou m ust designat e a dat abase t o hold y our new t able. Specify t his dat abase wit h t he USE st at em ent . The follow ing sam ple script assigns it s new t able, Em ailCont act s, t o t he Chapt er02 dat abase; r ecall t hat t he “ Chapt er Resources” sect ion includes a script for creat ing a fresh copy of t his dat abase. Wit hin t he CREATE TABLE st at em ent , y ou can specify colum n nam es for t he t able along w it h dat a t ypes and ot her set t ings for each colum n. The script cr eat es a t able nam ed Em ailCont act s wit h four colum ns nam ed Cont act I D, First Nam e, Last Nam e, and Em ail1. The Cont act I D colum n ser ves as a prim ary k ey. The colum n’s specificat ion includes a nam e ( Cont act I D) , a dat a t ype ( int ) , and a specificat ion for it s nullabilit y ( NOT NULL) ; and t he last key word designat es t he colum n as a pr im ary key. Because t he t able includes addit ional colum ns, t he colum n declarat ion ends wit h a com m a. The rem aining t hree declarat ions wit hin t he CREATE TABLE st at em ent specify colum ns for holding cont act dat a. Each of t hese declarat ions begins wit h a colum n nam e follow ed by a dat a t ype and a nullabilit y assignm ent . A com m a separat es t he declarat ion for each colum n. I n cont rast wit h t he colum n serv ing as t he prim ary k ey for t he t able, t he t hr ee colum ns for st or ing cont act dat a can be null. This allows a user t o creat e a row for a cont act at one t im e and t hen populat e t he row at a lat er t im e. SQL Ser ver has a default set t ing for t he nullabilit y of colum ns t hat y ou can configur e. The default configurat ion is for ANSI com pat ibilit y, w hich allows nulls for new colum ns. Nev ert heless, it is good pract ice t o designat e t he nullabilit y of colum ns ex plicit ly. --CreateEmailContactsTable_01 --Execute statements after USE from Chapter02 database USE Chapter02 --Create EmailContacts with three columns. CREATE TABLE EmailContacts ( ContactID int Not Null PRIMARY KEY, FirstName nvarchar(20) NULL, LastName nvarchar(35) NULL, Email1 nvarchar (255) NULL ) GO The script will work t he first t im e y ou run it . Howev er , if you t ry t o run t he script a second t im e, it will fail wit h a m essage rem inding y ou t hat t he Em ailCont act s t able is already in t he dat abase. I n order t o rer un t he CREATE TABLE st at em ent successfully, you can condit ionally dr op t he Em ailCont act s t able. You need t o drop t he t able condit ionally because t he DROP TABLE st at em ent w ill fail if t he t able isn’t already in t he dat abase. While y ou’r e edit ing t he preceding script , it m ight be nice t o add som e dat a and t hen r un a sim ple SELECT query t o see how t o insert and ret rieve dat a fr om t he t able. The next script dem onst rat es t echniques for achiev ing t hese r esult s. This nex t script illust rat es broad design issues for r unning T- SQL script s in Query Analyzer. For exam ple, t he USE st at em ent designat es a source dat abase t o use for r unning t he st at em ent . USE isn’t a T- SQL st at em ent ; rat her , it is a keyw ord for Quer y Analyzer t hat inst ruct s it t o connect t o a dat abase on t he ser ver for t he curr ent Query Analyzer session. I f t he dat abase doesn’t ex ist on t he connect ion, Query Analyzer r et ur ns an er ror m essage. Not ice also t hat bat ches of T- SQL st at em ent s ar e delim it ed by t he GO k eyw ord. This is a keyword for Query

Analyzer as w ell. The GO k eyw ord inst r uct s Quer y Analyzer t o int erpret and run t he preceding T- SQL st at em ent s. Posit ion t he GO k eyw ord in script s t o ensur e t hat a set of st at em ent s will r un before y ou st ar t anot her set of st at em ent s. This keyw ord is convenient for isolat ing er rors. Aft er t he USE st at em ent , t he script t est s for t he prior ex ist ence of t he Em ailCont act s t able. I f it does ex ist in t he curr ent dat abase, t he script inv ok es a DROP TABLE st at em ent t o r em ov e t he pr ior v er sion of t he t able. An I F EXI STS st at em ent based on an I NFORMATI ON_SCHEMA view is a com m on m eans of t est ing for t he ex ist ence of a dat abase obj ect . I NFORMATI ON_SCHEMA views ret ur n m et adat a about m any classes of SQL Serv er dat abase obj ect s besides t ables. A subsequent sect ion dwells on t his t opic m or e specifically. The CREATE TABLE st at em ent is ident ical t o t he preceding T- SQL list ing. Howev er, in t he cont ext of t his sam ple, y ou can r er un t he script r epeat edly wit hout encount er ing an err or m essage about t he obj ect already ex ist ing. Aft er creat ing t he t able, t he follow ing list ing populat es t he t able w it h t wo row s. I t uses t he I NSERT I NTO st at em ent t o add r ows. Because t hese st at em ent s designat e colum n values for all t he t able’s colum ns in t he order in w hich t hey appear in t he t able, t he st at em ent s can sim ply r eference t he VALUES k ey word follow ed by t he colum n values for a row . --CreateEmailContactsTable_02 --Execute statements after USE from Chapter02 database. USE Chapter02 GO --Remove prior version of EmailContacts if it exists. IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ’EmailContacts’ ) DROP TABLE EmailContacts --Create EmailContacts with three columns. CREATE TABLE EmailContacts ( ContactID int Not Null PRIMARY KEY, FirstName nvarchar(20) NULL, LastName nvarchar(35) NULL, Email1 nvarchar (255) NULL ) GO --Populate EmailContacts and run a SELECT query. INSERT INTO EmailContacts VALUES(1,’Rick’, ’Dobson’, ’[email protected]’) INSERT INTO EmailContacts VALUES(2,’Virginia’, ’Dobson’, ’[email protected]’) SELECT * FROM EmailContacts GO A SELECT st at em ent closes t he scr ipt . When t he SELECT st at em ent r uns, Query Analyzer displays t he r esult set in t he Result s pane, as show n in Figure 2- 1. Figu re 2 - 1 . Th e r e su lt se t from t h e script t o cre a t e, popu la t e , a n d list va lu e s for t h e Em ailCon t a ct s t a b le.

Vie w in g M e t a da t a Met adat a is t he inform at ion about dat a, such as a dat abase ser ver and it s obj ect s, including dat abases, t ables, and k eys. So far, t his chapt er has review ed t wo m ain T- SQL sam ples. One of t hese cr eat ed a dat abase—Chapt er02. The ot her creat ed a t able—Em ailCont act s—wit hin t he dat abase. I n t ur n, t he t able has several colum ns, and one of t hose colum ns is defined as a prim ary key . I t is oft en useful t o be able t o generat e report s t hat cont ain inform at ion about t he cont ent s of a dat abase ser ver and it s obj ect s. For exam ple, t he pr ev ious sam ple show ed t hat det erm ining w het her a t able alr eady ex ist ed in a dat abase would allow y our applicat ion t o avoid an er ror—t ry ing t o creat e a new t able w it h t he sam e nam e as an ex ist ing one. SQL Serv er I NFORMATI ON_SCHEMA v iews can der iv e t his k ind of inform at ion for your applicat ions. This sect ion exam ines t his capabilit y by dem onst rat ing it . The follow ing T- SQL script includes four bat ches of st at em ent s—each t erm inat ed by t he GO k eyw ord—t hat illust rat e different uses and form at s for der iv ing m et adat a w it h I NFORMATI ON_SCHEMA v iews. The init ial bat ch dem onst rat es t he synt ax for r eport ing t he dat abases wit hin a connect ion. I n t his case, t he connect ion is t he one based on y our login t o Query Analyzer and t he m ast er dat abase for t he SQL Serv er inst ance. The m ast er dat abase is one of t he syst em dat abases t hat SQL Ser ver creat es w hen you inst all it . This dat abase is vit al t o t he proper operat ion of a SQL Serv er inst ance. One funct ion of t his dat abase is t o t rack inform at ion about all t he dat abases on a SQL Ser ver inst ance. The SCHEMATA v iew of t he I NFORMATI ON_SCHEMA ret ur ns a high- lev el sum m ary of t hat inform at ion. The next bat ch of T- SQL st at em ent s begins by changing t he cont ext for t he st at em ent s from t he m ast er dat abase t o t he Chapt er02 dat abase. This bat ch ret ur ns all t he colum ns from t he TABLES v iew of t he I NFORMATI ON_SCHEMA for which t he t able’s nam e doesn’t begin w it h eit her sys or dt p. While user s can creat e t ables w it h nam es t hat begin w it h eit her of t hese charact er st rings, SQL Ser ver uses t ables beginning wit h t hese charact ers t o m anage a dat abase. Ther efor e, excluding t ables t hat begin wit h t hose charact ers can ret ur n inform at ion about user - defined t ables. Of course, if y our applicat ion creat es any t ables beginning w it h t hese prefixes, t hey w on’t appear in t he result set for t he TABLES v iew.

N ot e The TABLES v iew for I NFORMATI ON_SCHEMA r et urns

infor m at ion about views as well as t ables. Specify a TABLE_TYPE colum n value of VI EW in t he WHERE clause for a SELECT st at em ent t o ret urn only v iews. Wit h t he COLUMNS v iew of t he I NFORMATI ON_SCHEMA, you can ret urn inform at ion about colum ns in a dat abase. The t hird bat ch illust rat es t his applicat ion. I t also r eveals a new sy nt ax for specify ing t he dat abase ser ving as t he source for t he v iew. Not ice t hat t he specificat ion of t he v iew nam e has t hr ee part s. The first of t hese is t he dat abase nam e—Chapt er02. Designat ing a dat abase nam e as t he first part r em ov es t he need t o designat e a dat abase cont ext w it h a USE st at em ent . This is because no m at t er w hat dat abase cont ext t he st at em ent ex ecut es, it always ext ract s infor m at ion fr om t he dat abase—t hat is, t he first part of t he I NFORMATI ON_SCHEMA view nam e. The second and t hird part s follow t he conv ent ion for t he pr eceding bat ches except for t he nam e of t he specific I NFORMATI ON_SCHEMA v iew ( COLUMNS) . The sam ple also includes a WHERE clause t o reference a part icular t able—in part icular, Em ailCont act s. Wit hout t he WHERE clause, t he T- SQL st at em ent in t he bat ch will ret ur n inform at ion for all t he colum ns w it hin t he Chapt er02 dat abase, including t hose from syst em and user - defined t ables. The final bat ch shows t he I NFORMATI ON_SCHEMA sy nt ax for r eport ing about t he keys in a dat abase. These include t he pr im ar y k eys, for eign k eys, and unique keys. The inform at ion is r eally about t he colum ns on w hich an applicat ion defines it s keys. As w it h t he pr eceding bat ch, t his sam ple rest r ict s t he r esult only t o keys for t he Em ailCont act s t able. --INFORMATION_SCHEMA_Samples --List databases on current server. USE master SELECT * FROM INFORMATION_SCHEMA.SCHEMATA GO --List user-defined tables in Chapter02 database. USE Chapter02 SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE NOT(SUBSTRING(TABLE_NAME,1,3) = ’sys’ OR SUBSTRING(TABLE_NAME,1,3) = ’dtp’) GO --List all columns in EmailContacts table. SELECT * FROM Chapter02.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ’EmailContacts’ GO --List data on columns constrained as keys in --the EmailContacts table. SELECT * FROM Chapter02.INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = ’EmailContacts’ GO Figur e 2- 2 displays an excerpt from t he r esult set for t he preceding script . The ret ur n for each bat ch begins wit h a new set of colum n headers. The list of dat abases includes our user - defined dat abase, Chapt er02, along w it h t he t wo SQL Ser ver sam ple dat abases, pubs and Nort hwind, as well as t he four syst em dat abases. The second header shows j ust one r ow for t he lone t able in Chapt er02. The t hird header r ows rev eal t he nam es of t he four colum ns wit hin t he Em ailCont act s t able. This view pr ov ides m uch addit ional inform at ion about each colum n, such as it s nullabilit y, dat a t ype, and relat ed set t ings, including it s precision and scale if appropriat e. The row for t he last set of colum n headers prov ides inform at ion about t he lone k ey for t he Em ailCont act s t able. This is t he t able’s pr im ar y k ey . Each k ey has a nam e, w hich appears in t he

CONSTRAI NT_NAME colum n. Because our synt ax for t he creat ion of t he t able didn’t specify a nam e for t he pr im ar y k ey , t he last row of out put in Figure 2- 2 shows t he syst em - generat ed nam e for t he t able’s pr im ary key in t he CONSTRAI NT_NAME colum n. A subsequent sam ple in t he “ Scr ipt ing Key s and I ndexes” sect ion illust rat es t he synt ax for assigning a specific nam e t o a pr im ar y key . Figu r e 2 - 2 . Sa m ple ou t pu t from a se t of fou r T- SQL ba t ch e s illust r a t in g t h e b eh avior of I N FORM ATI ON _ SCH EM A vie w s.

I NFORMATI ON_SCHEMA offers m any m or e v iew s besides t hose illust rat ed in t he preceding four bat ches. For exam ple, you can gat her inform at ion about check const raint s for colum n v alues, t able const raint s, st ored pr ocedur es, and userdefined funct ions. Refer t o t he “ I nform at ion Schem a View” t opic in Book s Online for an overv iew of t he I NFORMATI ON_SCHEMA views along w it h links defining t he result set for each t ype of view available.

W or k ing w it h Colum n D a t a Type s The “ Cr eat ing a Table” sect ion int r oduced t he CREATE TABLE st at em ent synt ax and dem onst rat ed how t o declar e t ypical syst em dat a t ypes such as int and nvarchar. Apply ing t his fram ework w ill enable y ou t o assign t he ot her dat a t ypes t o colum ns as well. I n spit e of t he sim plicit y of t he ov erall approach, t her e are special issues for som e dat a t ypes, and one dat a t ype hasn’t been cov ered yet . This sect ion r ev iews t hese issues. Com p ar in g t im e st am p an d da t e t im e D at a Typ e s Those w ho ar e m igrat ing t o SQL Ser ver m ay be confused at first by t he t im est am p dat a t ype and whet her it has anyt hing t o do w it h dat et im e dat a ( it doesn’t ) . The row version alias for t im est am p act ually sum m ar izes t he purpose of

t he t im est am p dat a t ype m ore precisely. This m ay be one r eason w hy Micr osoft plans t o use t he r owv er sion nam e m or e prom inent ly in t he fut ur e. The follow ing scr ipt cont rast s t he t im est am p and dat et im e dat a t ypes. The cont rast r elies on t wo t ables, t 1 and t 2, each w it h t hr ee colum ns, col1, col2, and col3. The col1 colum n has an int dat a t ype and offers a value for program m at ically populat ing r ows in each t able. The col2 and col3 colum ns populat e aut om at ically . The dat a t ype for col2 is dat et im e, but it has a DEFAULT const raint t hat assigns t he cur r ent t im e aut om at ically . Users and your applicat ion’s code can override t his default value. The t im est am p dat a t ype also aut om at ically populat es col3 in bot h t ables. Howev er , for t his dat a t ype, only SQL Ser ver updat es t he value. This occurs wit h t he insert ion of a new row or t he revision of any value in an ex ist ing row . Aft er creat ing t he t 1 and t 2 t ables, t he scr ipt does a couple of operat ions t o cont rast t im est am p and dat et im e dat a t ypes. The script insert s a recor d int o each t able w it h a delay of 1 second bet w een each insert ion. The WAI TFOR DELAY st at em ent act ually suspends t he operat ion of SQL Ser ver for t he durat ion of it s argum ent . Ther efore, t he insert ion for t able t 2 can occur m or e t han 1 second aft er t he insert ion for t able t 1 because SQL Ser ver r equires t im e t o per for m t he operat ion. Aft er running a SELECT query t o show t he colum n values in t ables t 1 and t 2, t he script next updat es t he value of col1 in t able t 2. Then it reruns t he SELECT quer y t o dem onst rat e t he im pact of t he operat ion on t he colum n values in t he sam ple. At t he sam ple’s conclusion, t he script r em ov es t he t 1 and t 2 t ables from t he Chapt er02 dat abase. --CompareTimestampToDatetime --Execute statements after USE from Chapter02 database. USE Chapter02 --Create two tables named t1 and t2. CREATE TABLE t1 ( col1 int, col2 datetime DEFAULT GETDATE(), col3 timestamp ) CREATE TABLE t2 ( col1 int, col2 datetime DEFAULT GETDATE(), col3 timestamp ) GO --Insert a row in tables t1 and t2 with --a one-second delay between tables. INSERT INTO t1 (col1) VALUES (1) WAITFOR DELAY ’00:00:01’ INSERT INTO t2 (col1) VALUES (1) GO --Run queries on tables t1 and t2. SELECT ’t1’ AS ’Table Name’, * FROM t1 SELECT ’t2’ AS ’Table Name’, * FROM t2 GO --Update column col1 in table t2. UPDATE t2 SET col1 = col1 + 2 GO --Re-run queries on tables t1 and t2.

SELECT ’t1’ AS ’Table Name’, * FROM t1 SELECT ’t2’ AS ’Table Name’, * FROM t2 GO --Drop tables t1 and t2. DROP TABLE t1 DROP TABLE t2 GO Figur e 2- 3 shows t he Result s pane fr om Query Analyzer for t he pr eceding script . The col2 value for t he second row is 1 second plus a SQL Serv er clock t ick ( 3 m illiseconds) behind t he col2 value for t he first r ow. This clock t ick is t he t im e t hat it t ak es t o com plet e t he r ow insert ion for t able t 2. The col3 values for t he first and second rows ar e displaced by 1. Because t he insert ion for t able t 2 occurr ed im m ediat ely aft er t he one for t able t 1, t his is appropr iat e. I f ot her insert ions t ook place bet ween t he init ial insert ion for t able t 1 and t able t 2, t he difference in t he binary value for col3 w ould be great er . The updat e of col1 for t able t 2 dem onst rat es t his point . The second pair of rows in Figur e 2- 3 also displays t he colum n values for t ables t 1 and t 2 aft er an updat e t o col1 in t able t 2. I n t he case of t able t 1, t he col3 value r em ains unalt er ed. How ev er , t he col3 value for t able t 2 gr ows by 1 fr om it s init ial value aft er t he insert ion. This incr eased v alue r eflect s t he im pact of t he updat e t o col1 in t able t 2. While t he second pair of r ows var ies from t he first pair for col3 in Figure 2- 3, t he col2 values are ident ical bet w een t he first and second pair of rows. This is because updat ing v alues of ot her colum ns has no im pact on t he dat et im e values in col2, but updat ing any v alue in a r ow does im pact t he value of t he t im est am p colum n value in t he row .

N ot e You can hav e j ust one colum n per t able w it h a t im est am p dat a t ype. Figu r e 2 - 3 . Sa m p le ou t pu t con t r a st in g t h e beh avior of d a t e t im e an d t im e st a m p d a t a t ype s.

Usin g sql_ va ria n t Da t a Typ e V alu e s The sql_v ar iant dat a t y pe is t he only dat a t ype t hat let s y ou st or e differ ent dat a t ypes in t he sam e colum n. This capabilit y is useful for st oring a collect ion of values in a colum n in w hich y ou don’t k now in advance what t ypes of values you’ll

hav e. This can ar ise in a sit uat ion in which y ou let a user define values on an ad hoc basis. Consider a t able t hat st or es m iscellaneous infor m at ion about cont act s. Som et im e your applicat ion m ay need t o st or e a m oney dat a t ype, anot her t im e a user m ay want t o specify a dat e, and in y et ot her cases, your applicat ion m ay need t o designat e a var iable- lengt h charact er value. This kind of scenar io is t ypical of sit uat ions in w hich y our applicat ion needs t o charact er ize elem ent s but t he com plet e set of elem ent s and t heir at t r ibut es isn’t known at t he t im e t hat you dev elop t he applicat ion. The follow ing scr ipt assigns a set of ext ended propert ies t o a t able of cont act s ident ified by a Cont act I D colum n. Not ice t hat t he CREATE TABLE st at em ent uses t hr ee colum ns t o charact er ize t he cont act s. The m ost im port ant colum n is PropValue, w hich has a sql_var iant dat a t ype. This colum n st ores t he act ual value t hat charact er izes a cont act . I n som e cases, t he cont act charact er ist ic is a m onet ary value, in ot her cases it is a dat e, and in st ill ot her cases it is a st ring value, such as t he nam e of a fav or it e sport or st or e. Pr opI D and PropNam e describe t he charact erist ic for t he cont act . Pr opNam e m ak es it easy t o follow what t he Pr opValue colum n values describe w it hout r equir ing anot her t able t o decode t he PropI D colum n values. A subsequent sam ple w ill ret urn t o t he Cont act Ext Pr ops t able and link it t o ot her t ables cont aining cont act and propert y nam es. I n addit ion, t hat sam ple w ill add a pr im ary key t o t he t able. These refinem ent s ar en’t necessary t o dem onst rat e t he behavior of sql_var iant dat a t ypes. The I NSERT I NTO st at em ent s t hat add v alues t o t he Pr opValue colum n use CAST funct ions t o est ablish sub dat a t ypes w it hin t he sql_var iant colum n. This isn’t st rict ly necessary, but t he CAST funct ion confir m s t he abilit y of t he sql_var iant dat a t ype t o accept m ult iple ot her dat a t ypes. --SQL_variantSample --Execute statements after USE from Chapter02 database. USE Chapter02 GO --Remove prior version of ContactExtProps if it exists. IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ’ContactExtProps’ ) DROP TABLE ContactExtProps GO --Create ContactExtProps with four columns. CREATE TABLE ContactExtProps ( ContactID int NOT NULL, PropID int NOT NULL, PropName nvarchar(20), PropValue sql_variant ) GO --Populate ContactExtProps with values. INSERT INTO ContactExtProps VALUES(1, 1,’Birthday’, CAST(‘9/9/1944’ AS datetime)) INSERT INTO ContactExtProps VALUES(1, 2, ’Salary’, CAST(50000 AS money)) INSERT INTO ContactExtProps VALUES(1, 3, ’Bonus’, CAST(30000 AS money))

INSERT INTO ContactExtProps VALUES(1, 4, ’Favorite Sport’, ’Boxing’) INSERT INTO ContactExtProps VALUES(2, 1, ’Birthday’, CAST(‘1/1/1950’ AS datetime)) INSERT INTO ContactExtProps VALUES(2, 2, ’Salary’, CAST(60000 AS money)) INSERT INTO ContactExtProps VALUES(2, 3, ’Bonus’, CAST(40000 AS money)) INSERT INTO ContactExtProps VALUES(2, 5, ’Favorite Store’, CAST(‘Tailspin Toys’ AS nvarchar(2 0))) GO --Select all records with a Favorite Store property. SELECT ContactID, PropName, PropValue FROM ContactExtProps WHERE PropName = ’Favorite Store’ GO --Select Salary and Bonus properties and add one to --money data type for Salary and Bonus properties. SELECT ContactID, PropName, Cast(PropValue AS money)+1, PropValue FROM ContactExtProps WHERE PropID >=2 and PropID =2 and PropID 0 ALTER TABLE ContactExtProps DROP CONSTRAINT pk_ContactExtProps_ContactID_PropID GO --Add Primary Key based on ContactID and PropID. ALTER TABLE ContactExtProps ADD CONSTRAINT pk_ContactExtProps_ContactID_PropID PRIMARY KEY NONCLUSTERED ( ContactID, PropID ) --List primary key columns in ContactExtProps. EXEC sp_pkeys ’ContactExtProps’ The preceding script closes by inv ok ing t he sp_pkeys syst em st ored pr ocedur e. The out put from t he procedur e appears in Figur e 2- 7. Not ice t hat it cont ains t wo rows—one for each colum n t hat cont r ibut es t o t he pr im ary key for t he Cont act Ext Pr ops t able. Figu r e 2 - 7 . Ou t p ut fr om t h e sp_ pk eys syst e m st or ed pr oced u r e t h a t sh ow s a pr im ar y k ey d efin ed on t w o colu m n s.

Fore ig n Keys Foreign keys ar e colum n values in one t able t hat point t o t he pr im ary k ey or unique k ey in anot her t able. Specifying a for eign k ey enforces refer ent ial int egr it y bet ween t he t wo t ables. Refer ent ial int egr it y r equir es all new r ecords added t o t he t able wit h t he for eign k ey t o m at ch eit her a prim ary or a unique key value in t he ot her t able if it isn’t null. You can opt ionally specify act ions t o occur when y ou updat e or rem ov e a pr im ary or unique k ey in t he t able on t he ot her end of t he for eign k ey relat ionship. Specifically, you can cascade t he change fr om t he t able wit h t he pr im ary or unique k ey t o t he one w it h t he for eign k ey. Alt er nat ively, y ou can choose no act ion t o occur in t he t able wit h t he foreign k ey as a consequence of updat es t o t he t able wit h t he pr im ary or unique k ey. The follow ing scr ipt adds a couple of for eign k eys t o t he Cont act Ext Props t able. The first foreign key uses t he Cont act I D in t he Cont act Ext Props t able t o refer t o t he prim ary key for t he Em ailCont act s t able. The second for eign k ey uses t he Cont act Ext Pr ops t able v ia it s PropI D colum n values t o r efer t o t he Ex t Pr ops t able. Because t he Ext Props t able doesn’t init ially hav e a pr im ary or a unique k ey, t he t able cannot part icipat e in a foreign key r elat ionship. Ther efor e, t he script first adds const raint s t o t he PropI D colum n in Ext Props so t hat it serv es as t he t able’s prim ary k ey. Then it declar es t he for eign k ey r elat ionship bet w een t he

Cont act Ext Pr ops t able and t he Ext Pr ops t able. Alt hough t he first for eign key doesn’t declare any cascading act ion, t he declarat ion for t he second foreign key specifies cascading updat es. The script sam ple illust rat es t he sy nt ax for designat ing cascading updat es in it s declarat ion. Aft er t he second foreign k ey declarat ion, t he script t est s t he cascading updat e behav ior by m ak ing a change t o a Pr opI D value in t he Ex t Props t able and t hen v er ify ing t hat t he updat e cascades t o t he corr esponding PropI D value in t he Cont act Ext Pr ops t able. The script sam ple concludes by rest oring t he values and t he dat abase design t o t heir form er st at e befor e t he addit ion of eit her for eign k ey. This m ak es it possible t o r erun t he script wit hout any m anual set up act ivit y bet ween runs. You add a for eign k ey t o a t able as a const raint . The synt ax for perfor m ing t his t ask has at least t hr ee st eps, and it can hav e m or e if you specify a cascading act ion. Begin t he foreign k ey declarat ion inside an ALTER TABLE st at em ent . Aft er you open t he ALTER TABLE st at em ent , t he first st ep is t o indicat e t hat you want t o add a const raint w it h t he ADD and CONSTRAI NT k ey words. You can, opt ionally, assign a nam e t o t he foreign k ey const raint . Next add t he FOREI GN KEY k ey word and follow it wit h parent heses cont aining t he nam es of t he colum ns from t he cur rent t able part icipat ing in t he r elat ionship. Third add REFERENCES as a keyword. Follow t his k eyw ord w it h t he nam e of t he t able t o which t he relat ionship refers. Then, in parent heses aft er t he t able nam e, add t he colum n nam es fr om t hat t able t hat part icipat e in t he r elat ionship. By default , updat e and delet e act ions don’t cascade fr om t he t able wit h t he unique key or prim ary key t o t he t able wit h t he for eign k ey . How ev er, y ou can opt ionally add an ON UPDATE or ON DELETE clause t o t he for eign k ey declarat ion. I nclude in eit her clause CASCADE t o t ransfer t he act ion from t he t able wit h t he prim ary or unique k ey t o t he one w it h t he foreign key. --ForeignKeysSamples --Beginning of first FOREIGN KEY sample. USE Chapter02 --Remove FOREIGN KEY constraint if it exists already. EXEC sp_fkeys @fktable_name = N’ContactExtProps’ IF @@ROWCOUNT > 0 BEGIN ALTER TABLE ContactExtProps DROP CONSTRAINT ContactExtProps_fkey_ContactID END --Then, add a new FOREIGN KEY constraint. ALTER TABLE ContactExtProps ADD CONSTRAINT ContactExtProps_fkey_ContactID FOREIGN KEY (ContactID) REFERENCES EmailContacts(ContactID) --Verify addition of new constraint. EXEC sp_fkeys @fktable_name = N’ContactExtProps’ --End of first FOREIGN KEY sample

--Beginning of second FOREIGN KEY sample. --Convert PropID in ExtProps to NOT NULL. ALTER TABLE ExtProps ALTER COLUMN PropID int NOT NULL GO --Then, define a primary key on PropID. ALTER TABLE ExtProps ADD CONSTRAINT pk_PropID PRIMARY KEY CLUSTERED (PropID) GO

ALTER TABLE ContactExtProps ADD CONSTRAINT ContactExtProps_fkey_PropID FOREIGN KEY (PropID) REFERENCES ExtProps(PropID) ON UPDATE CASCADE --Verify addition of new constraint. EXEC sp_fkeys @fktable_name = N’ContactExtProps’ GO --List ExtProps and ContactExtProps rows before --update to ExtProps. SELECT * FROM ExtProps SELECT * FROM ContactExtProps --Then, make a change in ExtProps that --cascades to ContactExtProps. UPDATE ExtProps SET PropID = 50 WHERE PropID = 5 --List ExtProps and ContactExtProps rows after --update to ExtProps. SELECT * FROM ExtProps SELECT * FROM ContactExtProps GO --End of second FOREIGN KEY sample. --Do cleanup chores. --Start to restore by resetting PropID values. UPDATE ExtProps SET PropID = 5 WHERE PropID = 50 --Next, drop FOREIGN KEY constraints. ALTER TABLE ContactExtProps DROP CONSTRAINT ContactExtProps_fkey_ContactID ALTER TABLE ContactExtProps DROP CONSTRAINT ContactExtProps_fkey_PropID --Then, drop PRIMARY KEY constraint first . ALTER TABLE ExtProps DROP CONSTRAINT pk_PropID --Finally, restore NULL setting for column. ALTER TABLE ExtProps ALTER COLUMN PropID int NULL GO --End of restore from second FOREIGN KEY sample. Figur e 2- 8 shows t wo excerpt s fr om t he pr eceding script ’s out put . The t op panel shows t he Ext Props t able rows ov er t he Cont act Ext Props t able r ows. This is befor e an updat e of t he PropI D value 5 t o a new value of 50 in t he Ex t Pr ops t able. The bot t om panel shows t he sam e t w o t ables aft er t he updat e of t he value in t he Ext Pr ops t able. Not ice t hat t he change t o t he Ext Props t able cascades t o t he Cont act Ext Props t able. Figu r e 2 - 8 . Th e t op an d bot t om pa n e ls sh ow t h e Ext Pr ops t a ble ove r t h e Con t a ct Ext Pr op s t a ble b efore a n d a ft er a ch a n ge t o t h e Ex t Pr op s t ab le .

I n de x e s Many dat abases can achieve perform ance gains t hrough t he addit ion of an index . I ndexes are gr eat at speeding look ups and sort s. On t he ot her hand, t her e are t im es w hen t he ov er head associat ed w it h m aint aining an index can slow an applicat ion. This is part icular ly t rue w hen one or m or e indexes over lap wit h a clust er ed pr im ar y k ey . Oft en dev elopers and adm inist rat ors hav e t o r esort t o t im ing r uns for t y pical t asks t o det erm ine t he best configurat ion of indexes for a dat abase applicat ion. Wit h t his in m ind, t he value of being able t o add and drop index es program m at ically is considerable as y ou perform y our t im ing r uns t o discer n t he opt im al index configurat ion. The last script in t his chapt er illust rat es sev eral t echniques for w ork ing wit h index es t hat y ou ar e lik ely t o find useful. The script begins by creat ing a user defined st ored procedure, List UserDefinedI ndex es, t hat list s t he index es for user defined t ables in a dat abase. ( You’ll r ead m uch m or e about st or ed procedures in Chapt er 4.) See Figur e 2- 9 for sam ple out put . This procedure draws on bot h t he sysobj ect s and sysindex es t ables—t wo syst em cat alog t ables. While y ou should generally av oid m anipulat ing syst em t ables, som e advanced dev eloper s find it useful t o do so. The Nam e colum n from t he sysobj ect s t able ( sysobj ect s.nam e) ret ur ns t he t able for an index , and t he Nam e colum n from t he sysindex es t able

( sysindex es.nam e) is t he nam e for a specific index in a t able ( if t her e is one) . The indid colum n present s t he index ident ifier colum n values. An indid value of 1 indicat es a clust er ed index, such as one cr eat ed wit h t he CREATE I NDEX st at em ent or one associat ed w it h a prim ary k ey . Values of indid bet ween 2 and 250 ar e for nonclust ered indexes. An indid value of 0 indicat es t her e is no clust er ed index for a t able. The indid colum n v alue also conv eys infor m at ion about t ables cont aining large dat a t ypes, such as t ext , nt ext , and im age. See t he “ Table and I ndex Archit ect ur e” t opic in Books Online for addit ional det ail.

N ot e I nst ead of using t he List User DefinedI ndexes st or ed procedure in t he scr ipt below, y ou can use t he syst em st or ed procedure sp_helpindex t o collect infor m at ion about indexes. This syst em st or ed procedure works sim ilarly t o sp_pkeys and sp_fkey s, but it pr ov ides infor m at ion for indexes. However, List User DefinedI ndexes gives you ex posur e t o t echniques for w ork ing w it h syst em cat alog t ables, which ar e a rich source of cont ent about a dat abase’s design. You can add an index t o a t able w it h t he CREATE I NDEX st at em ent . The list ing below init ially dem onst r at es t he synt ax for creat ing an index based on one colum n. Follow CREATE I NDEX wit h t he nam e of your index . Then follow t he index nam e w it h an ON clause. I n t he ON clause, include t he t able nam e wit h t he colum n or colum ns for t he index. Place t he colum n nam e in par ent heses aft er t he t able’s nam e. The sam ple illust rat es t he applicat ion of t he CREATE I NDEX sy nt ax t w ice. The first use of t he st at em ent is for adding an index based on t he Last Nam e colum n in t he Em ailCont act s t able. This exam ple dem onst rat es how t o use t he CREATE I NDEX st at em ent as described in t he pr eceding paragr aph. A second applicat ion of t he st at em ent shows how t o creat e a unique index based on t w o colum ns from t he Cont act Ext Pr ops t able—nam ely, Cont act I D and PropI D. The synt ax for t his exam ple uses t he UNI QUE k eyw ord. This k eyw ord is appr opriat e for a t able wit h a candidat e k ey because it specifies a second index t hat is unique for each record besides t he pr im ary key . I n ot her words, t he colum n( s) cont r ibut ing t o a unique index ar e candidat es for t he pr im ary key. By default , t he CREATE I NDEX st at em ent generat es nonclust ered indexes. Howev er , y ou can insert CLUSTERED aft er eit her CREATE or UNI QUE ( if it is pr esent ) t o m ak e a clust er ed index. Use t he DROP I NDEX st at em ent t o rem ov e a user - defined index ( for ex am ple, one you cr eat e w it h t he CREATE I NDEX st at em ent ) . The sy nt ax for t he DROP I NDEX st at em ent uses a t wo- part nam e t o designat e t he index t o dr op. The first part is t he t able nam e, and t he second part is t he index nam e. A per iod delim it s t he t wo part s. Our st or ed procedure list s t he index es for prim ary k eys and syst em - defined index es. You can delet e t he index for a pr im ar y k ey by dropping t he key. I f t he SQL Serv er set t ings for a ser ver perm it it , you can rem ove t he index for a for eign k ey dir ect ly from t he sysindex es t able. See t he “ How t o set t he allow updat es opt ion ( Ent erpr ise Manager ) ” and “ Err or 259” t opics in Books Online for m ore det ail on dir ect ly m anipulat ing syst em cat alog t ables, such as sysindexes. --IndexSamples USE Chapter02 --Create a stored procedure to list for user-defined --tables object name from sysobjects, and name and --indid from sysindexes. IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ’ListUserDefinedIndexes’) DROP PROCEDURE ListUserDefinedIndexes

GO CREATE PROCEDURE ListUserDefinedIndexes AS SELECT sysobjects.id AS [sysobjects.id], sysindexes.id AS [sysindexes.id], sysobjects.name AS [sysobjects.name], sysindexes.name AS [sysindexes.name], sysindexes.indid FROM sysobjects INNER JOIN sysindexes ON sysobjects.id = dbo.sysindexes.id WHERE (LEFT(sysobjects.name, 3) ’sys’) AND (sysobjects.name N’dtproperties’) GO --List indexes data. EXEC ListUserDefinedIndexes --Create an Index for LastName in EmailContacts. CREATE INDEX ind_EmailContacts_LastName ON EmailContacts(LastName) --List indexes data. EXEC ListUserDefinedIndexes --Remove previously created index. DROP INDEX EmailContacts.ind_EmailContacts_LastName --Remove primary key for ContactExtProps based --on ContactID and PropID. EXEC sp_pkeys ContactExtProps, dbo, Chapter02 IF @@ROWCOUNT > 0 ALTER TABLE ContactExtProps DROP CONSTRAINT pk_ContactExtProps_ContactID_PropID GO --List indexes data. EXEC ListUserDefinedIndexes GO --Create an Index for LastName in EmailContacts. CREATE UNIQUE INDEX ind_ContactExtProps_ContactID_PropID ON ContactExtProps(ContactID, PropID) --List indexes data. EXEC ListUserDefinedIndexes GO --Attempt to enter a record with duplicate key values for --ContactID and PropID. INSERT INTO ContactExtProps Values (1, 1, ’Birthday’, ’9/9/1964’) GO --Remove previously created index. DROP INDEX ContactExtProps.ind_ContactExtProps_ContactID_PropID --List indexes data. EXEC ListUserDefinedIndexes GO --Insert and then delete record with duplicate values for --ContactID and PropID columns. INSERT INTO ContactExtProps Values (1, 1, ’Birthday’, ’9/9/1964’)

DELETE FROM ContactExtProps WHERE PropValue = ’9/9/1964’ GO Besides synt ax issues, t he pr eceding sam ple script illust rat es design issues for wor k ing w it h index es, such as t est ing t he behavior of a unique index. To isolat e t he effect of t he index, t he script drops a prim ar y k ey t hat requir es uniqueness on t he sam e t wo colum ns as t he ind_Cont act Ext Props_Cont act I D_PropI D index. The t est for t he validit y of t his unique index is an at t em pt t o ent er a record wit h a duplicat e k ey v alue. Aft er failing, t he script dr ops t he unique index and confirm s t hat y ou can add t he record if t he unique index isn’t present ; t he script closes by rem ov ing t he t est r ecor d. Figur e 2- 9 shows an ex cerpt from t he beginning of t he script w it h t he out put from t he first t wo uses of t he List UserDefinedI ndex es st ored pr ocedur e. The m ain point t o t ak e away from t he out put is t hat t he first list ing of index es doesn’t include a reference t o ind_Em ailCont act s_Last Nam e, but t he second one does. I n bet w een t he t wo r uns of t he List UserDefinedI ndex es st ored pr ocedure, t he script inv ok es t he CREATE I NDEX st at em ent t o gener at e t he index . The t w o result set s also show t he index es for clust ered and nonclust ered prim ar y k eys. For ex am ple, pk_Em ailCont act s_Cont act I D is a clust er ed pr im ary key; not ice t hat it s indid value is 1. The index for t he nonclust er ed pr im ary key , pk_Cont act Ext Pr ops_Cont act I D_PropI D, has an indid value of 2. Finally, t he _WA_Sys_PropI D_77BFCB91 index is for a foreign k ey fr om a preceding sam ple. SQL Ser ver didn’t rem ove t he index when t he script dr opped t he key. Figu re 2 - 9 . Th e se t w o re su lt set s iden t ify t h e a ddit ion of a n inde x, ind _ Em a ilCon t act s_ Last N am e , t o t h e Em a ilCon t act s t a ble . By con t ra st in g t h e fir st w it h t h e se con d list in g, you can se e t h e e ffe ct of t h e CREATE I N DEX st a t em e n t for ind _ Em a ilCon t act s_ La st N am e .

Cha pt e r 3 . Pr ogr a m m ing D a t a Acce ss w it h T- SQL This chapt er present s T- SQL program m ing t echniques for dat a access. You can use t hese t echniques in m any env ironm ent s—in Query Analyzer , encapsulat ed wit hin v iews, in st or ed procedur es and user - defined funct ions—and in Visual Basic .NET. When y ou finish wor k ing t hrough t his chapt er, y ou should possess a foundat ion for ext ract ing precisely t he dat a y ou need fr om a SQL Serv er dat abase for any applicat ion. The obj ect iv e of t his chapt er is t o dem y st ify T- SQL dat a access t echniques so t hat you can creat e T- SQL SELECT st at em ent s as easily as you used t o w rit e DAO and ADO dat a access code. Alt hough t he chapt er assum es you’r e wor k ing in Query Analyzer, t he t echniques y ou lear n w ill apply equally w hen y ou use T- SQL st at em ent s in Visual Basic .NET. The chapt er begins by int r oducing t he SELECT st at em ent and describing how t o filt er colum ns and rows from a row source, such as a t able. Next t he chapt er focuses on t echniques for aggr egat ing dat a acr oss a whole r ow source as well as specific groups w it hin t he row source. The chapt er explor es part icular t echniques for m oney and dat et im e var iables, and t he dat et im e t opic gains a sect ion of it s own. The concluding sect ion exam ines ways of com bining r ow sources wit h j oins and subquer ies. I f you hav e had difficult y underst anding j oins befor e, spend som e t im e w it h t he script sam ples in t he chapt er and t he accom panying com m ent ary t o build y our grasp of t his im port ant capabilit y.

N ot e By t he t erm r ow source, I refer t o a collect ion of rows from a dat abase. Alt hough t his can be a t able, it can also be a view based on one or m or e t ables. I n addit ion, a row sour ce can be t he r esult set gener at ed by a st ored pr ocedure or a t ablevalued user- defined funct ion. The T- SQL sam ples for t his chapt er ar e available in an .sql file on t he com panion disk . You can use t he script s as st art ing point s for your own cust om ext rapolat ions of t he t echniques. You can r un all t he sam ples fr om Query Analyzer if y ou have t he Nort hwind and pubs dat abases inst alled on a SQL Ser ver inst ance t o w hich y ou can connect .

I n t r odu ct ion t o D a t a Acce ss w it h T- SQL Creat ing efficient , speedy, and flex ible dat a access solut ions for SQL Ser ver dat a will inev it ably involve pr ogram m ing T- SQL. I n part icular, you w ill r equir e a firm foundat ion in t he design of SELECT st at em ent s. This sect ion int roduces t he SELECT st at em ent by review ing it s archit ect ur e. You’ll find code sam ples designed t o illust rat e t he basic operat ion of t he st at em ent ’s m ain elem ent s, including t he SELECT list as well as t he FROM and WHERE clauses, and you’ll be int r oduced t o t he t opic of calculat ed colum ns.

Ov e r vie w of t h e SELECT St a t e m e n t

Learning t he synt ax and clauses for t he SELECT st at em ent is t he surest way t o guarant ee your pr oduct ivit y w it h SQL Serv er. As m ent ioned in t he int roduct ion t o t his chapt er , y ou can use t he SELECT st at em ent in SQL bat ches for Query Analyzer, v iews, st or ed procedur es, and user - defined funct ions. I t is com m on t o use t he SELECT st at em ent for dat a access wit h SQL Serv er. A SELECT st at em ent can generat e a set of v alues. SQL Serv er lit erat ur e calls t he values r et urned by a SELECT st at em ent it s r esult set . A t ypical SELECT st at em ent can r et ur n a scalar value, a single colum n of values, or a t w o- dim - ensional ar ray of values. I n norm al dat a access scenarios, t he t wo- d- im ensional array of values will be t he m ost com m on design for a result set . At a m inim um , a SELECT st at em ent includes a SELECT list and a FROM clause. The list designat es t he colum ns t hat populat e a result set . You can use t he ent r ies in t he SELECT list t o filt er colum ns and calculat e new colum ns based on t he r ow source for t he SELECT st at em ent . The FROM clause designat es t he r ow source for a SELECT st at em ent . The row source for a t ypical SELECT st at em ent can be a t able, a view, a user - defined funct ion, or ev en a subquer y. This subquer y is sim ply anot her SELECT st at em ent . At a m inim um , a SELECT st at em ent used for dat a access m ust include a list and a FROM clause argum ent . The FROM clause m ust appear aft er t he SELECT list . As w it h ot her SELECT clauses, y ou separat e t he FROM clause from t he SELECT list by a space or a carr iage r et ur n. You won’t always want t o see all t he dat a from t he source specified in t he FROM clause. The SELECT list enables you t o specify a subset of t he row source’s colum ns t hat should be included in t he r esult set . Sim ilar ly, t he WHERE clause enables you t o designat e a subset of t he rows. The WHERE clause supplies t he crit er ia used t o filt er rows in t he argum ent for a FROM clause. The WHERE clause is opt ional. I f you don’t use a WHERE clause, t he SELECT st at em ent includes all t he r ows designat ed by t he FROM clause in it s r esult set . When y ou do use a WHERE clause, be sur e t o r efer ence colum ns in t he FROM clause argum ent . A general way of denot ing t he synt ax for a basic SELECT st at em ent is: SELECT select_list FROM row_source WHERE criteria_expressions

Or de r in g D a t a in t h e Re su lt Se t The rows in a r elat ional dat a source don’t have any special order . How ever, y ou’ll oft en want t he result set fr om a SELECT st at em ent t o be arranged a cer t ain way—for exam ple, in alphabet ical or num er ic order based on one or m or e colum ns. You can achieve t his w it h t he ORDER BY clause. The colum ns you designat e t o use for t he ordering can or iginat e wit h t he r ow source in t he FROM clause, or t hey can be calculat ed colum ns. You can designat e ascending ( ASC) or descending ( DESC) sort orders for any colum n in an ORDER BY clause. Ascending order is t he default . That m eans y ou hav e t o specify an order only when y ou requir e a descending or der. The ORDER BY clause should always com e aft er all ot her SELECT st at em ent clauses except for t he COMPUTE and COMPUTE BY clauses ( w hich I ’ll describe short ly ) . Grou ping D a t a in t h e Re su lt Se t

Just as you’ll probably want t o ar range dat a in a r esult set in a cert ain way, you’ll oft en want t o group dat a t o m ake it m ore useful. Wit h t he GROUP BY clause, you can gr oup set s of rows in a result set . This clause is especially useful w hen you want t o apply an aggregat e funct ion t o one or m or e colum ns in a r ow source. Aggr egat e funct ions ar e useful for dev eloping sum m ary st at ist ics, such as t he count , sum , or average of colum n v alues by gr oup. The result set s generat ed wit h t he GROUP BY clause support business decision m aking. For exam ple, y ou can use t he clause t o develop t ot al sales by region of t he count ry or by pr oduct cat egory . The GROUP BY clause w orks hand- in- hand w it h t he HAVI NG clause. The HAVI NG clause enables y ou t o filt er groups in t he sam e way t hat t he WHERE clause perm it s y ou t o filt er r ows. The GROUP BY and HAVI NG clauses belong aft er t he FROM and WHERE clauses in a SELECT st at em ent . Just as wit h t he WHERE clause, t he HAVI NG clause is opt ional. I f y ou do include it , posit ion it aft er t he GROUP BY clause. ( Lat er in t he chapt er , I prov ide a sam ple script t hat uses HAVI NG.) Gen er at in g Su m m a ry V a lu es w it h COM PUTE an d COM PUTE BY I n addit ion t o m ak ing a result set m ore useful by order ing and gr ouping, y ou’ll som et im es find t hat a sum m ar y of t he dat a is j ust as im port ant as t he dat a it self. That ’s w hen y ou m ight decide t o use t he COMPUTE and COMPUTE BY clauses t o generat e not only det ail ( t he rows in t he result set ) but also sum m ary values ( aggr egat e t ot als and subt ot als) . You’ll recall t hat t he GROUP BY clause r et urns a single result set . I n cont rast , COMPUTE and COMPUTE BY generat e m ult iple result set s. Wit h t he COMPUTE BY clause, a SELECT st at em ent pr epar es a separat e r esult set for t he r ow s in each group and anot her collect ion of r esult set s w it h t he sum m ary st at ist ics for each group. The COMPUTE BY clause designat es aggregat e funct ions, colum ns for t heir applicat ion, and gr ouping colum ns all in one clause. The COMPUTE clause can generat e t w o r esult set s—one cont aining all t he r ows in t he r ow source for a SELECT st at em ent and a second r esult set w it h sum m ary st at ist ics for t he full set of rows. The COMPUTE clause cr eat es grand t ot al st at ist ics, but t he COMPUTE BY clause creat es subt ot al st at ist ics for each group.

N ot e The COMPUTE BY and COMPUTE clauses cont rast wit h ot her t echniques for prepar ing t ot als and subt ot als, such as t he ROLLUP and CUBE operat ors. For m ore det ail on t hese operat or s, see t he Books Online t opics “ Sum m arizing Dat a Using ROLLUP” and “ Sum m ar izing Dat a Using CUBE.” You can use t he GROUP BY and COMPUTE BY clauses in t he sam e SELECT st at em ent . When y ou use t he t w o clauses t oget her, t he COMPUTE BY sum m ar y st at ist ics apply t o t he gr oups of rows specified in t he GROUP BY clause. Apply ing t he COMPUTE BY clause wit hout t he GROUP BY clause perm it s t he COMPUTE BY clause t o generat e r esult s for indiv idual r ows designat ed by t he FROM clause. Whenev er y ou designat e eit her t he COMPUTE BY or COMPUTE clause, it should always appear as t he last clause in t he SELECT st at em ent . When you use bot h, t he COMPUTE clause belongs aft er t he COMPUTE BY clause. I prov ide m ore det ail on ordering, gr ouping, and aggregat ing result set s lat er in t his chapt er .

Spe cifyin g Colu m n s a n d Row s

You use t he SELECT st at em ent t o r et ur n a r esult set or set s fr om a r ow source. Howev er, you w on’t alw ays want t o ret ur n all colum ns and r ows in t he row source. SELECT offers y ou various ways of filt er ing out w hat you don’t want . Ret u r n ing All Colu m n s The m ost elem ent ar y SELECT st at em ent is t hat which designat es t he r et ur n of all colum ns fr om each row wit hin a r ow source. Ther e are t wo differ ent ways t o do t his. The m ost fam iliar uses an ast er isk ( * ) t o denot e all t he colum ns in a r ow source. For exam ple, if you want ed t o creat e a result set w it h all t he colum n values for each row in t he Cust om ers t able, you could use t he follow ing code: --SelectAllColumns --Select all columns from all rows. USE Northwind SELECT * FROM Customers Not ice t hat t he USE key word specifies t he Nort hwind dat abase as t he dat abase cont ext for t he st at em ent . Unless you ex plicit ly designat e ot herw ise, y our SELECT st at em ent will apply t o t he curr ent dat abase. Subsequent sam ples w ill illust rat e how t o ov erride t his default select ion.

N ot e The USE keyword is an inst ruct ion t o Query Analy zer. This key word is not a part of T- SQL. You set t he dat abase cont ext differ ent ly for ot her SQL Ser ver client s. Ther e is anot her, less com m on, appr oach t o ret ur ning all colum ns t hat achiev es t he sam e result as using an ast er isk: you can separat ely denot e each colum n nam e in t he Cust om ers t able. The follow ing code shows t he first couple of colum n nam es, an ellipsis, and t he last nam e from t he Cust om ers t able in t he Nort hw ind dat abase. ( Not e t hat SQL Serv er synt ax doesn’t allow an ellipsis; it ’s used her e wit h a few colum n nam es t o represent t he full list of Cust om ers colum ns.) USE Northwind SELECT CustomerID, CompanyName, ..., Fax FROM Customers The t wo different approaches w ill generat e equivalent r esult s for t he st andard Cust om ers t able in t he Nort hwind sam ple dat abase. How ev er , in som e circum st ances t hey can ret urn divergent result set s. I f y ou add a new colum n t o t he Cust om ers t able, t he init ial sam ple w ill r et urn t he new colum n along wit h t he old. On t he ot her hand, t he second sam ple t hat list s t he indiv idual colum ns w ill om it t he new colum n because it s nam e isn’t in t he SELECT list . So w hich approach is best ? The answer depends on y our needs. I n general, evaluat e v er y carefully w het her y ou need all t he colum ns from a r ow source. You can speed an applicat ion’s perform ance by choosing j ust t he colum ns t hat an applicat ion t ruly requir es. Ret u r n ing a Su bse t of Colu m n s You can filt er out unwant ed colum ns in m any different ways. The following sam ple ret urns values for all r ows in t he Cust om ers t able, but only fr om t he Count ry, Cit y, and Com panyNam e colum ns. Because t here ar e 91 cust om ers in t he Nort hw ind dat abase, t his r esult set has t hr ee colum ns and 91 r ows—one for each row in t he t able. --SelectSomeColumns

--Select some columns from all rows. SELECT Country, City, CompanyName FROM Customers You m ay have not iced t hat unlik e t he pr ev ious code sam ple, t his one doesn’t include a specific reference t o t he Nort hwind dat abase. That ’s because Query Analyzer w ill cont inue t o use Nort hw ind unt il y ou specify a different dat abase wit h a new USE st at em ent . All t he r em aining sam ples in t his chapt er assum e t hat t he dat abase is Nort hw ind.

N ot e The elem ent s in a SELECT list and t he argum ent in a FROM clause are ex am ples of ident ifiers for dat abase obj ect s. SQL Server has precise rules for nam ing obj ect s and using obj ect nam es as well as a r ich collect ion of Book s Online t opics for describing t hem . For ex am ple, see t he t opic “ Using I dent ifiers” for a delineat ion of t he four rules for regular ident ifier s. When dealing w it h obj ect s t hat cont ain ident ifier s wit h em bedded spaces, such as t he Or der Det ails t able, you can oft en appr opr iat ely r efer t o t hem by enclosing t heir ident ifier s in br acket s or single quot at ion m ar ks—for exam ple, FROM [Order Details] . Ret u r n ing a Su bse t of Row s Just as you can lim it w hich colum ns are ret ur ned, you also can lim it which r ows are in t he result set . The follow ing script r et urns Count r y, Cit y , and Com panyNam e colum n values for a subset of t he rows in t he Cust om ers t able. The expression in t he WHERE clause denot es t he pr ecise subset —nam ely, t hose cust om ers from a count ry beginning w it h eit her t he let t er B or C. That filt er ing is done by using t he SUBSTRI NG funct ion t o exam ine j ust t he first charact er in each Count ry colum n value. I will show y ou a sim pler way t o expr ess t his in a subsequent sam ple, but you’ll lik ely find t his ex posur e t o t he SUBSTRI NG funct ion useful. Any legit im at e expression works in a WHERE clause. Your ex pr ession can apply t o t he values for any colum n designat ed by t he r ow source in t he FROM clause. The code also dem onst rat es t he use of t he ORDER BY clause. Because of t he t wo argum ent s in t he clause, t he result set appears in alphabet ical order by count ry . Wit hin each count ry, t he cit ies ar e, in t ur n, sort ed in alphabet ical order. --SomeColumnsFromSomeRows --Select some columns from some rows. SELECT Country, City, CompanyName FROM Customers WHERE SUBSTRING(Country,1,1)‘A’ ORDER BY Country, City The result set from t he SELECT st at em ent cont ains 14 r ows, inst ead of t he 91 rows in t he preceding sam ple. This differ ence isn’t significant for a single user. Howev er, if m any users repeat edly r un a query t hat r et urns less t han one- sixt h as m any rows, y our ov erall net w or k perform ance w ill im pr ov e. The follow ing sam ple r epeat s t he code fr om t he prev ious one but also print s t he num ber of rows ret urned, using a cust om form at . By default , SQL Serv er w ill report t he num ber of rows affect ed, w hich is t he num ber of rows in a r esult set for a SELECT st at em ent . The sam ple t ur ns off t he default m essage w it h t he SET

NOCOUNT ON st at em ent . Then it declares a local st r ing var iable—@st r Rows—for it s cust om r eport about t he num ber of r ows r et ur ned. The @@ROWCOUNT global variable r et urns t he num ber of r ecords affect ed by t he last T- SQL st at em ent . Because t his funct ion ret ur ns an int eger, a CAST funct ion is used t o conv ert t he num er ic value ret urned by @@ROWCOUNT t o charact er dat a. The charact er dat a is t hen concat enat ed w it h a st r ing t hat inform s t he user how m any r ow s are in t he r esult set , which t he PRI NT st at em ent sends t o t he Messages Pane in Query Analyzer. --CustomCount --Select some columns from some rows --with custom count of rows affected. SET NOCOUNT ON Declare @strRows nvarchar(50) SELECT Country, City, CompanyName FROM Customers WHERE SUBSTRING(Country,1,1)‘A’ ORDER BY Country, City SET @strRows = ’Rows returned = ’ + Cast(@@ROWCOUNT AS nvarchar) PRINT @strRows SET NOCOUNT OFF

Using t he LI KE Ope r a t or w it h W ildca r ds Many developer s and end users creat ing T- SQL st at em ent s for dat a access will use t he LI KE oper at or t o specify a pat t er n m at ch. This operat or appear s in SELECT st at em ent s wit hin t he WHERE clause. The LI KE oper at or t ypically work s wit h one of t hree wildcard par am et ers—% , _, and ^ . The % param et er r epresent s any set of 0 or m ore charact ers. You can use it at t he beginning or end of a sear ch st r ing. The _ param et er designat es a single char act er in a sear ch st r ing. You can posit ion t he _ par am et er at t he beginning or end of a sear ch st r ing or wit hin a search st ring. The ^ par am et er specifies t hat ret ur n values not m at ch a pat t er n. This param et er always appears in a sear ch st r ing w it hin square bracket s. You can apply it t o an individual charact er or a range of char act ers. Square bracket s can denot e a pat t ern range wit h or wit hout t he ^ par am et er. The following code dem onst r at es t he use of t he % param et er t o ret ur n all rows in t he Cust om er s t able of t he Nort hwind dat abase wit h U as t he first let t er of t heir Count ry colum n value. The ret urn set includes rows wit h t he Count ry colum n values USA and UK. --Return rows with Country values beginning with U. SELECT CompanyName, Country FROM Customers WHERE Country LIKE ’U%’ By applying t he _ param et er in t he argum ent for a LI KE operat or, a SELECT st at em ent can r et urn j ust rows t hat cont ain USA inst ead of UK. The ’U_A’ ar gum ent fails t o m at ch r ows wit h t he Count r y colum n value UK. --

Return rows with Country values beginning with U followed -by any character, the letter A and any other characters. SELECT CompanyName, Country FROM Customers WHERE Country LIKE ’U_A’ Using t he LI KE Operat or wit h Wildcards ( cont inued) Wit h t he ^ operat or in squar e br acket s, we can ret ur n all rows fr om t he Cust om ers t able ex cept t hose t hat st ar t t heir Count r y colum n value wit h U. -Return rows with Country values that don’t begin with U. SELECT CompanyName, Country FROM Customers WHERE Country LIKE ’[^U]%’ Using t he square brack et s t o denot e a range can sim plify som e expressions. For exam ple, y ou can r et ur n row s fr om t he Cust om ers t able t hat have Count ry colum n values beginning wit h eit her B or C wit h t he square brack et s and t he LI KE operat or . The following SELECT st at em ent shows t he sy nt ax. --Return rows with Country values beginning with B or C. SELECT CompanyName, Country FROM Customers WHERE Country LIKE ’[B-C]%’

Ca lcula t e d Colu m n s A calculat ed colum n is one t hat doesn’t appear wit hin t he row source for a SELECT st at em ent . I nst ead, you specify t he calculat ed colum n w it h an expr ession inside t he SELECT st at em ent . Because norm alized t ables aren’t supposed t o cont ain colum ns t hat depend on ot her colum ns in t he sam e t able, y ou w ill oft en need t o dev elop calculat ed colum ns w hen work ing w it h pr oper ly designed dat abases. For exam ple, y ou can com put e ext ended pr ice in t erm s of quant it y , price, and discount for t he line it em s in a t able of order det ails. I n addit ion, y ou can com put e how lat e a shipm ent or a paym ent is by com put ing t he difference bet ween dat es. The follow ing sam ple illust rat es how t o specify a calculat ed colum n as well as how t o use t he CAST funct ion t o t ransform t he dat a t ype for a calculat ed result . The script list s four r egular colum ns from t he Order Det ails t able in t he Nort hw ind dat abase and also a couple of calculat ed colum ns t hat com put e ext ended pr ice. The calculat ed colum ns each m ult iply one regular colum n value by anot her t o com put e ext ended pr ice, but t hey differ in form at t ing. Alt hough Quant it y is a sm allint dat a t ype and Unit Pr ice is a m oney dat a t ype, Quantity*UnitPrice ret ur ns a result w it h a m oney dat a t ype. However, m ult iply ing by (1-Discount) conv ert s t he dat a t ype for t he expr ession t o a r eal dat a t ype. Wit hout any conv ersion, t he ext ended pr ice w ill appear in scient ific not at ion ( wit h an E in t he result ) . The sam ple shows how t o convert t he r esult t o eit her a m oney dat a t ype or a decim al dat a t ype wit h t w o places aft er t he decim al point . Bot h of t hese conv ersions pr eser ve t he ext ended price r esult as a num er ic value.

N ot e See t he “ Dat a Type Precedence” t opic in Books Online for an int roduct ion t o how SQL Serv er ret ur ns result s when t her e is a calculat ion bet ween colum n v alues wit h different dat a t y pes. --AddCalculatedColumn --Add a calculated column to the result set formatted --to two different numeric formats. SELECT OrderID, Quantity, UnitPrice, Discount, CAST(Quantity*UnitPrice*(1Discount) AS money) AS ’Price as money’, CAST(Quantity*UnitPrice*(1Discount) AS dec(9,2)) AS ’Price as dec(9,2)’ FROM [Order Details] Figur e 3- 1 displays an excerpt from t he r esult set for t he preceding script . The t wo colum ns on t he r ight show t he out com e fr om t he t w o CAST funct ions. The label t o t he r ight of each CAST funct ion appear s as t he colum n heading in t he result set excerpt . The CAST funct ion t hat conv ert s ext ended price t o m oney shows four places t o t he right of t he decim al point . This is t he scale for t he m oney dat a t ype. The CAST funct ion t hat t ransform s t he ext ended pr ice int o a decim al dat a t ype show s j ust t wo places aft er t he decim al point . This is consist ent wit h t he dec( 9,2) dat a t ype specified in t he CAST funct ion. Figu r e 3 - 1 . An e x cer pt sh ow ing t h e r esu lt of t w o diffe re n t CAST fu n ct ion s for a re a l d at a t yp e .

The next sam ple illust rat es how t o com put e and r eport t he difference bet ween t wo dat et im e v alues. The T- SQL bat ch uses t he DATEDI FF funct ion t o com put e t he differ ence bet w een t wo local var iables. While t his sam ple isn’t explicit ly for colum n values, t he sam e t echniques apply t o calculat ed colum n values. ( See t he “ Perform ing Dat e Ar it hm et ic” sect ion for det ails t hat sp- ecifically pert ain t o colum n values.) The GETDATE funct ion r et ur ns a cur rent dat e and t im e. The bat ch deposit s t he cur rent dat e and t im e int o t wo different local v ar iables— @dt St art at t he t op of t he bat ch and @dt End in t he next - t o- last st at em ent . The DATEDI FF funct ion com put es t he differ ence bet ween t hese t wo local v ariables. The DATEDI FF funct ion enables you t o ext ract t he differ ence bet ween dat et im e values in any of sev eral unit s. Using m s as t he first argum ent ex t ract s t he difference in m illiseconds. You can use a pr ocedure lik e t his one for a quick snapshot of t he t im e it t akes t o run som e T- SQL st at em ent s. Ot her, m ore com prehensiv e, per for m ance m easures are av ailable fr om SQL Serv er ; see, for exam ple, “ Query Window St at ist ics Pane” in Books Online for m or e det ail.

N ot e

A local var iable in T- SQL oper at es like a m em ory var iable in Visual Basic. Ch a pt e r 3 provides explicit coverage of T- SQL local variables. --ComputeWithDatediff --Demonstrates use of DATEDIFF function to compute --a difference in milliseconds. DECLARE @dtStart datetime DECLARE @dtEnd datetime DECLARE @intOrderID int SET @intOrderID = 10700 SET @dtStart = GETDATE() SELECT OrderID, Quantity, UnitPrice, Discount, CAST(Quantity*UnitPrice*(1Discount) AS money) AS ’Price as money’, CAST(Quantity*UnitPrice*(1Discount) AS dec(9,2))’Price as dec(9,2)’ FROM [Order Details] WHERE OrderID < @intOrderID SET @dtEnd = GETDATE() SELECT DATEDIFF(ms, @dtStart, @dtEnd) ’Time to run (ms)’

Aggr e ga t in g a nd Gr oupin g Row s T- SQL aggr egat e funct ions can apply t o all t he rows in a r esult set or j ust subset s of t hem , such as t hose ident ified w it h a GROUP BY clause. For exam ple, you can count t he num ber of ov erall rows in a r ow sour ce, or y ou can count t he rows by count ry ( or by any ot her value on which y ou gr oup r ows) . You can choose t o ret ur n aggr egat e values and t he r ows t hey sum m ar ize, or j ust t he aggr egat e values.

Su m m a r y of Aggr e ga t e Fu nct ion s Table 3- 1 it em izes t he aggregat e funct ions by list ing t heir nam es wit h a short descript ion. The purpose of m any of t hese funct ions is im plied by t heir nam e. For furt her det ails about funct ionalit y and synt ax , search Books Online for a t opic wit h t he funct ion nam e. 7DEOH6XPPDU\RI764/$JJUHJDWH)XQFWLRQV 1DPH

'HVFULSWLRQ

AVG

Ret urns an average

BI NARY CHECKSUM

Can ret urn t he binary check sum for a row

CHECKSUM

Com put es a checksum for use in const ruct ing hash index es

CHECKSUM AGG Per form s a checksum com put at ion for a group COUNT

Count s t he it em s in a gr oup or ov erall row source; r et ur ns an int value

COUNT BI G

Like Count funct ion but ret ur ns a bigint value

GROUPI NG

For use wit h CUBE and ROLLUP operat ors

MAX

Ret urns m axim um value in a colum n

MI N

Ret urns m inim um value in a colum n

SUM

Ret urns sum of values in a colum n

STDEV

Com put es st andard dev iat ion for t he sam ple of values in a colum n

STDEVP

Com put es st andard dev iat ion for t he populat ion of values fr om which a colum n sam ples

VAR

Com put es var iance for t he sam ple of v alues in a colum n

VARP

Com put es var iance for t he populat ion of values from w hich a colum n sam ples

Aggr e ga t in g W it hou t Gr ou pin g Like som e of t he ot her aggregat e funct ions, t he COUNT funct ion has m ult iple for m s. For exam ple, COUNT(*) ret ur ns t he num ber of values in a row source, including null and duplicat e values. The WHERE clause can const rain t he range of rows ov er which COUNT(*) com put es a r esult . I n t he next sam ple, t he r ow source includes all cust om ers fr om a count ry st art ing w it h t he let t er B or C. Because t he Cust om ers t able in t he Nort hwind dat abase has a pr im ary key, t he r ow s are all unique. This SELECT st at em ent ret ur ns a scalar value of 14, w hich is t he num ber of rows in t he Cust om er s t able m eet ing t he cr it er ion expr ession in t he WHERE clause. Not ice t hat like an earlier sam ple in t his chapt er , t his code lim it s count ries t o t hose st art ing w it h t he let t er B or C but does so using LEFT and I N rat her t han SUBSTRI NG. You can use eit her m et hod, but t his one requir es a lit t le less t yping. --CountRows --Count all rows meeting a criterion. SELECT Count(*) FROM Customers WHERE LEFT(Country,1) IN (‘B’,’C’) You can use t he COUNT funct ion t o r et ur n j ust t he num ber of non- null v alues wit hin a colum n by replacing t he ast er isk w it h t he nam e of a specific colum n. Any rows w it h null values for a specific colum n in t he row source for t he quer y w on’t be t allied as part of t he ret ur n value for t he COUNT funct ion. Changing t he ast erisk t o a specific colum n nam e—Count ry —won’t change t he result in t he prev ious sam ple because t here aren’t any null v alues in t he Count ry colum n. But you can change t he r esult by using t he DI STI NCT k eyw ord as a pr edicat e t o t he specific colum n. Posit ion t he k ey word inside t he par ent heses t railing t he funct ion. The follow ing scr ipt illust rat es t his synt ax. The query st at em ent r et ur ns t he value 3 because t her e are only t hree dist inct count r ies in t he Cust om ers t able st art ing wit h t he let t er B or C—Belgium , Brazil, and Canada. --CountIncidences --Count distinct incidences. SELECT Count(DISTINCT Country) FROM Customers WHERE LEFT(Country,1) IN (‘B’,’C’)

Aggr e ga t in g w it h Gr ou pin g I t is com m on t o use aggregat e funct ions w hen grouping on one or m or e colum ns. For inst ance, inst ead of j ust com put ing t he t ot al count of cust om ers, you can der iv e m or e det ailed inform at ion by com put ing t he count of cust om ers by cit y and count ry . One approach t o per form ing t his t ype of calculat ion uses a GROUP BY clause in a SELECT st at em ent . When y ou add a GROUP BY clause, t his, in t ur n, places rest r ict ions on t he ent r ies in a SELECT list . I t is t ypical t o have j ust t wo t ypes of ent r ies in t he list —aggr egat e funct ions, such as COUNT and SUM, for specific colum ns; and colum ns t hat appear in t he GROUP BY clause.

The colum ns in t he GROUP BY clause det erm ine t he span ov er which an aggregat e funct ion com put es. By adding Count r y t o t he GROUP BY clause and including Count ry and COUNT( Cust om erI D) in t he list for a SELECT st at em ent , you can com put e t he count of cust om ers by count ry . The GROUP BY clause can t ake m ult iple colum ns as argum ent s. Therefore, adding Cit y t o bot h t he GROUP BY clause and t he SELECT list t ells t he COUNT funct ion t o count t he cust om ers by cit ies wit hin count ry. The follow ing script illust rat es t his approach for cust om ers who com e from count ries beginning wit h t he let t er B or C. --CountCustomers --Count column value instances meeting a criterion --that is grouped and ordered by two columns. SELECT Country, City, Count(CustomerID) ’# of Customers’ FROM Customers WHERE LEFT(Country,1) IN (‘B’,’C’) GROUP BY Country, City ORDER BY Country, City The result set from t he preceding script ( see Figur e 3- 2) shows how t he 14 cust om ers from count r ies beginning w it h B or C ar e dist r ibut ed by count ry and cit y. I t shows cust om er s in nine cit ies wit hin t hree count r ies. The m ost cust om ers in any cit y are in São Paulo, Brazil. The closing ORDER BY clause ar ranges t he rows in t he r esult set alphabet ically by cit y w it hin count ry. Figu r e 3 - 2 . A re su lt se t sh ow in g g rou pin g b y cit y w it h in cou n t ry for a cou n t of cu st om e r s.

The result set for t he preceding scr ipt count s t he cust om ers by cit y, but it doesn’t break out result s separat ely by gr oup or pr ov ide any subt ot als for t he num ber of cit ies wit hin each count ry. You can use t he COMPUTE BY and COMPUTE clauses of a SELECT st at em ent t o generat e r esult s lik e t hese. The follow ing script shows how t o use t he COMPUTE BY clause t o split t he result s by count r y and add a count of t he num ber of cit ies wit hin each count r y. A COMPUTE BY clause requir es a m at ching ORDER BY clause; bot h clauses m ust specify t he sam e colum n nam e as an argum ent . I n t his sam ple, not ice t hat Count ry appears in t he COMPUTE BY and ORDER BY clauses. The final COMPUTE clause adds a count of t he t ot al num ber of cit ies across all count r ies in t he collect ion of r esult set s for t he SELECT st at em ent . --CountCustomersInSpecifiedCountries --Count column value instances meeting a criterion that is --grouped and ordered by two columns and subtotaled by --one column. SELECT Country, City, Count(CustomerID) AS ’# of Customers’ FROM Customers WHERE LEFT(Country,1) IN (‘B’,’C’) GROUP BY Country, City

ORDER BY Country COMPUTE Count(City) BY Country COMPUTE Count(City) The script generat es seven result set s t hat appear in a single Result s Pane w it hin Query Analyzer, as shown in Figur e 3- 3. A separat e colum n header denot es t he beginning of each result set . The t op r esult set shows t he count of cust om ers by cit y w it hin Belgium . The second r esult set displays a count of t he num ber of cit ies in Belgium . The next t wo pairs of result set s pr ov ide com parable infor m at ion for cust om ers from Br azil and Canada. The final r esult set shows t he t ot al count of cit ies across t he pr eceding result set s for each count r y. Figu re 3 - 3 . A colle ct ion of re su lt se t s de m on st r at in g t h e op er a t ion of t h e COM PUTE BY a n d COM PUTE cla u se s.

The next exam ple r et ur ns t o a m ore basic applicat ion of t he GROUP BY clause, but t his script dem onst r at es t he aggr egat ion of a calculat ed colum n—nam ely , ext ended pr ice based on t he Quant it y , Unit Pr ice, and Discount colum ns in t he Order Det ails t able. Because t he script groups by OrderI D colum n values, t he result set displays t he t ot al ext ended pr ice for all t he it em s w it hin each order . This script groups by Or derI D, and it also aggr egat es by OrderI D. The count of a single OrderI D colum n v alue w it hin an order r et ur ns t he num ber of line it em s for an order . The sum of t he expr ession for ext ended pr ice pr ov ides t he t ot al ext ended pr ice for an or der. This is t he first sam ple script in t his book t hat illust rat es t he synt ax for t he HAVI NG clause. I n t his inst ance, t he clause rest rict s

t he ent ries in t he result set t o orders w it h a t ot al ext ended pr ice of m or e t han $11,000. The final ORDER BY clause in t he script is necessary t o arrange t he r ows in descending order based on t ot al ext ended pr ice. --CountAndSum --Count one real column (OrderID) and SUM one calculated column --to get total Extended Price for each order. --Format money data type as characters for display. SELECT OrderID, COUNT(OrderID) ’Line items’, ’$’ + CONVERT(varchar,CAST(SUM(Quantity*UnitPrice*(1Discount)) AS money),1) AS ’Extended Price’ FROM [Order Details] GROUP BY OrderID HAVING SUM(Quantity*UnitPrice*(1-Discount)) > 11000 ORDER BY SUM(Quantity*UnitPrice*(1-Discount)) DESC Not ice t he use of a CAST funct ion nest ed w it hin a CONVERT funct ion. The CAST funct ion t ransform s t he real t ot al ext ended pr ice for an order int o a m oney value. The CONVERT funct ion repr esent s t he m oney v alue as a char act er st r ing for m at t ed for curr ency wit h com m a delim it ers bet ween ev ery t hr ee digit s t o t he left of t he decim al point and j ust t wo digit s t o t he r ight of t he decim al point . A st ring ex pr ession adds a leading cur rency sign. The CONVERT funct ion offers t hr ee different st yles for render ing cur rency as a charact er value. The t hird argum ent for t he CONVERT funct ion designat es t he st yle. The follow ing t able sum m ar izes t he effect of each possible value for t he t hird argum ent . The default value is 0. &219(576W\OH$UJXPHQW

6W\OH$UJXPHQW)RUPDW(IIHFWV

9DOXHVIRU5HQGHULQJ0RQH\ 0

No com m as, but j ust t wo digit s t o t he r ight of t he decim al point

1

Com m as separat ing ev ery t hr ee digit s t o t he left of t he decim al point and j ust t wo digit s t o t he r ight of t he decim al point

2

No com m as, and four digit s t o t he right of t he decim al point

Pr ocessing Da t es Dat es ar e different fr om ot her dat a t ypes, and processing t hem can be t ricky . For one t hing, SQL Ser ver t ypically saves dat e values in a num er ic form at wit h eit her a dat et im e or a sm alldat et im e dat a t ype. For anot her , dat es repr esent a calendar in w hich t he t ot al days per m ont h ar en’t consist ent fr om one m ont h t o t he next . Also, you can gr oup dat es by day , w eek, m ont h, quart er, and y ear. Fort unat ely , SQL Ser ver offers som e highly useful funct ions t o sim plify t he use of dat es t hat don’t apply t o ot her dat a t ypes. This sect ion ex plor es som e of t hese funct ions and ot her t echniques t hat can help you pr ocess dat es wit h SQL Serv er.

Cou n t ing by Ye a r a nd M on t h I t is com m on t o need t o aggregat e dat a by y ear and m ont h. This sect ion prov ides four code sam ples t hat dem onst rat e how t o do it . I n part icular , t he scr ipt t ackles

t he problem of count ing t he orders per per iod of t im e, such as by y ear or by m ont h w it hin a y ear. Cou n t in g by Ye a r The first sam ple generat es a r esult set t hat accum ulat es t he num ber of orders by year. I t t akes j ust t hr ee lines t o do t his. The first is a SELECT st at em ent wit h a list t hat includes t wo ent ries. The first ent ry is t he DATEPART funct ion for t he OrderDat e from t he Orders t able. The DATEPART funct ion ret urns an int eger t hat reflect s a part of a dat et im e value, such as t he m ont h num ber for a dat e. The funct ion t akes t w o argum ent s. The first argum ent denot es t he dat e par t t o ext ract . The sam ple uses yyyy t o ext ract t he year as a four - digit num ber. The second argum ent is t he act ual dat et im e value. This can be an expr ession or a colum n value. The sam ple r efer ences t he OrderDat e colum n value from t he Orders t able. I n order t o accum ulat e a quant it y by y ear , t he T- SQL sam ple includes t he sam e DATEPART funct ion as t he ar gum ent for a GROUP BY clause. The second list ent ry is a COUNT funct ion. The funct ion uses OrderI D as it s argum ent t o count t he num ber of orders wit hin a y ear . --CountOrdersByYear --Count one column by year date part. SELECT DATEPART(yyyy, OrderDate), COUNT(OrderID) FROM Orders GROUP BY DATEPART(yyyy, OrderDate) The DATEPART funct ion is ext r em ely flex ible. I t can ext ract any of 11 different dat e part s from a dat et im e value. You can also use t he DATEPART funct ion w it h sm alldat et im e values, but t he funct ion cannot ext ract m illiseconds for sm alldat et im e values because t he dat a t ype doesn’t support t his lev el of precision. The funct ion offers m ult iple argum ent s for specify ing w hich dat e part s t o ext ract . At a m inim um , y ou can designat e dat e part s by t heir nam e or t heir abbrev iat ion. Many dat e part s give you t he choice of t wo abbr ev iat ions for referencing t hem . You can use t he DATEPART funct ion w it h one of it s part s t o replace t he Year , Mont h, and Day funct ions. Table 3- 2 shows t he possible dat e part argum ent s available for t he DATEPART funct ion. 7DEOH$UJXPHQWVIRUWKH'$7(3$57)XQFWLRQ 'DWH3DUW1DPH

'DWH3DUW$EEUHYLDWLRQ

year

yy, y y yy

quart er

qq, q

m ont h

mm, m

dayofy ear

dy, y

day

dd, d

week

wk, ww

weekday

dw

hour

hh

m inut e

m i, n

second

ss, s

m illisecond

ms

Cou n t in g by M on t h Dev eloping a r esult set t hat r et ur ns t he num ber of orders by m ont h w it hin y ear builds on t he t echniques t hat y ou learned previously. I t is j ust a m at t er of put t ing t he elem ent s t oget her cor rect ly. You include t hr ee it em s in t he list for t he SELECT

st at em ent t o ret ur n t he year, m ont h, and count of orders in a t im e per iod. Specify t he Orders t able as t he argum ent for t he FROM clause. I n t he GROUP BY clause, use a DATEPART funct ion st at em ent t o r et ur n t he year followed by a com m a and a DATEPART funct ion t o ret ur n t he m ont h, like t his: GROUP BY DATEPART(yyyy, OrderDate), DATEPART(mm,OrderDate) Because we want t he result rows ordered by m ont h w it hin y ear , t he SELECT st at em ent r equires an ORDER BY clause w it h t he sam e argum ent s as t he GROUP BY clause. The follow ing scr ipt shows t he SELECT list it em s. The first t wo it em s in t he list m at ch t he argum ent s for t he GROUP BY clause. The last SELECT list argum ent is t he aggr egat e funct ion, COUNT, t hat count s t he num ber of orders per dat e unit . This sam ple design is v er y general. You can use any ot her aggr egat e funct ion or m or e aggregat e funct ions t han t hose in t he sam ple. For exam ple, you can add a DATEPART funct ion t o count orders by w eek wit hin y ear inst ead of or in addit ion t o m ont h w it hin y ear. --CountOrdersByYearAndMonth --Count one column by year and month date parts of another. SELECT DATEPART(yyyy, OrderDate) AS ’Year’, DATEPART(mm,OrderDate) AS ’Month’, COUNT(OrderID) AS ’Orders’ FROM Orders GROUP BY DATEPART(yyyy, OrderDate), DATEPART(mm,OrderDate) ORDER BY DATEPART(yyyy, OrderDate), DATEPART(mm,OrderDate) Figur e 3- 4 shows an ex cerpt of t he result s fr om t he script . Not ice t hat m ont hs ar e represent ed by t heir num ber . They are sort ed wit hin y ear, w hich also happens t o be a num ber. At least som e of y our client s ar e bound t o r equest t he r eplacem ent of t he m ont h num bers wit h nam es. Figu re 3 - 4 . An e x cer pt fr om a r e su lt se t t ha t displa ys t h e n u m be r of or de rs by m on t h w it h in yea r .

The next script illust rat es an appr oach t o generat ing t he report in Figure 3- 4, but wit h nam es inst ead of num bers t o designat e m ont hs. You can use t he DATENAME funct ion t o ext ract a m ont h nam e as a charact er st r ing from a dat e. The DATENAME funct ion t ak es t wo argum ent s—j ust lik e t he DATEPART funct ion. Bot h funct ions use t he sam e codes t o represent dat e part s, and t he t wo also use a dat et im e v alue as t he second argum ent . ( You can use a sm alldat et im e v alue as well.) I n t he case of a m ont h dat e part , t he DATENAME funct ion pr ov ides t he

m ont h’s full nam e, such as January or February , inst ead of a num ber, such as 1 or 2. --ShowMonthNames --Count one column by year and month date parts of another --column while showing month names instead of month numbers. SELECT DATEPART(yyyy, OrderDate) AS Year, DATENAME(mm, OrderDate) AS Month, COUNT(OrderID) AS Orders FROM Orders GROUP BY DATEPART(yyyy, OrderDate), DATENAME(mm, OrderDate) ORDER BY DATEPART(yyyy, OrderDate) Howev er, t her e’s a problem . The scr ipt sort s t he result set by m ont h nam e, but t he alphabet ical order of t he m ont hs doesn’t corr espond t o t heir t em poral order. That ’s w hy t he m ont hs in t he r esult set are ar ranged alphabet ically w it hin each year r at her t han chr onologically. One solut ion t o t his problem is t o set up a onet o- one cor respondence bet ween t he m ont h nam es ret ur ned by t he DATENAME funct ion and t he m ont h num bers r et urned by t he DATEPART funct ion. The follow ing scr ipt shows an approach t o m apping m ont h nam es t o m ont h num bers t hat relies on only t he SELECT list it em s along w it h t he GROUP BY and ORDER BY clauses. The SELECT list cont ains t hr ee t erm s: t he y ear r et urned by t he DATEPART funct ion, t he m ont h nam e r et ur ned by t he DATENAME funct ion, and t he COUNT funct ion t o com put e t he num ber of orders by y ear and m ont h. ( Any ot her aggr egat e funct ion would w or k as w ell.) Because t he DATENAME funct ion appears in t he SELECT list , it m ust also be an argum ent for t he GROUP BY clause. The t r ick is t o place t he DATENAME funct ion in t he GROUP BY clause in bet ween t he first DATEPART funct ion for y ear and a second DATEPART funct ion for m ont h. Because m ont h nam es m ap per fect ly t o m ont h num bers, t he t w o GROUP BY argum ent s aft er t he DATEPART for y ear gr oup t he rows in an ident ical way . The DATENAME ar gum ent for m ont h has t o appear in t he GROUP BY clause because y ou need it in t he SELECT list . I n addit ion, t he DATEPART funct ion t hat ret ur ns a m ont h’s num ber in t he GROUP BY clause is also necessary because t he ORDER BY clause r equir es it as it s second argum ent . The out put from t he follow ing scr ipt m at ches t he out put in Figure 3- 4 except t hat t he second colum n shows m ont h nam es inst ead of m ont h num bers. --ShowMonthNamesChronologically --Count one column by year and month date parts of another --column while showing month names instead of month numbers. --Order months by names chronologically, not numerically. SELECT DATEPART(yyyy, OrderDate) AS Year, DATENAME(mm, OrderDate) AS Month, COUNT(OrderID) AS Orders FROM Orders GROUP BY DATEPART(yyyy, OrderDate), DATENAME(mm, OrderDate), DATEPART(mm, OrderDate) ORDER BY DATEPART(yyyy, OrderDate), DATEPART(mm, OrderDate)

Pe r f or m in g D a t e Ar it h m e t ic The “ Calculat ed Colum ns” sect ion dem onst rat ed how t o t ak e advant age of SQL dat et im e local var iables t o com put e t he difference bet w een t w o dat et im e v alues. That illust rat es dat e ar it hm et ic. This sect ion dr ills down m ore deeply int o t he t opic. Let ’s st art out w it h a sam ple t hat screens order s t o find t hose w it h an est im at ed arr ival dat e t hat is lat er t han t he required dat e for t he order . These are lat e

orders because t hey arriv e at t he cust om er aft er t he r equired dat e. The SELECT list for t he sam ple includes OrderI D and t hr ee dat et im e colum ns: OrderDat e, RequiredDat e, and ShippedDat e. Because t he t im e of day t hat an order ships is im m at erial, t he t hr ee dat et im e colum ns are in LEFT funct ions t hat st r ip off j ust t he first 11 charact ers for display. This perm it s t he display of t he dat es in t hr ee part s: a t hr ee- charact er part for t he m ont h, up t o t wo digit s for t he display of t he day num ber in t he m ont h, and a four - digit field for t he year. A single blank charact er delim it s t he first part from t he second part and t he second part from t he t hird part . The WHERE clause for t he follow ing SELECT st at em ent per form s t he dat e arit hm et ic. The expression for t he clause r et urns all r ows from t he Orders t ables whose RequiredDat e is less t han ShippedDat e plus 3. Values in dat et im e form at represent one day w it h an int eger value of 1. By adding 3 t o t he ShippedDat e colum n value, t he WHERE clause expression com put es a proj ect ed ar r ival dat e t hat is t hree days aft er t he order ships. I f t he proj ect ed arr ival dat e is great er t han t he Requir edDat e colum n value, t he order is lat e. The SELECT st at em ent includes only lat e order s in it s result set . --ListLateOrders --List just date portion of datetime column values --with a criterion based on day offset between two columns. SELECT OrderID, Left(OrderDate,11) ’OrderDate’, Left(RequiredDate,11) ’RequiredDate’, Left(ShippedDate,11) ’ShippedDate’ FROM Orders WHERE RequiredDate < ShippedDate + 3 The result set from t he preceding script is useful for get t ing a basic grasp of lat e orders, but all it does is list t he orders. The v iewer of t he r esult set is r esponsible for com put ing t he num ber of days t hat an order is lat e as well as finding t hose orders t hat m issed t he r equired dat e by a wide m argin. The following script rem edies bot h of t hese weak nesses. The rem edy fashions a solut ion based on dat e ar it hm et ic. The sam ple’s ar it hm et ic wit h dat et im e values relies on t he DATEADD and DATEDI FF funct ions. I t uses t hese t w o syst em funct ions t o com put e t he num ber of days t hat an order is lat e. Wit h t he DATEADD funct ion, t he follow ing script adds 3 days t o ShippedDat e t o com put e a proj ect ed arr ival dat e. The script com put es t he num ber of days an order is lat e by depending on t wo expressions. First t he WHERE clause expr ession filt ers for j ust t hose r ows in w hich t he proj ect ed ar r ival dat e v alue is great er t han t he RequiredDat e colum n v alue. The orders on t hese rows fr om t he Orders t able are lat e. Second t he script com put es t he num ber of days t hat an order is lat e. The ex pression for com put ing t his nest s t he expression for t he proj ect ed ar r ival dat e v alue inside a DATEDI FF funct ion wit h t he Requir edDat e colum n value. DATEDI FF explicit ly refer ences day s as t he m et r ic for com put ing t he difference bet ween t he t w o values. This DATEDI FF funct ion appears in t he SELECT list and in t he ORDER BY clause. The SELECT list for t his DATEDI FF funct ion includes in t he result set t he num ber of days t hat an order is lat e; t he nam e of t his calculat ed colum n is Days Lat e. The ORDER BY clause includes t he DESC key word so t hat SQL Ser ver w ill sort t he r esult set w it h t he lat est orders list ed first . --CalculateDaysLate --Demonstrates uses of DATEDIFF for Days Late calculation --and DATEADD for day offset in criterion expression. SELECT OrderID, Left(OrderDate,11) ’OrderDate’, Left(RequiredDate,11) ’RequiredDate’, Left(ShippedDate,11) ’ShippedDate’, DATEDIFF(day,RequiredDate,DATEADD(day, 3, ShippedDate)) ’Days Lat e’ FROM Orders

WHERE RequiredDate < DATEADD(day, 3, ShippedDate) ORDER BY DATEDIFF(day,RequiredDate,DATEADD(day, 3, ShippedDate)) DESC The last sam ple script in t his sect ion illust rat es how t o aggregat e a calculat ed value and t hen group it by quart er w it hin y ear . The sam ple also shows t he synt ax for filt er ing gr oups defined by a GROUP BY clause wit h a HAVI NG clause. The script does it s aggregat ing w it h a SUM funct ion defined on t he DATEDI FF expression for com put ing t he num ber of days an order is lat e. This SUM funct ion requir es a GROUP BY clause. The one in t he sam ple specifies an order’s year and quart er as grouping colum n values. Tw o separat e DATEPART funct ions der ive t he year and quart er for an OrderDat e. The SELECT list cont ains t hree it em s—t he t w o DATEPART funct ions for t he y ear and quart er and t he SUM funct ion for t he num ber of days lat e. A HAVI NG clause includes t wo separat e expr essions t o filt er groups in t he r esult set . First , only groups w it h a y ear value great er t han 1996 can belong t o t he r esult set . Second, t he HAVI NG clause ex cludes t he group corr esponding t o t he second quart er of 1998. The SELECT st at em ent ’s final line is an ORDER BY clause t hat ensur es r ows appear in order by quart er w it hin y ear . --DaysLatePerQuarter -Demo Sum aggregate function of DATEDIFF with GROUP BY and HAVING clau ses. SELECT DATEPART(yyyy, OrderDate) AS ’Year’, DATEPART(q, OrderDate) AS ’Quarter’, SUM(DATEDIFF(d, RequiredDate, DATEADD(day, 3, ShippedDate))) AS ’ Days Late’ FROM Orders WHERE (DATEDIFF(d, RequiredDate, DATEADD(day, 3, ShippedDate)) > 0) GROUP BY DATEPART(yyyy, OrderDate), DATEPART(q, OrderDate) HAVING DATEPART(yyyy, OrderDate) > 1996 AND NOT(DATEPART(yyyy, OrderDate) = 1998 AND DATEPART(q, OrderDate) = 2) ORDER BY DATEPART(yyyy, OrderDate), DATEPART(q, OrderDate)

Join s a nd Su bqu e r ie s Joins ar e a powerful t echnique for com bining t w o or m ore r ow sources in a single SELECT st at em ent . This sect ion int roduces j oins wit h a r ev iew of T- SQL t echniques for cr eat ing inner j oins bet w een t wo t ables. Then it goes on t o explore ot her form ulat ions for inner j oins and ot her kinds of j oins. The sect ion closes w it h a couple of sam ples dem onst rat ing ways of for m ulat ing SELECT st at em ent s w it h subquer ies. This approach is a way of m ak ing SELECT st at em ent s dynam ic because t he subquery can ret ur n t he m ost current value t o t he SELECT st at em ent referencing it .

An I n n e r Join Be t w e e n Tw o Ta ble s By using an inner j oin, your dat abase solut ions can r efer sim ult aneously t o t he cont ent fr om t w o different row sources. So far, t he sam ples in t his chapt er have focused on j ust one t able. For exam ple, som e sam ples used t he Order Det ails t able t o dev elop an expr ession for ext ended pr ice. Ot her sam ples work ed wit h t he days t hat an order was lat e. These sam ples used t he Orders t able. No sam ple processed cont ent from bot h t he Orders and Or der Det ails t ables in a single solut ion. I nner j oins enable t his t ype of funct ionalit y. An inner j oin m ost t ypically m erges t wo t ables when t heir values m at ch on a com m on field, such as a prim ary key fr om one t able and it s m at ching foreign key in anot her t able.

The first j oin sam ple list s t wo colum ns from t wo differ ent t ables—t he Or ders and Order Det ails t ables. The SELECT st at em ent r et urns t he OrderI D colum n from t he Orders t able and t he Pr oduct I D colum n from t he Order Det ails t able. The OrderI D colum n appears in bot h t ables. Therefore, t he SELECT st at em ent m ust use a t able qualifier t o indicat e fr om which t able t o ext ract t he OrderI D colum n values. The JOI N key word in t he FROM clause designat es t he t w o t ables cont r ibut ing colum n values t o t he r esult set from t he SELECT st at em ent . The ON k ey word point s t o t he colum ns w it hin each t able on which t o j oin t he t ables. --JoinColumns --Join columns from two tables. SELECT Orders.OrderID, ProductID FROM Orders JOIN [Order Details] ON (Orders.OrderID = [Order Details].OrderID) The next sam ple uses t he OrderI D colum n values fr om t he Orders t able t o m erge it s cont ent w it h m at ching records based on OrderI D in t he Order Det ails t able. An inner j oin im plem ent s t he m erge. As a result of t he m erge, a single SELECT st at em ent can access cont ent from bot h t ables. The r esult set for t he SELECT st at em ent r et urns bot h t he Days Lat e calculat ed colum n from t he Orders t able and t he Ex t . Pr ice calculat ed colum n fr om t he Order Det ails t able. You can specify an inner j oin w it h eit her t he FROM clause or t he WHERE clause of a SELECT st at em ent . The follow ing sam ple dem onst rat es t he synt ax for t he FROM clause. Wit hin t he FROM clause, posit ion t he JOI N keyword bet ween t he t wo r ow sources part icipat ing in t he inner j oin. I t is im m at er ial w hich t able is on t he left and right sides of t he JOI N keyword. You can opt ionally r eplace JOI N w it h I NNER JOI N. Your FROM clause also r equires an ON keyword. The argum ent for t he ON keyw ord expr esses how t o m erge t he r ows from t he t w o sources for t he j oin. The ON argum ent expression w ill oft en denot e an equivalence bet w een t wo colum n nam es, one from each of t he row sources part icipat ing in t he j oin. The ON keyw ord expr ession dict at es which colum ns t o com par e and how t o com pare t hem bet w een t he t w o r ow sources. Not ice t hat t he expr ession includes a t able nam e qualifier for t he colum n nam e. This is one way t o dist inguish t he source for a colum n. I t is vit ally im port ant t hr oughout a SELECT st at em ent wit h a j oin t o indicat e t he source for a colum n w hen t he colum n has t he sam e nam e for t he row source on eit her side of t he JOI N k eyw ord. I f t he colum n doesn’t hav e t he sam e nam e in bot h row sources, t he designat ion of a t able nam e qualifier is opt ional. The sam ple script inst r uct s SQL Ser ver t o m at ch OrderI D colum n values from t he Orders t able w it h Order I D colum n values fr om t he Order Det ails t able. The result set cont ains j ust t hose r ows fr om t he Order Det ails t able w it h m at ching OrderI D values fr om t he Orders t able. Because a single order can spread acr oss m ult iple rows in t he Order Det ails t able, colum n values from t he Orders t able r epeat for each of t he m ult iple rows wit hin an order. As w it h any SELECT st at em ent , t he SELECT list specifies t he colum n nam es for t he r esult set . The sam ple includes a m ix of r eal and calculat ed colum ns. OrderI D from t he Orders t able is a r eal colum n. All t he ot her colum ns are calculat ed. The colum ns w it h t he nam es OrderDat e, RequiredDat e, and ShippedDat e m er ely apply a LEFT funct ion t o ext ract t he dat e port ion of a dat et im e value. The colum ns w it h t he nam es Days Lat e and Ext . Pr ice inv ok e m or e sophist icat ed expressions t o calculat e t heir colum n values. The last colum n, Ext . Pr ice, references t he Order Det ails t able. The WHERE clause filt ers for orders proj ect ed t o ar riv e aft er t he RequiredDat e value. The ORDER BY clause k eeps t he line it em rows for an order t oget her. Because t he OrderI D colum n is in bot h r ow sources for t he j oin, it is necessary t o use a t able nam e qualifier for t he colum n nam e. --DaysLateUsingJoin --List results from two tables based on day offset criterion. SELECT Orders.OrderID, LEFT(Orders.OrderDate,11) AS ’OrderDate’, LEFT(Orders.RequiredDate,11) AS ’RequiredDate’, LEFT(Orders.ShippedDate,11) AS ’ShippedDate’,

DATEDIFF(day,RequiredDate,DATEADD(day, 3, ShippedDate)) ’Days Lat e’, CAST([Order Details].Quantity*[Order Details].UnitPrice* (1-[Order Details].Discount) AS dec(9,2)) AS ’Ext. Price’ FROM Orders JOIN [Order Details] ON (Orders.OrderID = [Order Details].OrderID) WHERE RequiredDate < DATEADD(day, 3, ShippedDate) ORDER BY [Order Details].OrderID Figur e 3- 5 displays an excerpt from t he r esult set for t he preceding script . The OrderI D colum n is from bot h r ow sources. The OrderI D colum n value r epeat s for each line it em wit hin an order. The OrderDat e, RequiredDat e, ShippedDat e, and Days Lat e colum ns are from t he Orders t able. The values in t hese colum ns r epeat across t he m ult iple r ow s wit hin an order. Wit hin t he excerpt , t he Ext . Pr ice colum n values are unique for each r ow in t he r esult set . The scr ipt calculat es t hese colum n values based on t hree colum ns in t he Order Det ails t able. Figu r e 3 - 5 . An ex ce rp t from a re su lt set t h at d ispla ys con t e n t from t w o row sou r ces.

Usin g Alia se s W it h in a n I n n e r Join Because SELECT st at em ent s can get long and difficult t o r ead w it h t able nam e qualifiers, it is com m on t o use aliases. An alias is an alt er nat ive nam e for a t able t hat y ou specify w it hin your SELECT st at em ent . Use t he alias as a short nicknam e for t he or iginal t able nam e. You can specify y our alias w it hin t he FROM clause im m ediat ely aft er specifying a t able by it s nam e. Howev er, y ou can use an alias anyw her e t hr oughout a SELECT st at em ent , such as in t he SELECT list or t he ORDER BY clause. The follow ing sam ple illust rat es a j oin for t he t it les and t it leaut hor t ables from t he pubs dat abase. The FROM clause designat es t he alias t for t he t it les t able and t a for t he t it leaut hor t able. You can also see t he use of t hese aliases in t he SELECT list and ORDER BY clauses in t his excerpt fr om t he script on t he following page. SELECT ta.au_id, t.title, t.ytd_sales, t.price FROM pubs..titles t JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) ORDER BY ta.au_id The sam ple also illust rat es t he synt ax for r efer r ing t o a r ow source out side t he curr ent dat abase cont ex t . Recall t hat all t he sam ples t hroughout t his chapt er use t he Nort hw ind dat abase, and t hey r ely on an a USE st at em ent fr om t he second sam ple t o specify t he dat abase connect ion for t he sam ple. The follow ing sam ple

has t he sam e dat abase cont ext , but it uses t hr ee- part nam es t o reference a r ow source in anot her dat abase—t he pubs dat abase. The first part is t he dat abase nam e, and t he second part is t he r ow source ow ner’s nam e. When t he owner ’s nam e is dbo ( as in t he cur r ent inst ance) , you can leav e t he second part null ( w hich m eans you end up w it h t w o consecut ive per iods) . The t hird part is t he r ow source nam e. I n t his sam ple, t hat is eit her t it les or t it leaut hor. Ther e is one ot her special feat ur e about t he sam ple. I t includes T- SQL code t o print t o t he Messages Pane t he num ber of r ows in t he r esult set . An earlier sam ple in t he “ Specify ing Colum ns and Rows” sect ion describes t he approach applied in t he sam ple below . The reason for ex plicit ly count ing t he row s is t o com pare t he num ber of rows in t his result set , 25, w it h a subsequent sam ple t hat uses a differ ent k ind of j oin. --InnerJoinWithAliases --Inner join between authors titles and titleauthor. --Returns 25 matching rows from both tables. SET NOCOUNT ON Declare @strRows nvarchar(50) SELECT ta.au_id, t.title, t.ytd_sales, t.price FROM pubs..titles t JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) ORDER BY ta.au_id SET @strRows = ’Rows returned = ’ + Cast(@@ROWCOUNT AS nvarchar) PRINT @strRows SET NOCOUNT OFF

An I n n e r Join Be t w e e n Th r e e Ta ble s I t is oft en necessary t o m erge t he r esult s of m ore t han t w o row sources in a single SELECT st at em ent . How ever, y ou can j oin only t wo r ow sources at a t im e. The wor kar ound t o t his predicam ent is t o use a j oined r ow source as one of t he row sources for a new j oin. This sect ion dem onst rat es how t o im plem ent t his logic for t he j oining of t hree t ables fr om t he pubs dat abase. This k ind of j oin is part icular ly appropr iat e for m odeling a pair of t ables in a m any- t o- m any relat ionship wit h a j unct ion t able bet ween t hem . The general approach t o dev eloping j oins in t his sect ion is applicable for m or e t han t hree row sources. See t he T- SQL scr ipt for t he I nv oices view in t he Nort hwind dat abase for a sam ple script t hat j oins six t ables.

N ot e You can use Ent erpr ise Manager t o view t he scr ipt for a dat abase obj ect . For m ore inform at ion on Ent erprise Manager and ot her SQL Ser ver 2000 t ools, see Books Online. The special synt ax for a t hr ee- t able j oin is in t he FROM clause of your SELECT st at em ent . Add t ables t o t he FROM clause in t he order t hat you want t hem t o j oin—st art ing from t he ext r em e left t able. Join t his t able t o one of y our r em aining t wo t ables. Use t he sy nt ax pr ev iously present ed for j oining t wo t ables. Aft er t he argum ent for t he ON key word, add a second inst ance of t he JOI N k eyw ord follow ed by t he nam e of t he t hird t able. Next add a second inst ance of t he ON keyw ord t hat specifies how t o j oin t he t hird t able w it h t he j oined first and second t ables. Aft er specify ing t he FROM clause as described, y ou are fr ee t o r efer t o colum ns fr om any of t he t hr ee t ables. You can ev en creat e calculat ed colum ns t hat draw on colum ns fr om t wo or t hree t ables. The FROM clause in t he follow ing scr ipt dem onst rat es how st raight for ward it is t o j oin t hr ee t ables. This sam ple script j oins t he t it les t able w it h t he t it leaut hor

t able. Then t he scr ipt m erges t he j oined t it les and t it leaut hor t ables w it h t he aut hors t able. The script illust rat es t he sy nt ax for j oining t he t hree t ables as well as t he use of colum ns fr om all t hr ee t ables in t he SELECT list . --InnerJoinWithThreeTables --List results from three tables. SELECT aut.au_fname, aut.au_lname, t.title, t.ytd_sales, t.royalty, ta.royaltyper FROM pubs..titles t JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) JOIN pubs..authors aut ON (ta.au_id = aut.au_id) I n addit ion t o list ing colum ns fr om all t hr ee t ables, y ou can use t he j oin t o com put e calculat ed colum ns wit h input s fr om t wo or m ore t ables. The follow ing script illust rat es t his design feat ur e wit h a calculat ed colum n for r oyalt y paid t o an aut hor for a t it le. The calculat ed colum n draws on yt d_sales and roy alt y from t he t it les t able and royalt yper fr om t he t it leaut hor t able. Bot h roy alt y and roy alt yper represent percent ages as int egers. Ther efore, t he calculat ed field div ides t he pr oduct for all t hr ee colum ns by 10,000. --JoinWithCalculatedColumn --List results from three tables, including a calculated column --based on two tables. SELECT aut.au_fname, aut.au_lname, t.title, CAST(t.ytd_sales * t.royalty * ta.royaltyper AS money)/10000, t.advance FROM pubs..titles t JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) JOIN pubs..authors aut ON (ta.au_id = aut.au_id) ORDER BY t.title, aut.au_lname, aut.au_fname

Ou t e r Join s An out er j oin cont rast s wit h an inner j oin by adding in all t he rows fr om a r ow source w het her or not t he r ow sat isfies an expr ession for t he ON keyw ord. There are t hree t ypes of out er j oins: a left out er j oin, a right out er j oin, and a full out er j oin. When perform ing one of t hese out er j oins, replace JOI N or I NNER JOI N wit h an appropr iat e alt ernat ive t erm , such as LEFT OUTER JOI N, RI GHT OUTER JOI N, or FULL OUTER JOI N. Wit h a left out er j oin, all t he r ows from t he row source on t he left side of t he LEFT OUTER JOI N keyword phrase appear in t he r esult set whet her or not t hey sat isfy t he expr ession in t he argum ent for t he ON keyw ord. A right out er j oin w or ks sim ilar ly t o a left out er j oin, but it adds in all t he r ows from t he r ow source on t he right of RI GHT OUTER JOI N. A full out er j oin adds in all t he rows fr om r ow sources on bot h sides of FULL OUTER JOI N. Aside from t he keyw ord phrase nam e, t he synt ax for t he t hr ee t ypes of out er j oins is t he sam e as for an inner j oin. The follow ing sam ple dem onst rat es t he synt ax for a left out er j oin bet w een t he t it les t able and t he t it leaut hor t able in t he pubs dat abase. Not ice t hat t he synt ax exact ly follows t he pr eceding inner j oin sam ple bet ween t hese t ables ex cept for t he r eplacem ent of t he JOI N k ey word by t he LEFT OUTER JOI N k eyw or d phr ase. I n addit ion, t he result set for t his SELECT st at em ent includes 26 r ows inst ead of t he 25 rows in t he preceding sam ple. The ext ra row is fr om a book t it le t hat doesn’t hav e an aut hor designat ed for it . The pr eceding SELECT st at em ent screened out t his ex t ra row because t he t it les t able t it le_id colum n value had no m at ch in t he t it leaut hor t able. However, because t he t it les t able is on t he left side of t he LEFT OUTER JOI N, t he j oin forces in t he r ow fr om t he t it les t able, alt hough it has no corr esponding t it le_id colum n value in t he t it leaut hor t able. --LeftOuterJoin --Left outer join between authors titles and titleauthor.

--Returns 26 rows (25 matching rows + 1 non-matching row --from the titles table). SET NOCOUNT ON Declare @strRows nvarchar(50) SELECT ta.au_id, t.title, t.ytd_sales, t.price FROM pubs..titles t LEFT OUTER JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) ORDER BY ta.au_id SET @strRows = ’Rows returned = ’ + Cast(@@ROWCOUNT AS nvarchar) PRINT @strRows SET NOCOUNT OFF One pract ical use for left and r ight out er j oins is t hat of list ing rows on one side of a j oin w it hout a m at ching row on t he ot her side. For exam ple, w e can use an adapt at ion of t he preceding sam ple t o list t he specific r ow in t he t it les t able t hat has no m at ching t it le_id colum n value in t he t it leaut hor t able. The following script dem onst rat es t he synt ax for t he solut ion. Not ice t hat t he basis for t he solut ion is a WHERE clause t hat screens for a null value fr om t he t able w it hout t he m at ching row . --RowsWithNoMatch --Find rows in the left table without a match in the right table. SELECT ta.au_id, t.title, t.ytd_sales, t.price FROM pubs..titles t LEFT OUTER JOIN pubs..titleauthor ta ON (t.title_id = ta.title_id) WHERE ta.au_id IS NULL

Se lf Join s a n d Cr oss Joins Two special k inds of j oins, w hich serv e cont rast ing purposes, ar e self j oins and cross j oins. A self j oin m erges a t able w it h it self. Use a self j oin w hen you need t o relat e t he v alues in one colum n t o t he values in anot her colum n of t he sam e t able. A cr oss j oin cr eat es a r esult set t hat com bines colum n values from all t he rows in one row source wit h colum n values from all t he rows in a second row source. This is differ ent from an out er j oin because a cross j oin doesn’t creat e any null values in it s result set . You w ill t ypically use t his k ind of j oin w hen at least one of your row sources is v er y sm all, such as a scalar value or a r ow source w it h j ust a couple of rows. Wit hin t he cont ext of t he Nort hw ind dat abase, t he classic sit uat ion calling for t he applicat ion of a self j oin is t he t ask of r et ur ning t he nam es of t he m anagers from t he Em ploy ees t able. This t able cont ains a separat e r ow for each em ploy ee, wit h t wo set s of colum ns t hat cont r ibut e t o t he t ask. The first set includes t he Em ploy eeI D, First Nam e, and Last Nam e colum ns. The second set includes a single colum n, Report sTo. The Report sTo colum n cont ains t he Em ploy eeI D value for t he m anager t o w hom an em ployee r eport s. You can find t he m anager nam es by m erging t he Report sTo colum n values in t he second set w it h t he Em ployeeI D values in t he first set . The First Nam e and Last Nam e colum n values for t he m at ching records ar e t he m anager nam es. Manager nam es w ill r epeat for as m any direct report s as t hey hav e. Therefore, adding a DI STI NCT pr edicat e t o t he SELECT st at em ent rem oves t he duplicat es. The sy nt ax for a self j oin is t he sam e as for an inner j oin. How ev er, t he sam e r ow source appears on bot h sides of t he JOI N keyw ord. Wit h a self j oin, t he use of aliases is m andat ory . I t is t hrough t he aliases t hat you designat e t he left and right row sources. The follow ing sam ple shows t he T- SQL for finding t he m anagers from t he Em ployees t able. Not ice t hat t he ex pr ession for t he ON keyw ord m at ches t he Report sTo colum n values t o t he Em ploy eeI D colum n values. The DI STI NCT pr edicat e aft er SELECT r em ov es t he m ult iple inst ances of m anager nam es fr om t he r esult set .

--SelfJoin --Self join to find managers in Employees table. SELECT DISTINCT em.ReportsTo, e.FirstName, e.LastName FROM Employees em JOIN Employees e ON (em.ReportsTo = e.EmployeeID) The cr oss j oin does hav e it s ow n key word phrase t o denot e it s j oin t ype. ( Not surprisingly , t he keywor d phr ase is CROSS JOI N.) The sy nt ax is dist inct ive as well. This is because t he FROM clause doesn’t need t he ON k ey word t o specify colum ns for com par ing bet ween t he t wo r ow sources. A cross j oin aut om at ically m erges all t he rows fr om one source w it h each row from t he ot her source; in ot her words, it generat es one row for each possible pairing of rows fr om t he t wo sources. That m eans it ’s im port ant t hat at least one of t he row sources hav e j ust one row or very few rows. A cross j oin of t wo t ables w it h j ust 10,000 rows each generat es a result set w it h 100,000,000 r ows! You can lim it t he size of t he r esult set t hr ough WHERE clause argum ent s t hat r est rict t he rows part icipat ing in t he cross j oin from eit her t he left or t he r ight row source. The follow ing sam ple shows a sim ple cr oss j oin t hat m erges Com pany Nam e colum n values fr om each row in t he Shippers t able w it h OrderI D colum n values from t he Orders t able t hat are less t han or equal t o 10,249. Only t wo OrderI D values m at ch t his condit ion, and t here ar e j ust t hr ee r ows in t he Shippers t able, so t he r esult set for t he cross j oin cont ains only six r ows. The sy nt ax for t he cross j oin appears below , and Figur e 3- 6 shows t he result set . --CrossJoin --Cross join selected rows from one table with all --selected rows from a second table based on a --WHERE clause. SELECT OrderID, CompanyName FROM Orders CROSS JOIN Shippers WHERE OrderID 4 GROUP BY OrderID ORDER BY COUNT(OrderID) Alt er nat ively, w e could r eplace t he inner query wit h a HAVI NG clause, as shown in t he follow ing script . Cor relat ed subquer ies usually car ry a perform ance penalt y, so you hav e t o evaluat e carefully whet her any benefit der ived fr om t he correlat ed subquery is w ort h t he penalt y . When y ou ar e form ulat ing ad hoc quer ies for use a lim it ed num ber of t im es, cor relat ed quer ies m ay m ak e sense if t he subquery for m ulat ion is easier for you t o st at e t han ot her, m ore efficient , appr oaches. --CorrelatedSubqueryWithHaving

--HAVING clause alternative to the preceding --correlated subquery sample. SELECT OrderID, COUNT(OrderID) ’Line items’, ’$’ + CONVERT(varchar,CAST(SUM(Quantity*UnitPrice*(1Discount)) AS money),1) FROM [Order Details] GROUP BY OrderID HAVING COUNT(OrderID) > 4 ORDER BY COUNT(OrderID)

Cha pt e r 4 . Pr ogr a m m ing Vie w s a nd St or e d Pr oce dur e s The preceding chapt er int r oduced y ou t o program m ing dat a access wit h T- SQL. This chapt er builds on and goes beyond t he int r oduct ion in t w o explicit ways: First it int roduces v iews by descr ibing t heir uses wit h various t ypes of r ow sources. Second it int r oduces you t o st ored procedur es by rev iew ing t heir uses and t he st at em ent s for creat ing and alt ering t hem , and by focusing on t he use of param et ers and local variables t hat are oft en found in st or ed pr ocedur es. A view is a cont ainer for a single SELECT st at em ent . Your SQL Serv er applicat ions can r efer t o t he v iew nam e as a short cut t o t he SELECT st at em ent w it hin t he view. I n t his chapt er, y ou w ill lear n t he sy nt ax for creat ing and using v iews. Special at t ent ion goes t o cr eat ing views for dat a on rem ot e serv ers and for dat a in ot her dat abase form at s, such as Access and any ODBC dat a source. St or ed pr ocedur es are com piled set s of T- SQL st at em ent s. Aft er int r oducing t he synt ax for creat ing st or ed pr ocedures, t he chapt er drills dow n on t he sy nt ax for m anipulat ing param et er s and ret ur n st at us values, pr ogram m ing t he insert ion and delet ion of r ows as well as t he updat ing of colum n values in row sources, and t he r et ur n of condit ional r esult set s fr om a st or ed procedure. The resources for t his chapt er include a dat abase, Chapt er04, w it h com plet ed versions of t he sam ple views and st or ed procedur es discussed as well as T- SQL script s for creat ing t he views and st or ed procedur es from scrat ch. Unless explicit ly st at ed, all script s should be r un fr om t he Chapt er04 dat abase. See t he “ Chapt er Resources” sect ion in Chapt er 2 for m or e det ail on at t aching dat abase files t o a serv er and cr eat ing a new blank dat abase fr om w hich you can inv ok e t he script s. The chapt er also r efer ences ot her com m only available dat abases, including t he SQL Ser ver Nort hw ind dat abase, t he pubs dat abase, and t he Access Nort hwind dat abase. The first t wo dat abases ar e inst alled w it h SQL Ser ver; t he t hird dat abase is inst alled w it h Access. For t he references t o r em ot e serv ers, y ou will need an inst ance of SQL Ser ver r unning on t wo different com put ers or t w o inst ances of SQL Serv er r unning on t he sam e com put er .

I n t r odu ct ion t o View s A SQL Serv er v iew is a virt ual t able. As w it h a t able, y ou can use a v iew in m any way s, but unlik e a t able, a v iew doesn’t act ually st ore r ows of dat a. I nst ead, w hat it st or es is a SELECT st at em ent , such as one of t hose cover ed in Chapt er 2. The result set of t he SELECT st at em ent const it ut es t he dat a available t hr ough a v iew . The FROM clause of t he view’s SELECT st at em ent can r efer ence ot her v iews as well as base t ables.

Use s for Vie w s You can use v iews as a way of insulat ing users from t he dat abase design in t he schem a of a cust om applicat ion. This benefit m akes y our applicat ions m or e robust in t he face of ongoing r equirem ent s t o updat e schem a designs. The I NFORMATI ON_SCHEMA views discussed in sev eral sect ions t hroughout Chapt er 2 illust rat e t his use for v iews. This appr oach t o ex posing dat a perm it s your cust om solut ions t o change an applicat ion’s schem a but st ill prov ide t he sam e inform at ion t o t he end users of an applicat ion. All y ou need is t o updat e t he view so t hat it select s t he sam e dat a as before t he schem a change.

You also can use v iews t o secure eit her t he r ows or t he colum ns from a base t able. Wit h t he SELECT list and t he WHERE clause for a v iew’s SELECT st at em ent , you can filt er dat a from a base t able. I n ot her w ords, a v iew perm it s you t o expose a subset of a row source. For exam ple, you could base a Visual Basic .Net applicat ion on a v iew inst ead of a t able if y ou want ed t o rest r ict t he access of t he applicat ion users t o j ust rows t hat m at ch t he cr it eria in t he WHERE clause. This approach “ secures” t he rows filt er ed out of t he view. I nst ead of filt er ing rows wit h a WHERE clause, y ou can exclude select ed colum ns w it h sensit iv e dat a from a SELECT list , such as colum ns for salary and bonus. Again, by excluding dat a, y ou “ secur e” t he dat a from t hose w it hout aut hor it y t o v iew it . A view is part icular ly valuable for com bining t he dat a for t w o or m ore base t ables int o a single r ow source. The various j oin clauses enable t his capabilit y very flex ibly. You can also use a UNI ON operat or t o com bine t he dat a from t wo or m or e t ables. A UNI ON operat or cont rast s wit h j oin clauses by concat enat ing one row source aft er anot her. Join clauses st it ch r ow sources t oget her side by side.

N ot e See “ Com bining Result s w it h UNI ON” in Books Online as a st ar t ing point for m ore coverage of UNI ON queries. Using t he OPENROWSET funct ion allows access t o rem ot e, het erogeneous dat a sources t hrough a v iew. This funct ion perm it s you t o access non- SQL Ser ver dat a from SQL Ser ver v iews. I n addit ion, you can ret ur n dat a and ev en j oin dat a fr om ot her com put ers. The OPENROWSET funct ion depends on an OLE DB prov ider for connect ing t o a dat a source; t he prov ider det er m ines t he t ype of funct ionalit y available from t he source. This funct ion is part icularly appr opr iat e for ad hoc quer ies. The OPENROWSET funct ion w or ks wit h what ev er user nam e and password your applicat ion supplies it . Alt er nat ives t o t he OPENROWSET funct ion include t he OPENDATASOURCE funct ion and link ed serv ers. Books Online r ecom m ends link ed ser vers for fr equent ly used connect ions t o dat a sources out side t he scope of t he act iv e SQL Ser ver inst ance. ( See t he “ Rem ar ks” sect ion of t he “ OPENDATASOURCE” t opic.) Adm inist er ing a link ed ser ver requir es a login t hat belongs t o t he sysadm in or set upadm in fix ed server role. Anot her purpose for a v iew is t he represent at ion of aggr egat ions fr om a base t able. A view can count or sum colum n values in a base t able ov er all or by groups. This capabilit y of present ing dat a sum m aries confirm s a v iew as a decision- support t ool. Because views encapsulat e SELECT st at em ent s for r euse, you can add new v iews t o a dat abase based on T- SQL quer ies dev eloped by, or in coordinat ion w it h, t he end users of an applicat ion. This feat ure m ak es views desirable for ex t ending t he funct ionalit y of applicat ions in ways t hat you know hav e user appeal. I t is im port ant t hat you grasp t he not ion of a v iew as a virt ual t able because t his conv eys som e pow erful clues about t he needs t hey can fulfill in a cust om solut ion. Your applicat ions can insert , updat e, and delet e dat a t hrough a view . These capabilit ies depend on t he char act er ist ics of t he v iew . For exam ple, you can perform insert / updat e/ delet e funct ions for v iews of a single base t able but not for views t hat expose aggregat es of a base t able. The “ Rem ar ks” sect ion of t he “ CREATE VI EW” t opic in Books Online det ails r ules for t he m odificat ion of t he row source behind a v iew. You can index views t o speed t heir perform ance—j ust as y ou can w it h t ables. I ndexed v iews deliv er benefit s w hen you’re w ork ing w it h v ery large t ables. See t he “ Creat ing an I ndexed View” t opic in Books Online for a st art ing point for lear ning m ore about index ed v iews. Part it ioned v iews repr esent a m eans of segm ent ing a t able over m ult iple com put ers each r unning SQL Serv er ; you aggregat e t he part it ions of a v iew wit h

UNI ON operat ors. Thr ough part it ioned v iews, a view on each serv er w it h a segm ent can browse, add, updat e, and delet e r ows in t he w hole t able ( across all serv ers) . Part it ioned v iews are a r obust way of wor k ing w it h very large dat abases. See t he “ Cr eat ing a Part it ioned View ” t opic in Books Online for help wit h pr eparing part it ioned v iews.

St a t e m e nt s for Cr e a t in g a nd Alt e r ing Vie w s You can generat e and m odify v iews w it h t he T- SQL CREATE VI EW and ALTER VI EW st at em ent s. I n it s m ost basic form , a CREATE VI EW st at em ent specifies a nam e for t he v iew and a SELECT st at em ent t o designat e it s r esult set . Posit ion t he v iew’s nam e aft er a space delim it er follow ing t he CREATE VI EW key word phrase. Then use t he AS keyword t o separ at e t he v iew’s nam e fr om it s SELECT st at em ent . For exam ple, y ou can cr eat e a new view wit h t his synt ax : CREATE VIEW view_name AS SELECT list_of_columns FROM base_table_name

View nam es are st andar d SQL Serv er ident ifiers. Ther efor e, t hey m ust follow t he rules for all obj ect ident ifiers. Refer t o t he “ Using I dent ifiers” t opic in Books Online for a sum m ary of t he rules for specify ing ident ifiers. I n addit ion, user defined v iews are obj ect s like ot her syst em and user - defined SQL Serv er obj ect s. Because SQL Serv er obj ect s share a com m on nam espace, y ou m ay car e t o use prefixes t o r eflect t he t y pe of obj ect and avoid nam e conflict s. For exam ple, t his chapt er uses t he vew pr efix for all user- defined views. Just as wit h t ables and ot her dat abase obj ect s, you cannot creat e a new v iew wit h t he sam e nam e as an ex ist ing v iew. You m ust r em ov e t he pr ior v ersion of t he v iew before creat ing a new v iew w it h t he sam e nam e as an ex ist ing one in a dat abase. The DROP VI EW st at em ent support s t he r em oval of an ex ist ing v iew. A couple of I NFORMATI ON_SCHEMA v iews r et ur n t he nam es of t he v iews in a dat abase. This chapt er dem onst rat es t he use of t hese v iews. You can inv ok e t he ALTER VI EW st at em ent t o change an ex ist ing v iew wit hout delet ing it t ot ally. The ALTER VI EW st at em ent preserv es perm issions set on a view and doesn’t alt er t he dependency of an I NSTEAD OF t r igger or a st or ed procedur e on a view.

Re st r ict ions on SELECT St a t e m e n t s f or Vie w s While y ou do have access t o m ost of t he SELECT st at em ent funct ionalit y, t here are som e design lim it at ions as w ell as som e differ ences in behav ior for SELECT st at em ent s in v iews. For exam ple, a SELECT st at em ent in a v iew cannot cont ain a COMPUTE or COMPUTE BY clause because eit her clause can r et ur n m ult iple r esult set s. Views m ust always r et urn a single r esult set . I n t his way, a v iew em ulat es a t able. The single r esult set fr om a v iew can ser ve as a t able in m any ot her T- SQL st at em ent s. You cannot use an ORDER BY clause by it self in t he SELECT st at em ent for a v iew. The Books Online docum ent at ion at sev eral point s m akes t his assert ion wit hout bot her ing t o not e an im port ant case t hat perm it s t he use of an ORDER BY clause inside t he SELECT st at em ent for a v iew. I n t his special case, y ou use t he TOP

predicat e inside t he SELECT st at em ent . Subsequent sam ples will dem onst rat e t he synt ax for t his. The WI TH CHECK OPTI ON clause is a special clause t hat applies t o SELECT st at em ent s inside v iews. This clause can rest r ict a user’s abilit y t o insert new records t hrough a view or m odify t he values in t he r esult set t hat a view exposes. The WI TH CHECK OPTI ON clause r equir es t hat all m odificat ions t o t he r ow source for a v iew com ply w it h crit er ia st at em ent s in t he SELECT st at em ent for a v iew .

Vie w At t r ibut e s Three v iew at t r ibut es help t o r efine t he funct ionalit y t hat a v iew prov ides. A view’s at t ribut e specificat ion can appear follow ing it s nam e in a CREATE VI EW or ALTER VI EW st at em ent . Use WI TH as a k eyw ord befor e t he at t r ibut e nam e. Using t he ENCRYPTI ON at t ribut e encrypt s t he SELECT st at em ent for t he v iew. Users get t he sam e result set for an encrypt ed or unencr ypt ed view , but t he encr ypt ed v iew prot ect s t he T- SQL st at em ent for t he v iew . I f y ou need t o m odify a v iew in t he fut ur e, save out side t he dat abase an unencrypt ed v ersion of t he view’s CREATE VI EW st at em ent . You can do t his wit h Quer y Analyzer by saving t he unencry pt ed T- SQL st at em ent t hat was used for cr eat ing t he encr y pt ed v iew. The SCHEMABI NDI NG v iew at t r ibut e int egrat es a v iew w it h it s row sour ces so t hat y ou cannot r em ove or change a r ow source for a v iew in a way t hat will m odify t he r esult set . To specify t he SCHEMABI NDI NG at t r ibut e for a view, you m ust designat e all underly ing r ow sources for t he v iew w it h a t w o- part nam ing conv ent ion t hat designat es t he owner nam e and t he nam e for t he r ow source. I f you cr eat e index es for a view, y ou m ust also designat e t he SCHEMABI NDI NG at t ribut e for t he v iew.

N ot e The SELECT st at em ent for a v iew wit h SCHEMABI NDI NG cannot include a SELECT list wit h * in it if it is an index ed view. The VI EW_METADATA at t ribut e is t he t hird at t r ibut e for a v iew. Specify t his at t ribut e for v iews t hat are int ended for use w it h SQL Ser ver 2000 Met a Dat a Ser vices. You can invok e t hese serv ices fr om eit her Ent erpr ise Manager or a special st and- alone Micr osoft Managem ent Console snap- in. Met a Dat a Ser vices is a specialized t opic out side t he scope of t his book . See t he “ Met a Dat a Ser vices Ov erv iew” t opic in Book s Online for an int r oduct ion t o t he uses for Met a Dat a Ser vices.

Cr e a t in g a n d Usin g View s As explained ear lier , creat ing a v iew perm it s y ou t o expose a subset of a row source t hrough t he v iew. The SELECT st at em ent for a v iew det erm ines t he subset t hat a v iew r et urns. Nest ing a SELECT st at em ent in a CREATE VI EW st at em ent generat es a new v iew w it h a result set det erm ined by t he SELECT st at em ent . This sect ion illust rat es t ypical sy nt ax conv ent ions for t he CREATE VI EW st at em ent . I t also present s som e special r equirem ent s for SELECT st at em ent s nest ed in CREATE VI EW st at em ent s.

Cr e a t in g a n d Se le ct ing f r om a Vie w

To cr eat e a v iew, y ou m ust hav e an init ial r ow source. This row source can reside in t he current dat abase or in anot her dat abase t o w hich y our v iew can connect . The m ost st raight forwar d solut ion is t o use a r ow source in t he curr ent dat abase. The follow ing scr ipt creat es a r ow source as a t able nam ed Em ailCont act s in t he dat abase for t his chapt er and t hen populat es t he t able w it h a couple of rows. Next , aft er dropping t he view if it already ex ist s, t he script cr eat es a v iew based on t he t able. Finally, a SELECT st at em ent pr ov ides a r esult set based on t he v iew. The port ion of t he script creat ing and populat ing t he t able is excerpt ed from Chapt er 2 w it h a m inor adapt at ion for it s use in t he dat abase for t his chapt er. Aft er t he I NSERT I NTO st at em ent s, t he script displays new code specific t o v iews. Befor e inv ok ing t he CREATE VI EW st at em ent , t he script uses t he I NFORMATI ON_SCHEMA.VI EWS v iew t o v er ify w het her a v iew already exist s wit h t he nam e for t he new v iew. I f t he v iew does ex ist , t he script dr ops t he prior version. You can also use t he I NFORMATI ON_SCHEMA.TABLES v iew for t he sam e purpose. Aft er ensuring t hat t he nam e for t he new v iew won’t conflict wit h an ex ist ing one, t he script invokes t he CREATE VI EW st at em ent . This st at em ent dem onst rat es t he synt ax for nam ing a v iew. Not ice t he vew pr efix . While t his pr efix isn’t st rict ly necessary , r ecall t hat nam es for views and t ables occupy t he sam e nam espace. Ther efor e, y ou m ust specify a view ’s nam e dist inct ly fr om a t able serv ing as t he view’s row source. Because t he Em ailCont act s t able r esides in t he sam e dat abase as t he view and it s ow ner is t he dbo user, you can use a one- part nam e t hat sim ply refer ences t he t able’s nam e in t he FROM clause of t he v iew ’s SELECT st at em ent . Aft er t he creat ion of t he v iew, t he script inv ok es a new SELECT st at em ent t o ret ur n t he view’s result set . Not ice t hat t he FROM clause in t he concluding SELECT st at em ent refers t o t he v iew’s nam e, vewEm ailCont act s. --CreatevewEmailContacts USE Chapter04 GO --Remove prior version of EmailContacts if it exists. IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ’EmailContacts’ ) DROP TABLE EmailContacts --Create EmailContacts with three columns. CREATE TABLE EmailContacts ( ContactID int Not Null PRIMARY KEY, FirstName nvarchar(20) NULL, LastName nvarchar(35) NULL, Email1 nvarchar (255) NULL ) GO --Populate EmailContacts. INSERT INTO EmailContacts VALUES(1,’Rick’, ’Dobson’, ’[email protected]’) INSERT INTO EmailContacts VALUES(2,’Virginia’, ’Dobson’, ’[email protected]’) GO --Drop prior version of view if it exists. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewEmailContacts’)

DROP VIEW vewEmailContacts GO --Create view to select all columns for --all rows from the EmailContacts table. CREATE VIEW vewEmailContacts AS SELECT * FROM EmailContacts GO --Select all columns for all rows from --the vewEmailContacts view. SELECT * FROM vewEmailContacts

Con t r a st ing Un e n cr ypt e d a n d Encr ypt e d Vie w s Wit h m inor ext ensions, t he preceding sam ple can serv e as a t em plat e for t he creat ion of any v iew. The following script illust rat es one of t hese ext ensions. I t creat es a view in t he Chapt er04 dat abase t hat has t he Shippers t able in t he Nort hwind dat abase as it s base t able. While t he row source for a v iew can reside in anot her dat abase, t he CREATE VI EW st at em ent can creat e a v iew only in t he curr ent dat abase. Sim ilarly, t he DROP VI EW st at em ent can r em ove a view only from t he cur rent dat abase. An easy way t o r efer ence a row source fr om anot her SQL Serv er dat abase is t o use a t hree- part nam e. The first part refers t o t he alt ernat e dat abase nam e, Nort hwind in t his case. The second part designat es t he owner of t he obj ect prov iding t he row source. When t he row source ow ner is t he default dbo user, you can om it it s ex plicit designat ion ( as in t he follow ing script ) . The t hird nam e part denot es t he nam e of t he dat abase obj ect prov iding t he r ow source for a view. Figure 4- 1 shows t he r esult set from t he SELECT st at em ent based on t he vew Shippers v iew. Not ice t hat it m at ches t he values in t he Nort hwind..Shippers t able, which is t he source for t he v ew Shippers v iew. Not ice t hat unlik e t he fir st code sam ple, t his one doesn’t include a specific reference t o t he Chapt er04 dat abase. That ’s because Query Analyzer w ill cont inue t o use Chapt er04 unt il y ou specify a different dat abase wit h a new USE st at em ent . --CreatevewShippers --Search for, and remove if found, the --vewShippers view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewShippers’) DROP VIEW vewShippers GO --Create a new version of the vewShippers --view in the Chapter04 database from the --Shippers table in the Northwind database. CREATE VIEW vewShippers AS SELECT * FROM Northwind..Shippers GO --Select all rows and columns from the --vewShippers view in Chapter04. SELECT * FROM vewShippers

Figu re 4 - 1 . Th e r e su lt se t from a vie w ba se d on t h e Sh ip pe rs t a ble in t h e N or t h w in d da t ab ase .

The ENCRYPTI ON at t r ibut e isn’t set by default . Set t ing encrypt ion doesn’t change t he r esult set from a SELECT st at em ent . I nst ead, it encodes t he T- SQL for a view’s definit ion. You can v erify t his by t r y ing t o display t he script for a view. The VI EW_DEFI NI TI ON colum n for t he I NFORMATI ON_SCHEMA.VI EWS v iew r et urns t he script for a v iew on each of it s r ows. The follow ing scr ipt dem onst rat es t he synt ax for inv ok ing t he ENCRYPTI ON at t ribut e. The script also dem onst rat es t he sy nt ax for ret urning t he script t hat defines a view . This script includes all com m ent s as well as t he operat ional T- SQL st at em ent s for creat ing t he v iew; t hese st at em ent s include t he CREATE VI EW st at em ent for generat ing a new v iew and t he SELECT st at em ent for defining a view’s result set . I n t his case, t he SELECT st at em ent is ident ical t o t he one in t he preceding v iew. How ev er, t he CREATE VI EW st at em ent includes t he WI TH ENCRYPTI ON clause t hat encodes t he T- SQL for t he v iew. Aft er cr eat ing t he v iew, t he script perfor m s a sim ple SELECT quer y t o v er ify t he cont ent s of t he view ’s result set . The final port ion of t he script creat es anot her r esult set w it h t he definit ion for each user- defined v iew in t he curr ent dat abase, w hich is Chapt er04 in t he sam ple. Om it t ing all rows beginning w it h “ sys” for t heir TABLE_NAME colum n value in t he I NFORMATI ON_SCHEMA.VI EWS v iew excludes all sy st em views from t he final result set . --CreatevewShippersEncrypted --Search for, and remove if found, the --vewShippersEncrypted view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewShippersEncrypted’) DROP VIEW vewShippersEncrypted GO --Create a new version of the vewShippersEncrypted --view in the Chapter04 database from the --Shippers table in the Northwind database. CREATE VIEW vewShippersEncrypted WITH ENCRYPTION AS SELECT * FROM Northwind..Shippers GO --Select all rows and columns from the --vewShippersEncrypted view in Chapter04. SELECT * FROM vewShippersEncrypted --List user-defined view names in Chapter04 database --along with their scripts. SELECT TABLE_NAME, VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE LEFT(TABLE_NAME,3) ’sys’

Figur e 4- 2 shows an ex cerpt from t he result set s for t he pr eceding script s. This excerpt is from t he Result s pane of Query Analy zer w it h a Result s To Grids set t ing. The t op r esult set shows t he sam e t hree rows as in Figure 4- 1. This confirm s t hat encr ypt ing a v iew doesn’t alt er t he result fr om it s SELECT st at em ent . The second result set in Figur e 4- 2 displays t he nam es of t he t hree views creat ed t o t his point in t he chapt er . Next t o each v iew nam e is t he beginning of t he script for t he v iew. Because t he script s st art w it h com m ent s, t he VI EW_DEFI NI TI ON colum n values st art w it h t hese com m ent s. Wit h a Result s To Text set t ing for t he Result s pane, y ou can exam ine t he w hole script for each v iew except v ew ShippersEncr ypt ed. The WI TH ENCRYPTI ON clause in t he CREATE VI EW st at em ent for t his view secur es it s script so t hat t he VI EW_DEFI NI TI ON colum n of t he I NFORMATI ON_SCHEMA.VI EWS v iew cannot expose t he T- SQL t hat generat es t he view . Figu re 4 - 2 . An e xce r pt sh ow in g t h e re su lt set fr om a n e n cr yp t e d vie w a s w e ll as t h e VI EW _ D EFI N I TI ON colu m n va lu es fr om t h e I N FORM ATI ON _ SCH EM A.V I EW S vie w for t h re e vie w s in a d at a ba se .

Sor t in g a n d Gr oupin g W it h in a V ie w The SELECT st at em ent t hat defines a v iew has generally t he sam e synt ax as t hat wit hin a st and- alone script . For exam ple, gr ouping rows t o aggr egat e a colum n value w or ks t he sam e in bot h st and- alone script s and t hose inside v iew s. Sim ilar ly , t he I N keyword in a WHERE clause w ork s t he sam e as well. I n cont rast , t he ORDER BY clause in a SELECT st at em ent requir es slight ly different sy nt ax inside a view t han it does out side a view . I n part icular, ORDER BY inside a v iew requir es t he TOP pr edicat e aft er t he SELECT k eyw ord. The TOP predicat e, in t urn, r equires an argum ent t o designat e how m any r ecords t o ret ur n. I f you want all t he rows from a source, follow TOP w it h 100 PERCENT. You can designat e any ot her percent age as w ell as a num ber for any num ber of rows. Trailing TOP w it h t he num ber 10 w it hout t he PERCENT k eyw ord ret ur ns t he first 10 rows in t he r esult set . When you use an ORDER BY clause, t hose r ows will be t he highest or low est colum n values on a sort dim ension depending on t he sort order . The synt ax for designat ing a sort order in an ORDER BY clause is t he sam e in a SELECT st at em ent in or out of a v iew. The follow ing scr ipt shows t he cr eat ion and r et urn of values from a v iew t hat groups and sort s colum n values. The SELECT st at em ent for t he v iew also includes a crit er ion t hat filt ers ex clusively for count r ies beginning wit h t he let t er B or C. Chapt er 3 included a sim ilar st and- alone script for count ing t he num ber of cust om ers by cit y w it hin count r y. The SELECT st at em ent in t he follow ing script is dist inct because of it s use of t he TOP pr edicat e. While t he TOP predicat e w ill w or k in a st and- alone scr ipt , it isn’t necessary. --CreatevewCustomersInCountryCity --Search for, and remove if found, the --vewCustomersInCountryCity view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewCustomersInCountryCity’)

DROP VIEW vewCustomersInCountryCity GO --Create a new version of the vewCustomersInCountryCity --view in the Chapter04 database. --To use ORDER BY clause in view you need TOP predicate --with modifier of 100 PERCENT. CREATE VIEW vewCustomersInCountryCity AS SELECT TOP 100 PERCENT Country, City, Count(CustomerID) ’# of Customers’ FROM Northwind..Customers WHERE LEFT(Country,1) IN (‘B’,’C’) GROUP BY Country, City ORDER BY Country, City GO --Select all rows and columns from the --vewCustomersInCountryCity view in Chapter04. SELECT * FROM vewCustomersInCountryCity

Vie w s for Re m ot e a n d H e t er oge n e ou s Sou r ce s I t is oft en necessary t o view dat a r esiding on anot her SQL Serv er inst ance or ev en in anot her t ype of dat abase form at . T- SQL prov ides sev er al appr oaches t o sat isfying t hese k inds of r equirem ent s. The OPENROWSET funct ion is a flex ible approach because it can accom m odat e ad hoc quer ies as w ell as t hose perform ed on a regular basis. As m ent ioned prev iously, Books Online r ecom m ends t hat y ou use link ed ser vers w hen it is necessary t o query a r em ot e or het er ogeneous source on a regular basis. Howev er, you can inv ok e t he OPENROWSET funct ion for a user id t hat doesn’t hav e m em bership in t he sysadm in or set upadm in fix ed serv er r oles. The OPENROWSET funct ion depends only on t he perm issions for t he user id passed t o t he ot her dat a source. This sect ion present s a ser ies of OPENROWSET sam ples designed t o help you underst and r em ot e dat a access.

Cr e a t in g a Vie w f or An ot he r SQL Se r ve r I n st a n ce One t ypical requir em ent is t o v iew a SQL Serv er row source, such as a t able, on anot her serv er. You can use t he OPENROWSET funct ion t o perform t his t ask, wit h argum ent s t hat specify a prov ider, ot her elem ent s of a connect ion st ring, and a SELECT st at em ent . The OPENROWSET funct ion can serv e as an argum ent for t he FROM clause of a SELECT st at em ent . This out er SELECT st at em ent , in t urn, m ust reside in a CREATE VI EW st at em ent w hen y our goal is t o cr eat e a v iew in t he curr ent dat abase t hat exposes a row source in anot her dat abase. When t he inner SELECT st at em ent —t he one in t he call t o t he OPENROWSET funct ion—point s at anot her SQL Serv er inst ance, t he prov ider for t he funct ion should be SQLOLEDB. Next y ou can denot e t he rem aining elem ent s of t he connect ion st r ing for t he ot her ser ver in t he follow ing order : t he ser ver inst ance nam e, a SQL Serv er login for t he serv er , and a password for t he login. Follow t he prov ider nam e by a com m a, but use a sem icolon for a delim it er aft er t he ser ver nam e and login nam e. A com m a separat es t he password fr om t he SELECT st at em ent . The follow ing scr ipt creat es a v iew on one SQL Ser ver running SQL Server 2000 t hat point s at a t able on t he cabxli ser ver running t he MSDE v ersion com pat ible wit h SQL Ser ver 7. You need t w o inst ances of SQL Ser ver t o evaluat e t his script ,

but y ou can nam e t he inst ances anyt hing y ou want . Just change t he r eferences t o cabxli t o t he nam e of a SQL Ser ver inst ance t o which you can connect . By t he way , t he t able is t he aut hors t able in t he pubs dat abase; MSDE doesn’t rout inely inst all w it h t he pubs dat abase. Because cabx li is an int er nal t est serv er running Windows 98, t he serv er is available w it h sa and an em pt y password. Pr oduct ion serv ers should always have a password for t he sa login if you ar en’t for cing Windows aut hent icat ion. The SELECT st at em ent r efer ences t he aut hors t able in t he pubs dat abase on t he cabx li ser ver. The ORDER BY clause along wit h t he TOP predicat e sort s t he result set by aut hor first nam e w it hin aut hor last nam e. The out er SELECT st at em ent t ak es t he OPENROWSET funct ion as t he ar gum ent for it s FROM clause. The SELECT list for t he out er SELECT st at em ent list s t he aut hors by first nam e, last nam e, and phone num ber, in t hat order. --CreatevewAuthorsSortedOnCabxli --Search for, and remove if found, the --vewAuthorsSortedOnCabxli view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewAuthorsSortedOnCabxli’) DROP VIEW vewAuthorsSortedOnCabxli GO --Create a new version of the vewAuthorsSortedOnCabxli --view in the Chapter04 database from the --Shippers table in the Northwind database. CREATE VIEW vewAuthorsSortedOnCabxli AS SELECT au_fname, au_lname, phone FROM OPENROWSET(‘SQLOLEDB’,’cabxli’;’sa’;’’, ’SELECT TOP 100 PERCENT * FROM pubs..authors ORDER BY au_lname, a u_fname’) GO --Select all rows and columns from the --vewAuthorsSortedOnCabxli view in Chapter04. SELECT * FROM vewAuthorsSortedOnCabxli GO

Cr e a t in g a Vie w f or a n Acce ss D a t a ba se I t isn’t uncom m on t o need t o upgrade Access applicat ions for t he use of an Access dat abase via a SQL Ser ver solut ion. While y ou can perform a full- scale upsizing, it is possible t hat t he OPENROWSET funct ion can dram at ically r educe t he effort of w ork ing w it h Access dat a from SQL Ser ver. That ’s because t he funct ion perm it s a SQL Ser ver solut ion t o v iew Access dat a wit hout t he need of t ransport ing t he dat a from Access t o SQL Serv er. Ther efore, y ou save t he conv ersion effort . I n addit ion, your client s av oid t he disrupt ion t hat could ar ise if t heir fam iliar Access solut ion wer e unavailable because y ou replaced it wit h a SQL Ser ver applicat ion. At t he sam e t im e, new applicat ions can expose dat a from t he Access dat abase. So long as you don’t expect t o exper ience bot t lenecks relat ed t o t he capacit y of t he Access dat abase, t his appr oach bears considerat ion. I n any ev ent , t he approach support s t he easy av ailabilit y of Access dat a from SQL Ser ver v iews. You can use an OPENROWSET funct ion t o connect wit h an Access dat abase m uch lik e y ou use t he funct ion t o connect w it h a SQL Ser ver dat abase on anot her SQL Ser ver inst ance. The OPENROWSET funct ion is t he argum ent for t he FROM clause of a SELECT st at em ent . When connect ing t o an Access dat abase, you m ust specify t he Jet dat a provider followed by t he pat h t o t he Access dat abase file, a login nam e, and a password. The OPENROWSET funct ion also has it s own SELECT

st at em ent t hat specifies t he r ow source in t he Access dat abase as well as any special set t ings, such as a WHERE clause. The follow ing scr ipt dem onst rat es a connect ion t o an Access dat abase file on t he current com put er. The pat h point s t o t he default inst allat ion of t he Nor t hw ind sam ple dat abase for Access 2002. The connect ion st ring specifies a login by t he adm in user w it h an em pt y password. This is norm al for an unsecur ed Access dat abase file, such as t he Access Nort hw ind sam ple. The SELECT st at em ent inside t he OPENROWSET funct ion call designat es t he r et ur n of all rows w it h a Count ry colum n value of USA. When designat ing a st ring in t his inst ance, t he norm al synt ax is t o enclose t he st ring argum ent , USA, wit h a pair of single quot at ion m arks. How ev er, w it hin t he OPENROWSET funct ion, single quot at ion m arks ar e alr eady used around t he SELECT st at em ent , so it ’s necessary t o use t w o single quot at ion m arks on each side of USA. I n t he follow ing script , t he out er SELECT st at em ent displays all t he colum ns from t he inner SELECT st at em ent . --CreatevewUSACustomersFromAccess --Search for, and remove if found, the --vewUSACustomersFromAccess view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewUSACustomersFromAccess’) DROP VIEW vewUSACustomersFromAccess GO --Create a new version of the vewUSACustomersFromAccess --view in the Chapter04 database from the Customers table --in the Access Northwind database. (You should install the --Northwind sample if it isn’t already installed. Also, you --may need to change the path to Northwind.) CREATE VIEW vewUSACustomersFromAccess AS SELECT * FROM OPENROWSET( ’Microsoft.Jet.OLEDB.4.0’, ’c:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb ’; ’admin’;’’, ’SELECT * FROM Customers WHERE Country=‘‘USA’’’) GO --Select all rows and columns from the --vewUSACustomersFromAccess view in Chapter04. SELECT * FROM vewUSACustomersFromAccess GO

Cr e a t in g a Vie w f or a n OD BC Row Sou r ce View ing an ODBC dat a source m ay be t he ult im at e in flex ibilit y because ODBC drivers are available for so m any different t ypes of dat abases. I n addit ion, t he MSDASQL prov ider, w hich is inst alled w it h Micr osoft Dat a Access Com ponent s, offers a st andard int erface t o ODBC dat a sources. The OPENROWSET funct ion t hr ough it s SELECT st at em ent let s your applicat ions choose a specific row source wit hin a dat a source or ev en filt er a r ow source t o der iv e a new cust om source for an applicat ion. Using t he OPENROWSET funct ion t o connect w it h a row source in an ODBC dat a source bears a st rong r esem blance t o using t he funct ion t o connect w it h SQL Ser ver and Jet r ow sour ces. The m ain differences ar e in t he connect ion st ring specificat ions. First y ou m ust designat e t he MSDASQL prov ider inst ead of t he

SQLOLEDB or Jet prov ider . Second you specify connect ion st ring elem ent s t hat are appr opr iat e for t he dat a source t o which y ou want t o connect . The follow ing scr ipt shows t he sy nt ax for an applicat ion of t he OPENROWSET funct ion wit h t he MSDASQL prov ider for an ODBC dat a source. I n fact , t he sam ple connect s t o a SQL Serv er dat a source wit h t he ODBC dr iv er, but t he general synt ax issues ar e t he sam e as for any dat a source. This sam ple requir es t wo inst ances of SQL Serv er . For exam ple, t he connect ion st r ing elem ent s point t o t he cab2000 server running a SQL Serv er dat abase. You can r eplace t he r efer ence t o cab2000 wit h t he nam e of any ot her inst ance of SQL Serv er on y our net wor k. The user id and password ar e, respect iv ely, sa and password. The inner SELECT st at em ent for t he OPENROWSET funct ion chooses all t he rows from t he Orders t able in t he Nort hw ind dat abase whose OrderDat e is in 1998. A WHERE clause and a DATEPART funct ion part icipat e in t he designat ion of an appropr iat e cr it er ion for t he SELECT st at em ent . The out er SELECT st at em ent ret ur ns all colum ns fr om t he Orders t able. --Createvew1998OrdersOnCab2000 --Search for, and remove if found, the --vew1998OrdersOnCab2000 view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vew1998OrdersOnCab2000’) DROP VIEW vew1998OrdersOnCab2000 GO --Create a new version of the vew1998OrdersOnCab2000 --view in the Chapter04 database from the Orders table --in the Northwind database on the Cab2000 server. CREATE VIEW vew1998OrdersOnCab2000 AS SELECT * FROM OPENROWSET(‘MSDASQL’, ’DRIVER={SQL Server};SERVER=cab2000;UID=sa;PWD=password’, ’SELECT * FROM Northwind..Orders WHERE DATEPART(yyyy, OrderDate) = 1998’) GO --Select all rows and columns from the --vew1998OrdersOnCab2000 view in Chapter04. SELECT * FROM vew1998OrdersOnCab2000

Join ing Row Sou r ce s for a Vie w The value of being able t o pr ocess rem ot e and het erogeneous dat a sources m ult iplies when y ou can j oin t wo r ow sources fr om different serv ers or different dat abases. Ther e are at least t wo approaches t o t his t ask. The first one is t o creat e a SELECT st at em ent t hat cont ains a JOI N operat or. I n t his appr oach, each side of t he j oin has it s own explicit OPENROWSET funct ion. The ot her approach is t o creat e t w o new v iews, each based on it s ow n OPENROWSET funct ion. Then y ou can creat e a new, t hird, view t hat j oins t he t wo views. Eit her appr oach em pow ers an applicat ion t o process concurrent ly r ow sour ces from differ ent dat abase serv ers in differ ent dat abase form at s! The follow ing scr ipt shows t he sy nt ax for t he fir st approach. Lik e sev er al of t he prev ious OPENROWSET funct ion sam ples, t his one r equir es t wo inst ances of SQL Ser ver. The scr ipt j oins rows fr om t he Orders t able in a SQL Ser ver dat abase wit h rows fr om t he Cust om er s t able in an Access dat abase file. The OPENROWSET funct ion declarat ions follow t he sy nt ax of pr ev ious sam ples t hat used t he funct ions separat ely as t he source for a view . This script sam ple j oins t he

Cust om ers r ows w it h t he Orders rows based on t heir Cust om erI D colum n values. An advant age of nest ing t he t wo OPENROWSET funct ions as t he argum ent for t he FROM clause of t he out er SELECT st at em ent is t hat y our applicat ion doesn’t requir e separat e v iews for each r ow source obj ect t hat get s j oined. This saves your applicat ion from opening t he v iews. --CreatevewAccessCustomersCab2000Orders --Search for, and remove if found, the --vewAccessCustomersCab2000Orders view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewAccessCustomersCab2000Orders’) DROP VIEW vewAccessCustomersCab2000Orders GO --Create the vewAccessCustomersCab2000Orders view --in the Chapter04 database from the --OPENROWSET of CustomersFromAccess and --OPENROWSET of 1998OrdersOnCab2000. CREATE VIEW vewAccessCustomersCab2000Orders AS SELECT TOP 100 PERCENT c.CompanyName, c.ContactName, c.Phone, o.OrderID, LEFT(o.OrderDate, 11) ’Order Date’ FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’, ’C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb ’; ’admin’;’’, ’SELECT * FROM Customers WHERE Country=‘‘USA’’’) AS c JOIN OPENROWSET(‘MSDASQL’, ’DRIVER={SQL Server};SERVER=cab2000;UID=sa;PWD=password’, ’SELECT * FROM Northwind.dbo.Orders WHERE DATEPART(yyyy, OrderDate) = 1998’) AS o ON c.CustomerID = o.CustomerID ORDER BY c.CompanyName, o.OrderID GO --Select all rows and columns from the --vewAccessCustomersCab2000Orders view in Chapter04. SELECT * FROM vewAccessCustomersCab2000Orders The next script shows t he synt ax for t he alt er nat iv e appr oach t o j oining t wo het er ogeneous dat a sources. Again, y ou need t wo SQL Server inst ances t o r un t he sam ple. This alt er nat iv e j oins t wo pr ev iously creat ed v iews. I n t his inst ance, each v iew is fr om a pr ior sam ple in t his chapt er. I n addit ion, t he t wo v iews corr espond t o t he SELECT st at em ent s for each of t he nest ed OPENROWSET funct ions in t he pr ior sam ple. Ther efore, t he r esult is ident ical for t he next script and t he prior scr ipt . Howev er , t he code for t he next script is dram at ically sim pler. By segm ent ing t he t wo OPENROWSET funct ions int o separat e v iews, t he second approach m ak es it easier t o debug t he synt ax . On t he ot her hand, wit h t his approach your applicat ion r equir es t he addit ional ov er head of m anaging t wo separat e v iews. This includes cr eat ing, m aint aining, and opening t he v iews. --Createvew2JoinedViews --Search for, and remove if found, the --vew2JoinedViews view in the Chapter04 database. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vew2JoinedViews’) DROP VIEW vew2JoinedViews GO

--Create a new version of the vew2JoinedViews --view in the Chapter04 database from --two other previously existing views. CREATE VIEW vew2JoinedViews AS Select TOP 100 PERCENT c.CompanyName, c.ContactName, c.Phone, o.OrderID, LEFT(o.OrderDate, 11) ’Order Date’ FROM vewUSACustomersFromAccess c JOIN vew1998OrdersOnCab2000 o ON (c.CustomerID = o.CustomerID) ORDER BY c.CompanyName, o.OrderID GO --Select all rows and columns from the --vew2JoinedViews view in Chapter04. SELECT * FROM vew2JoinedViews GO

I n t r odu ct ion t o St or e d Pr oce dur es St or ed pr ocedur es are com piled bat ches of T- SQL st at em ent s. The bat ch of st at em ent s can cont ain near ly all t he T- SQL st at em ent t ypes. While a st or ed procedur e can r et ur n a result set t he sam e way a v iew does, st ored pr ocedur es are m ore pow erful in several r espect s. A v iew is a v irt ual t able; a st or ed procedur e is m or e lik e a procedure in Visual Basic. You can pass it param et ers, and it can r et urn values t hrough it s result set , out put param et ers, and r et urn st at us values. I n fact , st or ed procedur es can ret ur n m ult iple r esult set s, while views ar e lim it ed t o a single r esult sim ilar t o a t able.

Use s for St or e d Pr oce du r e s St or ed pr ocedur es have four m ain uses. First , t hey can r et urn one or m or e result set s. You can program a st ored procedure t o r et ur n m ult iple r esult set s as easily as including m ult iple SELECT st at em ent s w it hin a single st ored procedure. Anot her way st ored pr ocedur es can ret urn r esult set s is via out put param et ers. An out put param et er is a scalar value. A scalar value is a single value, such as a st ring or an int eger , t hat isn’t a part of a rowset . While a r esult set can cont ain a scalar value, r esult set s norm ally cont ain set s of values. Out put param et ers prov ide an efficient m eans for st ored pr ocedures t o r et ur n scalar values. St ored procedur es can also ret ur n int eger values t hat indicat e how a st or ed pr ocedur e t erm inat es. SQL Serv er docum ent at ion r efers t o t hese r et ur n values as ret ur n st at us values. When a st or ed pr ocedur e can follow any of sev eral int er nal processing pat hs, ret ur n st at us values can indicat e t o a calling r out ine which pat h a st ored pr ocedur e pursued. A second m aj or use of st or ed pr ocedur es is t he processing of input param et ers. These param et ers enable y our applicat ions t o cont rol dynam ically t he t hings t hat a st ored pr ocedur e ret urns. Not all T- SQL st at em ent s t ak e param et ers. I n t hese circum st ances, you can com bine t he use of param et ers w it h cont rol- of- flow st at em ent s, such as I F…ELSE st at em ent s, t o det erm ine what a st or ed procedure ret ur ns. One com m on use for param et ers is in t he WHERE clause of SELECT st at em ent s. By using input param et er values as crit er ion values for WHERE clause expr essions, y our applicat ions can dy nam ically cont rol a st or ed procedur e’s result set . When users set t he par am et er values, y ou enable users t o cont r ol an applicat ion dynam ically at run t im e.

A t hird m aj or use for st ored procedur es is t he m anagem ent of insert / updat e/ delet e operat ions for r ow sources. I n t his cont ext , a st ored procedur e prov ides v alue t o an applicat ion w it hout r et ur ning a r esult set , a param et er value, or a r et urn st at us v alue. The procedur e sim ply m odifies a r ow source. Because st ored procedur es can set param et ers based on user input and t he procedures can use param et ers for insert / updat e/ delet e operat ions, users can cont r ol t he m odificat ions t o a r ow source at r un t im e. Fourt h, y ou w ill lear n how t o use st or ed procedur es as pr ogram s im plem ent ed wit h a bat ch of T- SQL st at em ent s. This fourt h use under lies and ext ends t he ot her t hree uses for st ored pr ocedures. These st at em ent s can include SELECT st at em ent s, ot her st at em ent s for insert / updat e/ delet e operat ions, and cont r ol- offlow st at em ent s, such as I F…ELSE st at em ent s. I n addit ion, you can specify any of four t ypes of values—local variables, global var iables, param et ers, and ret ur n st at us values—t o cont rol t he dy nam ic behav ior of a st or ed pr ocedur e and how it com m unicat es wit h it s calling procedure.

N ot e See t he “ Cont rol- of- Flow” t opic in Book s Online for a good st ar t ing point t hat helps you t o learn about t radit ional program m ing t echniques for st or ed procedures. Anot her especially useful Books Online t opic for learning about st ored procedure pr ogr am m ing is “ Pr ogram m ing St ored Procedures.” Re u sin g T- SQL St a t e m e n t s w it h St or e d Pr oce du r e s One of t he m aj or advant ages of st ored pr ocedures is t hat t hey can package TSQL st at em ent s for r euse. Four T- SQL st at em ent s help y ou m anage t hese blocks of code. Two st at em ent s, CREATE PROCEDURE and ALTER PROCEDURE, enable t he definit ion and refinem ent of t he code wit hin a st or ed pr ocedur e. Wit h t he DROP PROCEDURE st at em ent , you can rem ov e a st or ed pr ocedur e fr om a dat abase. The EXECUTE st at em ent perm it s you t o run a st ored pr ocedure. The CREATE PROCEDURE st at em ent let s you creat e a st or ed procedur e. You can abbrev iat e t his st at em ent as CREATE PROC. Follow t he st at em ent nam e wit h t he nam e for your st or ed pr ocedur e. SQL Serv er has a r ich collect ion of syst em st ored procedures, which t ypically st art w it h sp_. Chapt er 2 includes ex am ples of how t o use syst em st or ed pr ocedur es w it h t ables. Syst em st ored pr ocedur es are available for m anaging ev ery aspect of SQL Ser ver perform ance and adm inist rat ion. To avoid conflict s wit h syst em st or ed procedur es, av oid st art ing your own user - defined st or ed pr ocedur es w it h t he sp_ prefix. This chapt er uses udp as a prefix for user - defined st ored pr ocedur es. Lik e view nam es, st or ed procedur es should follow t he st andard rules for SQL Serv er ident ifiers. The CREATE PROC st at em ent s t ypically hav e t hr ee or four m ain elem ent s. First , CREATE PROC declares t he st or ed pr ocedur e and assigns a nam e t o it . Second, you can specify one or m or e param et ers for t he pr ocedur e. The param et er declarat ions ar e opt ional. Third, t he AS keyword serv es as a t ransit ional word bet ween t he declarat ion elem ent s and t he T- SQL code ( t he fourt h elem ent ) t hat enables a st or ed procedur e t o perform a t ask. The follow ing t em plat e illust rat es how t o ar range t hese st or ed procedur e elem ent s. CREATE PROC procedurename Parameter specifications AS

T-SQL code

Aft er y ou creat e a st ored procedure, y ou can change it s code in at least t wo different way s. First , y ou can inv ok e t he DROP PROCEDURE ( or DROP PROC) st at em ent t o rem ov e t he pr ior v ersion and t hen inv ok e a new CREATE PROC st at em ent wit h t he sam e nam e as t he rem ov ed procedure. To delet e an ex ist ing st ored procedure w it h t he DROP PROC st at em ent , sim ply follow t he keyword phrase w it h t he nam e of t he st ored procedure t hat y ou want t o r em ov e. Wit h t his approach, y ou w ipe out any perm issions assigned t o users for t he dropped st or ed procedur e. Alt ernat iv ely , y ou can inv ok e t he ALTER PROCEDURE ( or ALTER PROC) st at em ent . This allows y ou t o respecify t he param et ers and t he code w it hin a st ored procedure w hile it m aint ains any perm ission set t ings for t he st or ed procedur e t hat y ou m odify . Except for t he keyw ord declar ing it , t he ALTER PROC st at em ent has t he sam e form at as t he CREATE PROC st at em ent . Your applicat ions can use t he EXECUTE ( or EXEC) st at em ent t o inv ok e a st ored procedur e init ially cr eat ed w it h a CREATE PROC st at em ent . I n it s m ost basic represent at ion, follow t he EXEC k eyw ord w it h t he nam e of t he st ored procedur e t hat y ou want t o r un. The synt ax for t he EXEC st at em ent perm it s y ou t o assign values for input param et ers as well as accept out put param et er and ret ur n st at us values. I n addit ion, t he EXEC st at em ent can also r et ur n one or m ore result set s— depending on t he T- SQL code t hat populat es t he st ored procedure. This chapt er includes num erous sam ples t hat illust rat e t he synt ax for inv ok ing st or ed procedur es w it h t he EXEC st at em ent .

Usin g Pa r a m e t e r s, Loca l Va r ia bles, a n d Globa l V a r ia ble s Alt hough param et ers, local variables, and global var iables can, of course, be used elsewhere, using t hem wit h st or ed procedures especially enhances t he value of t he procedures in an applicat ion. Ther e are t wo basic kinds of param et ers—input param et ers and out put param et ers. Param et er nam es m ust begin w it h t he @ sym bol. The r em ainder of a param et er’s nam e m ust follow t he st andar d SQL Ser ver ident ifier convent ions. Param et ers have dat a t ypes t hat correspond t o t hose for t able colum n v alues. ( See Chapt er 3. ) I nput param et ers perm it y ou t o cust om ize t he operat ion of a st or ed procedur e at run t im e. For exam ple, you can use input param et ers t o specify t he colum n values for a st ored procedure t hat adds a new r ow t o a row source. The CREATE PROC and ALTER PROC st at em ent s perm it y ou t o assign default values for input param et ers. These default values allow a st ored procedure t o use a param et er wit hout t est ing for a null v alue ev en if t he user om it s t he specificat ion of a param et er when inv ok ing t he st or ed procedur e. Out put param et ers repr esent values developed from w it hin a st or ed pr ocedur e. These can be values com put ed by t he pr ocedur e or SQL Serv er . A st ored procedur e can pass back as an out put param et er t he I DENTI TY value for a new row in a t able so t hat anot her st ored pr ocedur e can use t he out put par am et er as a foreign k ey value for a new row in a relat ed t able. I n t his scenar io, t he out put param et er value from one st or ed pr ocedur e ser ves as t he input param et er value for a second one. A local var iable is a m em or y v ar iable t hat you assign for use inside a st or ed procedur e. Use t he DECLARE keyword for designat ing local var iables and t he SET keyw ord for assigning v alues t o a local var iable. You can also assign a v alue t o a local v ar iable w it h a SELECT st at em ent t hat ret urns a scalar value, such as t he count of t he num ber of rows in a t able. The scope of a local v ariable is t he st ored procedur e t hat declares t he var iable. Like param et ers, local v ariable ident ifiers m ust begin wit h t he @ sym bol. The rem ainder of t he local v ariable nam e m ust follow st andard SQL Serv er ident ifier

conv ent ions. The DECLARE st at em ent for a local var iable m ust include a dat a t ype for t he var iable. You can use any dat a t ype except for t ext , nt ex t , and im age. A local var iable’s dat a t ype specificat ion det erm ines t he t ype of cont ent t hat t he var iable can hold. Local var iables can be used in expr essions and as argum ent s for cont r ol- of- flow st at em ent s t o cont r ol t he operat ion of a st ored procedur e. Local var iables can w or k in coordinat ion wit h param et ers by accept ing values fr om param et ers and passing values t o t hem . Dev elopers fam iliar w it h SQL Serv er v ersions pr ior t o 7.0 m ay be fam iliar wit h t he t erm global var iables. SQL Ser ver 2000 r efers t o t hese global var iables as funct ions. A global var iable funct ion nam e st art s wit h @@. These global var iable funct ions ret ur n values t o st or ed pr ocedur es t hat cont ain syst em infor m at ion. You can display t he full list of 33 @@ var iable funct ions from t he I ndex t ab in Books Online by ent er ing @@ as t he keyword. This chapt er illust rat es t he use of t he @@ROWCOUNT funct ion, which ret ur ns t he num ber of r ows affect ed by t he last T- SQL st at em ent . Ot her @@ funct ions t hat I r egular ly find part icular ly conv enient include @@I DENTI TY, @@ERROR, and @@DBTS. These t hr ee funct ions ret ur n t he last I DENTI TY value insert ed, t he er ror num ber associat ed wit h t he last T- SQL st at em ent , and t he cur rent t im est am p value w it hin a dat abase.

Cr e a t in g a n d Usin g St or e d Pr ocedu r e s The purpose of t his sect ion is t o int roduce you t o synt ax for creat ing and using st ored procedures. This sect ion shows you t ypical ways of apply ing t he CREATE PROC st at em ent . I n addit ion, you learn com m on ways of specify ing t he EXEC st at em ent t o run a st or ed pr ocedur e. The sect ion illust rat es t echniques for designat ing input param et ers when y ou cr eat e a st ored pr ocedur e as w ell as way s of specify ing input param et er values when you r un a st or ed procedur e.

D yna m ica lly Se le ct in g f r om a Row Sou r ce One of t he m ain advant ages of st or ed pr ocedur es com par ed w it h v iews is t hat st ored procedures perm it t he use of param et ers. Bot h views and st or ed procedur es can inv ok e SELECT st at em ent s. Howev er , st or ed procedur es let y ou assign values t o param et ers in WHERE clause expressions at r un t im e. This capabilit y m eans y our applicat ions can t ak e input from users t o designat e w hich rows a st ored pr ocedur e ret urns in it s result set . Wit h v iews, you would hav e t o preprogram a differ ent view for each set of r ow s you want ed. The follow ing scr ipt has t hr ee bat ches of T- SQL code. The first bat ch r em ov es any prior v ersion of t he udpList ShippersRow in t he curr ent dat abase. The first bat ch uses t he I NFORMATI ON_SCHEMA.ROUTI NES view t o search for an ex ist ing st ored procedur e wit h t he nam e udpList ShippersRow. I f one alr eady exist s wit h t hat nam e, t he bat ch invokes t he DROP PROCEDURE st at em ent t o rem ov e it . The second bat ch inv ok es t he CREATE PROC st at em ent t o creat e a new st or ed procedur e nam ed udpList ShippersRow. This procedur e t ak es a single param et er nam ed @RowI D w it h an int dat a t ype. The pr ocedur e uses t he param et er t o specify t he ShipperI D colum n value for t he r ow it ret urns; see t he WHERE clause for t he sy nt ax of how t o do t his. The basic SELECT st at em ent ret urns all t he colum ns fr om t he Shippers t able in t he Nort hwind dat abase. You can t ell from t he synt ax t hat t his is t he SQL Ser ver v ersion of t he dat abase. ( Not ice t he FROM clause argum ent .) All t he rem aining st ored pr ocedur e sam ples use j ust SQL Ser ver dat abases. The final bat ch consist s of a single EXEC st at em ent . The st at em ent r uns t he st ored procedure creat ed in t he pr ev ious bat ch and designat es a v alue for t he

RowI D par am et er . Failing t o specify a RowI D param et er value causes t he procedur e t o fail wit h an er ror m essage. Designat ing a nonex ist ent ShipperI D colum n value w it h RowI D produces an em pt y result set . On t he ot her hand, specify ing any of t he ex ist ing ShipperI D colum n values causes t he procedure t o generat e a r esult set w it h all t he colum ns for t hat row in t he Shippers t able. --CreateudpListShippersRow --Delete previous version of udpListShippersRow --stored procedure if it exists. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpListShippersRow’) DROP PROCEDURE udpListShippersRow GO --Create udpListShippersRow with an --input parameter to specify a row. CREATE PROC udpListShippersRow @RowID int AS SELECT * FROM Northwind..Shippers WHERE ShipperID = @RowID GO --Run udpListShippersRow with an --input parameter of 2. EXEC udpListShippersRow 2

Re t u r n ing a Sor t ed Re su lt Se t Ev en a basic SELECT st at em ent can y ield benefit s when it is m ade available from a st ored pr ocedur e. For exam ple, t he use of t he ORDER BY clause in a v iew requir es t he concurr ent use of t he TOP predicat e. While t his is cert ainly not com plicat ed, it is j ust one m or e t hing you hav e t o rem em ber t o get r ight . The synt ax for using t he ORDER BY clause in a st or ed pr ocedur e is j ust like t hat in a st and- alone T- SQL script . I n ot her w ords, you don’t need a TOP pr edicat e for your SELECT st at em ent . The follow ing scr ipt shows t he ORDER BY clause w it hin a SELECT st at em ent t hat det erm ines t he result set fr om a st ored pr ocedure. The SELECT st at em ent generat es a result set based on t he Shippers t able, w it h t he r ows sort ed by Com panyNam e colum n values. This r et urns t he rows in a different order t han t he default one based on t he ShipperI D colum n values. The scr ipt again relies on a t hr ee- part st rat egy. The first part rem ov es an old version of t he udpShippersSort edBy Com panyNam e st ored procedur e. The second part inv ok es t he CREATE PROC st at em ent t o add t he new st or ed procedur e. The t hird part runs t he new ly cr eat ed st ored procedure w it h t he EXEC st at em ent . Because t his st ored procedure doesn’t t ake any param et ers, you can j ust follow t he EXEC keyw ord wit h t he nam e of t he st or ed pr ocedur e. There is no need for anyt hing else aft er t he EXEC k ey word. --CreateudpShippersSortedByCompanyName --Delete previous version of udpShippersSortedByCompanyName --stored procedure if it exists. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpShippersSortedByCompanyName’) DROP PROCEDURE udpShippersSortedByCompanyName

GO --Create udpShippersSortedByCompanyName with an --input parameter to specify a row. CREATE PROC udpShippersSortedByCompanyName AS SELECT * FROM Northwind..Shippers ORDER BY CompanyName GO --Run udpShippersSortedByCompanyName. EXEC udpShippersSortedByCompanyName GO

Re t u r n ing t h e Scr ipt f or a V ie w St or ed pr ocedur es are an ext r em ely flex ible t ool. You can use SELECT st at em ent s in t he full range of cases t hat use v iews and st and- alone T- SQL st at em ent s. For exam ple, y ou can quer y I NFORMATI ON_SCHEMA views t o uncov er infor m at ion about t he obj ect s in a dat abase. An advant age of a st or ed pr ocedur e is t hat t he T- SQL it cont ains is com piled. A st and- alone T- SQL st at em ent m ust be com piled befor e SQL Server can use it . Therefore, t he st or ed procedur e can r un t he sam e T- SQL code fast er.

N ot e The sp_execut esql sy st em st ored procedur e offers som e of t he benefit s of st ored procedur es for st and- alone T- SQL SELECT st at em ent s. The follow ing scr ipt dem onst rat es t he use of a st ored pr ocedur e t o quer y t he I NFORMATI ON_SCHEMA.VI EWS v iew. The r esult set for t his v iew cont ains a r ow for each v iew in t he cur rent dat abase. The v iew ’s VI EW_DEFI NI TI ON colum n ret ur ns t he T- SQL script defining a v iew. The TABLE_NAME colum n r et ur ns t he nam e for a v iew. The st or ed procedure accept s a param et er t hat designat es a v iew ’s nam e. The st ored procedure’s SELECT st at em ent passes t he T- SQL scr ipt for a v iew t o a local variable, @st rDefinit ion. The local variable accept s t he value in t he VI EW_DEFI NI TI ON colum n value for t he row wit h a TABLE_NAME colum n value equal t o t he param et er passed t o t he st or ed pr ocedur e. Then a PRI NT st at em ent displays t he cont ent s of t he local var iable in t he Messages pane. The st or ed procedure’s approach works for v iews wit h up t o 8000 charact ers fr om t he default code page for t he com put er on w hich y ou dev eloped t he st ored procedur e. This is because t he varchar dat a t y pe for t he @st rDefinit ion local variable has a m ax im um lengt h of 8000 charact ers in t he default code page for a com put er. I f y ou expect your v iew script s t o have m or e charact ers or y our applicat ion r uns on com put ers using m ult iple code pages, you need anot her approach for st or ing t he view’s T- SQL script . For exam ple, y ou can use an out put param et er inst ead of a local var iable. Assign a t ext or an nt ext dat a t ype t o t he param et er. When using t he out put param et er approach, y ou can pr int t he script in t he calling rout ine for t he st or ed procedur e. Recall t hat a t ext dat a t ype can hold up t o 2 31 - 1 charact ers, and a dat a t ype value can hold up t o 2 30 - 1 charact ers. Users can alt er t he r et urn value t hat appears in t he Messages pane by changing t he nam e of t he v iew passed t o t he st or ed procedur e. The EXEC st at em ent t o inv oke t he st ored pr ocedur e encloses t he param et er in single quot at ion m arks.

This is because t he st or ed pr ocedur e assigns a varchar dat a t ype t o t he param et er st or ing a view’s nam e. --CreateudpScriptForView --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpScriptForView’) DROP PROCEDURE udpScriptForView GO --Create stored procedure to print definition --for a view in the current database. CREATE PROC udpScriptForView @vewName varchar(128) AS DECLARE @strDefinition varchar(8000) SET @strDefinition = (SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = @vewName) PRINT @strDefinition GO --Run stored procedure and pass view name. EXEC udpScriptForView ’vewShippers’ GO

Pr ocessing St or e d Pr ocedu r e Ou t pu t s One of t he t asks t hat st or ed procedur es ser ve especially w ell is get t ing dat a back t o a calling procedur e. St or ed pr ocedur es can achiev e t his goal in sev eral ways. First , t hey perm it t he t r ansfer of dat a back t o t he calling pr ocedur e in t he form of result set s. You can r et ur n m ult iple r esult set s from a single st or ed procedur e. Second, a st ored pr ocedur e can r et ur n scalar values v ia out put param et ers. Third, code calling a st ored pr ocedure can process ret ur n st at us values. I n any one applicat ion, y ou can concurr ent ly use any com binat ion of t hese t hr ee processes for ret ur ning v alues. This sect ion elaborat es on t hem and dem onst rat es t he synt ax for im plem ent ing each.

Re t u r n ing Tw o Re sult Se t s fr om a St or e d Pr oce du r e I t ’s sim ple t o r et ur n m ult iple r esult set s fr om a single st ored pr ocedur e: j ust include a separat e SELECT st at em ent for each r esult set t hat y ou want a st ored procedur e t o ret urn. I n cont rast , v iews can hav e only a single SELECT st at em ent . Once y ou st art using m ult iple SELECT st at em ent s in a st or ed pr ocedur e, you’ll find t hat it has consider ably m ore flex ibilit y t han ret ur ning rows from a t able or view. The follow ing scr ipt creat es a st ored pr ocedur e wit h t w o r esult set s. The first result set cont ains a r ow w it h t he nam e and cr eat ion dat e for each user- defined st ored procedure in a dat abase. Recall t hat t he dat abase cont ext for t hese sam ples is Chapt er04. ( You can set t he cont ext wit h a USE st at em ent .) To ret urn j ust t he user - defined st or ed procedur es from t he I NFORMATI ON_SCHEMA.ROUTI NES v iew, you need t wo cr it er ia expr essions. One expression select s j ust r ows w it h a ROUTI NE_TYPE colum n value of PROCEDURE. This expression filt ers out any user - defined funct ions. The second expr ession

rem ov es any rows w it h a ROUTI NE_NAME colum n value t hat begins w it h dt _. Because SQL Serv er uses dt _ as a pr efix for t he st or ed procedures t hat it cr eat es in a dat abase, t his expr ession leav es only user - defined st ored procedures. The second SELECT st at em ent ret urns t he value of t he @@ROWCOUNT funct ion. This funct ion is always t he value of r ecords affect ed by t he last T- SQL st at em ent . I n t his case, t he last one ret ur ns t he nam es and creat ion dat es of t he userdefined st ored procedures in a dat abase, so t he second SELECT st at em ent ret ur ns t he num ber of user- defined st or ed procedur es in t he curr ent dat abase cont ext . --CreateudpReturn2ResultSets --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpReturn2ResultSets’) DROP PROCEDURE udpReturn2ResultSets GO --Create stored procedure to return one result --set for listing stored procedure names and dates --and another with the count of the stored procedures. CREATE PROC udpReturn2ResultSets AS SELECT ROUTINE_NAME, CREATED FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND LEFT(ROUTINE_NAME,3) ’dt_’ ORDER BY CREATED DESC SELECT @@ROWCOUNT ’Number of stored procedures’ GO --Run stored procedure that returns two result sets. EXEC udpReturn2ResultSets GO Figur e 4- 3 shows t he out put from r unning t he udpRet urn2Result Set s st ored procedur e. ( This is t he out put from t he preceding script .) Not ice t hat t he t op result set cont ains ROUTI NE_NAME and CREATED colum n values. This result has a row for each user - defined st ored pr ocedur e. The last row includes t he nam e and creat ion dat e for t he elevent h st or ed pr ocedur e. The second r esult set cont ains a num ber t hat is t he count of t he num ber of user - defined st ored pr ocedur es—11. Figu r e 4 - 3 . Th e re t u r n fr om a u ser - d e fin e d st or ed pr oced u r e t h a t spe cifie s t w o r esu lt se t s.

Re t u r n ing On e Re su lt Se t a n d One Pa r a m e t e r Va lu e The preceding sam ple uses a SELECT st at em ent t o ret urn a scalar value, nam ely t he cur rent value for @@ROWCOUNT. By ent er ing t he @@ROWCOUNT global variable funct ion in a SELECT st at em ent , t he sam ple r et ur ns t he current value of @@ROWCOUNT in a result set . The next sam ple illust rat es how t o ret ur n t he @@ROWCOUNT value as an out put param et er from a st or ed procedure. This inv olves a special declar at ion for t he param et er inside t he st ored pr ocedur e as well as an assignm ent expression in t he EXEC st at em ent t o r et r iev e t he value for t he out put param et er. I n t he T- SQL code t hat calls t he st or ed procedur e, you need t o t ransfer t he out put param et er t o a local var iable for use locally. I n addit ion, t he EXEC st at em ent m ust explicit ly designat e t he out put param et er. The follow ing code shows t he exact synt ax for ret ur ning @@ROWCOUNT as an out put param et er . First not ice t he line im m ediat ely aft er t he CREATE PROC st at em ent : @NumberOfRows int OUTPUT This line declares t he param et er. Not ice t hat it ends wit h t he k eyw ord OUTPUT. This k eyw ord designat es t he @Num berOfRows param et er as an out put param et er. Lat er in t he st ored procedure, a SET st at em ent assigns t he curr ent value of @@ROWCOUNT t o t he @Num berOfRows param et er , lik e t his: SET @NumberOfRows = (SELECT @@ROWCOUNT) This st or ed pr ocedur e div erges from t he preceding one by ex plicit ly inv oking t he SET NOCOUNT st at em ent w it h t he value ON. This st at em ent suppresses t he aut om at ic SQL Server m essage about t he num ber of r ows affect ed, which happens t o be t he value of @@ROWCOUNT. At t he conclusion of t he st ored procedur e, t he sam ple inv ok es t he SET NOCOUNT st at em ent a second t im e w it h t he set t ing OFF. This second invocat ion of t he SET NOCOUNT st at em ent r est ores t he default behav ior of print ing t he rows affect ed by a T- SQL st at em ent . Using a param et er ret ur ned by a st ored pr ocedure also r equir es special synt ax. First y ou need a local v ariable t o accept t he out put param et er value. This is because y ou cannot w ork dir ect ly wit h t he out put param et er in t he code t hat calls t he st or ed pr ocedur e. The sam ple code declar es a local v ariable nam ed @Ret ur nedParam Value t o st or e t he out put param et er value locally. Second you need an assignm ent st at em ent . This st at em ent m ust end w it h t he OUTPUT keyw ord. I n addit ion, t he local var iable m ust be on t he r ight side of t he equal

sign, and t he out put par am et er should appear on t he left side. Third t he out put param et er ret ur ns an int dat a t ype value. How ever, t he Pr int st at em ent t hat report s t he num ber of st ored procedur es r equir es a char act er dat a t ype, nam ely varchar. Therefore, t he code applies t he CAST funct ion t o t he local variable st oring t he out put param et er v alue; t he funct ion represent s t he int eger value as a st ring. The expression for @st rFor Pr int er com bines a st r ing const ant wit h t he CAST funct ion value. The PRI NT st at em ent t akes @st rForPr int er as it s argum ent t o pr int t he num ber of st or ed pr ocedur es w it h a br ief descr ipt ive label. --CreateudpReturn1ResultSet1Parameter --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpReturn1ResultSet1Parameter’) DROP PROCEDURE udpReturn1ResultSet1Parameter GO --Create stored procedure to return one result --set for listing stored procedure names and dates along --with another containing the count of the stored procedures. CREATE PROC udpReturn1ResultSet1Parameter @NumberOfRows int OUTPUT AS SET NOCOUNT ON SELECT ROUTINE_NAME, CREATED FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND LEFT(ROUTINE_NAME,3) ’dt_’ ORDER BY CREATED DESC SET @NumberOfRows = (SELECT @@ROWCOUNT) SET NOCOUNT OFF GO --Run stored procedure that returns two result sets. DECLARE @ReturnedParamValue int DECLARE @strForPrinter varchar(100) EXEC udpReturn1ResultSet1Parameter @NumberOfRows = @ReturnedParamValue OUTPUT SET @strForPrinter = ’Number of stored procs: ’ + Cast(@ReturnedParamValue AS varchar(3)) PRINT @strForPrinter GO

Re t u r n ing On e St r ing Pa r a m e t e r The code y ou use t o r et urn a st ring value as an out put param et er is essent ially t he sam e code y ou use t o r et urn a num ber value. The m ain dist inct ion is t he declarat ion of t he dat a t ype for t he param et er. The follow ing scr ipt r et urns t he nam e of t he oldest user- defined st or ed procedur e in a dat abase. I t passes back t he nam e of t he st or ed pr ocedur e v ia an out put param et er nam ed @st r Nam eOfOldest SPr oc. Not ice t hat t he out put par am et er declarat ion uses a varchar dat a t ype t hat is consist ent w it h t he m ax im um lengt h of a SQL Serv er ident ifier . I f your applicat ion runs in m ult iple locat ions t hat use different code pages, you m ay want t o use an nvarchar r at her t han a v archar dat a t ype specificat ion for t he param et er. I n t his case, t he t echnique for finding t he st or ed procedure is as int erest ing as t he t echnique for declaring t he out put param et er. The SET ROWCOUNT st at em ent t ells SQL Ser ver t o st op processing a st at em ent aft er t he designat ed num ber of

records. The ORDER BY clause in t he SELECT st at em ent sort s t he st or ed procedur es so t hat t he nam e of t he oldest st ored pr ocedur e appears first . Ther efor e, st opping aft er pr ocessing t he first r ow r et urns t he oldest st ored procedur e. The t echnique for processing an out put param et er in t he calling r out ine is about t he sam e whet her t he out put param et er has an int or a varchar dat a t y pe. This part icular sam ple appears slight ly sim pler t han t he preceding one m ost ly because it doesn’t label t he ret ur n value t hat is pr int ed in t he Messages pane. Because t he local v ar iable for holding t he out put param et er is alr eady a st ring, t her e is no need t o conv ert it so t hat it can be used as an argum ent for t he PRI NT st at em ent . --CreateudpReturn1StringParameter --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpReturn1StringParameter’) DROP PROCEDURE udpReturn1StringParameter GO --Create stored procedure to return one --parameter with a string value. CREATE PROC udpReturn1StringParameter @strNameOfOldestSProc varchar(128) OUTPUT AS SET ROWCOUNT 1 SET @strNameOfOldestSProc = (SELECT TOP 1 ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE LEFT(ROUTINE_NAME,3) ’dt_’ AND ROUTINE_TYPE = ’PROCEDURE’ ORDER BY CREATED) GO --Run stored procedure that returns one string parameter. DECLARE @ReturnedParamValue varchar(128) EXEC udpReturn1StringParameter @strNameOfOldestSProc = @ReturnedParamValue OUTPUT PRINT @ReturnedParamValue GO

W or k ing w it h Re t ur n St a t u s V a lu e s St or ed pr ocedur es considered t o t his point in t he chapt er proceed in a st raight line fr om t he first t o t he last st at em ent in t he pr ocedur e. However, t his isn’t a requir em ent . Cont rol- of- flow st at em ent s, such as t he I F…ELSE st at em ent , m ake it possible for a st ored procedur e t o ex ecut e condit ionally. You can end t he processing w it hin a st or ed pr ocedur e w it h one or m or e RETURN st at em ent s at t he end of each of several pat hs t hr ough t he code. Each RETURN st at em ent can pass back an int dat a t ype value t o t he calling procedure as it closes t he st or ed procedur e. Alt hough you can have m ult iple RETURN st at em ent s wit h different ret ur n st at us values, any one invocat ion of a st or ed procedur e can r et urn j ust one ret ur n st at us value. This m akes it possible for code inv ok ing a st or ed pr ocedur e t o k now pr ecisely at w hich line t he st ored pr ocedur e closed. The follow ing code sam ple creat es a st or ed procedur e t hat searches for a st or ed procedur e by a nam e in a dat abase. I f t he search finds a st ored pr ocedur e w it h t he t arget nam e, t he r et ur n st at us value is 1. Ot herw ise, t he ret urn st at us value is 0. I t is com m on t o set ret ur n st at us values w it h a RETURN st at em ent inside an I F…ELSE st at em ent ( alt hough t his sam ple’s design is ext raordinar ily sim ple) .

The calling T- SQL code for t he st or ed procedure in t he follow ing sam ple causes t he procedure t o search for eit her of t wo nam es: udpList ShippersRow or SP1. Make sur e your dat abase has a st ored procedur e nam ed udpList Shipper sRow and t hat y our dat abase doesn’t hav e a st or ed pr ocedur e nam ed SP1. I f you hav e been doing t he sam ples in t he order t hat t hey appear in t his chapt er, y our Chapt er04 dat abase will hav e a st ored pr ocedure nam ed udpList ShippersRow. This let s you use t he sam ple T- SQL code t hat calls t he st ored procedure t o ver ify t hat t he ret ur n st at us values r eflect t he pr esence or absence of a st ored pr ocedur e. The calling T- SQL code for t he st or ed pr ocedur e displays t he r et ur n st at us value in a result set t hat cont ains eit her 0 or 1. These values m at ch each of t he r et ur n st at us values set in t he st ored procedure. The sy nt ax for capt uring a r et ur n st at us value in a calling pr ocedure deviat es slight ly fr om t hat for an out put param et er. I n bot h cases, y ou need a local variable t o r epr esent t he v alue r et ur ned from t he st or ed pr ocedur e. Howev er , t o capt ure t he ret ur n st at us value, y ou use an assignm ent expression t hat set s t he st ored procedure equal t o t he local var iable for t he ret ur n st at us value. This assignm ent expr ession is act ually int egrat ed int o t he call of t he st or ed procedur e as an argum ent for an EXEC st at em ent . I n t he sam ple, a local v ariable specifies t he v alue for t he procedure t o pass t o t he st ored procedure. As t he code appears, t he calling code passes t he nam e udpList ShippersRow. Howev er, y ou can com m ent out ( w it h t wo leading hyphens) t he assignm ent st at em ent for t he @st r ProcNam e local var iable and rem ov e t he hyphens from t he assignm ent st at em ent t hat set s t he local variable t o SP1. This t ransit ion w ill cause t he r et urn st at us value t o swit ch fr om 1 t o 0. --CreateudpReturnStatusValue --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpReturnStatusValue’) DROP PROCEDURE udpReturnStatusValue GO --Create stored procedure to pass back --a return status value of 0 or 1. CREATE PROC udpReturnStatusValue @strName varchar(123) AS SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = @strName AND ROUTINE_TYPE = ’PROCEDURE’ IF @@ROWCOUNT = 0 RETURN 0 ELSE RETURN 1 GO --Pass a procedure name to udpReturnStatusValue. DECLARE @strProcName varchar(128) DECLARE @return_status int --Use the following SET statement for a 1. SET @strProcName = ’udpListShippersRow’ --Use the following SET statement for a 0. --SET @strProcName = ’SP1’ EXEC @return_status = udpReturnStatusValue @strProcName SELECT @return_status AS ’Return Status’

I nse r t in g, Upda t in g, a n d D e le t in g Row s Dat a m anipulat ion is anot her ar ea in which st or ed pr ocedures shine—unlike views, w hich cannot ex ecut e t he I NSERT I NTO, UPDATE, or DELETE st at em ent . The capabilit y of t ak ing param et ers as argum ent s wit h t hese st at em ent s perm it s a single st ored procedur e t o m odify a dat abase in different ways at r un t im e based on user input . This sect ion has t w o m ain goals. First it int r oduces t he synt ax for t he SQL Serv er dat a m anipulat ion st at em ent s w it hin a st or ed procedur e. Second it illust rat es how t o perform dat a m anipulat ion w it h param et er values for st or ed procedur es.

Alt e r in g a St or e d Pr oce du r e for D a t a M a n ipu la t ion The sy nt ax for insert ing, updat ing, and delet ing rows fr om a row source is st raight forward. The sam ple for t his sect ion separat ely illust rat es how t o perform each t ask for a t able in t he local dat abase. I n order t o k eep t he sam ple easy t o underst and, t he insert / updat e/ delet e code uses const ant s t o w ork w it h specific values for a specific r ow . I n addit ion t o clar ify ing t he synt ax for per form ing t he t ask, t he sam ple dem onst rat es how t o alt er an ex ist ing st ored pr ocedur e t o perform a different funct ion. Recall t hat alt er ing a st ored pr ocedur e w it h t he ALTER PROC st at em ent allows you t o preserv e t he perm issions assigned for t he st ored procedure. I f you drop and r e- cr eat e a st ored pr ocedure, any user perm issions for t he old version of t he st ored pr ocedur e are lost unless you r eassign t hem t o t he new v ersion of t he st or ed pr ocedur e. I don’t necessar ily recom m end y ou alt er a single st ored procedur e t hat y ou m odify for each of t hr ee differ ent funct ions in pr oduct ion syst em s. The sam ple design has t he t ut or ial value of reinforcing your underst anding of t he t echnique for alt er ing a st or ed procedur e. The sam ple r euses t he sam e st or ed procedure for t hree t asks successively. First t he script st art s t o cr eat e a new copy of t he udpI nsert Updat eDelet eSam ples st ored procedure by rem ov ing any ex ist ing v ersion of t he obj ect from t he dat abase. Then t he script inv ok es t he CREATE PROC st at em ent t o m ak e a fresh version of t he st ored pr ocedur e wit h t he code t o add a r ecord t o t he Em ailCont act s t able. ( See t he “ Cr eat ing and Select ing fr om a View ” sect ion earlier in t his chapt er for t he sam ple code t o creat e and init ially populat e t his t able.) The st ored procedure adds a new record t o t he t able for Tony Hill. The st or ed procedure dem onst rat es t he use of t he I NSERT I NTO st at em ent for adding a new row t o t he Em ailCont act s t able, lik e t his: CREATE PROC udpInsertUpdateDeleteSamples AS INSERT INTO vewEmailContacts (ContactID, FirstName, LastName, Email1) VALUES (3, ’Tony’, ’Hill’, ’[email protected]’) GO The st at em ent can w or k direct ly wit h t ables, but t he sam ple illust rat es it s capabilit y of work ing w it h a v iew —nam ely , v ew Em ailCont act s. An ear lier sam ple in t his chapt er creat ed t his v iew . The I NTO k ey word is opt ional. I n ot her words, you can specify I NSERT wit h or wit hout I NTO. Not ice t he list of colum n nam es in parent heses following t he I NTO k eyword and t he v iew nam e. The sy nt ax r ules for t he st at em ent r equir e t his list w hen you are insert ing values for som e but not all colum ns or you ar e insert ing colum n values in a differ ent order t han t he one in which t hey appear in t he row source. Because t he sam ple assigns a value t o each

colum n in t he order t hat t he colum ns appear in t he t able, t he list isn’t m andat ory. Howev er, including t he list is a good pract ice because it m ak es it clear which values t he st at em ent assigns t o indiv idual colum ns. The VALUES k eyw ord is m andat ory. This keyw or d m arks t he st art of t he values for t he new r ow. I nclude t he values t hat y ou want t o add w it hin par ent heses.

N ot e Ther e are several int er est ing adapt at ions of t he I NSERT I NTO or I NSERT st at em ent . For exam ple, you shouldn’t specify colum n values for colum ns w it h an I DENTI TY pr opert y or com put ed colum ns because SQL Server aut om at ically det er m ines t he values for t hese colum ns. I n addit ion, y ou can t ransfer dat a fr om one t able t o anot her by using a SELECT clause wit hin an I NSERT I NTO st at em ent . See t he “ I NSERT” t opic in Books Online for t he pr ecise synt ax t o im plem ent t his. When you com bine t his feat ure wit h t he OPENROWSET funct ion or anot her m eans of select ing rows fr om a het er ogeneous or r em ot e dat a source, t he I NSERT I NTO st at em ent prov ides a conduit for t ransferr ing dat a bet ween dat abases. The init ial version of t he Em ailCont act s t able has j ust t wo rows, for Rick Dobson and Virginia Dobson. I nvok ing t he st or ed pr ocedur e w it h an EXEC st at em ent adds a t hird r ow. The sam ple script r uns t he EXEC st at em ent for t he st ored procedur e and t hen per form s a SELECT st at em ent t hat ret ur ns all r ows from t he Em ailCont act s t able. The result set fr om t he SELECT st at em ent confirm s t he addit ion of t he new r ow t o t he t able. Aft er insert ing a new r ow, t he sam ple script pr ogresses by invok ing t he ALTER PROC st at em ent : ALTER PROC udpInsertUpdateDeleteSamples AS UPDATE vewEmailContacts SET FirstName = ’Anthony’, Email1 = ’[email protected]’ WHERE ContactID = 3 GO This st at em ent m odifies t he synt ax for t he udpI nsert Updat eDelet eSam ples st ored procedur es from an insert pr ocedur e t o an updat e procedure. The new version of t he st or ed pr ocedur e changes t he First Nam e and Em ail1 colum n values for t he row added wit h t he I NSERT I NTO dem onst rat ion. The sy nt ax for t he UPDATE st at em ent r ev eals how t o change t w o colum n values wit hin a single UPDATE st at em ent . St art by follow ing t he UPDATE keyw ord wit h t he nam e of a t able or v iew t hat point s at a t able w it h colum n values y ou want t o updat e. To use a v iew in t his way ( as t he sam ple does) , t he v iew m ust perm it updat ing of it s underly ing colum n values. Aft er t he UPDATE k ey word and it s t arget row source, y ou can st art a new line w it h t he SET k eyw ord. Each updat e for a colum n value r equires an assignm ent st at em ent w it h t he new value for t he colum n. Delim it successiv e assignm ent st at em ent s wit h com m as. The WHERE clause is part icularly cr it ical w it h UPDATE and DELETE st at em ent s because it specifies t o which row ( s) t o apply t he st at em ent . I n t he script below, using t he WHERE clause expression ContactID = 3 indicat es t hat t he UPDATE st at em ent applies t o j ust t he row for Tony Hill, w ho has a Cont act I D colum n value of 3. Aft er alt ering t he st or ed procedure, y ou m ust r un it for t he change t o have an effect . The EXEC st at em ent achieves t his. A SELECT st at em ent confirm s t hat t he

updat e occur red. The row for Tony Hill includes new values for it s First Nam e and Em ail1 colum ns. The last part of t he sam ple script shows how t o alt er a st ored procedur e for t he addit ion of a DELETE st at em ent . This st at em ent doesn’t requir e a list , as is com m on w it h t he SELECT st at em ent . That ’s because t he DELETE st at em ent rem ov es one or m ore rows at a t im e; t he st at em ent doesn’t operat e on indiv idual colum ns w it hin a row . The FROM clause in t he sam ple denot es t he r ow source from w hich t o r em ov e r ows. The WHERE clause is cr it ical. Use y our WHERE clause expression t o designat e which r ows t o r em ov e from t he row source. Wit hout a WHERE clause, t he DELETE st at em ent r em oves all r ows fr om it s r ow source.

N ot e I f you do want t o r em ove all r ows, you can specify t he st at em ent as DELETE row sourcenam e, such as DELETE pubs..authors t o rem ove all t he r ows from t he aut hors t able in t he pubs dat abase. However , when y ou want t o rem ove all t he rows fr om a t able w it h m any rows, t wo ot her t echniques will do t he j ob fast er . I nv oke t he TRUNCATE TABLE st at em ent t o rem ove all t he rows fr om a t able w it hout logging t he delet ions t o t he log file while preserv ing t he t able’s design. Alt er nat iv ely , you can inv ok e t he DROP TABLE st at em ent t o rem ove concur rent ly t he cont ent s and t he design for a t able. The last part of t he following script cr eat es a st or ed procedur e t hat rem ov es t he row wit h a Cont act I D colum n v alue of 3 by applying t he DELETE st at em ent . Then t he script execut es t he st ored procedure t o r em ov e t he r ow w it h a Cont act I D value of 3. Finally t he script concludes by inv ok ing a SELECT st at em ent t hat displays t he r em aining r ows in t he Em ailCont act s t able. Figure 4- 4, aft er t he script , shows t he t hr ee result set s it produces. --CreateudpInsertUpdateDeleteSamples --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpInsertUpdateDeleteSamples’) DROP PROCEDURE udpInsertUpdateDeleteSamples GO --Insert into a table via a view. CREATE PROC udpInsertUpdateDeleteSamples AS INSERT INTO vewEmailContacts (ContactID, FirstName, LastName, Email1) VALUES (3, ’Tony’, ’Hill’, ’[email protected]’) GO --Confirm new result set. EXEC udpInsertUpdateDeleteSamples SELECT * FROM EmailContacts GO --Modify table column values via a view. ALTER PROC udpInsertUpdateDeleteSamples AS UPDATE vewEmailContacts

SET FirstName = ’Anthony’, Email1 = ’[email protected]’ WHERE ContactID = 3 GO --Confirm new result set. EXEC udpInsertUpdateDeleteSamples SELECT * FROM vewEmailContacts GO --Delete newly added row directly from table. ALTER PROC udpInsertUpdateDeleteSamples AS DELETE FROM EmailContacts WHERE ContactID = 3 GO --Confirm new result set. EXEC udpInsertUpdateDeleteSamples SELECT * FROM EmailContacts GO

Figu re 4 - 4 . Th e r et u r n from view s t h a t su cce ssive ly in ser t , u pd at e , a n d de le t e r ow s from a row sou r ce .

Pe r f or m in g D a t a ba se M a in t e n a n ce w it h Pa r a m e t e r s Ty pically, y ou won’t r un dat a m anipulat ion st at em ent s, such as I NSERT I NTO, UPDATE, and DELETE, wit h const ant s as in t he preceding sam ple. The purpose for t he preceding scr ipt was t o pr ov ide a basis for describing t he sy nt ax for including dat a m anipulat ion st at em ent s in st ored procedures. The real pow er of st ored procedur es w it h t hese st at em ent s is t hat y ou can pass param et ers t o t he procedur es t o specify t he rows t hat t he st at em ent s insert , updat e, or delet e fr om a row source. The sam ple script in t his sect ion builds on t he prior one by dem onst rat ing t he synt ax for using param et ers w it h dat a m anipulat ion st at em ent s. Again, t he em phasis is on clarit y, so t he scr ipt accom plishes t he sam e k ind of t ask s as t he preceding one. A significant change, how ev er , is t hat t he t arget row source is from anot her dat abase on t he sam e serv er—t he Nort hwind SQL Ser ver dat abase.

The script in t his sect ion follows t he m odel of t he pr ev ious one by alt er ing one st ored procedure inst ead of cr eat ing t hr ee separat e st ored pr ocedur es—one for insert ing, anot her for updat ing, and a t hird for delet ing. Because of t he sim ilar it y of t his script ’s design t o t he pr eceding one, I w ill explain j ust t he first part of t he script for insert ing a new r ecord. Lik e t he preceding sam ple, t his one swit ches back and fort h bet w een using a t able and a v iew as a r ow source for t he dat a m anipulat ion st at em ent s. This is t o reinforce y our underst anding t hat y ou can perform dat abase m aint enance chores w it h eit her t ype of obj ect serv ing as a row source. Aft er rem ov ing any prior v ersion of t he udpPar am sForI nsert Updat eDelet e st or ed procedur e, t he script cr eat es a new v ersion t hat includes an I NSERT I NTO st at em ent . The CREATE PROC st at em ent for creat ing t he st ored procedure has t wo input param et ers—one for t he Com pany Nam e colum n value and anot her for t he Phone colum n value. A com m a delim it s t he t wo par am et er declarat ions. The dat a t ype set t ings follow t hose for t he colum ns in t he Nort hw ind Shippers t able. The param et er nam es appear again in parent heses aft er t he VALUES k eyword. These param et ers replace t he st r ing const ant s used in t he pr eceding script sam ple. The code illust r at ing t he sy nt ax for t he UPDATE and DELETE st at em ent s follows t he sam e pat t er n. First it declares t he param et er . Second it uses t he param et ers as var iables in dat abase m aint enance st at em ent s. The EXEC st at em ent for t he st or ed procedur e specifies values for passing t o t he st ored procedure. This is one way y our Visual Basic .NET applicat ions can use values ent ered by users as part of dat a m anipulat ion st at em ent s. Chapt er 10 illust rat es how t o use Visual Basic .Net for t his kind of t ask. The user input set s t he param et er v alues in t he code t hat calls t he st ored procedure. Aft er adding t he new record t o t he Shippers t able in t he Nort hw ind dat abase by calling t he st or ed procedur e, t he script invok es a SELECT st at em ent t o display all t he r ows in t he Shippers t able. --CreateudpParamsForInsertUpdateDelete --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpParamsForInsertUpdateDelete’) DROP PROCEDURE udpParamsForInsertUpdateDelete GO --Insert values into a table in another database. CREATE PROC udpParamsForInsertUpdateDelete @newCompanyName nvarchar(40), @newPhone nvarchar (24) AS INSERT INTO Northwind..Shippers (CompanyName, Phone) VALUES (@newCompanyName, @newPhone) GO --Confirm new result set. EXEC udpParamsForInsertUpdateDelete ’CAB Delivers’, ’(123) 456-7890’ SELECT * FROM Northwind..Shippers GO --Modify table column values in another database --via a view pointing at the table in this database. ALTER PROC udpParamsForInsertUpdateDelete @newPhone nvarchar(24), @newCompanyName nvarchar(40)

AS UPDATE vewShippers SET Phone = @newPhone WHERE CompanyName = @newCompanyName GO --Confirm new result set. EXEC udpParamsForInsertUpdateDelete ’(234) 567-8901’, ’CAB Delivers’ SELECT * FROM vewShippers GO --Delete newly added row in other database --from view pointing at row in this database. ALTER PROC udpParamsForInsertUpdateDelete @newCompanyName nvarchar(40) AS DELETE FROM vewShippers WHERE CompanyName = @newCompanyName GO --Delete newly added row directly from table. EXEC udpParamsForInsertUpdateDelete ’CAB Delivers’ SELECT * FROM Northwind..Shippers GO

Pr ogra m m in g Con dit iona l Resu lt Se t s Ev en t hough a st or ed pr ocedure is com piled, it can st ill ex ecut e in differ ent ways at run t im e, depending on t he v alues of param et ers. The preceding sect ion show ed how t o accom plish t his for insert / updat e/ delet e operat ions. This sect ion shows how you can m odify t he out put fr om a procedur e at r un t im e in a m ore advanced way t han set t ing values for WHERE clause expressions. This sect ion st art s wit h a sam ple t hat list s t he v iews in a dat abase. I f t her e are no views in t he dat abase, it doesn’t display t he colum n headers for it em izing v iews. The second and t hird sam ples show how t o ret urn t he t op x rows wit h a SELECT st at em ent . Users can vary t he num ber of r ows r et ur ned.

Con dit iona lly List in g Obj e ct s A SELECT st at em ent displays t he colum n headers for a r esult set ev en if t he result set is em pt y. I f y ou happen t o be ret ur ning m ult iple result set s from a st ored procedure, t he w rit ing of headers for em pt y result set s can clut t er t he Result s pane and dist ract at t ent ion from populat ed r esult set s. I n any event , y ou m ay pr efer t o avoid pr int ing t he colum n headers for an em pt y result set —aft er all, t her e’s not hing t o it em ize below t he header s. The sam ple for t his sect ion t est s whet her t he result set for a SELECT st at em ent has any r ows before sending it s out put t o t he Result s pane. The logic is t o perform an aggregat e query t hat t est s for t he exist ence of it em s sat isfy ing a WHERE clause cr it er ion. I f t he count of r et urned rows is gr eat er t han 0, t he procedur e ex ecut es a SELECT st at em ent t hat r et urns t he indiv idual it em s. Ot herwise, t he st or ed procedur e j ust wr it es a st at em ent t o t he Messages pane saying t her e are no it em s in t he result set .

This sam ple enum erat es t he views in a dat abase connect ion. A USE st at em ent at t he t op of t he script specifies t he t arget dat abase. This book has t w o cust om dat abases so far. The dat abase for t his chapt er, Chapt er04, has nine v iews. Chapt er01, t he dat abase for t his book ’s first chapt er, has zer o v iews. Ther efor e, by changing t he USE st at em ent t o point at one or t he ot her dat abase, t he sam ple script can dem onst rat e condit ional out put s fr om t he sam ple st ored pr oj ect . The st or ed procedure uses a local var iable, @int Views, t o st ore t he result fr om a SELECT st at em ent w it h a COUNT funct ion. The funct ion aggregat es t he num ber of virt ual t ables ( or v iews) in a dat abase. The I NFORMATI ON_SCHEMA.TABLES v iew is t he row source for t he SELECT st at em ent . An I F…ELSE st at em ent branches t o t he I F block or t he ELSE st at em ent depending on t he value of @int View s. I f t he local v ar iable is great er t han 0, t he pr ocedur e execut es t he BEGI N…END block in t he I F clause of t he I F…ELSE st at em ent . Whenever y ou need t o condit ionally ex ecut e m ore t han one st at em ent in eit her clause of an I F…ELSE st at em ent , y ou m ust group t he st at em ent s bet w een BEGI N and END k eywords as t he sam ple dem onst rat es. The t wo st at em ent s in t he BEGI N…END block pr int t he num ber of v iews in t he dat abase connect ion t o t he Messages pane and show t he r esult set for a SELECT st at em ent list ing t he indiv idual v iew nam es in t he Result s pane. I f @int Views is 0, t he procedure m er ely pr int s a sent ence t o t he Messages pane say ing t her e are no views. Because t his requir es j ust one st at em ent , t he ELSE clause doesn’t require a BEGI N…END block. To unclut t er t he Messages pane, t he procedure invok es t he SET NOCOUNT ON st at em ent at it s st art and rest or es t he default set t ing ( SET NOCOUNT OFF) at it s close. Because t he st ored procedur e for t his sam ple is m eant for a one- t im e execut ion, t he script dr ops t he st or ed pr ocedur e at it s conclusion. Ther efore, t he st or ed procedur e isn’t st r ict ly necessary for t his sam ple. Feel free t o m odify t he sam ple t o r em ov e t he creat ion of t he st or ed pr ocedur e. I n any ev ent , lear n t he I F…ELSE design guidelines pr esent ed in t he sam ple. --CreateudpCountAndListViews --Designate database context. USE Chapter04 GO --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpCountAndListViews’) DROP PROCEDURE udpCountAndListViews GO --Create procedure to count and list views in the --current database connection. CREATE PROC udpCountAndListViews AS SET NOCOUNT ON DECLARE @intViews int SET @intViews = (SELECT COUNT(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ’VIEW’ AND LEFT(TABLE_NAME,3) ’sys’) IF (@intViews) > 0 BEGIN PRINT ’There were ’ + CAST(@intViews AS varchar(3)) + ’ views in the connection.’ SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ’VIEW’ AND

LEFT(TABLE_NAME,3) ’sys’ END ELSE PRINT ’There are no views in the connection.’ SET NOCOUNT OFF GO --Run the procedure to report on the views in --the current database connection. EXEC udpCountAndListViews GO --Drop the procedure from the current database connection. DROP PROCEDURE udpCountAndListViews GO

Re t u r n ing X I t e m s w it h t he TOP Pr e dica t e A t y pical r equest on SQL Serv er newsgr oups is, “ How do I r et ur n j ust t he t op x it em s, where I can var y t he value of x?” The quest ion is som et im es phr ased as, “ How do I use t he TOP predicat e t o r et ur n a variable num ber of it em s from a row source?” The TOP pr edicat e alone can’t solv e t his problem because it can accept only a const ant as t he num ber of it em s t o ret ur n. One w ay t o use a TOP predicat e t o ret ur n m ore t han a single num ber of it em s from a row source is t o nest SELECT st at em ent s wit h TOP predicat es w it hin t he clauses of an I F…ELSE st at em ent . Because y ou can nest I F…ELSE st at em ent s wit hin one anot her indefinit ely , t his approach perm it s y ou t o fine- t une t he lev el of precision on how m any rows t o r et ur n if y ou ar e w illing t o nest enough I F…ELSE st at em ent s w it hin one anot her. The next sam ple ret urns eit her t he t op 5 or 10 orders w it h t he longest delay in shipping aft er t he required dat e for an order. An input param et er value cont r ols which of t hese t w o result set s t he st ored pr ocedur e ret urns. I f t he input param et er, @Num berOfOrders, is less t han or equal t o 5, t he st or ed pr ocedur e ret ur ns t he 5 orders w it h t he longest delays in shipping. I f t he value of @Num berOfOrders is 6 or gr eat er, t he pr ocedur e ret urns t he 10 orders wit h t he longest delays in shipping. This is t he first sam ple in t he chapt er t o assign a default v alue t o a param et er. The equal sign in t he param et er declarat ion shows t he synt ax for assigning a default value of 5. Because of t his default value, t he procedur e ret urns t he 5 r ecords w it h t he longest delays if t he EXEC st at em ent t hat invokes t he udpLongest Lat eOrdersWit hTop st ored procedure fails t o designat e a param et er value. A single I F…ELSE st at em ent passes cont rol t o one of t he t wo SELECT st at em ent s in it s clauses based on t he @Num ber OfOrders param et er value. The SELECT st at em ent in t he I F clause uses a TOP pr edicat e wit h an argum ent of 5. On t he ot her hand, t he SELECT st at em ent in t he ELSE clause has exact ly t he sam e synt ax except t hat it s TOP predicat e has an argum ent of 10. The EXEC st at em ent for inv ok ing t he st or ed pr ocedur e passes t he param et er value 7. Because t his is great er t han 5, t he procedur e ret ur ns t he 10 or ders w it h t he longest shipping delays aft er t he required dat e. The param et er value 100, 1,000, or 10,000 w ill st ill ret ur n j ust 10 r ows in t he r esult set . This is because t he st ored procedure support s j ust t wo different TOP pr edicat e argum ent v alues. You can alt er t he procedure by adding nest ed I F…ELSE st at em ent s t o accom m odat e m or e TOP pr edicat e argum ent values. --CreateudpLongestLateOrdersWithTop --Remove prior version of stored procedure. IF EXISTS (SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES

WHERE ROUTINE_TYPE = ’PROCEDURE’ AND ROUTINE_NAME = ’udpLongestLateOrdersWithTop’) DROP PROCEDURE udpLongestLateOrdersWithTop GO --Create proc for itemizing late orders --with one of two TOP predicates. CREATE PROC udpLongestLateOrdersWithTop @NumberOfOrders int = 5 AS IF @NumberOfOrders 0 ORDER BY dbo.udfDaysShippedLate(OrderID) DESC Figur e 5- 1 shows an ex cerpt from t he result set for t he pr eceding SELECT st at em ent . The second colum n shows t he r et ur n values fr om t he UDF. You can confirm it s calculat ion w it h t he help of t he last t wo colum ns. Not ice also t hat rows appear in order based on t he value in t he second colum n, w hich display s t he UDF values for each row. Figur e 5- 1 shows t he Result s pane. How ev er, t he Messages pane cont ains t he num ber of r ows affect ed, or r et urned, by t he SELECT st at em ent . I t is only 37, which is subst ant ially less t han t he full num ber of 830 rows in t he or iginal Orders t able. Figu re 5 - 1 . An e xce r pt fr om a r esu lt se t ba se d on a SELECT st a t e m e n t t h at u ses a sca la r UD F in it s list a s w e ll as it s W H ERE a n d ORD ER BY cla u se s.

The next script shows anot her t ype of applicat ion for t he udfDaysShippedLat e UDF. This script uses t he UDF in an expression t hat serves as t he condit ion for an I F…ELSE st at em ent . Because t he value for t he UDF appears elsew her e in t he script besides t he condit ion for t he I F…ELSE st at em ent , t he script sav es t he UDF’s value in a local var iable, @DaysBefor eAft er . This assignm ent saves hav ing t o recom put e t he funct ion each t im e t he script needs t he UDF’s value. The script com put es and displays one of t wo possible m essages based on t he udfDaysShippedLat e UDF value. I f t he scalar UDF value is negat iv e, t he order shipped before t he requir ed dat e. Ot herw ise, t he order shipped on or aft er t he requir ed dat e. The expr ession for t he I F…ELSE st at em ent capt ur es whet her t he order shipped befor e or aft er t he required dat e. The I F clause of t he st at em ent com put es a st at em ent saying how m any days befor e t he requir ed dat e an order

shipped. This can be any value fr om 1 day t hrough t he m ax im um num ber of days in t he Orders t able t hat an order shipped befor e it s r equir ed dat e. The ELSE clause com put es a st at em ent det ailing how m any days aft er t he r equir ed dat e an order shipped. The st ring expressions in t he I F and ELSE clauses bot h refer ence t he @DaysBeforeAft er local var iable, w hich t he script uses t o st or e t he ret ur n value from t he udfDaysShippedLat e UDF. To see t he script in oper at ion, y ou need t o run it wit h OrderI D values for orders shipping before and aft er t heir r equir ed dat es. The sam ple script includes t wo such OrderI D values. As t he script appears below, it com put es a m essage for OrderI D 10777, w hich shipped 23 day s aft er it s r equired dat e. You can com m ent out t he SET st at em ent assigning 10777 t o t he @Tar get I D local var iable and rem ov e t he com m ent m ark ers for t he SET st at em ent assigning 10248 t o t he local variable. This act ion per m it s you t o run t he script in a m ode t hat com put es a m essage for t he num ber of days t hat an order shipped before it s required dat e. I n t his inst ance, t he order shipped 16 days before it s requir ed dat e. --udfDaysShippedLate_c --Invoke a user-defined function to compute a conditional message --for the number of days that an order ships before or after its --required date. DECLARE @TargetID int DECLARE @DaysBeforeAfter int DECLARE @ShipMessage varchar (1000) --Order 10248 shipped 16 days before its required date. --Order 10777 shipped 23 days after its required date. --SET @TargetID = 10248 SET @TargetID = 10777 --Save user-defined function value for reuse in script. SET @DaysBeforeAfter = dbo.udfDaysShippedLate(@TargetID) --Branch to compute one of two message formats based on the --user-defined function value. IF @DaysBeforeAfter < 0 BEGIN SET @ShipMessage = ’Order ’ + CAST(@TargetID AS varchar) + ’ shipped ’ + CAST(-1 * @DaysBeforeAfter AS varchar) + ’ days before the required date.’ Print @ShipMessage END ELSE BEGIN SET @ShipMessage = ’Order ’ + CAST(@TargetID AS varchar) + ’ shipped ’ + CAST(@DaysBeforeAfter AS varchar) + ’ days after the required date.’ Print @ShipMessage END GO

Cr e a t in g a n d I nvok in g Ta ble - Va lu ed UD Fs Bot h inline UDFs and m ult ist at em ent UDFs can ret ur n t ables inst ead of scalar values. The inline UDF has t he advant age of an exceedingly sim ple synt ax . I n addit ion, it support s par am et ers so t hat users can cont r ol it s r esult set at run t im e. Mult ist at em ent UDFs are subst ant ially m or e flex ible t han inline UDFs. The ext ra flex ibilit y com es at t he expense of m ore sophist icat ed T- SQL logic. SQL

Ser ver giv es you a choice. You can incorporat e t he t able- valued UDF t hat best fit s your needs. The sam ples in t his sect ion w ill help you see som e of t he capabilit ies of bot h appr oaches so t hat y ou can m ake an inform ed choice.

Pr ovidin g Pa r a m e t r ic V ie w s I nline t able- valued UDFs are alway s based on a single SELECT st at em ent —j ust lik e a v iew . Howev er, t he inline t able- v alued UDF offers one significant advant age ov er a v iew. You can pass param et ers t o t he SELECT st at em ent for an inline t able- v alued UDF, but t he synt ax for t he CREATE VI EW st at em ent offers no opport unit ies for specify ing param et ers. I nline UDFs height en t he power of t heir advant age by offer ing it wit h an exceedingly sim ple synt ax . Recall t hat all you hav e t o do w hen cr eat ing an inline UDF is declar e t he r et ur n dat a t ype as t able in t he RETURNS clause and t hen specify a SELECT st at em ent as t he argum ent for t he RETURN clause. You can r eference an inline UDF in T- SQL st at em ent s j ust as you w ould a v iew except t hat y ou can pass t he inline UDF param et er values. The follow ing scr ipt illust rat es t he sy nt ax for saving a SELECT st at em ent in a UDF. The SELECT st at em ent pr ov ides a result set w it h a row for each order by a cust om er . The synt ax for t he st at em ent j oins t he Cust om ers and Orders t ables in t he Nort hw ind dat abase. Not ice t hat t he SELECT st at em ent specifies t he input param et er @Cust I D in it s WHERE clause. The SELECT st at em ent is t he argum ent for t he RETURN clause in a CREATE FUNCTI ON st at em ent . The par ent heses aft er t he funct ion nam e t railing t he CREATE FUNCTI ON keyword phrase ar e wher e t he UDF declar es t he @Cust I D param et er value. The SELECT st at em ent t hat concludes t he follow ing script illust rat es t he synt ax for invoking an inline UDF. The sam ple specifies t he ret urn of all t he colum ns from t he source w it h t he * charact er. You can designat e indiv idual colum ns in t he list . The FROM clause designat es t he inline UDF as t he source for t he r esult set from t he SELECT st at em ent . I n t his applicat ion, it isn’t essent ial t hat you specify t he ow ner for t he UDF. The funct ion’s nam e is sufficient for designat ing an inline UDF ow ned by t he dbo user . The specificat ion of t he @Cust I D param et er in t he parent heses aft er t he UDF’s nam e is crit ical because t he funct ion expect s a param et er value and has no default value. This param et er allows t he funct ion t o ret ur n t he orders for a part icular cust om er . --udfOrdersForCustomerID --Drop old version of user-defined function if it exists. IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ’udfOrdersForCustomerID’) DROP FUNCTION udfOrdersForCustomerID GO --Create Inline table-valued function with a parameter. CREATE FUNCTION udfOrdersForCustomerID(@CustID varchar(5)) RETURNS TABLE AS RETURN( SELECT c.CompanyName, c.ContactName, c.Phone, o.OrderID, o.OrderDate FROM Northwind..Customers c JOIN Northwind..Orders o ON (c.CustomerID = o.CustomerID) WHERE c.CustomerID = @CustID ) GO --Specify a parameter for table returned from function. SELECT * FROM udfOrdersForCustomerID(‘BERGS’) GO

Usin g a Sca la r UD F in t he List f or a n I n lin e UD F By com bining different t ypes of funct ions, y ou can add consider able flex ibilit y t o your applicat ions. A scalar UDF r et urns a single value, but t he single value can change depending on input param et er values. An inline UDF r et ur ns a r esult set t hat can cont ain m ult iple rows. By condit ioning a scalar UDF on colum n values from t he r esult set of an inline UDF, y ou can cr eat e new values t hat com bine or ext end t he values in t he source for t he inline UDF. The script sam ple in t his sect ion illust rat es how t o use a scalar UDF t o define a colum n for t he result set fr om an inline UDF. The script also shows how t o use t he colum n defined by t he scalar UDF in t he WHERE clause for SELECT st at em ent s inv ok ing t he inline UDF. The script defines t wo UDFs. The first , udfManagerNam e, is a scalar UDF. This funct ion ret ur ns t he first and last nam e for an em ployee fr om t he Em ployees t able in t he Nort hw ind dat abase. An expr ession in t he list for t he SELECT st at em ent com bines t he First Nam e and Last Nam e fields int o a single scalar value wit h a space delim it er bet ween t hem . The WHERE clause for t he SELECT st at em ent includes a param et er for designat ing t he Em ploy eeI D colum n value. The ret ur n value fr om t he scalar UDF is t he nam e of t he em ploy ee w it h an Em ploy eeI D colum n value m at ching t he input param et er . The second UDF in t he follow ing script is an inline UDF, nam ed udfEm ploy eeExt ensionManager. The SELECT st at em ent for t he inline UDF specifies t he Em ployees t able in t he Nort hw ind dat abase as it s r ow source. The list for t he SELECT st at em ent designat es four colum ns. Three com e dir ect ly from t he r ow source; t hese are an em ploy ee’s first nam e, last nam e, and ex t ension. The fourt h colum n is t he ret ur n value fr om t he udfManagerNam e UDF. The param et er value passed t o t he scalar UDF is t he Report sTo colum n v alue from t he Em ploy ees t able. This colum n value is t he Em ployeeI D for t he m anager t o w hich an em ploy ee report s. The SELECT st at em ent specifies an alias, Manager ’s Nam e, for t he udfManager Nam e UDF r et urn value. Not ice t hat y ou can r epresent a single apost rophe w it hin a st r ing const ant wit h t wo single apost rophes. --udfEmployeeExtensionManager --Drop old version of user-defined function if it exists. IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ’udfManagerName’) DROP FUNCTION udfManagerName GO -Function to return the manager’s name matching a ReportsTo column val ue. CREATE FUNCTION udfManagerName (@reportsto int) RETURNS varchar(40) AS BEGIN RETURN(SELECT DISTINCT FirstName + ’ ’ + LastName FROM Northwind..Employees WHERE EmployeeID = @reportsto) END GO --Drop old version of user-defined function if it exists. IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ’udfEmployeeExtensionManager’) DROP FUNCTION udfEmployeeExtensionManager GO

--Inline tablevalued function to return employee first name, last name, --extension, and manager’s name. CREATE FUNCTION udfEmployeeExtensionManager() RETURNS TABLE AS RETURN(SELECT FirstName, LastName, Extension, dbo.udfManagerName(ReportsTo) ’Manager’’s Name’ FROM Northwind..Employees) GO --SELECT statement with inline tablevalued function in its FROM clause. PRINT ’Report for Full udfEmployeeExtensionManager Function’ SELECT * FROM udfEmployeeExtensionManager() --Print direct reports to Andrew Fuller. PRINT ’Report for Andrew Fuller Direct Reports’ SELECT FirstName, LastName, Extension FROM udfEmployeeExtensionManager() WHERE [Manager’s Name] = ’Andrew Fuller’ --Print direct reports to Steven Buchanan. PRINT ’Report for Steven Buchanan Direct Reports’ SELECT FirstName, LastName, Extension FROM udfEmployeeExtensionManager() WHERE [Manager’s Name] = ’Steven Buchanan’ --Print direct and indirect reports to Andrew Fuller. PRINT ’Report for Andrew Fuller Direct and Indirect Reports’ SELECT FirstName, LastName, Extension FROM udfEmployeeExtensionManager() WHERE [Manager’s Name] = ’Andrew Fuller’ UNION SELECT FirstName, LastName, Extension FROM udfEmployeeExtensionManager() WHERE [Manager’s Name] = ’Steven Buchanan’ Aft er creat ing t he t wo UDFs, t he script illust rat es wit h four different bat ches how t o invoke t he inline UDF cont aining a scalar UDF in it s SELECT list . The first bat ch ret ur ns all t he colum ns for all t he rows in t he r esult set fr om t he inline UDF. This result set has four colum ns and nine r ows: one colum n for each it em in t he list for t he SELECT st at em ent of t he inline UDF and one r ow for each row in t he Em ploy ees t able—t he base t able for t he SELECT st at em ent . The follow ing list ing shows t he result set s for each of t he four sam ples. The second and t hird bat ches show how t o refer ence in t he WHERE clause t he colum n ret ur ned by t he scalar UDF inside t he inline UDF. The sy nt ax for t his reference uses t he alias nam e for t he funct ion, Manager ’s Nam e, in t he SELECT list fr om t he inline UDF. The second sam ple r et ur ns all t he direct repor t s t o Andr ew Fuller. The t hird sam ple r et ur ns t he dir ect report s t o St ev en Buchanan. Because St ev en Buchanan report s dir ect ly t o Andrew Fuller , t he direct r eport s t o St even r eport indirect ly t o Andr ew Fuller. The fourt h bat ch const ruct s a UNI ON query st at em ent t hat r et ur ns all t he em ploy ees r eport ing direct ly or indirect ly t o Andrew Fuller . The UNI ON operat or com bines int o one r esult set t he r esult s from t he SELECT st at em ent s in t he second and t hird sam ples. This final r esult differs fr om t he one for t he first sam ple in a couple of w ays. First , it cont ains j ust t hree colum ns. Ther e is no need for a colum n w it h t he m anager’s nam e because all em ploy ees r eport dir ect ly or

indirect ly t o Andr ew Fuller. Second, t his final result set cont ains j ust 8 rows as opposed t o t he 9 r ows in t he r esult set for t he first sam ple. This is because Andr ew Fuller doesn’t appear in t he list of em ploy ees r eport ing t o him . The follow ing list ing present s all t he result set s ret ur ned t o t he Messages pane when y ou r un t he pr eceding sam ple from Quer y Analyzer w it h a Result s I n Text set t ing. Report for Full udfEmployeeExtensionManager Function FirstName LastName Extension Manager’s Name ---------- -------------------- --------- -------------------------------Nancy Davolio 5467 Andrew Fuller Andrew Fuller 3457 NULL Janet Leverling 3355 Andrew Fuller Margaret Peacock 5176 Andrew Fuller Steven Buchanan 3453 Andrew Fuller Michael Suyama 428 Steven Buchanan Robert King 465 Steven Buchanan Laura Callahan 2344 Andrew Fuller Anne Dodsworth 452 Steven Buchanan (9 row(s) affected) Report for FirstName ---------Nancy Janet Margaret Steven Laura

Andrew Fuller Direct LastName -------------------Davolio Leverling Peacock Buchanan Callahan

Reports Extension --------5467 3355 5176 3453 2344

(5 row(s) affected) Report for FirstName ---------Michael Robert Anne

Steven Buchanan Direct Reports LastName Extension -------------------- --------Suyama 428 King 465 Dodsworth 452

(3 row(s) affected) Report for FirstName ---------Anne Janet Laura Margaret Michael Nancy Robert Steven

Andrew Fuller Direct LastName -------------------Dodsworth Leverling Callahan Peacock Suyama Davolio King Buchanan

and Indirect Reports Extension --------452 3355 2344 5176 428 5467 465 3453

(8 row(s) affected)

En ca psu la t ing M or e Logic w it h M u lt ist a t e m e nt UD Fs

A m ult ist at em ent t able- valued UDF prov ides subst ant ially m or e flex ibilit y t han is available from an inline UDF. While an inline UDF rest r ict s you t o a single SELECT st at em ent , a m ult ist at em ent UDF can cont ain m ult iple SELECT st at em ent s along wit h ot her k inds of T- SQL st at em ent s. The w ide range of st at em ent s t hat y ou can place inside a m ult ist at em ent UDF allows you t o creat e m or e flex ible funct ions t hat can sim plify t he logic of T- SQL st at em ent s t hat r eference t hem or even recover from invalid input . The follow ing scr ipt cont ains a m ult ist at em ent UDF t hat can r et ur n t hree different t ypes of result set s. The UDF accom plishes t his feat w it h t he aid of a couple of input param et ers and nest ed I F…ELSE st at em ent s t hat t est t he input param et er values. The @Report sTo param et er designat es t he m anager for w hom t o r et urn a result set . The par am et er has an int dat a t ype, and it denot es a m anager ’s em ploy ee I D. The @I ndirect param et er has a bit dat a t y pe. The value 0 is for dir ect report s, and t he v alue 1 is for t he r et ur n of direct and indirect r eport s. The logic inside t he CREATE FUNCTI ON st at em ent accom m odat es one invalid pair of input param et ers t o dem onst rat e w hat y ou can accom plish w it h a m ult ist at em ent UDF. This logic t raps for a r equest of indir ect report s for t he m anager w hose Em ploy eeI D value is 5. This r equest is invalid for t he Em ployees t able in t he Nort hwind dat abase because t his m anager has only dir ect report s. The funct ion recovers from t he r equest by supply ing only dir ect report s t o t he m anager. Aft er creat ing t he m ult ist at em ent UDF wit h a CREATE FUNCTI ON st at em ent , t he script launches four SELECT st at em ent s t hat refer ence t he UDF. The param et ers for t he SELECT st at em ent s allow y ou t o confirm t he flex ibilit y of t he UDF. The first SELECT st at em ent ret urns t he direct r eport s t o Andrew Fuller , whose Em ploy eeI D is 2. The second SELECT st at em ent r et urns t he dir ect report s t o St even Buchanan, w hose Em ploy eeI D is 5. The t hird SELECT st at em ent includes t he Em ploy eeI D v alue for Andrew Fuller again, but it set s @I ndir ect t o 1. This perm it s t he UDF t o ret urn t he result for a union query inst ead of a sim ple param et er query ( as in t he first t wo SELECT st at em ent s) . The fourt h SELECT st at em ent r equest s t he dir ect and indirect r epor t s for t he m anager w hose Em ploy eeI D is 5, nam ely St ev en Buchanan. This request s passes cont rol t o t he last I F clause in t he UDF and ret urns j ust t he direct r eport s for St ev en Buchanan. The UDF could hav e pr int ed a cust om m essage wit h t he RAI SERROR st at em ent . --udfReportsTable --Drop old version of user-defined function if it exists. IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ’udfReportsTable’) DROP FUNCTION udfReportsTable GO --Create multistatement table-valued function. CREATE FUNCTION udfReportsTable(@ReportsTo int, @Indirect bit) RETURNS @TableOut TABLE( FirstName varchar(10) NOT NULL, LastName varchar(20) NOT NULL, Extension varchar(4) NULL ) AS BEGIN IF @Indirect = 0 INSERT @TableOut SELECT FirstName, LastName, Extension FROM Northwind..Employees WHERE ReportsTo = @ReportsTo ELSE IF @ReportsTo = 2 INSERT @TableOut SELECT FirstName, LastName, Extension FROM Northwind..Employees

WHERE ReportsTo = @ReportsTo UNION SELECT FirstName, LastName, Extension FROM Northwind..Employees WHERE ReportsTo @ReportsTo AND ReportsTo IS NOT NULL ELSE IF @ReportsTo = 5 INSERT @TableOut SELECT FirstName, LastName, Extension FROM Northwind..Employees WHERE ReportsTo = @ReportsTo RETURN END GO --Print direct reports to Andrew Fuller. SELECT * FROM udfReportsTable(2,0) --Print direct reports to Steven Buchanan. SELECT * FROM udfReportsTable(5,0) --Print direct and indirect reports to Andrew Fuller. SELECT * FROM udfReportsTable(2,1) --Demo recovery from Indirect Reports request for Steven Buchanan. SELECT * FROM udfReportsTable(5,1)

I n t r odu ct ion t o Tr igger s Tr iggers enable developers t o creat e st ored procedur es t hat fir e aut om at ically when an applicat ion m akes changes t o t ables or v iews t o which t he t riggers belong. This sect ion int r oduces core concept s about what a t r igger is, t he different t ypes of t r iggers available t o developers, and st at em ent s for m anaging t riggers in y our applicat ions.

Tr igge r s Ar e lik e Ev e nt Pr oce du r e s Visual Basic dev elopers m ay find it conv enient t o t hink of t r iggers as ev ent procedur es. Tr iggers encapsulat e T- SQL code m uch lik e st ored procedures except t hat t r iggers fire aut om at ically w hen ev ent s happen for an obj ect t o which t he t rigger belongs. I t is good pract ice t o back up y our t rigger code independent ly of t he obj ect s t o w hich t hey belong—especially as you are init ially defining t he obj ect s for a pr oj ect . This is because dropping an obj ect r em oves any t r iggers associat ed wit h t he obj ect . Ther e is no war ning m essage about t he ex ist ence of t rigger code t hat you m ight want t o save before rem ov ing an obj ect . The ev ent s t hat fir e a t rigger are insert s, updat es, and delet es. Wit h classic t riggers, t he ev ent s ar e for t ables. These t rigger s act ually fir e aft er t he init iat ion of a change ev ent but befor e t he com m it m ent of a change t o a dat abase t able. Wit hin t he code for a classic t rigger, you can per form m any different k inds of act ions, such as rolling back t he change t o t he t able, perform ing dat a int egrit y checks, and archiving or iginal and changed dat a. Before t he int r oduct ion of

declarat ive r efer ent ial int egr it y and cascading updat es and delet es, it w as com m on t o pr ogram t his kind of behav ior w it h t riggers. Ev en now, if an applicat ion r equires referent ial int egr it y bet w een t w o t ables in different dat abases, you m ust pr ogram it m anually . Tr iggers r epresent a nat ural place t o locat e t he code for pr ogram m ing r efer ent ial int egrit y across t wo t ables in different dat abases. I t is good pract ice t o k eep y our t r igger code short and uncom plicat ed. This is because a t rigger fir es whenev er it s ev ent occurs. Ther efor e, an updat e t r igger fires w henev er a user t r ies t o updat e a value in a t able. The updat e doesn’t com m it unt il SQL Serv er com plet es t he ex ecut ion of t he code in t he t r igger. I f t he code pr esent s a m essage t o t he user, y ou especially w ant t o k eep t he code br ief so t hat t he m essage ret ur ns t o t he user swift ly . A t r igger does hav e a decided advant age ov er an ev ent pr ocedur e t hat you can assign t o a form in Visual Basic. This is because t he t r igger always fir es no m at t er how a user opens t he obj ect wit h t he t r igger. Wit h an ev ent pr ocedur e for a form t hat uses a SQL Serv er t able for it s r ow source, dat abase users can bypass any logic in t he ev ent pr ocedur e by opening t he t able dir ect ly or opening t he t able wit h anot her form or user int erface t hat doesn’t hav e t he event procedur e. When you code y our change dir ect ly against t he dat abase obj ect w it h a t rigger, t he code fires no m at t er how users open t he row source.

Ty pe s of Tr igge r s SQL Ser ver offers t wo basic t ypes of t r iggers. Wit hin each t ype, y ou can hav e t hr ee event t ypes t hat fire t riggers: insert s, updat es, and delet es. You can cr eat e a t r igger for any com binat ion of t hese t hr ee event s. The preceding sect ion briefly descr ibed classic t riggers. This is t he first t ype of t rigger . SQL Ser ver docum ent at ion r efers t o t his kind of t r igger as an AFTER t rigger . You can cr eat e an AFTER t rigger only for a t able. The nam e for t he t ype of t rigger indicat es when t he t rigger fir es—nam ely, aft er t he st art of a change t o a t able. You can have m ult iple AFTER t riggers for t he sam e change event . Wit h t he help of t w o syst em st ored procedures, you can designat e t he first and last t rigger t o fir e for a change event t o an obj ect . By designat ing a first and last t rigger t o fir e, you can precisely cont r ol t he order in which up t o t hree t riggers fire. How ev er, rem em ber t hat all t he t r iggers ev ent ually fir e for a change ev ent t o an obj ect . Ther efore, t he m or e t riggers y ou hav e, t he longer it t akes for SQL Ser ver t o com m it t he insert , updat e, or delet e act ion. I n addit ion, m ult iple t riggers can delay cust om m essages sent back t o users.

N ot e I nv oke t he sp_set t riggerorder syst em st ored pr ocedure t o cont rol t he or der of execut ion for t riggers. The procedure t akes t hree argum ent s: one for t he t r igger nam e, anot her for t he order of firing, and t he t hird for t he t ype of event . The second t ype of t r igger is an I NSTEAD OF t r igger. You can cr eat e I NSTEAD OF t riggers for bot h t ables and v iews. This t ype of t rigger fir es before t he change ev ent for t he obj ect . Ther efor e, you cannot roll back a change t o a t able or v iew from an I NSTEAD OF t rigger because t he ev ent didn’t occur y et . Howev er , y ou can com plet e t he act ion, or an alt ernat iv e one, from w it hin t he t r igger code. Unlike AFTER t riggers, only one I NSTEAD OF t r igger can ex ist for each t ype of change event . I f you apply a change event t o a view and per m it direct access t o any base t ables for t he view, users can bypass t he t r igger for t he v iew by opening t he base t ables.

in se r t e d a nd de le t e d Ta ble s The insert ed and delet ed t ables ar e t wo logical t ables available w it hin a t rigger. The t ables hav e t he sam e st r uct ur e as t he t able or v iew t o which a t r igger belongs. Each of t he t hr ee change ev ent s im pact s t he cont ent s of t he insert ed and delet ed t ables differ ent ly . These t ables are conv enient for archiving changes t o a t able. You can select which colum ns y ou ar chiv e and add any dat a t hat y our requir em ent s dict at e, such as user ident ificat ion, dat e, and t im e. An I NSERT st at em ent populat es t he insert ed t able. I NSERT st at em ent s don’t populat e t he delet ed t able. The new colum n values for t he insert ed row ar e in t he insert ed t able. A DELETE st at em ent populat es t he delet ed t able, but t he st at em ent leaves t he insert ed t able em pt y . The delet ed t able w ill have as m any r ows as t he DELETE st at em ent r em oves from t he t able. The TRUNCATE TABLE st at em ent doesn’t log changes t o t he delet ed t able or fir e t r iggers. I n addit ion, t he DROP TABLE st at em ent doesn’t fir e a t rigger. I nst ead, t he st at em ent rem ov es t he t rigger along wit h t he t able. An UPDATE st at em ent populat es bot h t he insert ed and delet ed t ables. The r ows wit h t he new values ar e in t he insert ed t able. The r ows w it h t he old values are in t he delet ed t able. As w it h t he DELETE st at em ent , t he insert ed and delet ed t ables can cont ain m ult iple rows for a single UPDATE st at em ent .

St a t e m e nt s for Cr e a t in g a nd D r oppin g Tr igge r s An array of T- SQL st at em ent s ex ist for creat ing and m anaging t riggers. Many of t hese st at em ent s parallel t hose for ot her dat abase obj ect s, but som e are special for t r iggers. ( For exam ple, y ou already r ead about t he sp_set t r iggerorder syst em st ored procedure.) You can m ak e new t r iggers wit h t he CREATE TRI GGER st at em ent . This single T- SQL st at em ent facilit at es t he cr eat ion of AFTER and I NSTEAD OF t riggers by t he inclusion of a k eyw ord phrase specify ing t he t r igger t ype. The DROP TRI GGER st at em ent works for eit her t ype of t rigger, but t her e is no I NFORMATI ON_SCHEMA v iew t hat displays t he t r iggers in a dat abase. The follow ing sam ples dem onst rat e an appr oach t o checking for t he exist ence of a t rigger based on t he sysobj ect s t able in a dat abase. I t is som et im es conv enient t o disable a t r igger ( for ex am ple, t o ent er new r ows int o a t able t hat conflict wit h t he logic in t he t r igger) . Use t he ALTER TABLE st at em ent w it h t he DI SABLE keyw ord follow ed by t he t r igger nam e t o disable an ex ist ing t r igger . To r est or e t he t r igger, specify t he ENABLE keyw ord follow ed by t he t rigger ’s nam e w it hin an ALTER TABLE st at em ent . The CREATE TRI GGER st at em ent is flex ible because a single t em plat e accom m odat es bot h AFTER and I NSTEAD OF t r iggers. I n addit ion, y ou can specify eit her t r igger t ype for any com binat ion of t he t hr ee possible event s t hat can fire it . The t r igger nam e follow ing t he CREATE TRI GGER k eyw ord phrase is a norm al SQL Ser ver ident ifier. To m ak e t r igger nam es ident ify t heir obj ect t ype, t his chapt er begins t r igger nam es w it h t he t rg pr efix . Designat e t he obj ect t o w hich a t rigger belongs in t he ON clause. Specify t he obj ect by following t he ON keyword wit h t he nam e of a t able or v iew . The next line is w her e t he CREATE TRI GGER st at em ent offers m uch of it s flex ibilit y. You can st art t he line w it h eit her t he AFTER k ey word or t he I NSTEAD OF k eyw ord phrase t o declare t he t rigger t ype. Because AFTER is t he default t rigger t ype, y ou don’t need t o specify t he AFTER k eyw ord t o creat e an AFTER t rigger . The FOR clause specifies t he t ype of event s t hat w ill fir e an AFTER t rigger . The synt ax for t he I NSTEAD OF k eyw or d doesn’t requir e t he FOR k eyword ( as is t he case for AFTER t r iggers) . The follow ing code t em plat e shows all t hr ee ev ent s. Howev er, you can designat e any t wo or j ust one ev ent . The ev ent nam es in t he FOR clause det er m ine what act ions fire a t r igger. The AS keyw or d m arks

t he t ransit ion fr om t he t rigger declarat ions t o t he T- SQL code t hat a t r igger ex ecut es when it fir es. CREATE TRIGGER trigger_name ON tablename or viewname AFTER OR INSTEAD OF OR FOR INSERT, UPDATE, DELETE AS T-SQL statements for trigger CREATE TRI GGER w ill fail if you t ry t o cr eat e a new t rigger w it h a nam e for a prev iously exist ing t r igger . Ther e are a couple of workarounds t o t his pr oblem . First , you can m odify t he design of t he old t rigger w it h t he ALTER TRI GGER st at em ent . Second, you can condit ionally dr op t he old version of a t r igger. The synt ax for checking on t he ex ist ence of a previously exist ing t r igger is different from t hat for t ables, v iews, st ored procedur es, and UDFs. This is because t here is no I NFORMATI ON_SCHEMA v iew for list ing t r iggers. However, y ou can use t he nam e and t ype colum ns of t he sysobj ect s t able t o v er ify t he exist ence of a prev iously exist ing version of a t rigger in a dat abase. The sysobj ect s t able is a t able m aint ained by SQL Serv er t hat keeps t rack of t he obj ect s in a dat abase. I n t he follow ing t em plat e, cont r ol passes t o t he DROP TRI GGER st at em ent only if t he sysobj ect s t able cont ains a row w it h a nam e colum n value equal t o t r igger nam e and a t ype value equal t o TR. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’triggername’ AND type = ’TR’) DROP TRIGGER triggername You can use t he sam e synt ax for ver ify ing t he exist ence of m any ot her dat abase obj ect s. Change t ype t o V for views and P for st or ed procedur es. Use FN, I F, and TF for scalar, inline, and m ult ist at em ent UDFs, r espect iv ely.

Cr e a t in g a n d Ma na gin g Tr igger s Tr iggers ar e a valuable t ool for m anaging your dat abases. The “ I nt r oduct ion t o Tr iggers” sect ion r ev iew ed t he basic concept s for using t r iggers, som e pot ent ial applicat ions, and basic synt ax issues. This sect ion pr ov ides four sam ples t hat illust rat e t he synt ax for using t r iggers in dat abase applicat ions. The sam ples aren’t as im port ant t hem selv es as t he issues t hat t hey fram e, such as how t o use t he insert ed and delet ed t ables and how t o enforce business rules. Rev iew t he sam ples t o rapidly ram p up t o speed on cor e t r igger design and applicat ion issues. Then adapt and ext end t he sam ples for your ow n applicat ion developm ent needs.

Pr ot e ct in g a n d Un pr ot e ct in g a Ta ble fr om Ch a n ge s Because t r iggers can fir e w henev er t here is an at t em pt t o change a t able, it is possible t o wr it e a t r igger t hat guards t he cont ent s of a t able. For exam ple, y ou can block all at t em pt s t o m odify t he cont ent s of a t able. You can select iv ely rest r ict t he abilit y t o delet e r ows, change colum n values in rows, or insert new rows int o a t able wit h AFTER t riggers. You can prot ect a t able’s cont ent s uncondit ionally, or you can condit ion t he pr ot ect ion on a user’s m em bership in secur it y roles, t he t im e of day , day of t he week , or what ever. I f y ou elect t o block m odificat ions wit h a t rigger t o a t able uncondit ionally , y ou w ill probably encount er a need t o disable t he t r igger occasionally. Disabling a t r igger allows y ou t o reinv ok e it easily w it hout hav ing t o r e- creat e or m odify it in any way . To r einv ok e

a disabled t r igger, all y ou hav e t o do is enable it . Recall t hat you can disable and enable a t r igger w it h an ALTER TABLE st at em ent for t he t able w it h t he t rigger t hat y ou want t o disable t em porarily. The follow ing scr ipt dem onst rat es t he synt ax for cr eat ing a t rigger for t he MyTable t able cr eat ed earlier in t his chapt er. ( See t he “ Cr eat ing a Scalar UDF Wit hout Param et ers” sect ion.) The t r igger pr ot ect s t he t able fr om insert s, updat es, and delet es by rolling back t he t ransact ion associat ed w it h t he t r igger. The script st art s by r em ov ing any previous version of t he t rgKeepMyTableUnt ouched t r igger and t hen begins a CREATE TRI GGER st at em ent . Lik e m ost ot her CREATE st at em ent s, t he CREATE TRI GGER st at em ent m ust occur at t he t op of a bat ch. Therefore, t he code t o drop t he old version ends wit h t he GO k eyw ord. The ON clause of t he CREATE TRI GGER st at em ent designat es t he MyTable t able as t he one t o w hich t he t r igger w ill belong. The FOR clause indicat es t hat t he t r igger w ill fir e for insert , updat e, and delet e ev ent s. The first st at em ent aft er t he AS k eyw ord is a RAI SERROR st at em ent t hat sends a cust om m essage back t o t he Messages pane of Query Analyzer. An inform at ional m essage issued from a t rigger is useful for let t ing a user k now t hat a t r igger fired. The RAI SERROR st at em ent can serv e ot her funct ions as well, but it is a robust alt er nat iv e t o t he PRI NT st at em ent for sending m essages t o t he Messages pane. The st r ing for a cust om m essage can be up t o 400 charact ers. The t railing values 16 and 1 indicat e t he sever it y and st at e for t he err or. For sim ple inform at ional m essages, y ou can consist ent ly apply t hese v alues. The second TSQL st at em ent in t he script r olls back t he t ransact ion t o m odify t he t able. The ROLLBACK TRAN st at em ent is an abbrev iat ed version of t he ROLLBACK TRANSACTI ON st at em ent . I n eit her form , t his st at em ent rem ov es any insert ed rows, r est or es any colum n values t o t heir nonupdat ed st at e, and adds back any delet ed r ows. You w ill generally want t o use t he ROLLBACK TRAN st at em ent as t he last st at em ent in a t rigger because any st at em ent s aft er ROLLBACK TRAN can m odify t he t able for a t r igger. --trgKeepMyTableUntouched --Drop prior version of trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgKeepMyTableUntouched’ AND type = ’TR’) DROP TRIGGER trgKeepMyTableUntouched GO --Create new trigger to keep MyTable table untouched. CREATE TRIGGER trgKeepMyTableUntouched ON MyTable FOR INSERT, UPDATE, DELETE AS RAISERROR(‘Message from trgKeepMyTableUntouched.’,16,1) ROLLBACK TRAN GO The follow ing scr ipt is a collect ion of T- SQL st at em ent s t hat dem onst rat es t he behav ior of t he t rigger as well as how t o disable and rest ore t he t r igger. The first couple of bat ches in t he script at t em pt t o delet e all r ows fr om t he My Table t able and m odify a colum n value in t he t able. Neit her bat ch succeeds because t he t rgKeepMyTableUnt ouched t r igger pr ot ect s t he MyTable t able fr om delet e and updat e ev ent s ( as w ell as insert ev ent s) . I f it becom es essent ial t o m odify a t able wit h a t rigger t hat blocks changes, y ou can t em porar ily disable t he t r igger. The script dem onst rat es t he synt ax for t he t rgKeepMyTableUnt ouched t r igger . You have t o m odify t he My Table t able w it h t he ALTER TABLE st at em ent t o disable it s t r igger. Aft er disabling t he t r igger, t he script changes t he m axim um v alue in t he col1 colum n. Then, in anot her bat ch, t he script r est or es t he init ial m ax im um value. The script s use a scalar UDF dev eloped ear lier in t his chapt er t o accom plish t hese t asks. Aft er successfully

m odify ing t he t able w it h t he t r igger disabled, t he script enables t he t rigger again for t he MyTable t able w it h t he ALTER TABLE st at em ent . Just t o confirm t he t rigger ’s operat ion, t he script again at t em pt s t o delet e all rows fr om t he t able. The t rigger fir es and pr int s it s inform at ional m essage and rolls back t he t ransact ion t o rem ov e t he r ows from t he t able. --Demo_trgKeepMyTableUntouched --An attempt to delete all records fails with --trigger error message. DELETE FROM MyTable GO --An attempt to update the maximum value in --col1 in the MyTable table fails also. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() WHERE col1 = (SELECT MAX(col1) FROM MyTable) GO --Disable the trigger for MyTable without dropping it. ALTER TABLE MyTable Disable TRIGGER trgKeepMyTableUntouched GO --Update attempt for MyTable succeeds. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() WHERE col1 = (SELECT MAX(col1) FROM MyTable) SELECT * FROM MyTable GO --Restoring update event also succeeds. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() - 2 WHERE col1 = (SELECT MAX(col1) FROM MyTable) SELECT * FROM MyTable GO --Re-enable trigger. ALTER TABLE MyTable Enable TRIGGER trgKeepMyTableUntouched GO --An attempt to delete all records fails again --with trigger error message. DELETE FROM MyTable GO

Ar ch ivin g Ch a n ge s t o a Ta ble The logical t ables insert ed and delet ed cont ain t he changes t hat users m ake t o a t able. Unfort unat ely, t he insert ed and delet ed t ables are available only for t he t im e t hat a t r igger has cont rol of an applicat ion. When t he t r igger closes, SQL Ser ver in effect clears t he t ables. I f y ou want t o persist som e subset of t he changes t o a t able for perm anent ready access, you can use t r iggers t o save t he cont ent s of t he logical insert ed and delet ed t ables t o a t able in a SQL Ser ver

dat abase. Because changes ( insert s, updat es, and delet es) affect t he insert ed and delet ed t ables different ly, one approach is t o cr eat e a separat e t r igger for each t ype of change. This sim plifies t he t rigger logic, and it m ak es each t ype of change run fast er t han hav ing one t rigger t hat decipher s t he t y pe of change and t hen archiv es t he insert ed and delet ed t ables properly. The follow ing scr ipt creat es t hr ee t r iggers t o log insert s, updat es, and delet es t o t he My Table t able in t he ChangeLogFor MyTable t able. The script st art s by rem ov ing t he t r gKeepMyTableUnt ouched t r igger creat ed in t he pr ev ious sam ple. Recall t hat t he pr ev ious t r igger block s all changes t o t he MyTable t able. Next t his procedur e cr eat es a fr esh blank version of t he ChangeLogForMy Table t able. The t able has four colum ns—one for t he col1 values from t he insert ed or delet ed t able, a second for t he t ype of change, a t hird for t he dat e and t im e of t he change, and a fourt h colum n for t he login of t he user m ak ing t he change. Aft er creat ing a t able t o archiv e changes, t he script creat es a fresh copy of t he t rgI nsert ToChangeLog t rigger . This t r igger copies t he col1 value from t he insert ed t able t o a local var iable. Then it uses t he local v ariable in t he VALUES clause of an I NSERT I NTO st at em ent t o persist t he new value t o t he ChangeLogFor MyTable t able. The script uses a st ring const ant —I NSERT—t o designat e t he t ype of change. The CURRENT_TI MESTAMP and SYSTEM_USER k ey words denot e built - in funct ions t hat r et ur n t he curr ent dat e and t im e as well as t he login for t he cur rent user ( t he one who m ak es t he change) . The CREATE TRI GGER st at em ent s for t he t r gDelet eToChangeLog and t rgUpdat eToChangeLog t riggers persist t he delet e and updat e col1 values t o t he ChangeLogForMy Table t able. When logging delet es, you use t he delet ed t able inst ead of t he insert ed t able. I n t he case of updat es, you log t he cont ent s of t he delet ed and insert ed t ables t o t he ChangeLogForMyTable t able. Howev er, t he basic design of delet e and updat e t r iggers cor responds t o t he t rgI nsert ToChangeLog t rigger . --trgInsertUpdateDeleteToChangeLog --Drop prior version of trgKeepMyTableUntouched trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgKeepMyTableUntouched’ AND type = ’TR’) DROP TRIGGER trgKeepMyTableUntouched GO --Remove prior version of ChangeLogForMyTable table. IF EXISTS(SELECT TABLE_NAME = ’ChangeLogForMyTable’ FROM INFORMATION_SCHEMA.TABLES) DROP TABLE ChangeLogForMyTable --Create ChangeLogForMyTable table. CREATE TABLE ChangeLogForMyTable ( col1 int, type varchar (10), changedatetime datetime, changeuser varchar(128) ) GO --Drop prior version of trgInsertToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsertToChangeLog’ AND type = ’TR’) DROP TRIGGER trgInsertToChangeLog GO --Create trigger to monitor inserts. CREATE TRIGGER trgInsertToChangeLog ON MyTable

FOR INSERT AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM inserted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’INSERT’, CURRENT_TIMESTAMP, SYSTEM_USER) GO --Drop prior version of trgDeleteToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgDeleteToChangeLog’ AND type = ’TR’) DROP TRIGGER trgDeleteToChangeLog GO --Create trigger to monitor deletes. CREATE TRIGGER trgDeleteToChangeLog ON MyTable FOR DELETE AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM deleted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’DELETE’, CURRENT_TIMESTAMP, SYSTEM_USER) GO --Drop prior version of trgUpdateToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgUpdateToChangeLog’ AND type = ’TR’) DROP TRIGGER trgUpdateToChangeLog GO CREATE TRIGGER trgUpdateToChangeLog ON MyTable FOR UPDATE AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM deleted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’UPDATE’, CURRENT_TIMESTAMP, SYSTEM_USER) SET @col1value = (SELECT col1 FROM inserted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’UPDATE’, CURRENT_TIMESTAMP, SYSTEM_USER) GO The follow ing scr ipt should be r un im m ediat ely aft er y ou creat e t he t r iggers w it h t he preceding scr ipt . I t also benefit s from a fresh copy of t he MyTable t able, such as t he one generat ed by t he udfHigher ThanMax script in t he “ Cr eat ing a Scalar UDF Wit hout Param et er s” sect ion. The script m akes a ser ies of changes t o t he MyTable t able. Aft er each change, it uses SELECT st at em ent s t o ret urn t he MyTable t able and t he ChangeLogForMy Table t able. The first change is t o add a new row wit h t he value 25 for col1. Next it updat es t he value 25 t o 26. Finally it delet es t he r ow in t he MyTable t able w it h a col1 value of 26. --Demo_trgInsertUpdateDeleteToChangeLog --Insert a new row into MyTable and display --MyTable and ChangeLogForMyTable tables INSERT INTO MyTable (col1) VALUES (25) SELECT * FROM MyTable SELECT *

FROM ChangeLogForMyTable GO --Update inserted row value and display --MyTable and ChangeLogForMyTable tables. UPDATE MyTable SET col1 = 26 WHERE col1 = 25 SELECT * FROM MyTable SELECT * FROM ChangeLogForMyTable GO --Delete updated row and display --MyTable and ChangeLogForMyTable tables. DELETE FROM MyTable WHERE col1 = 26 SELECT * FROM MyTable SELECT * FROM ChangeLogForMyTable GO Exam ining t he Result s pane cont ent s will allow you t o follow t he changes t o t he MyTable t able as well as t he ChangeLogForMy Table t able. The first display of t he ChangeLogForMy Table t able shows a t able w it h j ust one row and a col1 value of 25. I n t he next display of t he t able, y ou can see t hr ee r ows. This is because an updat e adds t w o rows t o t he t able. I n it s final appearance in t he result s pane, t he ChangeLogForMy Table t able cont ains four rows.

En f or cin g a Busin e ss Rule on a Ta ble One of t he classic uses for t riggers is t he enforcem ent of business rules. Aft er all, t he t r igger always fir es befor e a change ev ent . The T- SQL in t he t r igger can assess t he change t o m ake sure it conform s t o business rules before com m it t ing t he change t o a t able. I f a change value doesn’t sat isfy a business rule, t he t rigger can t ak e an appropr iat e r em edy, such as r ej ect ing t he change or r ev ising t he change and inform ing t he user of any r em edial act ion. The next sam ple enforces a sim ple business r ule. The rule is t hat users can insert only even num bers int o col1 of t he My Table t able. Your norm al business rules can be subst ant ially m or e sophist icat ed t han t his sam ple, but t he t r iggers t o enforce t hose r ules can st ill use t he sam e logic. First you t est t he change value t o m ak e sure it adheres t o t he r ule. Second, if t he change value doesn’t confor m t o t he business r ule, y our t r igger can perform an appr opriat e r em edial act ion for t he invalid change value. Third, if t he change value sat isfies t he business rule, y ou insert it int o t he t able.

N ot e Befor e running t he sam ple script in t his sect ion, m ake sure you drop all ot her t r iggers for t he MyTable t able t hat can conflict wit h t he sam ple below. The sam ple script on t he book ’s com panion CD r em oves all prior t rigger s cr eat ed for

t he MyTable t able in t his chapt er. For br ev it y, t he list ing here doesn’t show t he code for dropping all t hese t riggers. The sam ple uses an I NSTEAD OF t r igger. Because t his t ype of t r igger fir es before t he change ev ent , t her e is no need t o r oll back a t ransact ion for an inv alid act ion. The sam ple uses t he m odulo operat or ( % ) t o check whet her a num ber div ides ev enly by 2. A rem ainder of 1 indicat es an odd num ber. This out com e calls for a rem edial act ion. The act ion in t his inst ance is t o add 1 t o t he input value from t he insert ed t able, const ruct a m essage indicat ing t he alt ernat iv e act ion t ak en, and finally insert t he new ev en num ber int o t he t able. A rem ainder of 0 indicat es an ev en num ber . Because ev en num bers sat isfy t he business r ule, t he t r igger can j ust insert t he value from t he insert ed t able int o col1 of t he My Table t able. Aft er t he cr eat ion of t he t r igger, t he script includes dat a m anipulat ion and SELECT st at em ent s t o t est t he t rigger ’s logic. You can run t he sam ple script and see t he t rigger aut om at ically add 1 when t he script at t em pt s t o input an odd num ber ( 25) int o col1 in t he My Table t able. On t he ot her hand, t he t r igger m erely accept s t he insert of an even num ber ( 24) int o col1 in t he MyTable t able. --trgInsteadOfInsert --Drop prior version of trgInsteadOfInsert trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsteadOfInsert’ AND type = ’TR’) DROP TRIGGER trgInsteadOfInsert GO --Create an INSTEAD OF trigger. CREATE TRIGGER trgInsteadOfInsert ON MyTable INSTEAD OF INSERT AS DECLARE @col1value int DECLARE @newcol1value int DECLARE @strMsg varchar(400) SET @col1value = (SELECT col1 FROM inserted) --If inserted value is odd, make it even --before inserting it. IF @col1value%2 = 1 BEGIN SET @newcol1value = @col1value + 1 SET @strMsg = ’The value you want to insert is: ’ + CAST(@col1value AS varchar(3)) + ’, but it violates a business rule.’ + CHAR(10) + ’ Therefore, I insert ’ + CAST(@newcol1value AS varchar(3)) + ’.’ RAISERROR (@strMsg,16,1) INSERT INTO MyTable (col1) VALUES(@newcol1value) END ELSE INSERT INTO MyTable (col1) VALUES(@col1value) GO --Try to insert an odd value into col1 in MyTable. INSERT INTO MyTable (col1) VALUES(25) --Display the col1 values in MyTable. SELECT * FROM MyTable --Delete the next even value after the odd value.

DELETE FROM MyTable WHERE col1 = 26 --Display the col1 values in MyTable. SELECT * FROM MyTable --Insert an even value into col1 in MyTable. INSERT INTO MyTable (col1) VALUES(24) --Display the col1 values in MyTable. SELECT * FROM MyTable --Delete the new even col1 value in MyTable. DELETE FROM MyTable WHERE col1 = 24 --Display the col1 values in MyTable. SELECT * FROM MyTable

En f or cin g a Busin e ss Rule on a Vie w Two of t he advant ages of views are t hat t hey perm it you t o insulat e your dat abase schem a from t he user int erface for an applicat ion and t hat you can select iv ely expose subset s from a t able w it hout exposing all t he dat a in a base t able. These feat ur es perm it y ou t o secur e t he base t able or t ables for a view from all or m ost users while y ou grant t hese sam e users access t o a subset of t he dat a fr om t he base t able or t ables t hrough a view. Unfort unat ely, AFTER t r iggers never applied t o v iews, so prev iously y ou couldn’t enforce business rules wit h t riggers for v iews. SQL Ser ver 2000 int r oduced I NSTEAD OF t r iggers, w hich apply t o v iews. Ther efor e, you can gain t he benefit s of ex posing dat a t hr ough views and st ill be able t o enforce business r ules v ia t r igger s. The sam ple in t his sect ion dem onst rat es t he sy nt ax for apply ing a business r ule for insert s int o a view . The v iew is vewMy Table. This v iew ret urns all t he rows for t he colum n in t he MyTable t able. The business rule is t hat t he insert ed col1 v alue can be only 1 gr eat er t han t he cur r ent m ax im um in col1 of t he MyTable t able.

N ot e As wit h t he sam ple script fr om t he pr eceding sect ion, you should rem ove all t r igger s t hat can conflict wit h t he new t r igger. The version of t he follow ing sam ple on t he book ’s com panion CD rem oves all prior t riggers creat ed for t he My Table t able in t his chapt er. For br ev it y, t he list ing her e doesn’t show t he code for dropping all t hese t riggers. The script below st art s wit h t he cr eat ion of t he vewMyTable v iew. Then t he script m ov es on t o creat e a fr esh v ersion of t rgI nst eadOfI nsert Forv ewMyTable. No special act ion is necessary for cr eat ing a t r igger for a v iew. I n t he ON clause for t he CREATE TRI GGER st at em ent , j ust nam e t he view—vew MyTable, in t his case. The t rigger ’s logic uses t he udfOneHigherThanMax UDF cr eat ed ear lier in t his chapt er . You should r un t he code t o cr eat e t his UDF if it isn’t available. The logic for enforcing t he business rule is t he sam e as for t he pr ev ious t r igger, alt hough

t he act ual business r ule is differ ent . An I F…ELSE st at em ent t est s for t he validit y of t he new value r elat iv e t o t he business rule. I f t he new value fails t he t est , t he t rigger perform s a r em edial act ion. This act ion print s a m essage let t ing t he user know t he new value is invalid. Because t he t r igger is an I NSTEAD OF t r igger, t here is no need t o r oll back t he insert . I f t he new v alue is valid, t he t r igger insert s t he new value int o v ewMy Table. Aft er t he script creat es t he t r igger, t he script goes on t o t est t he t r igger by t ry ing t o insert t w o new values. The first value v iolat es t he business rule, and t he t rigger r ej ect s it . The second value sat isfies t he business rule, and t he t rigger insert s t he new value int o col1 of t he MyTable t able. The final dat a m anipulat ion st at em ent in t he script rem oves t he value new ly insert ed int o t he v ewMyTable view t o rest ore t he base t able t o it s init ial st at e. --trgInsteadOfInsertForvewMyTable --Drop prior version of vewMyTable view. IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewMyTable’) DROP VIEW vewMyTable GO --Create vewMyTable view. CREATE VIEW vewMyTable AS SELECT * FROM MyTable GO --Drop prior version of trgInsteadOfInsertForvewMyTable trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsteadOfInsertForvewMyTable’ AND type = ’TR ’) DROP TRIGGER trgInsteadOfInsertForvewMyTable GO --Create an INSTEAD OF trigger for a view. CREATE TRIGGER trgInsteadOfInsertForvewMyTable ON vewMyTable INSTEAD OF INSERT AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM inserted) IF @col1value > dbo.udfOneHigherThanMax() RAISERROR(‘Value too high.’,17,1) ELSE INSERT INTO vewMyTable (col1) VALUES(@col1value) GO --Attempting to insert a value of 100 fails --through vewMyTable. INSERT INTO vewMyTable (col1) VALUES(100) SELECT * FROM vewMyTable GO --Attempting to insert a value one higher --than the maximum value succeeds. INSERT INTO vewMyTable (col1) VALUES(dbo.udfOneHigherThanMax()) SELECT * FROM vewMyTable GO

--Remove inserted value. DELETE FROM vewMyTable WHERE col1 = dbo.udfOneHigherThanMax()-1 GO

Cha pt e r 6 . SQL Se r ve r 2 0 0 0 XM L Fun ct iona lit y When Microsoft SQL Ser ver 2000 was launched, Microsoft com m it t ed it self t o prov iding t he best Ext ensible Mark up Language ( XML) funct ionalit y possible. XML is im port ant because it prom ises t o r ev olut ionize t he way dat abase and Web dev elopers im plem ent dat a access and dat a m anipulat ion capabilit ies in t heir solut ions. Microsoft said it would r ev ise t he init ial release wit h t im ely updat es t hat included new funct ionalit y r eflect ing t he rapidly ev olv ing XML st andards and relat ed developm ent issues. As t his chapt er was being pr epared, Micr osoft delivered on it s com m it m ent w it h t he r elease of it s lat est updat e—t he Microsoft SQL Ser ver 2000 Web Serv ices Toolk it . The t oolk it follows t wo ear lier r eleases: XML for SQL Server 2000 Web Release 1 and XML for SQL Ser ver 2000 Web Release 2. The Web Ser v ices Toolk it is based on SQLXML 3.0 and includes t he SQLXML 3.0 inst allat ion package. Microsoft says t hat t he feat ur es int roduced in SQLXML 1.0 and SQLXML 2.0 ar e included in t he SQLXML 3.0 package. See Chapt er 12 for coverage of t he com pat ibilit y of t he t oolk it w it h t he t wo prior Web r eleases. I n addit ion, see Ch a pt e r 1 3 for com m ent ary and sam ples using t he Web Ser v ices Toolk it . You will gain from t his chapt er an ov erall under st anding of XML funct ionalit y in SQL Ser ver w it h an em phasis on access t o t hat funct ionalit y v ia T- SQL, XML schem as and t em plat es, and hypert ext t ranspor t prot ocol ( HTTP) . Chapt er 12 w ill refocus on XML so t hat you can build on t he underst anding pr esent ed her e w hile you lear n how t o t ap t he XML capabilit ies in SQL Serv er w it h Visual Basic .NET and relat ed t echnologies, such as ADO.NET. Wit h XML, dev elopers can build incredibly pow er ful solut ions for r et r iev ing and m aint aining dat a ov er Web connect ions. As t he w ord get s out about how easy it is t o creat e t hese solut ions, you w ill becom e an evangelist for using XML wit h SQL Ser ver. This chapt er relies on t he Nort hw ind sam ple dat abase. The chapt er sam ples add a couple of new v iews and user- defined funct ions t o t he dat abase for use wit h XML files. T- SQL script s for creat ing t hese obj ect s ar e included wit h t he sam ple files for t his chapt er. The m ain r esource for t he chapt er is a collect ion of nearly 20 XML files along w it h an assort m ent of URLs. Som e of t he URLs dem onst rat e direct access t o a SQL Server dat abase, while ot her URLs inv ok e an XML file and access a SQL Serv er dat abase indir ect ly t hrough t he XML file.

Ove r view of XM L Su ppor t I n lear ning about XML funct ionalit y, it is im port ant t o r ecall t hat Micr osoft int r oduced XML pr ocessing power t o SQL Ser ver 2000 in m ult iple wav es. This m eans t hat select ed XML feat ures available fr om t he init ial version of SQL Ser ver 2000 hav e been obsolet ed, or at least depr ecat ed, by subsequent ly int r oduced XML t echniques. This is because Web Release 1 and Web Release 2—and now t he Web Ser v ices Toolk it —added new XML funct ionalit y not available in t he init ial release. The ov erv iew of XML capabilit ies in t his sect ion has t wo part s. First it briefly sum m ar izes im port ant XML feat ur es for t he init ial release of SQL Serv er 2000 and each of t he first t wo Web releases. Second it pr ov ides helpful inform at ion for inst alling t he Web releases. See Chapt er 12 and Ch a pt e r 1 3 for m ore

inform at ion about t he lat est Web release, t he Microsoft SQL Ser ver 2000 Web Ser vices Toolk it .

Su m m a r y of XM L Fe a t u r e s by SQL Se r ve r Re le a se The init ial r elease of SQL Serv er 2000 offered XML funct ionalit y in four m ain areas. •







The abilit y t o access SQL Serv er v ia HTTP. This for m of access relies on t he creat ion of a Micr osoft I nt ernet I nform at ion Ser v ices ( I I S) v irt ual dir ect or y for each dat abase for w hich you prov ide access via HTTP. Support for XDR ( XML- Dat a Reduced) schem as. You can use t hese schem as t o creat e XML- based v iews of SQL Ser ver r ow sources, and y ou can use a subset of t he XML Pat h ( XPat h) query language t o query t hese views. The full XPat h specificat ion is a Wor ld Wide Web Consort ium ( W3C) st andard ( as out lined at ht t p: / / w ww.w3.org/ TR/ xpat h) . Ret r iev ing and wr it ing XML dat a. Wit h t he FOR XML clause for t he T- SQL SELECT st at em ent , SQL Ser ver prov ides a rout e for reading it s dat a sources and ret ur ning result set s in XML form at . OPENXML is a new funct ion t hat can r et ur n a r owset based on an XML docum ent . Because TSQL enables t he use of t he OPENXML funct ion in a m anner sim ilar t o t hat of t he OPENROWSET funct ion, y ou can use I NSERT st at em ent s t o populat e dat a sources based on XML docum ent cont ent s. Enhancem ent s for XML t o Microsoft SQL Server 2000 OLE DB provider ( SQLOLEDB) . These XML im pr ov em ent s com e along wit h version 2.6 of Micr osoft Dat a Access Com ponent s. Using t he new capabilit ies perm it s you t o pass XML- form at t ed dat a t o a Com m and obj ect and ret urn XMLfor m at t ed dat a fr om a Com m and obj ect . I n eit her case, t he dat a passes as a St ream obj ect .

Web Release 1 was last updat ed on Febr uar y 15, 2001. This r elease adds select ed new XML capabilit ies t o t he XML feat ures int r oduced w hen SQL Serv er 2000 init ially shipped in t he fourt h quart er of 2000. As y ou can see, Microsoft wast ed no t im e enhancing t he init ial capabilit ies. Web Release 1 creat es t wo m aj or im pr ov em ent s along w it h a collect ion of m inor ones. •





Updat egram s enable t ransact ion- based dat a m anipulat ion using XML. Updat egram s offer an XML- based synt ax for insert ing, updat ing, and delet ing r ecords in a SQL Serv er r ow source. You can specify t ransact ions for set s of operat ions w it hin Updat egram s so t hat all t he dat a m anipulat ion t asks wit hin a t ransact ion occur or none occur. By using Updat egram s inst ead of t he OPENXML funct ion, dev elopers can im pr ov e t he perform ance of t heir insert s, updat es, and delet es w hile sim plify ing t he coding. XML Bulk Load t arget s m ov ing m assiv e am ount s of XML- based dat a int o SQL Ser ver. This feat ure addresses t he needs of dat abase adm inist rat ors and ot hers w ho r egular ly use eit her t he BULK I NSERT st at em ent or t he bcp ut ilit y. I n a non- t ransact ion- based m ode, y ou can insert XMLfor m at t ed dat a fast er t han w it h Updat egram s or t he OPENXML funct ion. Select ed ot her Web Release 1 enhancem ent s. New sy nt ax offers you t he abilit y t o specify w it h a param et er t he ret urn of binar y dat a fr om a SQL Ser ver dat a source. Virt ual direct or y m anagem ent t ools expand t o offer m or e precise cont r ol ov er how users can access a dat abase v ia a v irt ual dir ect or y. Synt ax enhancem ent s im pr ov e your abilit y t o m ap XML schem as t o SQL Serv er dat a sour ces and generally m anage XML t em plat es.

Web Release 2 cont inued t he pat t ern of int er m ediat e r eleases t hat enhance t he XML funct ionalit y of SQL Serv er 2000. The last updat e for Web Release 2 was Oct ober 15, 2001, eight m ont hs aft er Web Release 1. I n I nt er net t im e, t his gap is long enough for a m aj or upgr ade—and Microsoft t ook advant age of t he int erval t o offer significant new funct ionalit y . Web Release 2 is especially appropr iat e for t hose planning t o dev elop solut ions w it h a .NET language, such as Visual Basic .NET. I highlight four m aj or areas of XML funct ionalit y and operat ion associat ed wit h Web Release 2: •







Com pliance w it h t he W3C schem a specificat ion k now n as XML Schem a Definit ion ( XSD) . While t his release doesn’t drop support for t he propriet ary XDR schem a specificat ion, Microsoft adds new funct ionalit y t hat is com pliant only w it h t he indust ry - st andard XSD. Adopt ing XSD schem as in y our own work w ill ensur e t he int eroperabilit y of y our applicat ions w it h t hose of ot hers who subscribe t o t he XSD specificat ion. Client - side form at t ing perm it s t he XML form at t ing of SQL Serv er r owset s on t he I I S ser ver rat her t han t he dat abase serv er. This offers pot ent ial scalabilit y advant ages because m ult iple v irt ual dir ect or ies from differ ent I I S serv ers can point t o t he sam e dat abase on a dat abase serv er. I n addit ion, client - side for m at t ing r em oves processing from a dat abase serv er t hat m ight hav e ot her pr ocessing requirem ent s besides t hose for one or m or e I I S serv ers. Two new dat a access com ponent s enhance XML processing capabilit ies. First , t he SQLXMLOLEDB prov ider facilit at es m ult iple obj ect iv es, including client - side form at t ing and Act iv eX Dat a Obj ect s ( ADO) access t o Web Release 2 funct ionalit y. SQLXMLOLEDB isn’t a dat a prov ider; y ou use it in com binat ion w it h SQLOLEDB, t he SQL Serv er ADO dat a prov ider. Second, SQLXML Managed Classes explicit ly expose t he Web Release 2 obj ect m odel, SQLXML 2.0, t o t he .NET Fram ework . By using t hese m anaged classes, Visual Basic .NET dev elopers can apply DiffGram s as an alt er nat ive t o Updat egr am s for dat a m anipulat ion t asks. Side- by- side inst allat ion allows Web Release 2 t o r un on t he sam e m achine w it h Web Release 1. When using Web Release 2 in t his fashion, dev elopers need t o explicit ly r efer ence t he version t hey need for t heir applicat ions. For ex am ple, client - side pr ocessing is exclusiv ely available from v irt ual direct or ies com pliant w it h Web Release 2. Sim ilarly, t he XML Bulk Loading capabilit y is dependent on Web release. Each Web r elease has it s own dist inct DLL for im plem ent ing t he XML Bulk Loading feat ur e. You m ust regist er t he one t hat your applicat ion r equires.

W e b Re le a se I n st a lla t ion Bot h Web Release 1 and Web Release 2 ar e fully support ed r eleases for SQL Ser ver 2000. These r eleases shouldn’t be confused wit h serv ice packs t hat fix problem s. While a Web release can r em edy a problem , it s m ain goal is t o add new funct ionalit y not pr esent in an ear lier release. I n order t o inst all Web Release 1 or 2, your com put er m ust hav e inst alled SQL Ser ver 2000 RTM ( Version 8.00.194) . You can obt ain t he Web r eleases from ht t p: / / w ww.m icrosoft .com / sql/ dow nloads/ . Click t he link labeled XML For SQL Server Web Release 1 ( WR1) for Web Release 1. Click t he link labeled XML For SQL Serv er Web Release 2 ( SQLXML 2.0) for Web Release 2. You w ill t ypically be downloading t he r eleases t o a com put er equipped w it h an I I S serv er. Therefor e, you should t ak e t he norm al pr ecaut ions t o guard against acquir ing a v irus during your I nt er net connect ion t im e. Aft er y ou com plet e inst alling a Web release, y our Pr ogr am s m enu is updat ed w it h an it em for t he release. Web Release 1 adds a new m enu it em labeled Micr osoft

SQL Ser ver XML Tools, which includes a single it em —XML For SQL Docum ent at ion. This it em opens t he Soft war e Dev elopm ent Kit ( SDK) for t he package, w hich includes docum ent at ion on t he feat ures of t he r elease. Web Release 2 adds SQLXML 2.0 t o t he Pr ogram s m enu. The SQLXML 2.0 m enu cont ains t hr ee it em s: Configure I I S, SQL 2.0 Docum ent at ion, and SQLXML 2.0 Readm e. Configure I I S offers a new wizard for configur ing a v irt ual dir ect ory t o int eract wit h SQL Serv er ( updat ed fr om t he wizard in t he init ial r elease) . The inst allat ion of Web Release 2 also offers a new SQLI SAPI filt er and SQLXML DLL files for t he m iddle t ier t hat replace t he v ersions shipping w it h t he init ial r elease of SQL Serv er 2000. Cr eat ing a v irt ual direct or y w it h t he Configure I I S m enu it em perm it s y our applicat ions t o t ak e advant age of t he new feat ures enabled by t hese com ponent s. You can also use t he new w izard t o upgrade v irt ual dir ect ories t o t ake adv ant age of feat ures int r oduced w it h Web Release 2. I nst alling t he files for Web Release 2 doesn’t cause t he rem oval or ov erwr it ing of t he files for Web Release 1. I t is t his feat ur e t hat perm it s y ou t o t ap t he feat ures of eit her r elease side by side on t he sam e com put er.

XM L For m a t s a n d Sch e m a s XML is a rich and deep t echnology t hat pr om ises t o adv ance com put ing in t he first decade of t he t w ent y- first cent ur y as m uch as or m ore t han Visual Basic did in t he last decade of t he t went iet h cent ury . This sect ion deliv ers an int roduct ion t o XML- form at t ed dat a t hat part icular ly t arget s curr ent and pot ent ial applicat ions of XML w it h SQL Serv er. Subsequent sect ions will highlight how t o use XML w it h SQL Ser ver 2000; t his sect ion focuses on t hree XML t opics t hat w ill equip you t o underst and t he m at erial in t hose lat er sect ions. First I st art by descr ibing t he ov erall sy nt ax for XML docum ent s. Second I present t he basics of XML schem as as a dev ice for validat ing XML docum ent s. Third I rev iew XML annot at ed schem as as a m eans of creat ing a v iew for a SQL Ser ver dat a source.

XM L D ocum e n t s XML is especially well suit ed for r epr esent ing st r uct ur ed docum ent s, such as inv oices and row sources in a dat abase. There is an im m ense body of lit erat ur e about XML. Aside from t his sect ion, one place t o st art fam iliar izing yourself wit h XML conv ent ions for represent ing dat a is t he Wor ld Wide Web Consort ium ( W3C) sit e at ht t p: / / w ww.w3c.org/ XML. This sit e cont ains link s t o m any valuable XML resources, such as t he W3C Recom m endat ion for XML 1.0. I n addit ion, t her e are m any XML- based t echnologies, such as XML Schem a, XPat h, XSL, and XSLT. Links at t he W3C m ain Web sit e can ser ve as a st art ing point for learning about t hese r elat ed t echnologies. A t y pical XML docum ent can r epresent dat a w it h a collect ion of t ags and a st art ing declarat ion. These t ags ar e sim ilar in som e ways t o HTML t ags, but t hey differ in im port ant ways. XML t ags denot e dat a elem ent s inst ead of how t o form at dat a. HTML assigns a pr ecise m eaning t o t ags. For exam ple, t he < p> t ag m eans st art a new paragraph. XML, on t he ot her hand, doesn’t assign a predet erm ined m eaning t o a t ag. I ndeed, t he sam e t ag can hav e a differ ent m eaning in differ ent XML docum ent s, and it is even possible for one t ag t o hav e different m eanings in t he sam e XML docum ent . By using nam espaces, developers can resolv e pot ent ial conflict s when t he sam e t ag has t w o or m ore different m eanings in t he sam e docum ent . XML lit erat ur e t ypically refers t o t he t ags in a docum ent as elem ent s. An elem ent can cont ain ot her elem ent s, a dat a v alue, or bot h ot her elem ent s and a dat a

value. Elem ent s can have parent , child, and sibling r elat ionships wit h one anot her. When an elem ent cont ains anot her one, t he cont ainer elem ent is t he parent elem ent and t he cont ained elem ent is t he child elem ent . For exam ple, < ShipperI D> , < Com panyNam e> , and < Phone> t ags can be child t ags of a par ent t ag < Shippers> in an XML docum ent w it h dat a for t he Shippers t able. The t ags bet ween a part icular inst ance of < Shippers> and < / Shippers> can denot e a row in t he Shippers t able. XML docum ent s t hat cont ain m ult iple occurr ences of at least one t ag, such as < Shippers> , m ust hav e one t ag set t hat cont ains all ot her t ags, such as < root > and < / root > . This out erm ost t ag set can occur j ust once wit hin an XML docum ent . An XML t ag ( or elem ent ) can have one or m ore at t ribut es. The use of at t ribut es is opt ional. At t ribut es appear w it hin t he t ag for an elem ent , such as < Shippers> . You designat e at t ribut es wit h nam e- value pairs. The nam e denot es t he at t ribut e’s nam e, and t he value depict s it s dat a value. Dat a values can appear in eit her single or double quot at ion m ar ks follow ing an equal sign behind t he at t ribut e nam e. You can r epr esent t he dat a for a t able w it h elem ent values, at t ribut e values, or bot h. The follow ing docum ent depict s t he Shippers t able dat a from t he Nort hwind dat abase repr esent ed w it h elem ent s and no at t r ibut es. Not ice t hat t he first set of angle bracket s ( < > ) declar es t he docum ent as an XML docum ent in ver sion 1 for m at . The ut f- 8 designat ion for encoding denot es a conv ent ion for conv ert ing charact ers t o bit sequences inside a com put er. Because t he < Shippers> t ag is repeat ed t hree t im es in t he docum ent , a par ent t ag set t hat appears j ust once is necessary ; t he < root > and < / root > t ags m eet t his requir em ent . The nam e root has no special m eaning; any ot her legit im at e nam e for a t ag, such as ShippersRoot , can replace r oot . The < Shippers> t ag is t he par ent of t he < ShipperI D> , < Com panyNam e> , and < Phone> t ags. These lat t er t hr ee t ags are siblings of one anot her.





Figur e 6- 1 shows t he Shippers t able in t he XML form at for each of t he preceding XML docum ent files. The figur e reveals how t he XML appears w it hin a browser. Not ice t hat y ou can read t he dat a! Many ot her dat a form at s don’t appear so readable in a browser. XML’s charact er - based form at for represent ing dat a is one of t he advant ages of XML over ot her form at s for r epr esent ing dat a. I n t he browser view of shippers_elem ent s.xm l, you can collapse t he dat a for any indiv idual r ow in t he Shippers t able by click ing t he m inus sign ( - ) next t o t he opening < Shippers> t ag for a row. You can collapse t he dat a for all t hr ee rows by click ing t he m inus sign next t o t he opening < root > t ag for eit her docum ent . Figu r e 6 - 1 . A p air of scr ee n sh ot s illu st ra t in g t h a t u ser s ca n re a d ily e x a m in e t h e con t e n t s of a n X M L docu m e n t in a b row se r.

XM L Sche m a s An XML schem a pr ov ides a fram ework for describing t he st r uct ur e and v alidat ing t he cont ent s of an XML docum ent . Wit h an XML schem a, you can k now what values ar e legit im at e for any t ag or at t ribut e inst ance. You can also use schem as t o place const raint s on t he range of accept able values for a dat a elem ent . By specify ing cardinalit y for elem ent s wit h t he m inOccurs and m ax Occurs elem ent at t ribut es, you can specify how m any elem ent inst ances ar e legit im at e in an XML docum ent . You can addit ionally designat e w het her an elem ent has any at t ribut es, and t he relat ionships am ong elem ent s. The W3C appr ov ed on May 2, 2001, a r ecom m endat ion ( ht t p: / / www .w3.org/ 2001/ XMLSchem a) t hat serv es as t he indust ry st andard for expressing XML schem as. Dev elopers refer t o t he W3C schem a st andar d as an XSD schem a. St art ing w it h Web Release 2, SQL Ser ver adopt ed t his st andard. Befor e Web Release 2, SQL Ser ver work ed w it h XDR schem as—a precursor of t he XSD schem a. This chapt er uses exclusively XSD schem as. The follow ing scr ipt r epr esent s t he shell for a schem a. Not ice t hat an XSD schem a is an XML docum ent because it st art s w it h an XML declarat ion. This m eans t hat you can describe an XSD schem a w it h t he sam e synt ax t hat y ou use for any XML docum ent . I n addit ion, not ice t he reference t o t he nam espace at ht t p: / / www .w3.org/ XMLSchem a. This nam espace defines a set of t ags and at t ribut es for defining schem as as XML docum ent s. The xsd designat ion for t he nam espace is arbit rar y. ( For exam ple, you can use xs inst ead.) An XSD schem a can have m ore t han one nam espace r eference. Each nam espace can refer ence a different set of t ags and at t ribut es. By using a dist inct nam espace designat or for each nam espace, y ou can resolv e conflict s for ident ically nam ed t ags and at t ribut es bet ween t wo different nam espaces. The shell r efers t o t he t ags and at t ribut es wit h t he xsd nam espace designat ion. The schem a t ag or elem ent , which m arks t he beginning and end of a schem a, is fr om t he nam espace designat ed by xsd.

...

A basic underst anding of sev eral form at t ing convent ions can help you get st art ed wr it ing your own schem a ( or at least equip you t o r ead t hose w rit t en by ot hers) . Elem ent declarat ions can be for a sim ple or a com plex t ype. A com plex t y pe elem ent has at least one child elem ent or one at t ribut e. You can ex plicit ly define a child elem ent w it hin a parent elem ent or refer t o a child elem ent defined elsewhere wit hin a schem a. A sim ple t ype elem ent has neit her a child elem ent nor an at t ribut e. I n addit ion, t he declarat ion for a sim ple t ype elem ent classifies t he dat a t ype for t he elem ent according t o one of t he built - in XSD dat a t y pes. The XSD dat a t ypes generally cor respond t o SQL Serv er dat a t y pes. See t he “ Dat a Ty pe Coercions and t he sql: dat at ype Annot at ion” t opic in t he online docum ent at ion for Web Release 2 for a det ailed discussion of t he sim ilar it ies and differences bet w een SQL Serv er and XSD dat a t ypes. I n addit ion t o elem ent s, a schem a can also specify at t ribut es. According t o t he W3C convent ion, t he at t ribut es for a com plex t ype elem ent are designat ed follow ing t he specificat ions for or refer ences t o any child elem ent s. The follow ing XML docum ent is t he XSD schem a for t he shippers_elem ent s.xm l docum ent file present ed in t he preceding sect ion. Follow ing t he schem a t ag wit h t he nam espace declarat ion, t he schem a declar es a com plex elem ent t y pe for t he root t ag. The root elem ent is com plex because it has child elem ent s, nam ely, one or m ore Shippers elem ent s. The exact upper lim it for t he num ber of Shippers elem ent s wit hin t he root elem ent is unbounded. ( See t he assignm ent for m axOccurs.) The m inOccurs at t ribut e for t he choice specificat ion doesn’t appear

in t he schem a, but it s default value is 1. Ther efor e, t o allow an XML docum ent wit h no Shippers elem ent s, designat e t he value 0 for m inOccurs. Not ice t hat t he Shippers elem ent doesn’t appear nest ed w it hin t he r oot elem ent declar at ion. I nst ead, t he r oot elem ent declarat ion uses t he ref at t r ibut e t o refer t o t he Shippers elem ent . The Shippers elem ent declarat ion follows t he r oot elem ent declarat ion. The Shippers elem ent has t hree child elem ent s—ShipperI D, Com pany Nam e, and Phone. I n t he following schem a, t he declarat ions for t he child elem ent s appear nest ed w it hin t he Shippers elem ent . Each child elem ent has a dat a t y pe derived from an XSD built - in dat a t ype, such as t he int eger or st ring dat a t ype. The rest r ict ion elem ent in t he child declarat ions denot es t he dat a t ype for t he child elem ent s fr om t he built - in dat a t ype. I n t he case of t he ShipperI D elem ent , t he declarat ion lim it s t he elem ent ’s values t o int egers. I n t he case of t he Com panyNam e and Phone elem ent s, t he declar at ions lim it t he elem ent values t o st rings. I n addit ion, t he m axim um lengt h is 40 and 24 for t he Com pany Nam e and Phone elem ent s, r espect iv ely. By assigning m inOccurs t o 0, t he schem a per m it s t he Phone elem ent t o be opt ional for each Shippers elem ent . The ShipperI D and Com panyNam e elem ent s are required child elem ent s for each Shipper s elem ent .



























The next XML docum ent shows t he t em plat e for r efer encing t he preceding annot at ed schem a in an XPat h query . The quer y calls for t he ret ur n of all rows from t he j oining of t he Shippers t able w it h t he Orders t able. An excerpt fr om t he result set appears in Figur e 6- 3. The browser docum ent w indow shows t he t ransit ion from t he last few r ows for ShipperI D 1 t o t he first few rows for ShipperI D 2.

SELECT * FROM Shippers FOR XML RAW

Figur e 6- 11 illust rat es a URL point ing t o t he t em plat e file and t he corr esponding result set in t he browser. The FOR XML clause det erm ines t he XML for m at for t he result set . The br owser’s Addr ess box in Figur e 6- 11 point s t o t he ccs1 I I S serv er and t he My Nw ind v irt ual direct ory wit hin t he ser ver. The t em plat e folder has t he nam e t em plat e, but any ot her legit im at e folder nam e w ill w or k as well so long as you adj ust y our set t ings for t he v irt ual dir ect ory t o point t o it . The last it em in t he Addr ess box specifically nam es t he XML file w it h t he SELECT st at em ent . Figu re 6 - 1 1 . Th e Addr ess box sh ow s t h e form a t for re fe rr in g t o a t e m pla t e file t h a t r et u r n s all t h e r ow s from t h e Sh ippe rs t a b le .

The sim ple form at for t he Addr ess box in Figur e 6- 11 enables y ou t o r eadily creat e applicat ions t hat allow users t o t ap t he cont ent s of your dat abases from anyw her e in t he wor ld. Because t he dat a ret ur ned is in XML form at , t he cont ent s are r eadily usable by all applicat ions com pat ible wit h XML dat a. Also, you can disable running quer ies direct ly from t he URL and bet t er secure your applicat ions. Tem plat e files facilit at e sanct ioned r et rievals fr om your dat abase—nam ely, t hose t hat y ou pr epr ogram for users. I t is frequent ly desirable t o r et ur n a subset of records in a row source. This capabilit y is ev en m or e powerful w hen y ou m ak e it dy nam ic at r un t im e so t hat an applicat ion can det er m ine w hich subset t o r et urn according t o user input . Enabling t em plat e files t o r et ur n result s based on param et ers sat isfies t his requir em ent . Users can specify one or m or e par am et ers when t hey inv oke t he t em plat e file. The param et ers can det erm ine t he r esult set fr om t he t em plat e file.

Creat ing dy nam ic t em plat e files t hat accept r un- t im e input requir es an updat ed t em plat e design. I n part icular, y ou w ill need t o add a m inim um of t w o new elem ent s t o an XML t em plat e file. First , y ou need a sql: header elem ent . You can define one or m or e param et ers w it hin t his elem ent . Second, y ou need t he sql: param elem ent . Use st art ing and ending sql: param t ags for each param et er . Designat e t he param et er’s nam e as t he nam e at t ribut e for t he sql: param elem ent . You can opt ionally specify a default value for t he par am et er . I f t he user fails t o specify t he param et er at r un t im e, y our applicat ion can ex ecut e it s T- SQL st at em ent wit h it s default param et er value. Wit hin a T- SQL st at em ent , designat e t he param et er w it h a leading @ sym bol. I f y our param et er nam e is MyI D in t he sql: param elem ent , y our T- SQL st at em ent should refer t o it as @My I D.

N ot e One sql: header elem ent can cont ain m ult iple par am et er specificat ions. Reference m or e t han one param et er in a t em plat e file by adding a sql: par am elem ent wit h a unique nam e at t r ibut e for each addit ional param et er . The follow ing XML docum ent illust rat es an ex t ension of t he preceding sam ple. A WHERE clause in t he T- SQL uses a param et er t o specify which row t o r et riev e from t he Shippers t able. Your applicat ion can specify t his value at run t im e by including t he par am et er ’s nam e and value in t he URL invok ing t he t em plat e file. Not ice t hat t he sql: param elem ent nam es t he param et er MyI D, and t he WHERE clause designat es t he param et er as @MyI D. The par am et er’s default v alue is 1.

SELECT ’Shippers before an insert’ SELECT *, ’Before Insert’ as At FROM Shippers FOR XML AUTO INSERT INTO Shippers (CompanyName, Phone) VALUES (‘foo’,’(123) 45 6-7890’) SELECT ’Shippers after an insert’ SELECT *, ’After Insert’ as At FROM Shippers FOR XML AUTO DELETE FROM Shippers WHERE ShipperID > 3 SELECT ’Shippers after deleting records with ShipperID > 3’ SELECT *, ’After Delete’ as At FROM Shippers FOR XML AUTO

SELECT * FROM OrderTotalView AS Invoices FOR XML AUTO

The solut ion is desirable if y ou want a report w it h t he t ot als for all t he orders, but fr equent ly applicat ions r equire a result for j ust one it em or som e subset of it em s. To m eet t his r equirem ent , you can aggr egat e by gr oup and t hen filt er based on t he group values y our applicat ion needs t o r et urn. By r efer encing a t able- valued user - defined funct ion fr om a query in a t em plat e file, you can enable a t em plat e file t o dy nam ically filt er a rowset w it h r ows cont aining aggr egat ed values at run t im e. You can apply t he logic in t he pr eceding paragraph t o ext ending t he previous sam ple. The pr ocess requir es t wo st eps. First creat e a user- defined funct ion nam ed OrderTot alFunct ion t hat r et urns a t able based on t he I nv oices v iew. The funct ion’s SELECT st at em ent aggr egat es t he Ex t endedPrice colum n fr om t he I nv oices view by Order I D. The @My OrderI D par am et er in t he funct ion perm it s a calling rout ine t o det er m ine for which order t he funct ion r et ur ns a t able. I n t his case, t he t able consist s of a single row. Second creat e an XML t em plat e file ( t m pSelect From Aggregat ingTableUDF.xm l) t hat references t he user - defined funct ion. The t em plat e file can use t he funct ion as t he source for a SELECT st at em ent . I n addit ion, t he t em plat e file can pass a param et er t o t he funct ion t o designat e which r ow t he user- defined funct ion should ret urn. At run t im e for t he t em plat e, users can dy nam ically specify t he OrderI D of t he order for which t hey want a t ot al. Here’s t he script for cr eat ing t he user- defined funct ion. I t s SELECT st at em ent is ident ical t o t he SELECT st at em ent in t he v iew for t he pr eceding sam ple except for t he HAVI NG clause t hat filt ers t he r esult set and t he param et er, @MyOr derI D, t hat facilit at es t he r un- t im e designat ion of w hich order t o r et urn t he sum for. See t he “ Creat ing and I nv ok ing Table- Valued UDFs” sect ion in Chapt er 5 for a r ev iew of t he synt ax for user - defined funct ions. --CreateOrderTotalFunction.sql USE Northwind GO CREATE FUNCTION OrderTotalFunction(@MyOrderID int) RETURNS TABLE AS RETURN( SELECT OrderID, CAST(SUM(ExtendedPrice) AS DEC(8,2)) OrderTotal FROM Invoices GROUP BY OrderID HAVING OrderID = @MyOrderID ) GO The follow ing XML docum ent invok es t he Order Tot alFunct ion udf. This docum ent fulfills t he second st ep in t he applicat ion dev elopm ent pr ocess. I t defines a param et er nam ed MyOrderI D wit h a default value. Ther efor e, users can get a result set from r unning t he t em plat e file whet her or not t hey specify a param et er value. Figur e 6- 14 show s t he XML form at t ed result set in t he browser from a URL t hat specifies 10252 as t he param et er v alue.







I nst ead of specify ing t he new colum n values in a row as at t r ibut es of a single elem ent in t he Updat egr am , y ou can designat e new values in an elem ent - cent r ic hierarchical form at . The follow ing ex cerpt fr om updgI nsert CABDeliv ersElem ent sI n.xm l illust rat es how t o lay out t he pr eceding input dat a in a hierarchical form at . All ot her design feat ur es of t he updgI nsert CABDeliv ersElem ent s- I n.xm l Updat egram m at ch t hose in updgI nsert CABDeliv ers.xm l. The at t r ibut e- cent r ic and elem ent - cent r ic layout s generat e XML docum ent s wit h ident ical cont ent , but t he form at of t he generat ed docum ent changes bet w een t he t wo files—nam ely , updgI nsert CABDelivers.xm l vs. updgI nsert CABDeliv ersElem ent sI n.xm l.

CAB Delivers (123) 456-7890

You can r un t he elem ent - cent ric version of t he Updat egram by ent ering ht t p: / / ccs1/ m ynw ind/ t em plat e/ updgI nsert CABDeliversElem ent sI n. xm l in t he Addr ess box of a browser . As w it h XML t em plat e files, y ou designat e an I I S serv er nam e, a v irt ual dir ect or y nam e, a t em plat e folder nam e, and finally t he Updat egram filenam e. I f t he Updat egram succeeds, y our browser show s an XML

file w it h a single root elem ent cont aining t he Updat egram nam espace designat or. ( See Figure 6- 15.) Figu r e 6 - 1 5 . You in vok e an Upd a t e gr a m sim ila rly t o t h e w ay you r un a n X M L t e m plat e file .

Running t he t m pSelect AllShippers.xm l t em plat e file can confirm t he new r ow in t he Shippers t able w it h a Com pany Nam e colum n value of CAB Delivers and a Phone colum n v alue of ( 123) 456- 7890. The follow ing Updat egr am m odifies t he Phone colum n value for t he newly added r ow. I t cont ains a single set of colum n values in bot h t he updg: befor e and updg: aft er elem ent s. The values in t he updg: befor e elem ent m ust refer ence a specific row in t he Shippers t able. I f t he values denot e eit her m ore t han one row or no r ow, t he updat e fails w it h a m essage in t he br owser alert ing t he user t o t he problem . The values in t he updg: aft er elem ent include bot h t he Com pany Nam e t o ident ify t he row and a new Phone value t o r eplace t he ex ist ing one. You can inv ok e updgCABDeliversNewPhone.xm l in t he sam e way t hat you ran t he first Updat egram . Because t he Updat egram m er ely ret urns a single- lined docum ent wit h t he nam e of t he Updat egram nam espace, y ou m ight want t o rerun t m pSelect AllShippers.x m l t o confirm t he updat e.







The next sam ple dem onst rat es t he synt ax for using param et ers as you add a new row t o a t able. The Updat egram declar es t he param et ers w it h updg: par am elem ent s. The sam ple designat es t wo param et ers nam ed Com panyNam e and Phone. The code doesn’t assign default param et er v alues, so your applicat ion m ust specify values for t he param et ers at r un t im e. The updg: aft er elem ent dem onst rat es t he synt ax for r eferr ing back t o t he param et ers as you specify a new row for a t able. As you can see, t he r ule is t o use t he param et er nam e w it h a leading $.









Cha pt e r 7 . SQL Se r ve r 2 0 0 0 Se cur it y I n t hese t im es, all infor m at ion t echnology professionals, including dat abase dev elopers, hav e a pr essing need t o prot ect t heir syst em s. There is no m agic pill you can t ake t o inoculat e y our syst em s against any k ind of securit y at t ack t hat ev er did, or w ill, ex ist . Secur ing syst em s is a m at t er of learning your applicat ions and j udiciously apply ing t he securit y m easur es appropriat e for y our com put ing env ir onm ent . This chapt er exposes y ou t o t he securit y feat ures available w it h Micr osoft SQL Serv er 2000. Use t he foundat ion you get from t his chapt er as a basis for dr illing down deeper int o select ed t opics t hat you feel a need t o lear n in great er dept h. Aside from general securit y concer ns, SQL Serv er 2000 developers hav e specific reasons for needing t o k now about secur it y. For exam ple, t he only way users can connect t o a SQL Ser ver inst ance is by specify ing a login account . Aft er gaining access t o t he dat abase serv er w it h a login account , users cannot t ypically gain access t o a dat abase ex cept by hav ing a user account associat ed w it h t he login account . Furt herm ore, login account and user account m em bership in v arious fix ed and user - defined r oles enable users t o per for m server and dat abase funct ions, including m anaging secur it y for a ser ver and it s dat abases or ev en for t he dat abases on ot her serv ers. The t wo resources for t his chapt er ar e sim ilar t o t hose for Chapt er 2 t hrough Chapt er 5. The first resource is a set of T- SQL script s wit h t he sam ples in t his chapt er . These T- SQL script s oft en r efer ence t he second resource, t he Chapt er07 dat abase. I n fact , t here is a script am ong t he sam ples files for t his book t o cr eat e t he dat abase, but t he sam ples also include a copy of t he dat abase files t hem selves ( Chapt er07_dat .m df and Chapt er07_log.ldf) for y our easy reference. The script s in t his chapt er differ fr om t hose of preceding chapt ers in t hat t he login for dat abase connect ions var ies bet ween script s. By using differ ent logins, t he sam ples enable y ou t o evaluat e t he effect s of different t ypes of logins as well as underst and how t o cr eat e t hose logins.

Ove r view of SQL Ser v er Secu r it y SQL Ser ver secur it y cent ers on t he dat abase serv er, but t he m anagem ent of secur it y ext ends upwar d t o t he operat ing syst em on w hich SQL Serv er runs and downward t o client w or kst at ions t hat connect t o a com put er r unning SQL Ser ver.

Se cu r it y Accou nt s SQL Ser ver has t wo m ain k inds of securit y account s. The first k ind of secur it y account grant s access t o a dat abase serv er. SQL Serv er calls t his first k ind of secur it y account a login secur it y account or a login. The second kind of secur it y account grant s access t o a dat abase w it hin a serv er. SQL Serv er applies t he nam e user secur it y account or user t o t his k ind of secur it y account . User account s allow y our applicat ion’s users t o gain access t o t he r esources t hat y ou creat e for t heir use on a SQL Ser v er inst ance. Any one login account can have m ult iple user secur it y account s associat ed w it h it —one for each dat abase t o which t he login needs access. Ev er y SQL Ser ver inst ance m aint ains a collect ion of logins. A login is like a k ey t hat let s a user open t he door int o a SQL Serv er inst ance. There ar e t hree t ypes of logins. These t ypes of logins r elat e t o how SQL Server validat es a user. The

Windows User and Windows Group are t wo t y pes of logins t hat r elat e t o t he Windows operat ing syst em . These ar e account s validat ed by Windows. When y ou creat e a Windows User login t ype wit hin a SQL Server inst ance for a Windows user , t hat user can gain access t o t he SQL Serv er inst ance w it hout t he need t o revalidat e her cr edent ials. SQL Ser ver accept s t he Windows user account as valid for access t o SQL Serv er. The sam e general not ion applies t o a Window s Group t ype login except t hat SQL Ser ver accept s t he Windows login as valid for any m em ber of t he Window s group. SQL Ser ver can also m anage it s ow n login account s. These ar e st andar d logins. SQL Ser ver m ust m anage t he login nam e and password in t his case. While SQL Ser ver secur it y m anagem ent feat ures aren’t as rich as t hose for Windows, t her e are t im es w hen st andar d secur it y login account s are especially useful. First , if SQL Ser ver is running on an operat ing syst em ot her t han Windows NT, Windows 2000, or Windows XP, such as Windows 98, y ou m ust use st andard logins. Second, if your applicat ion has users w ho aren’t regist ered wit h a Windows dom ain serv er, t hese users r equir e SQL Serv er logins. Third, SQL Serv er logins are also necessary for com pat ibilit y wit h applicat ions cont aining dat a im port ed from dat abase v endors ot her t han Micr osoft . No m at t er what t ype of securit y login a user pr esent s, users also generally m ust hav e user securit y account s for dat abase access. The ex cept ions r elat e t o login account s w it h br oad aut hor it y and dat abases w it h guest account s for logins wit hout a user account . User secur it y account s r eside w it h each dat abase. Howev er, all user account s except t he guest account m ust relat e t o a specific login. The login t ype can be Windows User, Windows Group, or st andar d.

Au t h e nt ica t ion As t he prev ious sect ion indicat es, login secur it y account s ar e w hat client s present t o a dat abase serv er t o gain access t o t he ser ver. Aut hent icat ion is t he process by which a dat abase server accept s t he login secur it y account and det erm ines whet her it is v alid. SQL Ser ver 2000 support s t wo aut hent icat ion m odes. These are Windows Aut hent icat ion Mode, which is t he default m ode, and Mix ed Mode. For serv ers running wit h Windows Aut hent icat ion Mode, dat abase user s don’t need t o validat e t hem selv es when t hey at t em pt t o gain access t o a dat abase serv er. For serv ers running Mix ed Mode, only dat abase users wit h a SQL Ser ver login m ust subm it t heir login nam e and passwor d when at t em pt ing t o gain access t o a serv er . Dat abase users wit h a Windows User or Windows Group login t ype on t he SQL Serv er inst ance ar en’t pr om pt ed a second t im e t o validat e t heir credent ials. The default m ode allows login t o a SQL Serv er inst ance only v ia a Windows account . All dat abase users m ust m eet t wo cr it eria wit h Windows Aut hent icat ion Mode. First , dat abase users m ust have a valid Windows account w it h a dom ain serv er for eit her Windows NT or Windows 2000. Second, t he SQL Ser v er inst ance m anaging a dat abase m ust aut hor ize t he indiv idual user ’s Windows account or a Windows gr oup t o w hich t he indiv idual user belongs. Windows users w it hout an account t hat SQL Ser ver r ecognizes cannot ent er t he dat abase. When y ou inst all w it h t he default m ode, t he Windows Adm inist rat or account on t he com put er r unning t he SQL Serv er inst ance is t he serv ice account for SQL Ser ver. The inst allat ion process aut om at ically cr eat es a login for t he Adm inist rat or account t hat is a m em ber of t he sysadm in fixed serv er r ole. ( You can ov err ide t his select ion of a serv ice account .) As a result , t he login for t he Adm inist rat or account enj oy s near ly all t he pr iv ileges of t he t radit ional sa secur it y account . One cr it ical differ ence is t hat you can delet e t he login for t he Adm inist rat or account , but y ou cannot delet e t he sa login. Wit h Mixed Mode aut hent icat ion, users can log in if t hey hav e an account w it h a Windows dom ain serv er or a SQL Serv er login m aint ained by a SQL Serv er

inst ance. When y ou inst all t he dat abase server on a com put er running Windows 98 or Windows Millennium Edit ion, SQL Serv er aut om at ically r uns w it h Mix ed Mode aut hent icat ion. I f your SQL Serv er inst ance is an applicat ion serv er on a wor kgr oup inst ead of a dom ain, and one or m or e of y our client workst at ions r un Windows 98, y our serv er m ust support Mix ed Mode aut hent icat ion. This requir em ent exist s whet her or not SQL Serv er 2000 r uns on a com put er wit h eit her t he Windows 2000 or t he Windows NT operat ing syst em .

N ot e When y ou choose Mixed Mode aut hent icat ion during SQL Server 2000 inst allat ion, a r em inder appears t o assign a password t o t he sa login. Failing t o proper ly respond t o t his rem inder opens y our com put er t o unwarrant ed ent ry. Role s a n d Pe r m ission s Get t ing int o a SQL Serv er inst ance doesn’t necessarily ensur e t hat y ou can do anyt hing once you get t her e. The role m em bership of login and user secur it y account s conveys perm issions t o perform v arious t asks—bot h for t he serv er and for t he dat abases m aint ained on a serv er. Som e roles ar e fix ed—t hat is, specified by SQL Serv er. Two collect ions of fix ed r oles ar e t he fix ed ser ver roles and fixed dat abase roles. SQL Ser ver also perm it s t he cr eat ion of user - defined roles. Wit h user - defined r oles, you can assign t he pr ecise perm issions t hat an applicat ion requir es. Perm issions ar e of t wo general t ypes. First , you can assign perm issions for dat abase obj ect s, such as t he abilit y t o select rows fr om a t able or v iew. These perm issions are called obj ect perm issions. Second, you can gr ant t he aut horit y t o exercise a subset of t he T- SQL st at em ent s, such as CREATE TABLE. These perm issions are called st at em ent perm issions. The fixed serv er r oles convey perm issions for ser ver t asks, such as cr eat ing, alt er ing, and dropping dat abases or m anaging logins for ot her users and changing t heir passwords. SQL Ser ver 2000 offers eight fix ed serv er r oles. ( See Table 7- 1.) The Bulk I nsert role is new w it h SQL Serv er 2000. An indiv idual login can belong t o none, one, or m or e t han one of t hese r oles. Run t he sp_addsrvr olem em ber syst em st ored procedur e t o add a login t o a fix ed server role. Dat abase users inherit t he perm issions for any fix ed serv er r oles t o which t heir logins belong. You cannot dir ect ly assign a user account t o a fix ed serv er r ole. Use t he sp_helpsrvr ole syst em st or ed procedure t o obt ain a list of t he fix ed ser v er r ole nam es along w it h br ief descript ions. I nv ok e t he sp_srvroleperm ission syst em st ored procedure for a det ailed list of T- SQL st at em ent s and ser ver funct ions for which each fix ed ser ver role gr ant s perm ission. Fix ed dat abase roles conv ey right s w it hin a dat abase, such as t he abilit y t o select or change dat a as w ell as t he abilit y t o add new dat abase obj ect s. There ar e nine fix ed dat abase r oles. ( See Table 7- 2.) Use t he sp_addrolem em ber syst em st ored procedur e t o assign a user secur it y account t o a fixed dat abase r ole in t he current dat abase. The secur it y account used t o designat e m em bership in a fixed dat abase role can be a user account in t he curr ent dat abase based on a SQL Ser ver login, a Windows User login, or a Windows Gr oup login. Just as wit h t he fix ed ser ver roles, t her e ar e t w o syst em st or ed procedur es t o help you discover m ore about t he fix ed dat abase roles. I nvoke sp_helpdbfix edrole for a list ing of t he r oles w it h brief descript ions. Run sp_dbfix edr oleperm ission t o v iew t he com plet e list of perm issions associat ed wit h each fixed dat abase role. 7DEOH)L[HG6HUYHU5ROHV 5ROH1DPH

6HOHFWHG7DVNV

Sysadm in

Can per form any t ask and gain unr est rict ed access t o all dat abases

Ser veradm in

Can per form sp_configure and SHUTDOWN oper at ions

Set upadm in

Can designat e a st or ed procedur e t o run at st art up and m anage linked serv er specificat ions

Secur it yadm in Can per form sp_grant login, sp_addlogin, and sp_deny login procedur es; can also m anage dat abase cr eat ion perm issions and password changes Processadm in Can per form KI LL operat ions Dbcreat or

Can per form CREATE DATABASE, ALTER DATABASE, and DROP DATABASE operat ions

Diskadm in

Can per form sp_addum pdev ice and sp_dropdev ice procedures

Bulkadm in Can per form BULK I NSERT operat ions Besides t he nine fixed dat abase roles in Table 7- 2, anot her r ole ex ist s wit hin each dat abase: t he public r ole. All dat abase users belong t o t he public r ole and can ex ercise w hat ever perm issions t he role allows. Each dat abase has it s own set of fix ed dat abase r oles, including t he public r ole. Mem bership in a r ole wit hin one dat abase doesn’t conv ey m em bership in t he sam e role for any ot her dat abase. I n addit ion, t he public role in one dat abase can possess different per m issions t han t he public r ole in anot her dat abase.

N ot e I t ’s oft en good pract ice t o st r ip all perm issions from t he public role so t hat users in a dat abase derive no per m issions j ust from t heir abilit y t o access a dat abase. This pract ice is especially im por t ant when y ou have a guest user account in a dat abase because t he guest account , which allows dat abase access by any login, is a m em ber of t he public role. 7DEOH)L[HG'DWDEDVH5ROHV 5ROH1DPH

6HOHFWHG7DVNV

db_owner

Can per form any t ask in a dat abase

db_accessadm in

Can per form sp_grant dbaccess and sp_rev ok edbaccess procedur es

db_securit yadm in

Can per form sp_addrolem em ber and sp_dr opr olem em ber procedur es

db_ddladm in

Can ex ecut e CREATE, ALTER, and DROP st at em ent s for obj ect s in a dat abase

db_backupoperat or Can per form BACKUP DATABASE and BACKUP LOG operat ions db_dat areader

Can per form SELECT operat ions for any obj ect in a dat abase

db_dat awrit er

Can per form I NSERT, UPDATE, and DELETE operat ions for any obj ect in a dat abase

db_denydat ar eader Cannot perform SELECT operat ions for any obj ect s in a dat abase db_denydat awr it er Cannot perform I NSERT, UPDATE, or DELETE operat ions for any obj ect in a dat abase I n addit ion t o t he fix ed roles list ed in Tables 7- 1 and 7- 2, SQL Serv er perm it s t he creat ion of user- defined roles. Creat e user- defined r oles when y our securit y needs ar e m or e granular t han t hose accom m odat ed by t he fix ed roles. I nvok e t he sp_addrole syst em st or ed pr ocedure t o cr eat e a new user- defined r ole. Aft er creat ing a r ole, y ou can add m em bers t o it w it h t he sp_addrolem em ber syst em

st ored procedure. You can assign m em bers t o user- defined roles fr om t he sam e set of secur it y account s as for fix ed dat abase roles. Of course, y ou can m anage perm issions for a user - defined r ole so t hat m em bership has it s pr iv ileges! Just as wit h fix ed roles, user - defined r oles ex ist wit hin a dat abase. Two user - defined roles in differ ent dat abases wit h t he sam e nam e can hav e differ ent m em bers and conv ey differ ent perm issions wit hin each dat abase. You can m anage user- defined roles wit h any login t hat belongs t o t he sysadm in fixed serv er r ole or any user secur it y account t hat is a m em ber of t he db_ow ner or db_secur it y adm in fixed dat abase role. You can assign t w o t ypes of perm issions t o user - defined roles. The first k ind of perm issions grant s aut hor it y for dat abase obj ect s. These perm issions include SELECT, I NSERT, UPDATE, DELETE, REFERENCES, and EXECUTE. You can apply SELECT, I NSERT, UPDATE, and DELETE perm issions t o any ent ir e t able, v iew, or t able- v alued user - defined funct ion. I n addit ion, y ou can apply SELECT and UPDATE perm issions t o any subset of t he colum ns in a t able or v iew. EXECUTE perm ission pert ains t o bot h st or ed pr ocedur es and user- defined funct ions. REFERENCES allows t wo different t ypes of perm issions. First , you can use REFERENCES t o designat e perm ission t o r eference t he prim ary k ey in one t able as a m at ch for t he for eign k ey in anot her t able. Second, you can also invok e t he REFERENCES perm ission t o enable a view or a user - defined funct ion t o specify it s sources for SCHEMABI NDI NG. The REFERENCES perm ission becom es necessary for t hese cases w hen t he owner of t he t able wit h t he pr im ary k ey is differ ent from t he ow ner of t he t able wit h t he for eign key or t he ow ner of a view or user- defined funct ion is different fr om it s base t ables or v iews. The second k ind of per m issions t hat you can add t o user - defined r oles is T- SQL st at em ent perm issions. Wit h t hese perm issions, t he m em bers of y our userdefined roles can per for m t ask s such as creat ing dat abases and creat ing t ables, views, st or ed procedures, and user - defined funct ions w it hin a dat abase. The “ Managing Perm issions” t opic in Books Online includes t he full list of st at em ent s t o w hich y ou can grant perm issions. T- SQL offers t hree st at em ent s for m anaging t he perm issions t hat a role can conv ey t o it s m em bers. The GRANT st at em ent giv es a perm ission t o a r ole. The REVOKE and DENY st at em ent s can bot h disable a perm ission in a role. When t he sam e user belongs t o m ult iple roles, conflict s wit h perm issions can ex ist . A perm ission conv ey ed t hrough a GRANT st at em ent for one r ole over rides a REVOKE st at em ent for t he sam e perm ission in any ot her role. However , a DENY st at em ent ov er r ides any GRANT st at em ent . Ther efor e, a user belonging t o t w o different roles w it h a GRANT st at em ent and a DENY st at em ent for t he sam e perm ission in bot h r oles won’t hav e t he perm ission.

I n t r odu ct ion t o Specia l Se cu r it y I ssu es A var iet y of special SQL Serv er secur it y t opics fall out side t he scope of t he preceding sum m ary of secur it y account s, aut hent icat ion, r oles, and per m issions. This sect ion addr esses t hr ee of t hese t opics. First , you lear n about applicat ion roles, w hich are a cust om t ype of user - defined roles t hat can pr ov ide unique access t o a dat abase. Second, I explor e t echniques for w or king wit h linked serv ers, which let y our users fr om one SQL Ser ver inst ance r eadily connect w it h dat abase resources on anot her SQL Serv er inst ance. Third, t his sect ion includes an exam inat ion of secur it y issues relat ing t o v irt ual dir ect or ies, such as t hose review ed in Chapt er 6 for Web dat a access and m anipulat ion v ia XML.

Applica t ion Role s

Applicat ion r oles pr ov ide a dat abase access rout e t hat com plem ent s t hose afforded by SQL Serv er secur it y account s. I nst ead of accessing a dat abase wit h logins and user account s, an applicat ion r ole prov ides access t o a dat abase v ia t he applicat ion role’s nam e and password. Any dat abase user w it h perm ission t o creat e a user- defined r ole can creat e an applicat ion role wit h t he sp_addapprole syst em st ored procedur e. Aft er cr eat ing an applicat ion r ole, assign perm issions t o it in t he sam e way t hat you do for user - defined dat abase roles. Users log in t o an applicat ion r ole wit h t he sp_set approle syst em st or ed pr ocedur e. There are no role m em bership requirem ent s t o inv ok e sp_set approle. When a user invokes an applicat ion role, she loses all ot her securit y set t ings associat ed wit h a dat abase connect ion. A user m ust disconnect and reconnect t o regain st andard secur it y for her login and user secur it y account s. How ever, a user of an applicat ion r ole does enj oy any right s for t he guest user in t he cur rent dat abase or any ot her dat abase. Therefore, you m ust be careful t o adm inist er perm issions for t he guest user t o m anage t he perm issions available t hr ough an applicat ion r ole. One dist inct advant age of an applicat ion r ole is t hat it can prov ide a single point of access for a dat abase. All users m ust reference t he sam e applicat ion role nam e and password wit h sp_set appr ole t o open an applicat ion r ole. Furt herm or e, wit h t he st andard SQL Serv er secur it y feat ur es, y ou can r est rict access t o a dat abase so t hat t he only way for users t o gain access t o it is t hrough t he applicat ion role. I n t hat case, y ou can uniform ly define t he per m issions for all users of a dat abase ( except for sysadm in m em bers, w ho hav e unrest rict ed access t o all r esources on a dat abase ser ver) .

Link e d Se r ve r s Link ed serv ers are an alt er nat iv e t echnique t o t he OPENROWSET funct ion for im plem ent ing r em ot e serv er access and het erogeneous quer ies. See t he “ Views for Rem ot e and Het er ogeneous Sources” sect ion in Chapt er 4 for sam ples illust rat ing t he use of t he OPENROWSET funct ion. As t hat sect ion dem onst rat es, t his capabilit y facilit at es per form ing queries for a dat a source on anot her com put er, such as a SQL Serv er inst ance r unning on anot her com put er , or quer ies against a differ ent dat abase, such as Or acle or Access. Link ed ser vers readily support quer ies t o any dat a source for which t her e is an OLE DB dat a prov ider w it h t he proper feat ure set . Micr osoft explicit ly t est ed link ed serv ers w it h fiv e OLE DB prov iders: Micr osoft OLE DB pr ov ider for SQL Serv er , Micr osoft OLE DB pr ov ider for Jet , Microsoft OLE DB provider for Oracle, Micr osoft OLE DB prov ider for ODBC, and Micr osoft OLE DB pr ov ider for I ndex ing Serv ice. One advant age of t he OPENROWSET funct ion over a link ed ser ver is t hat t he OPENROWSET funct ion requir es only your ex ist ing user credent ials for t he r em ot e or het erogeneous dat a source. While t he sy nt ax for specify ing dist r ibut ed quer ies wit h linked ser vers is sim pler t han t hat for t he OPENROWSET funct ion, a m em ber from eit her t he sysadm in or set upadm in fix ed ser ver role m ust add t he link ed serv er befor e you can use it . Wit h t he OPENROWSET funct ion, any user w it h t he credent ials t o connect t o a rem ot e or het erogeneous dat a source can im m ediat ely m ake t he query. Your init ial query fr om a link ed serv er r equires t hr ee st eps. First y ou m ust creat e a link ed serv er refer ence on t he curr ent com put er r unning SQL Serv er 2000. Second y ou m ust m ap a login on t he cur r ent com put er t o a login for t he rem ot e or het erogeneous dat a source. Third y ou m ust specify t he query for t he rem ot e or het er ogeneous dat a source w it h a four - part nam ing conv ent ion. The first part denot es t he link ed serv er r efer ence. The second, t hird, and fourt h part s com plet e t he unique ident ificat ion of t he source; how you specify t hese part s can var y depending on t he source. The fourt h part t ypically specifies t he t able or v iew in t he t arget dat a source. Aft er t he init ial quer y, using a link ed serv er is easier t han

using t he OPENROWSET funct ion because t he link ed serv er sy nt ax is m or e st raight forward, and y ou no longer hav e t o perform t he first t wo st eps. Creat e a link ed serv er for a r em ot e or het er ogeneous dat a source w it h t he sp_addlinkedserv er syst em st ored procedure. This procedure can t ak e as m any as seven argum ent s, but you can use as few as t wo argum ent s for cr eat ing a reference t o a rem ot e SQL Server source and as few as four argum ent s for a linked ser ver point ing t o an Access dat a source. Aft er cor rect ly init ializing t he linked ser ver reference wit h t he sp_addlink edserver syst em st ored pr ocedur e, inv oke sp_addlink edsrv login for m apping logins on t he curr ent SQL Ser v er 2000 inst ance t o logins for t he rem ot e or het er ogeneous dat a source. When a user runs a query on t he local ser ver against t he link ed ser ver, t he local serv er logs in t o t he link ed serv er w it h t he credent ials specified when t he sp_addlink edsrv login syst em st ored procedur e was last r un for t he link ed serv er. You can invok e t he sp_linkedserv ers syst em st ored pr ocedur e t o it em ize in a r esult set t he link ed serv ers defined on a local serv er .

Se cu r it y for Vir t ua l D ir e ct or ie s Virt ual direct ories ar e necessary for Web dat a access t o SQL Serv er dat a sources via XML. Each dat abase t hat r equires Web access via XML m ust have a virt ual dir ect or y point ing t o it . As described in t he “ Virt ual Direct ory Managem ent ” sect ion of Chapt er 6, y ou m ust designat e a login for t he virt ual dir ect or y. All access t o t he dat abase is m apped t hrough t he login t hat y ou specify on t he Secur it y t ab of t he Pr opert ies dialog for a dir ect ory . Figur e 7- 1 shows t he Propert ies dialog box used for t he MyNwind v irt ual direct ory t hat ser ved as t he source for m ost of t he sam ples in Chapt er 6. Not ice t hat t he Secur it y t ab specifies I USR_CCS1 in t he User Nam e t ext box. The User Nam e t ext box cont ains t he login nam e for t he v irt ual dir ect ory . Select ing Windows as t he Account Type aut om at ically inst alls I USR_ser ver nam e as t he login. Windows 2000 Ser ver aut om at ically inst alls t he I USR_ser ver nam e user account . I I S aut om at ically uses t his Windows user account for anonym ous login. Since t he sam ples for Chapt er 6 r an fr om a serv er nam ed ccs1, t he dialog replaced serv er nam e w it h CCS1. Figu r e 7 - 1 . Use t h e Se cu r it y t a b for a vir t u a l dire ct or y t o spe cify t h e login by w h ich u se r s of t h e virt u a l dire ct or y w ill ga in a cce ss t o a SQL Se rve r.

I f you decide t o allow access t o your dat abase t hr ough t he I USR_serv ernam e Windows account , y ou m ust m anually cr eat e a login for t he Windows user on your SQL Serv er inst ance. Then y ou m ust creat e a user secur it y account in t he dat abase t o w hich t he v irt ual direct ory point s. Finally y ou m ust assign perm issions t o t he I USR_serv er nam e securit y account appropriat e t o t he needs of your applicat ion. For ex am ple, if y ou want t o enable br owsers t o r ead from any row source in t he dat abase, y ou can assign t he I USR_serv er nam e user account t o t he db_dat areader fix ed dat abase role. I f you have m ore r est rict ive requir em ent s, use t he T- SQL GRANT st at em ent t o specify m or e granular perm issions, such as t he abilit y t o view j ust one t able or v iew. Mak e sure t he dat abase has perm issions for t he public role t hat don’t allow t he I USR_ser vernam e account t o access t he dat abase wit h a differ ent set of perm issions t han t he one y ou specify explicit ly for t he v irt ual dir ect or y user account . When y ou decide t o per m it updat es, insert s, and delet es t o a dat abase t hr ough a virt ual ser ver, t he user securit y account for t he virt ual dir ect ory ’s login m ust enable t hese act ions. My adv ice is t o carefully r est rict t he row sources t hat y ou m ake available for updat ing over t he Web. Av oid assigning t he I USR_ser vernam e account t o t he db_dat awr it er fix ed dat abase r ole. I nst ead, assign I NSERT, UPDATE, or DELETE per m issions w it h t he T- SQL GRANT st at em ent for whichever dat abase obj ect s requir e m odificat ion ov er t he Web.

Sa m ple s for Login s a n d Use r s Login and user secur it y account s com plem ent one anot her. Recall t hat a login aut horizes access t o a ser ver, but a user account grant s access t o a dat abase on a serv er. The users of y our applicat ions t ypically need bot h t ypes of secur it y account s t o access a dat abase on a SQL Serv er inst ance. I n addit ion, t her e are t wo dist inct t ypes of logins. The sam ples in t his sect ion explore t he different k inds of logins for SQL Serv er and how t hey relat e t o user secur it y account s. All t he script s in t his sect ion ar e in t he LoginAndDr opUsers.sql sam ple file.

Add a SQL Se r ve r Login a n d Use r Recall t hat a login get s a user int o a ser ver but not necessarily int o any dat abases on t he serv er . This is because a login t ypically r equires a m at ching securit y account for each dat abase t o w hich a user is t o hav e access. Howev er, t her e are t wo ways in w hich a user can access a dat abase wit hout a user account for t he dat abase. First , t he dat abase can hav e a guest account . The user w ill t hen enj oy any perm issions assigned explicit ly t o t he guest account or indirect ly t o t he guest account t hr ough perm issions for a dat abase’s public role. Second, if a login is a m em ber of t he sysadm in fixed serv er role, it can access any dat abase on a serv er wit hout any r est rict ions on it s funct ionalit y . For t his reason, y ou want t o lim it t he num ber of logins w it h m em bership in t he sysadm in role. I f y ou need t o carefully specify how t he user of a login can int eract wit h a dat abase, y ou m ust creat e a user securit y account for t he login in t he dat abase. I nvok e t he sp_addlogin syst em st ored procedur e t o creat e a new SQL Ser ver login. Wit h t he sp_addlogin syst em st ored pr ocedur e, you can cr eat e a login t hat SQL Ser ver m anages. When users at t em pt t o gain access t o a SQL Serv er inst ance w it h t his login, t hey m ust explicit ly designat e bot h t he login nam e and it s associat ed password. To creat e a SQL Serv er login, y ou m ust be a m em ber of eit her t he sysadm in or securit yadm in fixed ser v er r ole. Any user can change her own password w it h t he sp_password syst em st or ed procedur e. Only m em bers of t he sysadm in and secur it yadm in fixed serv er r oles can invoke sp_password t o change t he password for a login differ ent fr om t heir ow n.

N ot e While a SQL Ser ver login enables a user t o connect t o a SQL Server inst ance by specifying a login nam e and password, it is t he SI D ( secur it y ident ifier) t hat SQL Ser ver uses t o ident ify and t r ack t he user. SQL Ser ver int ernally generat es a GUI D t o r epr esent t he SI D for SQL Server logins. I nvok e t he sp_grant dbaccess syst em st ored procedure t o cr eat e a user secur it y account in a dat abase for a login. Only m em bers of t he sysadm in fixed serv er r ole as well as t he db_owner and db_accessadm in fix ed dat abase r oles can run sp_grant dbaccess. Before r unning sp_grant dbaccess, m ake sur e t he dat abase cont ext is set t o t he dat abase in which you want t o creat e a user securit y account . For exam ple, inv ok e t he USE st at em ent for a dat abase nam e befor e running sp_grant dbaccess. The follow ing T- SQL script uses sp_addlogin t o creat e a new SQL Serv er login. I t is m andat or y t o specify t he @loginam e and @passwd argum ent s for t he sp_addlogin syst em st ored pr ocedure. You can opt ionally specify sever al ot her argum ent s t o change t he default set t ings deriv ed from y our SQL Serv er configurat ion. For exam ple, t he script dem onst r at es t he synt ax for designat ing a default dat abase of Chapt er07, t he sam ple dat abase for t his chapt er . I f t he script didn’t m ak e t his assignm ent for t he @defdb ar gum ent , t he default dat abase

would have been t he m ast er dat abase. The m ast er dat abase is one of t he built - in dat abases t hat SQL Ser ver uses t o adm inist er it self. While all users r equir e access t o t his dat abase, you probably don’t want t o m ake it t he default dat abase for t ypical users. Not ice t hat t he scr ipt ex plicit ly refer ences t he m ast er dat abase befor e invok ing sp_addlogin. This r efer ence isn’t st rict ly necessary since you can cr eat e a login secur it y account fr om any dat abase on a serv er . How ev er , t he sam ple script inv okes t he USE st at em ent t wo m ore t im es, and t hese t wo r efer ences are necessary . You m ust invok e t he USE st at em ent befor e r unning t he sp_grant dbaccess syst em st ored pr ocedur e. Recall t hat t his syst em st ored procedur e cr eat es a user secur it y account . Set t ing t he dat abase cont ext befor e inv ok ing sp_grant dbaccess det erm ines t he dat abase for w hich t he syst em st ored procedur e cr eat es a user secur it y account . --LoginAndDropUsers --Create a SQL Server login with access --to the Chapter07 and Northwind databases. USE master EXEC sp_addlogin @loginame = ’vbdotnet1’, @passwd= ’passvbdotnet1’, @defdb = ’Chapter07’ USE Chapter07 EXEC sp_grantdbaccess ’vbdotnet1’ USE Northwind EXEC sp_grantdbaccess ’vbdotnet1’ The vbdot net 1 login doesn’t st rict ly requir e a user secur it y account for t he Nort hwind dat abase because t his sam ple dat abase has a guest account , and t he public role for t he dat abase grant s perm issions t o all dat abase obj ect s in t he init ial version of t he dat abase. Howev er, cr eat ing a user account for t he vbdot net 1 login allows y ou t o rem ove t he guest account for t he dat abase and st ill m aint ain dat a access privileges. I n addit ion, a user account for t he vbdot net 1 login enables a dat abase designer t o fine- t une t he perm issions available t o t he login relat iv e t o ot her dat abase users.

Re m ove a SQL Se r ve r Login a n d Use r I n t he norm al course of dat abase m anagem ent , it becom es necessary t o rem ov e as well as add dat abase users. Since a SQL Ser ver dat abase user has t wo different secur it y account t ypes, y ou m ust rem ov e bot h t o flush a user com plet ely from a dat abase serv er. To pr ev ent orphaned user account s, SQL Ser v er doesn’t allow y ou t o delet e t he login for a user w it hout delet ing t he user account s associat ed wit h t hat login. Rem ov ing t he user account s wit hout elim inat ing t heir login st ill allows a user t o access a dat abase ser ver, and t he login can access any dat abases wit h a guest account .

N ot e I n addit ion t o being unable t o rem ove a login wit h one or m ore associat ed user account s, y ou cannot rem ove a login t hat is current ly in use, owns a dat abase, or owns a j ob in t he m sdb dat abase. A j ob is a sequence of st eps for aut om at ing a t ask t hat is defined in t he m sdb dat abase, one of t he built - in dat abases t hat SQL Server uses t o m anage it self. As m ent ioned prev iously, you can nev er r em ove t he sa

login from a SQL Server inst ance. Befor e y ou at t em pt t o r em ove a login, it ’s useful t o sur vey any associat ed user secur it y account s associat ed w it h t he login. This perm it s y ou t o m ak e sur e t hat you can r em ove all of t he user securit y account s associat ed w it h a login before at t em pt ing t o rem ov e t he login. I nv ok e t he sp_helplogins syst em st or ed procedur e wit h t he nam e of t he login for which you’re seek ing infor m at ion, as show n in t he follow ing code. The syst em st or ed procedur e r et urns a r esult set com prising t wo recordset s. The first r ecordset cont ains a single row for t he login t hat y ou specify. The second recordset cont ains a r ow for each user account associat ed wit h t he login nam ed as t he argum ent for t he sp_helplogins syst em st ored procedure. I f you don’t specify a login nam e as an argum ent w hen y ou inv oke sp_helplogins, t he syst em st or ed procedur e st ill r et urns t wo recordset s. Howev er, t hese recordset s ret urn inform at ion for all t he logins on t he cur rent SQL Ser ver inst ance. --Return info about a login, including --its database user accounts. EXEC sp_helplogins @LoginNamePattern=‘vbdotnet1’ Figur e 7- 2 shows t he t w o r ecordset s t hat result from running sp_helplogins vbdot net 1 aft er first inv ok ing t he scr ipt in t he preceding sect ion. The fir st recordset st art s wit h t he login nam e follow ed by a part ial display of t he login’s SI D. The nex t t wo colum ns indicat e t he default dat abase and language for t he login. The next - t o- last colum n, AUser, is yes w hen t he login has at least one corr esponding user account . The last colum n, ARem ot e, indicat es whet her t he login specifies a r em ot e login for a link ed serv er. The second recordset prov ides inform at ion about each user account for t he login. The first and t hird colum ns denot e, respect ively , t he login nam e and t he user nam e. By default , t hese are t he sam e, but you can ov er ride t his convent ion. The second colum n designat es t he dat abase t o w hich t he user account belongs. The last colum n specifies whet her t he user account is for an indiv idual user or a role. Figu r e 7 - 2 . Use t h e sp_ h e lplogin s syst e m st ore d pr oce d u r e t o le a rn a bou t a log in on a da t ab a se ser ve r .

Arm ed w it h t he inform at ion in Figure 7- 2, y ou can const ruct a T- SQL script like t he follow ing t o r em ove t he vbdot net 1 secur it y account s fr om t he serv er. St art by inv ok ing t he sp_r ev okedbaccess syst em st ored procedure in each dat abase wit h a user account for t he vbdot net 1 login. Specify t he user account nam e as t he argum ent for t he sp_r evokedbaccess syst em st or ed procedur e. Not ice t hat t he script inv ok es sp_rev ok edbaccess t wice—once in each dat abase for w hich t he vbdot net 1 login has a user account . The scr ipt closes by r unning t he sp_droplogin syst em st ored procedur e. This syst em st or ed pr ocedur e r equir es j ust one argum ent specify ing t he nam e of t he login t o r em ov e. The perm issions for rem ov ing user account s and logins m at ch t hose for adding t hem : a login at t em pt ing t o rem ov e a login m ust be a m em ber of t he sy sadm in or secur it yadm in fix ed ser ver r ole t o r un sp_droplogin. --Drop a SQL Server login, --first revoking its user accounts. USE Northwind

EXEC sp_revokedbaccess ’vbdotnet1’ USE Chapter07 EXEC sp_revokedbaccess ’vbdotnet1’ EXEC sp_droplogin @loginame = ’vbdotnet1’

Addin g a n d Re m ovin g Logins for a W in dow s Use r Managing a login based on a Windows user account for Windows NT, Windows 2000, or Windows XP is sim ilar t o m anaging a SQL Ser ver login. By a Windows user account , I m ean t he account by w hich Windows validat es a user . From a user perspect iv e, t he m ain difference is t hat a login based on a Windows user account doesn’t hav e t o specify a login and password w hen connect ing t o a SQL Ser ver inst ance. For a dat abase user w it h a login based on a Windows user account , all a user has t o do is select t he Windows Aut hent icat ion opt ion in t he Connect To SQL Serv er dialog box of Quer y Analyzer. I f t he t arget SQL Ser ver inst ance has a login for t he Windows user account , t he connect ion at t em pt succeeds. Howev er, a m em ber of t he sysadm in group m ust first creat e a login for t he Windows account in order for t he at t em pt t o succeed. The process for cr eat ing login and user secur it y account s based on a Windows user account is sim ilar t o t hat for m anaging SQL Serv er logins. When creat ing a login for a Windows user account , inv ok e t he sp_grant login syst em st or ed procedur e t o cr eat e a login for t he Windows user. When y ou designat e a login nam e for a Windows user account , t he nam e m ust hav e t w o part s delim it ed by a backslash ( \ ) . The part befor e t he backslash is t he nam e of t he Windows serv er. The part aft er t he backslash is t he nam e of t he Windows user. The sp_grant login syst em st ored pr ocedur e is analogous t o t he sp_addlogin syst em st ored procedur e. Bot h of t hese syst em st ored procedures creat e a new login. SQL Serv er saves bot h of t he logins in t he syslogins t able. SQL Ser ver also report s bot h t ypes of logins in t he sam e colum n of t he r esult set from t he sp_helplogins syst em st or ed procedur e. How ev er, t he login creat ed w it h sp_grant login is aut hent icat ed by a Windows 2000 or Windows NT serv er. When a Windows user at t em pt s t o connect , SQL Serv er st ores t he Windows secur it y ident ifier for t he Windows user . The Windows secur it y ident ifier is analogous t o t he SQL Serv er SI D. Howev er , t he Windows securit y ident ifier is m anaged by t he Windows serv er, and t he Windows secur it y ident ifier is longer t han t he SQL Ser ver SI D ( 85 byt es for Windows and 16 byt es for SQL Ser ver ) . Aft er y ou creat e a login for a Windows user account , t he login cannot connect t o any dat abase w it hout a user secur it y account unless t he dat abase has a guest account . You can creat e a user secur it y account for a login based on a Windows user account wit h t he ident ical procedure for a SQL Ser ver login. First set t he dat abase cont ext for t he user secur it y account . For exam ple, inv ok e t he USE st at em ent t o specify t he nam e of t he dat abase for which y ou want t o creat e a user account . Second r un sp_grant dbaccess wit h t he nam e of t he login as it s argum ent . The follow ing short script dem onst rat es t he synt ax for cr eat ing a login based on a Windows user account . The Windows user account resides on a Windows 2000 Ser ver nam ed CCS1. The nam e of t he account on t he Windows serv er is winvbdot net 1. The last t wo lines of t he script cr eat e a user secur it y account in t he Chapt er07 dat abase based on t he login cr eat ed wit h sp_grant login. --Create a Windows login with --access to Chapter07 database. EXEC sp_grantlogin ’CCS1\winvbdotnet1’ USE Chapter07 EXEC sp_grantdbaccess ’CCS1\winvbdotnet1’

N ot e I f t he Windows user account is for a Window s serv er t hat isn’t a dom ain ser ver but m er ely an applicat ion serv er , you m ust cr eat e a local account on a Windows NT Wor kst at ion or Windows 2000 Professional client com put er wit h t he sam e nam e and password as on t he Windows server . Rem oving t he login is a t wo- st ep pr ocess because t he login has a single user secur it y account associat ed w it h it . First r em ov e t he user account for t he Chapt er07 dat abase. The syst em st or ed procedur e for elim inat ing a user secur it y account based on a login for a Windows user account is t he sam e as for delet ing a user account based on a SQL Serv er login. Second r ev ok e t he login. When dropping a login, y ou use a differ ent sy st em st ored procedure for one based on a Windows user account t han for one creat ed by SQL Ser ver. Here is t he T- SQL code for im plem ent ing t he st eps. --Drop a Windows login with sp_revokelogin, --but first revoke its user accounts. USE Chapter07 EXEC sp_revokedbaccess ’CCS1\winvbdotnet1’ EXEC sp_revokelogin ’CCS1\winvbdotnet1’

W ho’s Using Your Applica t ion? By now, y ou should feel com fort able wit h t he idea t hat t her e are act ually t wo r easonable answer s t o t his quest ion. The fir st answer is t he login nam e. This nam e ident ifies a user as she ent er s a SQL Serv er inst ance. The second answer is t he nam e of t he user securit y account . This ident ifies a user w it hin a dat abase. I f a login doesn’t hav e a user securit y account assigned explicit ly t o it for a dat abase and t he dat abase has a guest account , t he login can ent er t he dat abase wit h t he guest user account . SQL Server 2000 offer s t wo built - in funct ions for t elling you t he login nam e and user account nam e of t he user perform ing a t ask in your dat abase. The SYSTEM_USER funct ion ret urns t he login nam e. The CURRENT_USER funct ion r et urns t he user account nam e. Befor e discussing a list ing t o clarify t he operat ion of t hese funct ions, I want t o m ent ion t he DB_NAME funct ion. When you ent er DB_NAME( ) in a SELECT st at em ent , it r et ur ns t he nam e of t he cur rent dat abase. The following short script invokes t he SYSTEM_USER and CURRENT_USER funct ions in t hree different dat abases— m ast er, Nor t hwind, and Chapt er 07. I f y ou r un t his script aft er connect ing t o a SQL Serv er inst ance wit h t he CCS1\ winvbdot net 1 login, you obt ain an ident ical result set fr om each SELECT st at em ent . However , t wo differ ent values are display ed for t he CURRENT_USER funct ion. I n t he m ast er and Nor t hw ind dat abases, t he CURRENT_USER funct ion

ret ur ns guest . I n t he Chapt er07 dat abase, t he CURRENT_USER funct ion ret urns CCS1\ winvbdot net 1. This is because t he login has a user account nam ed aft er it in t he Chapt er 07 dat abase. -Demonstrate functions telling who’s using a database. USE master SELECT DB_NAME(), SYSTEM_USER, CURRENT_USER USE Northwind SELECT DB_NAME(), SYSTEM_USER, CURRENT_USER USE Chapter07 SELECT DB_NAME(), SYSTEM_USER, CURRENT_USER

Pr oce ssin g Logins Ba se d on W in dow s Gr ou ps I n addit ion t o basing a login on an indiv idual Windows user account , you can also creat e a login for a Windows group account . The lat t er t y pe of Window s account prov ides a single nam e for r efer encing m or e t han one indiv idual Windows account . When you cr eat e a login based on a Windows group, all t he indiv idual m em bers of t he group inher it t he login assigned t o t he gr oup. I n addit ion, y ou can creat e separat e logins for a subset of t he indiv idual m em bers of a Windows group. These logins for indiv idual Windows account s com plem ent t he login based on t he Windows group account by pr ov iding an alt er nat ive r out e int o a SQL Ser ver inst ance and t he dat abases on it . The sam ple for t his sect ion works w it h a Windows group nam ed w invbdot net . The group cont ains t wo individual Windows user account s nam ed w invbdot net 1 and winvbdot net 2. All t he account s reside on a CCS1 Windows 2000 serv er. The follow ing T- SQL script shows t he code for cr eat ing dist inct logins for t he Windows group and t he indiv idual Windows account s t hat belong t o t he Windows group. Aft er t he ex ecut ion of t he script , bot h t he w inv bdot net 1 and w inv bdot net 2 users connect t o t he SQL Ser v er inst ance w it h t heir own logins as w ell as t he login for t he Windows group. I n addit ion, bot h individual Windows user account s hav e t heir own user account s in t he Chapt er07 dat abase, and t he Windows user account s m ap t o t he Chapt er07 user account for t he Windows group. --Create login for winvbdotnet Windows group. EXEC sp_grantlogin ’CCS1\winvbdotnet’ USE Chapter07 EXEC sp_grantdbaccess ’CCS1\winvbdotnet’ --Also create logins for group members individually. EXEC sp_grantlogin ’CCS1\winvbdotnet1’ EXEC sp_grantdbaccess ’CCS1\winvbdotnet1’ EXEC sp_grantlogin ’CCS1\winvbdotnet2’ EXEC sp_grantdbaccess ’CCS1\winvbdotnet2’ GO Ther e ar e act ually t w o ways t o m ak e a login unavailable for use. First , you can run t he sp_revok elogin syst em st ored procedur e as dem onst rat ed in t he preceding sect ion. This approach r em oves t he login for t he Windows user fr om t he dat abase server. Wit h t his approach in t he curr ent cont ext , r ev ok ing t he CCS1\ w invbdot net 1 Windows user login st ill per m it s t he w invbdot net 1 Windows m em ber of t he w invbdot net group t o connect t o t he dat abase serv er. This capabilit y is possible because t he Windows user can access t he dat abase serv er t hr ough t he login for t he w invbdot net Windows group.

The follow ing scr ipt shows t he sy nt ax for a second appr oach. I t denies login perm ission t o an ex ist ing login—in t his case, t he one for t he w invbdot net 1 Windows user. This approach st ill perm it s t he w invbdot net 2 Windows user t o access t he dat abase ser ver. However, by denying t he login perm ission for t he CCS1\ w invbdot net 1 login, t he script ov er rides t he abilit y of t he w invbdot net 1 Windows user t o access t he dat abase serv er t hr ough t he CCS1\ w invbdot net login. --This does not affect winvbdotnet2, --which is a member in winvbdotnet group. EXEC sp_denylogin ’CCS1\winvbdotnet1’ GO The follow ing one- line script blocks t he winvbdot net 2 Windows user fr om accessing t he dat abase serv er. The logins for t he w invbdot net 1 and w invbdot net 2 Windows users ar e st ill on t he dat abase serv er. I n addit ion, t he CCS1\ w invbdot net login st ill aut hor izes it s m em bers t o log in t o t he ser ver. A deny set t ing ( inst it ut ed by t he sp_deny login syst em st or ed procedur e) for t he indiv idual Windows account s ov er r ides t he access grant ed by t he sp_gr ant login syst em st ored procedur e for t he CCS1\ winvbdot net Windows gr oup account . This general r ule is t r ue for all perm issions. A deny set t ing ov er r ides a grant set t ing. --This does affect winvbdotnet2, --which is a member in winvbdotnet group. EXEC sp_denylogin ’CCS1\winvbdotnet2’ GO To rem ov e t he logins for t he indiv idual Window s users and t he Window s group t o which t he users belong, you should r ev ok e t he dat abase access t o t he user secur it y account s cor responding t o logins. Then you can r ev ok e t he specific logins for t he Windows users and Windows group. The follow ing scr ipt shows t he synt ax for accom plishing t hese t asks. While t he sp_denylogin syst em st ored pr ocedure disables a login fr om accessing a serv er , t his sy st em st ored pr ocedur e doesn’t rem ov e t he login from a SQL Serv er inst ance—inst ead, you need t he sp_revok elogin syst em st ored procedure t o accom plish t he t ask. --Cleanup account settings. USE Chapter07 EXEC sp_revokedbaccess ’CCS1\winvbdotnet’ EXEC sp_revokedbaccess ’CCS1\winvbdotnet1’ EXEC sp_revokedbaccess ’CCS1\winvbdotnet2’ EXEC sp_revokelogin ’CCS1\winvbdotnet’ EXEC sp_revokelogin ’CCS1\winvbdotnet1’ EXEC sp_revokelogin ’CCS1\winvbdotnet2’ GO

Sa m ple s for Assign in g Per m ission s This sect ion dem onst rat es t he essent ial T- SQL st at em ent s for organizing perm issions w it hin a dat abase. Specific t echniques ex ist for obj ect and st at em ent perm issions. I n addit ion, t he final t opic in t he sect ion rev eals how t o m anage perm issions w hen a user account can possess a perm ission dir ect ly as well as indirect ly t hrough it s m em bership in one or m ore Windows account s or SQL Ser ver roles. The sam ples in t his sect ion rely on a v ersion of t he Em ailCont act s t able. The “ Scr ipt ing Tables” sect ion of Chapt er 2 init ially present ed t he T- SQL code for t his t able. For t he purposes of t his chapt er, y ou can r e- cr eat e t his t able in t he Chapt er07 dat abase sim ply by changing t he references t o t he Chapt er02 dat abase in Chapt er 2 t o t he Chapt er07 dat abase. A copy of t he m odified code

ex ist s in t he sam ple file Creat eEm ailCont act sTable.sql for y our easy r eference. This sect ion also r elies on t he ex ist ence of t he four logins w it h t heir m at ching user securit y account s creat ed so far in t his chapt er. Recall t hat one login is a SQL Ser ver login ( vbdot net 1) , anot her t w o are Windows user logins ( CCS1\ w invbdot net 1 and CCS1\ winvbdot net 2) , and a fourt h login is a Windows group login ( CCS1\ winv bdot net ) com pr ising each of t he t wo Windows user account s. This sect ion present s t he T- SQL code for assigning perm issions t o t he user account s for t he logins. The perm issions relat e t o t he Em ailCont act s t able. Ther efor e, creat e t he Em ailCont act s t able w it h a m em ber of t he sysadm in fix ed serv er r ole, such as t he Windows Adm inist rat or user account or t he SQL Ser ver sa login.

Se le ct , I nse r t , a nd D e le t e Pe r m ission s for a Ta ble To evaluat e t he effect of perm ission assignm ent s, you w ill need t w o concurr ent act ive connect ions t o your dat abase server. Connect once as a m em ber of t he sysadm in fixed serv er role, and connect a second t im e w it h a SQL Ser ver login— nam ely, vbdot net 1. Not e t hat if you ran t he code show n ear lier t o drop t he vbdot net 1 login account , y ou’ll need t o rer un t he code t hat creat es t he account . To confirm t hat t he user account for t he vbdot net 1 login has no perm issions in t he Chapt er07 dat abase, at t em pt t o r un t he follow ing script w it h t he user account for t he login. Not ice t hat t he at t em pt ret urns an error m essage say ing, in effect , t hat SELECT perm ission is denied on t he Em ailCont act s obj ect in t he Chapt er07 dat abase. --SelectInsertDeletePermission --The SELECT succeeds if the user has --SELECT permission. USE Chapter07 SELECT * FROM EmailContacts To rem edy t he er r or condit ion, y ou need t o assign SELECT perm ission for t he Em ailCont act s t able t o t he vbdot net 1 user account . Fr om your session init iat ed by a sysadm in m em ber, run t he follow ing line of T- SQL. You m ust inv ok e t his line of code from y our session for t he sysadm in r ole m em ber. You can also always assign perm issions from a session w it h any m em ber of t he db_ow ner fixed dat abase roles. Sessions for select ed ot her user account s w ill w or k in special circum st ances; see t he “ GRANT” t opic in Books Online for det ails. Recall also t hat m em bers of t he sysadm in r ole hav e perm ission t o perform all t asks on a dat abase serv er . --Assign SELECT permission for the EmailContacts --table to the vbdotnet1 user account. GRANT SELECT ON EmailContacts TO vbdotnet1 Not ice t hat y ou can assign a SELECT perm ission wit h t he GRANT T- SQL st at em ent . The sam ple in t he preceding T- SQL st at em ent uses t he SELECT keyw ord. This k eyw ord denot es t he perm ission t o r un a SELECT st at em ent , such as t he sam ple t o select all colum ns for all rows from t he Em ailCont act s t able. You can opt ionally assign I NSERT, UPDATE, DELETE, and REFERENCES per m issions for a t able. When concurr ent ly assigning m or e t han one perm ission, delim it t he it em s in y our list of per m issions w it h com m as. Aft er t he perm issions, use t he keyw ord ON and t hen specify t he row source, which is t he Em ailCont act s t able in t his dem onst rat ion. Conclude t he GRANT st at em ent w it h t he TO k eyword followed by t he account t o w hich you ar e grant ing perm ission. The pr eceding GRANT st at em ent designat es t he user secur it y account for t he vbdot net 1 login. You can alt er nat ively specify a SQL Ser ver role for one or m or e user account s or t he user secur it y account s for a Windows user or a Windows gr oup account .

Aft er invok ing t he pr eceding GRANT st at em ent , t he session for t he vbdot net 1 user can ex ecut e a SELECT st at em ent against t he Em ailCont act s t able. Howev er, t he follow ing at t em pt s from t he vbdot net 1 connect ion t o insert a r ow and t hen delet e t he row fail w it h a pair of er r or m essages about denied I NSERT and DELETE perm issions. Again, t he problem is t hat t he vbdot net 1 user doesn’t have t he proper perm issions. --Run from Chapter07 database context for vbdotnet1 user. INSERT INTO EmailContacts VALUES(3,’Tony’, ’Hill’, ’[email protected]’) SELECT * FROM EmailContacts GO DELETE FROM EmailContacts WHERE Email1 = ’[email protected]’ SELECT * FROM EmailContacts GO Running t he follow ing st at em ent fr om t he sysadm in session enables t he vbdot net 1 user account wit h t he pr oper perm issions t o ex ecut e t he pr eceding script . Not ice t hat t he synt ax for adding m ult iple perm issions is t he sam e as for adding a single perm ission except t hat you delim it perm issions w it h a com m a. The follow ing st at em ent adds I NSERT and DELETE per m issions t o t he exist ing SELECT perm ission for t he vbdot net 1 user account . --Delimit more than one permission in a GRANT --statement by using a comma. GRANT INSERT, DELETE ON EmailContacts TO vbdotnet1 You can drop all perm issions for t he vbdot net 1 user account by r evok ing or denying t hem . When you are work ing w it h an indiv idual user account t hat doesn’t belong t o any role, you can eit her revoke or deny ex ist ing perm issions for t he account . Use t he REVOKE st at em ent w it h t he ALL k ey word t o r em ove any ex ist ing perm issions from a user account . The follow ing one- line script dem onst rat es t he synt ax for dr opping t he SELECT, I NSERT, and DELETE perm issions from vbdot net 1. --Use the ALL keyword to concurrently --drop all existing permissions. REVOKE ALL ON EmailContacts TO vbdotnet1

Pe r m ission t o Cr e a t e a Ta ble When y ou assign t he perm ission t o creat e a t able t o user account s for any login not in t he sysadm in fix ed serv er role, y ou com plicat e how an applicat ion m ust refer t o t ables. This is because all m em bers of t he sysadm in fix ed serv er r ole ar e t he dbo user. This dbo user belongs t o all dat abases. You cannot dr op t he dbo user from a dat abase—j ust as no one can dr op t he sa login fr om an inst ance of SQL Ser ver. The rules for r efer encing t ables cr eat ed by t he dbo user are differ ent t han t hose for t ables cr eat ed by any ot her dat abase user. Ev er y user can refer im plicit ly t o t ables cr eat ed by t he dbo user . When t he sam ples in t he preceding sect ion referenced Em ailCont act s, t hey im plicit ly referred t o dbo.Em ailCont act s because t he t able was cr eat ed by a m em ber of t he sysadm in fixed serv er role. SQL Serv er requires you t o explicit ly refer t o t ables creat ed by ot her users. When a user w ho doesn’t qualify as a dbo user creat es a t able, ot her users can refer t o t he t able by t he nam e of t he t able’s ow ner and t he t able’s nam e. For exam ple, if vbdot net 1, who isn’t a dbo user, cr eat es a t able nam ed Em ailCont act s in t he Chapt er07 dat abase, ot her users m ust r efer t o t he t able as

vbdot net 1.Em ailCont act s. The vbdot net 1 user can refer t o t he Em ailCont act s t able t hat it cr eat ed as eit her vbdot net 1.Em ailCont act s or j ust Em ailCont act s. Howev er, if t hat user w ant s t o reference t he dbo Em ailCont act s t able, he m ust specify dbo.Em ailCont act s. I f any ot her user, w ho didn’t herself cr eat e a t able nam ed Em ailCont act s, r efers t o a t able w it h Em ailCont act s, SQL Serv er aut om at ically int erpr et s t his as a r efer ence t o dbo. Em ailCont act s.

N ot e When y ou perm it non- dbo users t o creat e t ables, a best pract ice is always t o use t he owner qualifier when r eferring t o a t able. I f a dbo user cr eat es a t able nam ed Em ailCont act s, refer t o it as dbo.Em ailCont act s. I f a non- dbo user, such as v bdotnet 1, cr eat es a t able nam ed Em ailCont act s, refer t o it as vbdot net 1.Em ailCont act s. Because users who writ e t heir own T- SQL st at em ent s can dev iat e fr om t hese rules and t he rules lengt hen T- SQL st at em ent s in any ev ent , rest r ict t he per m ission t o creat e t ables t o t he dbo user if at all possible. The follow ing line of script shows t he sy nt ax for enabling t he vbdot net 1 user t o creat e a t able. Set t he dat abase cont ext if it isn’t already set t o t he dat abase for which you want t o grant t he perm ission. Not ice t hat t he sy nt ax for grant ing perm ission t o ex ecut e a st at em ent is slight ly different t han for an obj ect perm ission. Aft er t he GRANT k eyw ord, y ou list t he st at em ent for w hich you ar e grant ing perm ission, but t her e’s no need t o follow t his st at em ent w it h t he ON keyw ord. I n addit ion t o CREATE TABLE, y ou can refer ence CREATE DATABASE, CREATE VI EW, CREATE PROCEDURE, CREATE FUNCTI ON, and select ed ot her st at em ent s. ( See t he “ GRANT” t opic in Books Online for t he com plet e list .) As wit h grant ing obj ect per m issions, y ou can use a com m a delim it er when concur r ent ly grant ing perm ission for m or e t han one st at em ent . Close t he GRANT st at em ent wit h t he TO k eyw ord follow ed by t he nam e of t he account t hat is t o receiv e t he st at em ent perm ission. --PermissionToCreateATable --Set the database context before invoking. GRANT CREATE TABLE TO vbdotnet1 Aft er execut ing t he pr eceding GRANT st at em ent , t he vbdot net 1 user can cr eat e a t able, such as one nam ed Em ailCont act s. Because vbdot net 1 ow ns vbdot net 1.Em ailCont act s, it can aut om at ically insert and delet e r ows fr om t he t able—j ust like m em bers of t he sysadm in fix ed serv er r ole and t he db_ow ner fix ed dat abase r ole. Howev er , ow ning an obj ect doesn’t aut om at ically conv ey m em bership in any r ole. Since t he vbdot net 1 login isn’t a m em ber of t he sysadm in fixed dat abase role, t he vbdot net 1 user cannot be a dbo user. The follow ing scr ipt shows t he code for cr eat ing t he vbdot net 1.Em ailCont act s t able. Running t he script from t he session connect ion based on t he vbdot net 1 login m akes t he vbdot net 1 user t he t able’s owner . --Invoke the DROP TABLE statement if the EmailContacts --table already exists for the vbdotnet1 user. CREATE TABLE EmailContacts ( ContactID int Not Null PRIMARY KEY, FirstName nvarchar(20) NULL, LastName nvarchar(35) NULL, Email1 nvarchar (255) NULL )

List ing t he t ables from t he sysadm in session now shows t w o t ables w it h t he nam e Em ailCont act s. Use t he follow ing scr ipt t o display t he list of t ables wit h Em ailCont act s as t heir nam e locat ed in t he Chapt er07 dat abase. Figure 7- 3 shows t he r esult set from t he script . One r ow in t he result set is for t he dbo user , and t he ot her is for t he vbdot net 1 user . --List the EmailContacts tables after creating --a second one with the vbdotnet1 user. USE Chapter07 SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ’EmailContacts’

Figu r e 7 - 3 . Th e Tab le _ Sch e m a colu m n in t h e r e su lt se t from t h e I N FORM ATI ON _ SCH EM A.TABLES vie w d en ot e s a t a ble ow n e r ’s u ser n am e .

N ot e You cannot dr op a user and it s corr esponding login if t he user owns an obj ect , such as a t able, in a dat abase. I f t he obj ect s for a user are no longer required, sim ply drop t hem and t hen drop t he user and it s login. I f you requir e t he obj ect s t hat are owned by a user who m ust be dropped, inv oke t he sp_changeobj ect owner sy st em st ored procedure t o t r ansfer obj ect ownership t o a user who will rem ain in t he dat abase. Then dr op t he user and login. You can add r ows t o and delet e rows fr om t he vbdot net 1.Em ailCont act s t able wit h a script such as t he follow ing. Because t he script references t he t able wit h it s owner qualifier, you can r un t he script fr om any connect ion based on a login wit h a user hav ing perm ission t o select , insert , and delet e r ows from t he t able—for exam ple, t he dbo user or t he vbdot net 1 user . The script generat es a r esult set wit h t hr ee r ecordset s. The first recordset is em pt y because t he preceding script creat ing t he t able doesn’t insert any rows. The second r ecordset shows t he new row for Tony Hill. The t hird r ow shows t he t able em pt y again aft er t he delet ion of t he r ow for Tony Hill. --Run from Chapter07 database context. SELECT * FROM vbdotnet1.EmailContacts INSERT INTO vbdotnet1.EmailContacts VALUES(3,’Tony’, ’Hill’, ’[email protected]’) SELECT * FROM vbdotnet1.EmailContacts DELETE FROM vbdotnet1.EmailContacts WHERE Email1 = ’[email protected]’ SELECT * FROM vbdotnet1.EmailContacts

W indow s Use r s a nd Gr ou ps

Windows users t hat ar e part of Windows group account s in SQL Serv er creat e special challenges for set t ing secur it y . This is because an indiv idual Windows user account can deriv e it s perm ission for a t ask fr om m ult iple sources. Ev en if you revok e a perm ission fr om t he user account for a Windows user, t he Windows user m ay st ill be able t o perform t he t ask cont r olled by t he perm ission. This can happen because t he user account for a Windows group, t o which a Windows user belongs, grant s t he sam e perm ission r evoked for t he indiv idual Window s user account . I n fact , t his sam e scenar io applies t o SQL Ser ver user- defined roles. A SQL Ser ver account can belong t o m ult iple roles and hav e perm issions applied dir ect ly t o it . Revoking one perm ission m ay not fully close all t he r out es by which a SQL Serv er user account can der iv e perm ission t o per form t he t ask.

N ot e When work ing wit h a Windows user account t hat can belong t o a Windows group or a SQL Ser ver user account t hat can belong t o one or m or e user- defined roles, consider using a DENY st at em ent t o rem ov e a per m ission. This st at em ent block s t he per m ission t o per for m a t ask even if t he account is grant ed perm ission for t he t ask by virt ue of it s m em bership in anot her Windows gr oup or SQL Ser ver role. The sp_helpr ot ect syst em st ored pr ocedur e helps you m onit or t he per m ission assignm ent s for user account s. By default , sp_helpr ot ect r et ur ns a result set wit h t he obj ect and st at em ent perm issions for all t he user account s in all dat abases on a dat abase ser ver. You can filt er t he r esult set by specify ing select ed argum ent s. For exam ple, designat ing a dat abase in t he @nam e argum ent ret ur ns t he perm issions for j ust t hat dat abase. You can also filt er by t ype of perm ission ( obj ect or st at em ent ) , by account t o whom a perm ission is grant ed, and by w ho grant ed t he perm ission. I f you assign filt ers so t hat t he r esult set from sp_helpr ot ect is em pt y, t he pr ocedur e r et ur ns an error m essage for t he condit ion. The follow ing scr ipt t racks t he assignm ent of perm issions in t he Chapt er07 dat abase. Before t he ex ecut ion of any GRANT st at em ent in t he script , a dat abase connect ion t o t he Chapt er07 dat abase t hat is based on t he login for CCS1\ w invbdot net 1 cannot perform a SELECT st at em ent on t he dbo.Em ailCont act s t able. Aft er t he first set of GRANT st at em ent s, t he CCS1\ w invbdot net 1 user account can perform a SELECT st at em ent based on t wo dist inct perm issions. One perm ission is grant ed dir ect ly t o t he user in t he second GRANT st at em ent . The ot her perm ission is grant ed t o t he user account t hr ough t he CCS1\ w invbdot net Windows gr oup because CCS1\ w invbdot net 1 is a m em ber of t his Windows gr oup. The inv ocat ion of t he sp_helprot ect sy st em st or ed procedur e aft er t he first t hr ee GRANT st at em ent s confirm s t hese t wo perm issions and one m or e for t he CCS1\ w invbdot net 2 Windows user account . The next T- SQL st at em ent in t he script r evokes t he SELECT perm ission for t he dbo.Em ailCont act s t able for t he CCS1\ w invbdot net 1 Windows user . This rem ov es t he perm ission from t he collect ion of perm issions in t he dat abase. The execut ion of sp_helpr ot ect in t he next st at em ent confirm s t hat t he perm ission is m issing. Howev er, rem ov ing t he perm ission doesn’t block t he CCS1\ winvbdot net 1 Windows user fr om perform ing a SELECT st at em ent w it h t he dbo. Em ailCont act s t able as it s source. This is because t he CCS1\ w invbdot net 1 Windows user der ives SELECT perm ission for t he t able fr om it s m em bership in t he CCS1\ w inv bdot net Windows gr oup. Revok ing SELECT perm ission for t he CCS1\ w inv bdot net Windows group account in t he dat abase w ill block t he CCS1\ winvbdot net 1 Windows user fr om perform ing a SELECT st at em ent on t he Em ailCont act s t able. How ev er, t his act ion w ill also rem ov e SELECT perm ission for t he CCS1\ winvbdot net 2 Windows user. The script

inst ead invokes a DENY st at em ent for SELECT perm ission on t he dbo.Em ailCont act s t able for t he CCS1\ w invbdot net 1 user account . This st at em ent rest r ict s j ust t he abilit y of t he CCS1\ w invbdot net 1 Windows user t o perform a SELECT st at em ent w it h Em ailCont act s as t he source. Any ot her user in t he CCS1\ w invbdot net Windows gr oup st ill r et ains perm ission for a SELECT st at em ent against t he dbo. Em ailCont act s t able. The final execut ion of sp_helprot ect rev eals an explicit perm ission deny ing t he CCS1\ w invbdot net 1 user account fr om perform ing a SELECT st at em ent on t he dbo.Em ailCont act s t able. --DenyPermission --Before granting SELECT permissions, SELECT statements from --either CCS1\winvbdotnet1 or CCS1\winvbdotnet2 were denied. --Grant SELECT permission for dbo.EmailContacts for --a Windows group and its two individual Windows accounts. GRANT SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet] GRANT SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet1] GRANT SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet2] EXEC sp_helprotect @name=‘dbo.EmailContacts’ --After granting SELECT permission, SELECT statements from --either CCS1\winvbdotnet1 or CCS1\winvbdotnet2 were granted. --Revoke SELECT permission for dbo.EmailContacts --for CCS1\winvbdotnet1. REVOKE SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet1] EXEC sp_helprotect @name=‘dbo.EmailContacts’ --After revoking SELECT permission for CCS1\winvbdotnet1, the --account could still perform a SELECT statement for EmailContacts. --Deny SELECT permission for dbo.EmailContacts --for CCS1\winvbdotnet1. DENY SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet1] EXEC sp_helprotect @name=‘dbo.EmailContacts’ --Denying SELECT permission makes it impossible --for CCS1\winvbdotnet1 to SELECT from EmailContacts. --Clean up permission assignments. REVOKE SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet] REVOKE SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet1] REVOKE SELECT ON dbo.EmailContacts TO [CCS1\winvbdotnet2]

Cha pt e r 8 . Ove r vie w of t he .N ET Fr a m e w or k This book is aim ed at pr ofessional developers w ho have an int er est in program m ing SQL Serv er 2000 w it h Visual Basic .NET. Up t o t his point , t he book ’s focus was pr im ar ily on SQL Ser ver. I believ e t hat y ou cannot opt im ally program SQL Ser ver in any language w it hout a firm underst anding of it s basic wor k ings. Chapt ers 2 t hrough 7 provide a foundat ion in SQL Serv er t hat will serv e you especially well for dat a access and m anipulat ion t asks, as w ell as r elat ed dat a definit ion t asks. Chapt er 1 int roduces you t o beginning Visual Basic .NET and ADO.NET t echniques so t hat y ou hav e som e cont ext for underst anding how t o apply t he SQL Serv er 2000 t opics present ed in Chapt ers 2 t hr ough 7. This chapt er builds on t he init ial exposur e t o t echnologies for t he . NET Fr am ew ork t hat appears in Chapt er 1. I f you j um ped t o t his chapt er wit hout any pr ior ex posur e t o t he .NET Fram ework , now is a gr eat t im e t o look over Chapt er 1. To t ake m axim um adv ant age of Visual Basic .NET for cr eat ing SQL Ser ver solut ions, y ou need t his backgr ound. Chapt er 1 st art s t o conv ey t his background, and t his chapt er finishes t he t ask so you are ready t o dig int o t he .NET Fram ework code sam ples t hr oughout t he rest of t he book . Visual Basic .NET is one of t he core pr ogram m ing languages for t he . NET Fram ew ork, which Microsoft defines as “ a new com put ing plat form designed t o sim plify applicat ion dev elopm ent in t he highly dist ribut ed envir onm ent of t he I nt er net .” Micr osoft is t aking a whole new init iat iv e w it h t he .NET Fram ework t hat radically redefines how businesses can program and deploy solut ions as well as access resources ov er corporat e int ranet s or t he I nt ernet . I n m any present at ions on t he bet a v ersions, it was popular t o hear t hat Micr osoft w as bet t ing it s business on t he .NET Fr am ew ork . Whet her or not t his is pr ecisely t r ue, it is clear t hat Microsoft has inv est ed heav ily in pr ov iding a com pr ehensiv e new st ruct ur e for building solut ions, and t he firm has changed in a m aj or way it s m ost popular program m ing language—Visual Basic. The scope and m agnit ude of t he changes prov ide Visual Basic dat abase dev elopers wit h challenges and opport unit ies. This chapt er at t em pt s t o fam iliarize y ou w it h t he archit ect ure of t he .NET Fram ew ork and relat ed t echnologies, including ASP.NET and XML Web serv ices. See Chapt er 1 for int roduct ory m at er ial on Visual Basic .NET and ADO.NET. My goal in t his chapt er isn’t t o em pow er y ou as a program m er wit h t hese t echnologies. I nst ead, I aim t o show how t he t echnologies com plem ent one anot her. I n t he process, I feel you will dev elop an appreciat ion of w hy it is im port ant for y ou t o adopt t he .NET Fram ework and st art program m ing it wit h Visual Basic .NET. This book ’s r em aining chapt ers exam ine t he program m ing y ou use for t he t opics int roduced concept ually in t his chapt er and Chapt er 1. This chapt er cont ains a pr ogram m ing sam ple, but I put it t here j ust for reference purposes. This chapt er is about concept s—not code. ADO.NET, ASP.NET, and XML Web serv ices each are cov er ed in a separat e chapt er t hat drills down int o t echniques for dev eloping solut ions wit h t hem . Plus, t here’s anot her chapt er— Chapt er 12—on m anaging XML w it h Visual Basic .NET.

An I n t rodu ct ion t o t h e .N ET Fr a m e w or k This sect ion int roduces you t o cor e .NET Fram ewor k concept s. I t st art s wit h an ov erv iew of t he .NET Fr am ew ork archit ect ure. Next it m ov es on t o w hat ’s new

about source code com pilat ion. This is a nat ural ent ry point t o discussing how y ou m anage t he r efer encing of solut ions by client s and how t o deploy solut ions. The sect ion closes w it h brief looks at select ed .NET Fram ew ork feat ures t hat build on m at er ial cov ered earlier in t he sect ion and ar e im port ant t o how you will use .NET Fram ew ork solut ions.

.N ET Fr a m e w or k Ar chit e ct u r e Perhaps t he m ost dom inant archit ect ural elem ent of t he .NET Fram ew ork is it s com m on language runt im e. The r unt im e sit s on t op of t he operat ing sy st em . Program m ers w r it e t o t he r unt im e in any com pliant language. The runt im e ev ent ually wr it es w hat is called m anaged code t o t he specific operat ing sy st em on which it r uns. As I wr it e t his chapt er, t he operat ing syst em s t hat support t he com m on language runt im e include t hose based on t he 32- bit v ersions of Windows, including Windows 98, Windows Millennium , Windows NT, Windows 2000, and Windows XP. Micr osoft has a Window s .NET Serv er oper at ing syst em in bet a t hat likely w ill include t he .NET Fram ework . I n addit ion, y ou can ex pect t he runt im e t o pr oduce code suit able for t he fort hcom ing 64- bit version of Windows. While t he com m on language r unt im e r uns on t op of Windows syst em s, one of t he great st r engt hs of runt im e- com pliant solut ions is t heir int er operabilit y wit h ot her operat ing syst em s. This follows fr om r unt im e support for XML and XML Web serv ices. The core t echnologies for XML and XML Web serv ices r ely on indust ry wide st andards. Because ot her vendors are endorsing t hese st andards along w it h Microsoft , y ou can be assured of a level of int er operabilit y for t he solut ions t hat y ou cr eat e w it h t he r unt im e. I f v endors follow t hr ough on t heir endorsem ent s for t he st andards and y ou build y our solut ions w it h code m anaged by t he runt im e, y ou can achiev e lev els of int er operabilit y acr oss operat ing syst em s not pr ev iously enj oy ed by applicat ion dev elopers.

N ot e Learn m or e about XML in Chapt er 6 and Chapt er 12. XML Web services is t he t opic of t he closing sect ion in t his chapt er as well as t he whole of Ch a pt e r 1 3 . When y ou develop solut ions for SQL Serv er, y ou will benefit fr om t he fact t hat t he com m on language runt im e can be host ed by SQL Ser ver 7 and lat er v ersions and Micr osoft I nt er net I nfor m at ion Serv ices v ersions 4.0 and lat er ; I I S is t he Micr osoft Web serv er for Windows NT and Windows 2000. This giv es you a chance t o int egrat e t ight ly y our dat abase and Web solut ions w it h t he m anaged code generat ed by t he runt im e. For exam ple, t he .NET Fram ewor k ships w it h m anaged prov iders for SQL Ser ver and OLE DB dat a sour ces. The SQL Serv er provider offers subst ant ial per for m ance advant ages because of it s opt im izat ion for SQL Ser ver 7 and SQL Serv er 2000. I n addit ion, ASP.NET is a part of t he .NET Fram ew ork t hat I I S host s. ASP.NET is t he next generat ion of dev elopm ent t echniques for t hose creat ing solut ions w it h ASP now. I n order for ASP.NET pages t o r un, t hey m ust be com piled by t he r unt im e. ASP.NET is an int egr al part of I I S 4, j ust as I I S 3 host s t he ASP obj ect m odel. I n addit ion, ASP.NET can int eract wit h SQL Ser ver t hr ough t he .NET Fram ew ork dat a prov iders. ( See Chapt er 11.) Figur e 8- 1 shows a sim plified schem at ic of t he pat h from source code in Visual Basic .NET ( or anot her r unt im e- com pliant language) t hrough t o int eract ions w it h SQL Ser ver and browser s on a Web. The com m on language r unt im e t r anslat es t he source code t o m anaged code. This m anaged code can, in t urn, int eract wit h t he Windows operat ing syst em , SQL Serv er , and browsers. Wit h t he aid of a m anaged pr ov ider, such as t he one for SQL Serv er, y our solut ions can access and m anipulat e dat a. You can use t he ASP.NET com ponent of t he .NET Fram ework t o

creat e ASP.NET pages t hat r eside on an I I S ser ver. These pages can serv e dynam ic elem ent s t o br owsers on a Web. I n addit ion, t he pages can offer t he browsers t he opport unit y t o access and m anipulat e dat a on a SQL Ser v er . Figu re 8 - 1 . A sch e m a t ic illu st r a t ing t h e role of t h e com m on lan gu a ge ru n t im e an d it s m an ag e d cod e in in t e ra ct ing w it h t h e W ind ow s op er a t in g syst e m , SQL Se r ve r , a n d I I S.

Com pilin g Sou r ce Code The .NET Fram ew ork support s m ult iple program m ing languages in a com m on way . I n addit ion t o Visual Basic .NET, Visual St udio .NET support s t he preparat ion of source code in ot her languages, such as C# and Visual C+ + . Web developers who are used t o building solut ions in JScr ipt w ill appreciat e t he fact t hat t hey can creat e ASP.NET solut ions wit h JScript .NET. I n fact , t hese dev elopers can use JScr ipt .NET t o im plem ent solut ions across t he full range of .NET Fram ewor k capabilit ies because JScript .NET is runt im e- com pliant . I n addit ion, t hird- part y vendors ar e readying ot her languages for r unt im e com pliance. This pr oliferat ion of languages w ill offer dev elopers a wide range of opt ions in which t hey can program t he .NET Fram ework .

N ot e JScript .NET is an ext ension of t he Microsoft JScr ipt language, which was based on ECMAScript ( ECMA- 262) . ECMA is t he European Com put er Manufact urers Associat ion. JScript .NET is ex plicit ly developed for use w it h t he runt im e. Since JScr ipt .NET generally follows t he ECMAScript convent ions, it offers a st andards- based rout e t o creat ing .NET Fram ework solut ions w it h a popular script ing language am ong Web developer s. A w onderful t hing about t he .NET Fram ew or k is t hat all languages can have t he sam e capabilit ies if t hey ar e fully r unt im e- com pliant . For exam ple, Visual Basic .NET has t he sam e capabilit ies as C# ( and so does JScr ipt .NET) . I n addit ion, dev elopers in one language can fr eely use obj ect s creat ed by dev eloper s in ot her languages. This cross- language funct ionalit y wasn’t alway s easy t o im plem ent befor e t he .NET Fram ework because of slight incom pat ibilit ies in source code language com pilat ion pr ocessing. The .NET Fram ework act ually readies source code for execut ion t hrough a series of t w o com pilat ions. The first com pilat ion

conv ert s t he source code t o Microsoft I nt erm ediat e Language ( MSI L) . The second com pilat ion conv ert s MSI L t o CPU- specific code for t he com put er r unning t he code. The first com pilat ion from source code t o MSI L generat es a represent at ion of y our program t hat capt ur es it s program m ing inst ruct ions and m et adat a about t he program . The com pilat ion st or es it s out put in a port able ex ecut ion ( PE) file. MSI L is a language- independent way of expressing y our program m ing logic. The m et adat a describes t he t ypes t hat your code cr eat es as well as t heir m em bers, such as m et hods, propert ies, and ev ent s. A t ype is an elem ent , such as a class. Anot her im port ant m et adat a elem ent is t he descript ion of t he assem bly for an applicat ion. An assem bly is t he unit for st or ing a solut ion in t he .NET Fram ework . The assem bly descript ion in t he m et adat a includes an ident it y specificat ion for t he assem bly, export ed t y pes, r efer enced t ypes, and secur it y per m issions needed t o run. A r eference t o a t y pe is like a reference t o a class in a t ype library . Because t he m et adat a for an assem bly includes int ernal t ypes and ext er nally r efer enced t ypes, t here is no need for r efer ences t o t ype librar ies in Visual Basic .NET and ot her runt im e- com pliant languages. The second com pilat ion from MSI L t o m achine code readies y our code for ex ecut ion on a specific processor. The .NET Fram ework can accom plish t his wit h a Just - I n- Tim e ( JI T) com piler. JI T com pilers ar e specific t o each support ed CPU archit ect ure. JI T com pilat ion com piles t he cont ent s of t he PE file as a user references it s elem ent s dur ing a session. PE file elem ent s, such as a t y pe m em ber, ar en’t com piled unt il a user r efer ences t hem . Aft er t he init ial com pilat ion, t he runt im e aut om at ically r efers t o t he com piled v ersion, t hus reducing t he t im e t o ex ecut e t he code. This pr ocess also saves com pilat ion t im e by not com piling t hose elem ent s t hat a user doesn’t refer ence dur ing a session. Unless an adm inist rat or explicit ly designat es ot herw ise, t he com pilat ion t o m achine code exam ines t he MSI L and it s m et adat a t o det erm ine w het her it is t ype safe. The t erm t ype safe r efers t o t he fact t hat a t ype accesses only m em ory locat ions for which it has access perm ission. This securit y check allows t he .NET Fram ew ork t o enforce secur it y r est r ict ions.

Asse m blie s a nd M a n ife st s Assem blies and t heir m anifest s are an excit ing innovat ion int roduced w it h t he .NET Fr am ew ork. They are excit ing because t hey can clear ly elim inat e m any opport unit ies for .dll conflict s—popular ly referr ed t o as “ dll hell.” A .dll conflict can em erge w hen a user inst alls a new applicat ion t hat wr it es over an ex ist ing .dll file wit h a new version t hat isn’t fully backward com pat ible. I f anot her, prev iously inst alled, applicat ion relies on a t ype m em ber t hat is changed or elim inat ed in t he new .dll, t he pr ev iously inst alled applicat ion w ill fail. Assem blies and m anifest s offer a couple of workarounds t o t his pr oblem for solut ions based on COM com ponent s. A .NET Fram ework solut ion ex ist s as an assem bly of one or m ore files. These files can include t he MSI L as well as ot her r esources, such as im age files or ot her docum ent files t hat a solut ion references. An assem bly m ust include a m anifest , which cont ains m et adat a about t he assem bly. This m et adat a describes t he files in t he assem bly . I n t he case of a single- file assem bly, t he m anifest r esides wit hin t he solut ion’s .dll file, but ot herwise an assem bly ’s m anifest r esides in a separat e file. A solut ion’s assem bly can consist of up t o four t ypes of elem ent s. • • • •

The assem bly’s m anifest The MSI L code for t he solut ion The t ype m et adat a for t he MSI L code Resource files r equir ed by t he solut ion

The assem bly is t he deploym ent unit for solut ions in t he .NET Fram ew ork. Because all t he elem ent s for a solut ion can ex ist wit hin a single assem bly , y ou can deploy a solut ion by dist ribut ing t he solut ion’s assem bly of files. St or e t he assem bly as a dir ect ory or subdir ect or y on a t arget workst at ion. The com m on language r unt im e m ust be inst alled on t he work st at ion in order t o t ransfor m t he MSI L t o nat ive m achine code. This appr oach is part icular ly convenient w her e a solut ion perform s t asks t hat y ou don’t care t o share wit h ot her solut ions. Som e solut ions are ut ilit ies. When t hese ut ilit y solut ions are likely t o be a part of m any ot her solut ions, y ou can st ore t he ut ilit y solut ions in t he Global Assem bly Cache ( GAC) . There is one GAC per com put er. When y ou place an assem bly in t he GAC for shar ing by one or m ore ot her solut ions, t he shared assem bly in t he GAC m ust hav e a st rong nam e. The st r ong nam e uniquely ident ifies an assem bly in t he GAC t o avoid conflict s from t wo assem blies t hat m ay hav e t he sam e t ext for a nam e. Visual St udio .NET includes t ools t o sim plify t he cr eat ion of st rong nam es t hat are based on t he t ext for an assem bly’s nam e, it s v ersion num ber, cult ur e inform at ion, public key , and a digit al signat ure. The .NET Fram ew ork SDK discourages locat ing assem blies in t he GAC unless essent ial because it can com plicat e deploym ent and adm inist rat ion. For exam ple, deploy ing a solut ion can r equire copy ing t wo dir ect or ies—one for t he m ain assem bly and t he ot her for t he shar ed assem bly in t he GAC. I n addit ion, t he GAC resides in t he syst em direct ory. This direct ory oft en has r est rict ed access. These access rest r ict ions m ay necessit at e perm issions for copy ing an assem bly t o t he GAC t hat t he user inst alling an applicat ion doesn’t hav e.

D e ploy a Solut ion—X COPY a Folde r You can creat e .NET Fram ew ork solut ions for Windows t hat are t ot ally self- cont ained in a single folder . When y ou cr eat e a .NET Fr am ework solut ion using t he Windows Applicat ion t em plat e, Visual St udio .NET by default cr eat es a folder for your solut ion in t he last direct or y in w hich you sav ed a previous solut ion. This folder has a root folder and at least t wo subfolders—bin and obj . You can st or e t he resour ces for your solut ions, such as cust om classes, im age files, and XML schem a files, anywhere y ou need in t he r oot folder ( or even out side t he root ) . The advant age of st oring all files for a solut ion in t he r oot folder , or any of it s subfolders, is t hat you can t hen deploy y our solut ion wit h an XCOPY com m and, or any equiv alent t echnique, t hat copies t he solut ion’s folder. All t he Visual Basic .NET solut ions included in t his book ’s sam ple files ar e av ailable as folders t hat you can copy t o your com put er. I f y ou copy t hem t o a m achine wit h t he proper configurat ion—for ex am ple, one wit h t he com m on language runt im e—y ou can run t he solut ions from t he folder t o w hich y ou copy t hem . While I am t alk ing about solut ion folders, it is probably wort h m ent ioning a couple of special files w it hin a solut ion folder . The solut ion’s .exe file resides in t he bin subfolder . You can launch t he solut ion by inv oking t his file. By default , t he .exe file has t he sam e nam e as t he solut ion. Therefor e, if your solut ion has t he nam e WindowsApplicat ion1, t he .exe file for

st ar t ing t he solut ion has t he nam e Window sApplicat ion1.exe. To open t he solut ion for edit ing in Visual St udio .NET, y ou can open a file wit h t he solut ion’s nam e and t he ex t ension .sln, such as WindowsApplicat ion1.sln. This file resides in t he root folder for t he solut ion.

N ot e Whet her you deploy an assem bly in a dir ect ory or in t he GAC, t here is no need t o add set t ings t o t he syst em r egist ry in or der t o be able to use t he solut ion based on t he assem bly. Just reference t he solut ion assem bly in t he client applicat ion. Se le ct e d .N ET Fr a m e w or k Fe a t u r e s Ev en from t he short int r oduct ion t o t he .NET Fram ework t o t his point , it should be clear t hat t he .NET Fram ework is m assive in scope. This sect ion present s a few of t he feat ures t hat I find m ost wort hy of br ief m ent ion and discussion. I n order t o m anage t he book’s lengt h, I leav e out m any t hat also ar e wort hy of y our considerat ion. The runt im e garbage collect or can aut om at ically m anage t he r elease of m em or y for an applicat ion, and it can cut back on t he incidence of m em ory leak s for longrunning applicat ions. This is because t he garbage collect or can aut om at ically recover m em ory for r efer ence t ypes—t hings such as classes and ar rays—t hat consum e m em ory when t here are no longer point ers t o t hem in m em ory. The runt im e garbage collect or recovers unused m em ory based on sever al r ules, one of which has t o do wit h no m ore space available for recent ly creat ed reference t ypes. The good news is t hat you no longer hav e t o worr y about clearing m em or y for inact ive refer ence t ypes. The bad news is t hat you cannot t ell pr ecisely when t he garbage collect or w ill r ecover m em ory. I n addit ion, t he collect or doesn’t w ork for unm anaged r esources, such as r efer ences t o files. I n t his case, you can invok e t he Dispose m et hod, but y ou should also disable t he garbage collect or , checking for any obj ect s explicit ly disposed of. You can inv ok e t he Syst em .GC.Suppr essFinalize m et hod for t he obj ect disposed of t o accom plish t his t ask. Anot her appr oach is t o use t he Close m et hod, which calls t he Dispose m et hod. You can also use t he Close m et hod t o prom pt ly r em ove select ed m anaged obj ect s, such as SQL Ser ver dat abase connect ion obj ect s. Alt hough t he garbage collect or w ill ev ent ually r em ove such m anaged it em s as SQL Serv er connect ion obj ect s, you can im pr ove t he responsiveness of y our applicat ions by elim inat ing t hem when you k now t hey are no longer needed. Nam espaces ar e a m eans of organizing and refer r ing t o groups of elem ent s in t he runt im e. I n addit ion, y our own cust om applicat ions hav e nam espaces—by default , t hese nam espaces bear t he solut ion’s nam e. The .NET Fram ew ork SDK list s t he nam es of all t he runt im e nam espaces. As a dat abase dev eloper, y ou ar e lik ely t o hav e special int er est in t he Syst em .Dat a, Syst em .Dat a.SqlClient , Syst em .Dat a.SqlTypes, and Syst em .Dat a.OleDb nam espaces. Table 8- 1 includes brief sum m aries of each of t hese nam espaces. Not ice t hat t he nam es of t he nam espaces follow a hierarchical nam ing convent ion. The Syst em .Dat a nam espace repr esent s t he br oadest gr ouping of elem ent s in Table 8- 1. The Syt em .Dat a.SqlClient , Syst em .Dat a.SqlTypes, and Syst em .Dat a.OleDb nam espaces denot e subset s of t he broader Syst em .Dat a nam espace. 7DEOH6HOHFWHG5XQWLPH1DPHVSDFHVIRU'DWDEDVH'HYHORSHUV

1DPH Syst em .Dat a

6XPPDU\ Represent s m ost ly elem ent s in t he ADO.NET archit ect ure.

Syst em .Dat a.SqlClient Represent s elem ent s in t he SQL Serv er .NET dat a prov ider . Syst em .Dat a.SqlTypes Represent s elem ent s for SQL Serv er nat iv e dat a t ypes. Syst em .Dat a.OleDb Represent s elem ent s in t he OLE DB .NET dat a prov ider . The nam espaces parallel t he k ind of funct ionalit y t hat Visual Basic developers used t o enable by adding refer ences t o t ype libr aries. You can now accom plish t he sam e t hing by using t he I m port s st at em ent for a nam espace, w here t he elem ent s in a nam espace are analogous t o t he classes and m em bers of a t ype library . Look in Chapt er 10 for code sam ples illust rat ing t he use of t he Syst em .Dat a.SqlClient nam espace. As indicat ed in a not e in t he “ Assem blies and Manifest s” sect ion, t he way t o r efer ence a serv er solut ion assem bly in a client solut ion is t o refer ence t he serv er solut ion assem bly fr om t he client assem bly. An I m port s st at em ent in t he client solut ion assem bly perm it s you t o r efer ence t he nam espace for t he serv er solut ion assem bly. Chapt er 9 dem onst rat es t he synt ax for t his st at em ent . You will find num erous code sam ples im plem ent ing t he I m port s st at em ent t hroughout t he r est of t his book . By now , y ou should underst and t hat t he .NET Fr am ew ork is t he way of t he fut ur e for t hose dev eloping solut ions w it h Microsoft pr oduct s. Nevert heless, it is likely t hat y ou eit her hav e built or ar e using solut ions based on t he pr ev ious Micr osoft dev elopm ent fram ewor k —COM. Ther efor e, Microsoft int r oduced t echnology t o help ease you t hrough t he t ransit ion per iod. For exam ple, Visual St udio .NET offers graphical t echniques for im port ing COM obj ect s wit hin .NET Fram ework solut ions. Visual St udio .NET also offers graphical t ools for export ing .NET Fram ew ork solut ions so t hey can int er operat e w it h y our prev iously creat ed COM solut ions. Because t her e are fundam ent al incom pat ibilit ies bet w een COM and t he .NET Fr am ew ork, t hese t ools don’t alw ays work perfect ly. See t he “ Tr oubleshoot ing I nt eroperabilit y” t opic in t he Visual St udio .NET Help files for an enum erat ion of som e issues t hat you m ay encount er along w it h suggest ed rem edies.

An Ove r vie w of ASP.N ET ASP.NET is a specialized com ponent of t he .NET Fram ework . You can use ASP.NET t o cr eat e Web applicat ions t hat ar e accessible fr om browsers t hat can connect t o t he page. The sam e basic t echniques ( plus som e m or e) apply t o t he creat ion of XML Web ser vices solut ions. This chapt er aim s t o or ient you t o .NET Fram ew ork Web t echnologies.

H ow D oe s ASP.N ET Re la t e t o ASP? ASP.NET is sim ilar but not ident ical t o ASP ( Act ive Ser ver Pages) . Many professional Visual Basic developers found ASP a serv iceable way t o creat e Web solut ions. One im port ant reason for t his is t hat ASP can cr eat e form s on Web pages t hat any br owser can r ead. Nevert heless, ASP has dr awbacks. For exam ple, ASP m ix es HTML page design code and program m ing logic in t he sam e file. This leads t o a t ype of spaghet t i coding t hat is difficult t o r ead and int erpr et . I n addit ion, you can cr eat e y our program m ing logic in any of a v ar iet y of languages, but pur e Visual Basic isn’t one of t hem . The closest y ou can get is VBScr ipt . Furt herm ore, t he Visual Basic developm ent env ironm ent isn’t suit able for creat ing ASP Web pages. Som e Visual Basic dev elopers adopt ed Visual

I nt erDev, and t hese dev elopers could use t he Visual I nt erDev developm ent env ir onm ent . However, t he Visual I nt erDev dev elopm ent env ironm ent had a different look and feel t han t he one for Visual Basic. As a consequence, m any dev elopers used Not epad or anot her favor it e t ext edit or t o cr eat e ASP Web pages from scrat ch.

N ot e What do y ou need t o creat e solut ions w it h ASP.NET? Fir st , you need any Windows operat ing syst em t hat inst alls I I S aut om at ically or allows you t o inst all it opt ionally. This is because I I S is t he Web serv er for ASP.NET solut ions, and it cont ains t he ASP.NET obj ect m odel, j ust as it does t he ASP obj ect m odel. Second, you need t he .NET Fr am ework. I f y ou inst alled Visual St udio .NET on your m achine, your com put er alr eady has it . Visual St udio .NET provides a friendly, fam iliar dev elopm ent environm ent for creat ing ASP.NET solut ions. Thir d, you need MDAC version 2.6 or lat er for dat a access and m anipulat ion. Visual St udio .NET inst alls MDAC version 2.7, w hich is m ore t han sufficient . However , you can download t he lat est MDAC version, free of charge, fr om t he Microsoft sit e at ht t p: / / www.m icrosoft .com / dat a/ download.ht m . I believe ASP.NET w ill becom e im m ensely popular w it h Visual Basic dev elopers because it solv es t he t hree problem s described in t he pr eceding paragr aph. •





ASP.NET separat es page design and pr ogram logic int o t w o separat e but relat ed files. This ends t he need t o m ingle HTML layout code and pr ogr am logic code in t he sam e file. You can cr eat e ASP.NET Web solut ions w it h Visual Basic. No longer do you hav e t o develop in anot her language t hat is alm ost lik e Visual Basic— nam ely, VBScr ipt . I n addit ion, t he solut ions you dev elop w it h Visual Basic .NET can int eract wit h solut ions cr eat ed by Web dev elopers cr eat ing solut ions in JScr ipt .NET because bot h languages are runt im e- com pliant . The Visual St udio .NET dev elopm ent env ironm ent has t he sam e look and feel w hen you work w it h Web Form s as it does when y ou work w it h Windows Form s. For ex am ple, y ou hav e a Toolbox. You can drag and drop cont r ols on a Web page j ust as you do w it h a Windows form . I n addit ion, t he Toolbox insulat es y ou from t he HTML sy nt ax under ly ing t he cont rols you use on Web Form s.

N ot e Visual Basic dev elopers m igrat ing t o ASP.NET from ASP m ay not ice t hat a couple of fam iliar t ools are gone. First , y ou no longer code solut ions in VBScr ipt —as indicat ed above, y ou can creat e bot h Window s and Web solut ions wit h Visual Basic .NET. Second, Visual I nt erDev is gone t oo. Now y ou can use t he sam e Visual St udio .NET developm ent env ironm ent for Windows and Web solut ions. I f y ou are a Visual Basic

dev eloper who has been wait ing unt il t he t im e w as right t o do Web dev elopm ent , com e on in—developing for t he Web will feel fam iliar and be j ust as m uch fun t o const r uct as Windows applicat ions. I f you ar e a Visual Basic dev eloper who is experienced at Web developm ent , t her e’s no bet t er t im e t han r ight now t o drast ically speed up y our Web dev elopm ent cy cles by t aking advant age of ASP.NET. Ther e is anot her cr it ical difference bet w een ASP.NET and ASP t hat m er it s your at t ent ion. ASP.NET is com piled, and ASP code is int erpret ed. Com piled code r uns fast er, so y ou ar e lik ely t o enj oy perform ance benefit s w hen you ar e r unning t he com piled code. Of course, t he first t im e ASP.NET uses a m odule, t here is a delay associat ed wit h t he com pilat ion of t he code. As a dev eloper , y ou w ill lik ely encount er t his com pilat ion delay m uch m or e t han y our users sim ply because your j ob is t o fine- t une t he code for opt im al perform ance. Each fine- t uning adj ust m ent requir es a new com pilat ion. I n spit e of all t he differences bet ween ASP and ASP.NET, t here ar e m any sim ilar it ies. You can r un ASP and ASP.NET pages side by side on t he sam e Web serv er. Your ASP Web pages hav e an .asp ext ension. Your ASP.NET pages will t ypically hav e an .aspx ext ension. This side- by - side capabilit y allows y ou t o gradually int roduce new funct ionalit y wit h ASP. NET int o a prev iously ex ist ing solut ion init ially creat ed wit h ASP. Select ed obj ect s, such as Applicat ion and Session, ex ist in bot h ASP and ASP.NET. Applicat ion obj ect s serv e as global var iables across an applicat ion. When y ou need t o m ak e sur e t hat som e values are available t o all users of an applicat ion, Applicat ion obj ect s repr esent an opt ion. ASP.NET also offers t he ASP.NET cache as a m eans of shar ing dat a across all t he users of an applicat ion. As in t he past , Session st at e var iables allow t he shar ing of inform at ion bet ween HTTP ( Hypert ext Transport Prot ocol) request s of a browser w it hin a session. ASP.NET im prov es on t he Session var iables av ailable in ASP by allow ing you t o share Session var iables across a Web farm w it h m ult iple com put ers designed t o offer t he sam e Web applicat ion. I f an applicat ion sav es a Session v ariable in response t o an HTTP request t o one com put er in a Web farm , a second request from t he sam e user t o a differ ent com put er in t he Web farm can st ill gain access t o t hat sam e Session variable.

Cr e a t in g a n ASP.N ET W e b Applica t ion You can cr eat e a new ASP.NET solut ion by click ing t he New Proj ect link on t he Visual St udio St art Page and choosing t he ASP.NET Web Applicat ion t em plat e. When y ou do t his, Visual St udio suggest s a default locat ion for t he solut ion’s assem bly on t he local I I S serv er , such as ht t p: / / localhost / WebApplicat ion1. You can choose any ot her solut ion nam e on any ot her I I S t o w hich y ou can connect . ( You need t he .NET Fram ework inst alled on any com put er from which you plan t o run ASP.NET pages.) Clicking OK opens t wo folders—one on t he Web serv er and anot her in t he default locat ion w here Visual St udio st or es it s solut ion assem bly folders. I f t he applicat ion has t he nam e WebApplicat ion1, launching a new ASP.NET Web applicat ion cr eat es a new folder nam ed WebApplicat ion1 wit hin t he wwwr oot direct ory of t he inet pub dir ect or y.

N ot e To rem ove an ASP.NET solut ion from your com put er and elim inat e it from appearing in t he Visual St udio St ar t Page, you m ust delet e bot h of it s folder s.

When Visual St udio .NET opens your applicat ion, you see a blank page. Solut ion Explor er shows t hat t he page’s t it le is WebForm 1.aspx. You can assign a m ore m eaningful nam e for t he page’s t it le pr opert y fr om t he Propert ies window. The page init ially opens w it h a pageLayout propert y set t ing of GridLayout . This set t ing let s you align cont rols on t he Web page according t o t he gr id m ar ks. You can change t he pageLay out propert y in t he Propert ies window. The ot her possible pageLay out propert y set t ing is FlowLay out . I n t his m ode, Visual St udio arranges your cont rols from t op t o bot t om in classic Web page lay out m ode—lik e a word processor. Not ice t hat t he Solut ion Explorer and Propert ies windows serv e t he sam e k inds of funct ions for t his Web applicat ion as t hey do for ot her, non- Web, applicat ions. Choosing t he HTML t ab at t he bot t om of t he page exposes t he em pt y Web page in HTML v iew. Bet w een t he body t ags on t he Web page, not ice t he form t ags. The for m t ag has a r unat set t ing of serv er. ASP.NET pages ar e designed t o accept for m s and cont rols t hat run on t he serv er.

Addin g Con t r ols t o a n .a spx Pa ge Sw it ch back t o Design v iew by clicking t he Design t ab at t he bot t om of t he page. Choose Toolbox from t he View m enu. I f t he Toolbox isn’t open t o t he Web Form s sect ion, click t hat sect ion heading. This act ion perm it s y ou t o add Web serv er cont r ols t o y our .aspx Web page. Web ser ver cont r ols ar e highly abst ract ed for program m ing in your Visual St udio dev elopm ent env ir onm ent . They insulat e you from HTML convent ions and prov ide r icher funct ionalit y t han is available t hr ough st andard HTML form cont r ols, such as < input > elem ent s. I n addit ion, t he Web serv er cont rols offer a wider ar ray of cont rol opt ions t han is available wit h HTML for m cont r ols. For exam ple, t he Web server cont rols include a Calendar cont r ol and a configurable RadioBut t onList cont r ol. I n spit e of t he abst ract ing, Web serv er cont rols r ender HTML t o a browser .

N ot e I n som e cases, Web server cont rols r equir e client - side scr ipt ing t o per form proper ly. For t his r eason or per form ance reasons, you m ay care t o swit ch t o anot her t ype of Web cont rol for select ed applicat ions. You can add a cont r ol t o a Web form by double- click ing t he cont r ol in t he Toolbox . Then y ou can drag t he cont r ol t o w her e y ou want it on t he for m . Add a but t on cont rol and a label cont r ol from t he Toolbox t o t he Web form , WebForm 1.aspx.

Addin g Code Be hin d a n .a spx W e b Pa ge Now you’r e ready t o work w it h t he code behind t he Web form and it s cont r ols. On t he form , double- click t he but t on cont rol. This opens t he Code Edit or for t he file t hat cont ains t he form code. The filenam e is WebForm 1.aspx.vb, w hich appears on a t ab at t he t op of t he Code Edit or. You should be able t o see WebForm 1.aspx.vb in Solut ion Explor er. I f not , click t he Show All Files icon on t he Solut ion Explorer t oolbar and t hen click t he + next t o WebForm 1.aspx . ( Recall t hat Windows displays t he nam e of a t oolbar icon w hen you hold t he m ouse point er ov er it .) Ent er t he code in Figur e 8- 2 in t he Code Edit or for WebFor m 1.aspx.vb. When t he page opens init ially, t here is no code in eit her t he Page_Load or But t on1_Click ev ent procedure. The Page_Load event procedur e init ializes t he page by assigning a capt ion t o t he but t on and insert ing an em pt y st ring for t he label cont r ol. The

But t on1_Click ev ent pr ocedur e assigns t he t ext Hello Wor ld t o t he label when t he user clicks t he but t on. This page w orks in very old br owsers. For exam ple, I used I nt er net Explor er 4 t o v iew t he WebForm 1.aspx page, and it w ork ed per fect ly . The com put er running t he br owser didn’t hav e t he .NET Fram ew ork inst alled eit her . Figu re 8 - 2 . A pa ir of eve n t p roced u r e s for a n ASP.N ET W e b ap plica t ion w it h a ve r sion of t h e classic H ello W orld sa m ple.

XM L W eb Se r vices XML Web serv ices facilit at e com put er- t o- com put er int eract ion in t he sam e general way as ASP.NET facilit at es com put er- t o- hum an int eract ion t hrough an .aspx Web page. This sect ion t r ies t o acquaint y ou wit h w hy XML Web serv ices are a part of t he .NET Fram ework. The cont ent in t his sect ion pr ov ides an ov erv iew of t he t echnologies used t o im plem ent XML Web serv ices. See Ch ap t er 1 3 for m ore cov erage of XML Web serv ices, w it h specific at t ent ion t o how y ou creat e, t est , and deploy XML Web ser vices as well as how client s use an XML Web serv ice.

W ha t Ca n XM L W e b Se r vice s D o f or M e ? As a professional dev eloper, y ou should be v ery int erest ed in XML Web serv ices. This is because XML Web serv ices can expand t he r each of your ex ist ing solut ions. The m or e folk s your applicat ions serv e, t he m or e t hose applicat ions ar e wort h ( and t he m ore m oney you can m ak e fr om t hem ) . I n t he preceding sect ion on ASP.NET, you gained som e exposur e on how t o creat e classic Web solut ions w it h t he .NET Fram ework , or m ore specifically ASP.NET. I n ASP.NET, y ou creat e a Web- based solut ion for an indiv idual t o r ead and int eract wit h t hr ough a browser . Whet her users access your Web page for solv ing a problem ov er an int ranet or an ext ranet , t he end result is t hat it appears in a browser. A user, which is anot her nam e for a person, has t o do som et hing wit h it . XML Web serv ices pr ov ide an env ir onm ent for creat ing solut ions t hat m achines—not people—consum e. Howev er, bot h classic Web pages and solut ions creat ed w it h XML Web serv ices work ov er t he Web. To be m ore specific, XML Web ser v ices let m achines int eract wit h each ot her and shar e

inform at ion across t he Web. The beaut y of t he underly ing XML Web ser vices archit ect ure is t hat t he m achines can be using different operat ing syst em s ( Windows vs. Unix, for exam ple) , and t he pr ogram m ing language used t o cr eat e a solut ion on one m achine can be different fr om t he program m ing language in which a client solut ion accept s result s from a serv er ( Java vs. Visual Basic .NET) . So again, how are XML Web serv ices going t o help y ou? XML Web serv ices prom ise t o deliver univ ersal access t o y our soft ware solut ions no m at t er w hat t he operat ing syst em or pr ogram m ing language on client and ser ver m achines. I n addit ion, t his isn’t j ust a Microsoft init iat ive. I t is an open init iat ive w it h sponsors from leading soft w ar e firm s. For exam ple, one of t he t echnologies under ly ing XML Web serv ices is UDDI ( Univ ersal Descript ion, Discovery, and I nt egrat ion) . I ’ll describe t his t echnology in a m om ent , but I m ent ion it her e because it s dev elopers include I BM, I nt el, SAP, Ar iba, and Micr osoft . I n addit ion, t he t echnology rest s on indust ry st andards, such as t hose published by t he W3C ( Wor ld Wide Web Consort ium ) . By building new solut ions t hat t ak e advant age of XML Web serv ices or ret rofit t ing XML Web serv ices t o ex ist ing solut ions, you are posit ioning y our effort s t o support t he leading inform at ion t echnology firm s and t he m ost w idely accept ed com put ing indust ry st andards.

Ov e r vie w of t h e XM L W e b Se r vice s I n fr a st r u ct ur e XML Web serv ices perm it client and server m achines t o int eract w it h each ot her as if t hey wer e t wo m achines on t he sam e LAN wit h all com pat ible syst em s— except t hat t he m achines can be connect ed ov er a Web w it h incom pat ible syst em s. The client m achine can r equest t he server m achine t o perform a t ransact ion, such as m ove m oney from one account t o anot her. I n t his scenar io, t he client passes t he param et ers for t he t ransact ion. I n ret urn, t he serv er can perform t he t ransact ion and r et urn an out com e t o t he client workst at ion. Alt er nat ively, a client can request som e inform at ion fr om a serv er, such as a docum ent . The server can ret r iev e t he docum ent from it s archiv es and r et urn it t o t he client ov er popular t ransport pr ot ocols as a self- describing, t ext - based m essage ( t hink XML docum ent s described by XSD schem as) . XML Web serv ices support a couple of popular pr ot ocols—nam ely HTTP ( bot h PUT and GET m et hods) as well as SOAP ( Sim ple Obj ect Access Prot ocol) , w hich is a W3C st andard. I f I were a differ ent k ind of aut hor and t his w er e a different k ind of book, I would say XML Web serv ices dev elopers can use SOAP t o clean up t heir applicat ions. A couple of ot her r eally cool feat ur es about XML Web ser vices deser ve m ent ion. One of t hese is t hat t he t echnology helps y ou find XML Web serv ices; UDDI im plem ent s t his feat ure. The ot her bit of t echnology is a language t hat enables an XML Web serv ice r unning on a serv er t o describe it self so t hat a client m achine can det erm ine how t o int eract w it h it . This language has t he nam e Web Ser v ices Descript ion Language ( WSDL) .

A Close r Look a t t h e Un de r ly in g Te ch n ologie s The preceding ov erv iew confirm s t hat XML Web serv ices r ely on t hree m ain t echnologies. • • •

UDD I

UDDI can prov ide a way of discov ering w hat ’s available as a Web serv ice. WSDL is an XML- based gram m ar for describing XML Web serv ices. SOAP is anot her XML- based st andard, but t his st andard t arget s t he exchange of inform at ion bet ween t w o loosely coupled com put ers.

UDDI offers a direct ory serv ice capabilit y . The UDDI online dir ect ory ser vice allows firm s t o publish cont act inform at ion for t heir organizat ion, sum m ar ies of XML Web serv ices offer ed, and st andards w it h which client s m ust com ply t o access t heir XML Web ser vices. You can discover m ore about t he UDDI im plem ent at ion from it s organizat ion sit e at ht t p: / / www.uddi.org. Those w ho want t o m ak e t heir XML Web ser vices available for use can r egist er w it h a UDDI dir ect or y serv ice. Those searching for an XML Web serv ice can go t o t he UDDI sit e t o discover XML Web serv ices t hat m eet t heir search cr it er ia. The UDDI ret ur ns URLs for learning m or e about XML Web serv ices. A searcher can use t he URLs t o discover t he locat ion of one or m or e docum ent s in WSDL describing each XML Web serv ice.

N ot e At t he t im e t hat I w rit e t his chapt er , y ou can regist er y our business and it s XML Web services at ht t p: / / www.uddi.or g/ r egist er.ht m l. W SD L A WSDL docum ent describes t he t hings t hat an XML Web serv ice does. Such a docum ent also declar es wher e and how t o get t he XML Web serv ice t o m ake t hose t hings happen. A WSDL docum ent is expr essed in XML. The W3C for m al specificat ion is available at ht t p: / / www.w3c. org/ t r / wsdl. This docum ent describes and dem onst rat es t he obj ect ives of WSDL as w ell as specify ing t he XML synt ax , elem ent s, and at t r ibut es for a WSDL docum ent . A WSDL docum ent describes an XML Web serv ice in t er m s of six elem ent s, as shown in Table 8- 2. 7DEOH:6'/(OHPHQWV (OHPHQW t ypes

'HVFULSWLRQ Encloses dat a t ype definit ions used in m essages exchanged bet ween t he XML Web serv ice and it s client s.

m essage Specifies an abst ract definit ion of t he m essage exchanged bet w een t he XML Web serv ice and it s client s. port Type Refers t o an abst ract set of operat ions—each operat ion has an associat ed input m essage and one or m ore out put m essages. binding

Designat es a concr et e prot ocol and dat a form at specificat ion for operat ions and t heir m essages relat ing t o a specific port Type. The binding can be t o HTTP, SMTP ( Sim ple Mail Transport Prot ocol) , or som e ot her com m unicat ion prot ocol or m edium .

port

Designat es a single com m unicat ion endpoint , nam ely, t he com binat ion of a binding and an address, for an operat ion.

serv ice

Refers t o a collect ion of relat ed port s.

SOAP SOAP ( v ersion 1.1) is t he t hird m ain t echnology underpinning XML Web serv ices. Go t o ht t p: / / w ww.w3c.org/ t r / soap for t he st andard’s specificat ion. The SOAP st andard is an XML- based m echanism for exchanging m essages bet w een t wo com put ers. SOAP is a one- way m essaging form at for rem ot e procedure calls, but you can adapt it for a request / r esponse m essage par adigm as well as a request / m ult iresponse paradigm .

The SOAP specificat ion has t hree part s, but only t he first is m andat ory. This part designat es w hat ’s in a m essage, w ho t he m essage is for, and w het her t he m essage is opt ional or m andat ory. The SOAP specificat ion r efers t o t his first part as t he SOAP Env elope elem ent . The second and t hird part s aren’t m andat or y. I n it s second part , t he SOAP specificat ion designat es a ser ializat ion schem e for exchanging applicat ion- specific dat a t ypes t hat ar e out side t he scope of nat ive XML dat a t ypes. The t hird part of t he specificat ion denot es t echniques for r epresent ing r em ot e procedure calls and t heir responses. The client for an XML Web ser vice is lik ely t o call som e m et hod for t he XML Web ser vice. This m et hod can opt ionally require a r esponse. Visual St udio .NET perm it s you t o cr eat e XML Web serv ices and client s wit hout wor k ing int im at ely w it h t he UDDI , WSDL, and SOAP st andards. Nev ert heless, hav ing a basic grasp of t he issues t hat accom pany t hese underly ing st andards w ill equip you t o underst and bet t er how XML Web serv ices work and how t o cr eat e t hem .

Cha pt e r 9 . Cr e a t ing W indow s Applica t ion s Windows applicat ions in t he .NET Fram ew or k ar e applicat ions based on one or m or e Windows Form s. These applicat ions t arget com put ers t hat can run code behind a form and prov ide a r ich env ir onm ent t o t he user. Each form in a Windows applicat ion is an inst ance of t he Windows Form class. This class behaves sim ilar ly t o t he form s in prior v ersions of Visual Basic ( and ot her Microsoft dev elopm ent env ironm ent s such as Microsoft Access form s) , but t he Windows Form class in t he .NET Fram ework is dist inct and independent of form s in ear lier Micr osoft dev elopm ent env ir onm ent s. I t is conv enient t o t hink of t he Form class as a die or a m old from which you der ive specific inst ances of a form . Like ot her classes, t he For m class is a collect ion of propert ies, m et hods, and ev ent s. The for m s in y our Windows applicat ions inher it t he Form class propert ies, m et hods, and ev ent s. This chapt er cov ers creat ing solut ions wit h Windows applicat ions and m anaging t he Windows Form s in t hose applicat ions w it h Visual Basic .NET code. The focus of t he chapt er is on int erm ediat e t o advanced t opics, such as cr eat ing and using classes, inherit ance, ev ent pr ogram m ing, and handling run- t im e er ror s wit h st ruct ur ed except ion handling. For each t opic cov er ed, I ident ify what ’s new for t he t opic w it h Visual Basic .NET. I n som e cases, such as inherit ance and st ruct ur ed except ion handling, t he em phasis is wholly on what ’s new because Visual Basic .NET int r oduces t hese capabilit ies t o Visual Basic program m ers.

Ge t t in g St a r t e d w it h W in dow s For m s Windows Form s ex ist wit hin Windows applicat ions. You can cr eat e t he shell for a Windows applicat ion from a t em plat e w it hin Visual St udio .NET. Windows applicat ions ar e for env ironm ent s t hat find it efficient t o t ake advant age of t he local processing power of client w or kst at ions. When developing solut ions based on t he Windows Applicat ions t em plat e, you w ill oft en use Windows For m s t o m anage int eract ion w it h users and display infor m at ion t o users. This sect ion int r oduces y ou t o t he basics of creat ing and m anaging solut ions based on Windows Form s.

St a r t w it h For m 1 in a W in dow s Applica t ion You can cr eat e a Windows applicat ion fr om t he Visual St udio .NET St art Page. I f you follow ed t he inst r uct ions in Chapt er 1 for configuring Visual St udio .NET and t he St art Page, you can open t he St art Page by click ing t he Windows St art but t on, choosing Program s, select ing t he Micr osoft Visual St udio .NET folder, and t hen select ing Micr osoft Visual St udio. Begin a new Windows applicat ion by click ing New Proj ect on t he St art Page and select ing t he Windows Applicat ion t em plat e in t he New Pr oj ect dialog box . Next ent er a pr oj ect nam e, such as St art Wit hForm 1. Visual St udio assigns a default folder for your pr oj ect ’s assem bly of files, but y ou can ov err ide t he default locat ion for a solut ion’s assem bly folder by eit her t yping t he pat h t o t he alt er nat e folder or br owsing t o it . Figur e 9- 1 illust rat es t he New Pr oj ect dialog box set t o creat e a new Windows applicat ion nam ed St art Wit hForm 1 in t he VisualSt udioPr oj ect s folder of t he C: \ Docum ent s and Set t ings\ Adm inist rat or\ My Docum ent s pat h.

Figu re 9 - 1 . St ar t a W in dow s a pplica t ion by se le ct in g W ind ow s App licat ion in t h e N e w Proj e ct dia log box .

Click OK in t he New Pr oj ect dialog box t o creat e a new proj ect for a Windows applicat ion. Visual St udio opens t he Windows Form s Designer env ironm ent . The Windows Form s Designer w indow, Form 1.vb[ Design] , enables y ou t o graphically m anipulat e t he form for an applicat ion. For ex am ple, y ou can use t he Toolbox t o add cont r ols t o a form , and y ou can add code behind t he ov erall form as well as t he cont rols on a form in t he Windows Form s Designer. The ov erall Visual St udio .NET dev elopm ent env ir onm ent should show t hr ee w indows at t his point . I n addit ion t o t he Windows Form s Designer, y ou should see t o t he r ight Solut ion Explor er and t he Proper t ies w indow. Solut ion Explorer pr ov ides a t r ee view, m uch lik e Windows Explor er, of t he it em s in t he proj ect for a solut ion. The Pr opert ies window pr ov ides an int erface for m anually v iewing and updat ing t he set t ings for select ed it em s in Solut ion Explorer or t he it em s on a form . Figur e 9- 2 shows a blank form —Form 1—in t he Windows For m s Designer . Solut ion Explor er displays t he Form 1.vb file. This file cont ains your graphical design changes as well as any code behind t he form . The Assem bly .vb file in t he Visual St udio .NET dev elopm ent envir onm ent enables you t o v iew assem bly at t ribut es, such as version num bers for a solut ion. I expanded t he Refer ences folder in Solut ion Explorer so you can see select ed refer ences available t o Form 1, including t he Syst em .Windows.Form s nam espace. The Pr opert ies w indow below Solut ion Explor er shows t hat t he Text pr opert y set t ing for t he form is Form 1. You can t ype ov er t he Text propert y set t ing t o assign a m or e m eaningful capt ion t o y our form . Figu re 9 - 2 . Th e in it ia l layou t for a W in d ow s a pp lica t ion in clu de s a bla n k for m a n d a few re fe re n ce s t o se lect e d n a m esp ace s.

M a n a gin g W in dow s For m s You can m anage Windows Form s in at least t hr ee st andard ways. First , m anipulat e t he pr opert ies of t he form . Second, add cont r ols t o a form t hat facilit at es com m on t ask s, such as user input . Third, add code behind a for m . The for m in Figure 9- 2 inher it s it s init ial pr opert ies, m et hods, and ev ent s from t he Form class in t he Syst em .Windows.Form s nam espace. Use a form ’s Tex t pr opert y t o set it s capt ion, for ex am ple, from Form 1 t o My First Form . You can posit ion a for m w it h t he Deskt opLocat ion and Locat ion pr opert ies. Form class m et hods let you m anipulat e for m inst ances. Use t he ShowDialog m et hod t o open a m odal for m inst ance. When y ou open a form wit h t his m et hod, you m ust close t he form ( w it h t he Close m et hod) befor e you can nav igat e t o anot her open form . By opening a form wit h t he Show m et hod, you enable users t o nav igat e t o anot her for m w it hout closing t he m odeless form opened wit h t he Show m et hod.

N ot e A nam espace can serv e as a reference t o a t ype library in Visual Basic 6. I t is t he reference t o Syst em .Windows.For m s t hat prov ides t he proper t ies, m et hods, and event s for a Windows form , such as t he one in Figur e 9- 2. To m ake a form part of a Windows applicat ion, you can add cont rols t o t he form . I n addit ion, you can w r it e code for t he form and t he cont rols on it . Choose Toolbox fr om t he View Menu in Visual St udio .NET t o open a w indow wit h m ult iple sect ions, or t abs, from which you can drag cont rols and ot her obj ect classes ont o your form s. I n t he Windows Form s sect ion of t he Toolbox, y ou can select from several differ ent t ypes of cont rols. •





A w ide var iet y of fam iliar cont r ols facilit at e t he input and out put of inform at ion and user int eract iv it y. These fam iliar cont r ols include but t on, t ext box, com bo box, slider or t rackbar, label, t ab, radio but t on, check box, t w o differ ent list box cont r ols, and dat a grid. Anot her set of it em s in t he Windows Form s sect ion of t he Toolbox includes cont r ols t hat aren’t t ypical, but t hey st ill help y ou m anage t he way a form displays inform at ion and operat es. For exam ple, use t he Toolt ip class t o prov ide help t o a user about t he purpose of cont rols on a form when a m ouse hov ers ov er a cont r ol. Ot her special Toolbox it em s facilit at e t he creat ion and m anagem ent of m enus, t he highlight ing of cont rols w it h invalid dat a, and t he pr ov ision of Help. A final set of elem ent s in t he Windows Form s sect ion of t he Toolbox includes a num ber of com m on dialog box es. Applicat ion dev elopers can use t hese Toolbox elem ent s t o give t heir cust om applicat ions t he look and feel of a st andard Windows applicat ion. There are sev en of t hese cont rol it em s: OpenFileDialog, Sav eFileDialog, Font Dialog, ColorDialog, Print Dialog, Print Pr ev iewDialog, and PageSet upDialog.

The Toolbox m ak es ot her set s of select ions available fr om it s Com ponent s and Dat a sect ions. You can use t he Com ponent s sect ion of t he Toolbox t o insert built in com ponent s t hat ship wit h t he .NET Fram ew ork. You can also add t o t his Toolbox sect ion com ponent s t hat y ou acquir e fr om t hird- part y sources or dev elop int ernally. I will dem onst rat e t he use of one of t hese com ponent s, t he syst em t im er, lat er in t his chapt er . The Toolbox’s Dat a sect ion offers com ponent s t hat facilit at e ADO.NET t asks, such as m ak ing a connect ion wit h a dat a source, passing a SQL com m and t o a dat a source, and ret ur ning a result set fr om a dat a source. The it em s in t he Dat a sect ion of t he Toolbox are cov er ed in Chapt er 11. You can init ialize select ed form propert ies and respond t o user act ions wit h form cont r ols t hr ough event procedur es. For exam ple, in a Load event procedur e for a for m , y ou can set t he form ’s Deskt opLocat ion pr opert y. Wit h t he Click event procedur e for a but t on, you can run Visual Basic .NET code t o perfor m com m on act ions, such as opening a m essage box. Double- click a form ’s capt ion or a cont r ol on a form t o open t he Code Edit or window and display t he shell, or st ub, for an event procedure. The ev ent is t he default one for t he form or cont rol. When y ou double- click a form , Visual St udio .NET opens t he shell for t he Load ev ent procedure. You can place any code your applicat ion r equires in t he shell. The next t im e t he form opens, it runs t hat code. Double- clicking a but t on creat es t he shell for t he but t on’s Click ev ent . As w it h t he St art Page and t he Windows Form Designer, Visual St udio . NET opens t he Code Edit or on a t ab, which in t he case of Form 1 is labeled Form 1.v b. I n m ost way s, t he Code Edit or is lik e a st andard code window. You can ent er code int o

ev ent procedure shells. You can also add sub pr ocedur es and funct ion procedur es t o t he code behind a for m . You can use t he Proj ect m enu t o add ot her it em s t o a Windows applicat ion, such as m or e form s or new cust om classes. Use t he dr opdown list s at t he t op of t he Code Edit or t o select a class, such as a but t on, and t hen see all t he event s for t he class. Select ing an ev ent fr om t he list on t he r ight opens an event procedure shell for t hat event . Visual Basic .NET uses t he fam iliar nam ing convent ion of a class nam e followed by an underscore and t he ev ent nam e for event procedure shells. Ther efor e, t he Click event procedure for But t on1 has t he nam e But t on1_Click.

A W in dow s For m w it h Tw o But t on Con t r ols Dev elopers oft en place but t ons on for m s t o let users int eract wit h an applicat ion. This sect ion pr esent s a Windows applicat ion w it h t w o but t ons—one for saying hello and a second for m ov ing t he form around on t he deskt op. When t he applicat ion opens, t he Form 1_Load ev ent pr ocedur e posit ions Form 1 on t he deskt op and set s a capt ion for t he form . Form 1 is t he default st art up obj ect for a Windows applicat ion. The sam ple for t his sect ion com plet es t he St art Wit hForm 1 applicat ion init ially launched in t he com m ent ar y for Figur es 9- 1 and 9- 2. Figur e 9- 3 shows t hree ev ent procedures for t he St art Wit hForm 1 applicat ion. The ev ent procedures appear in t he Code Edit or , on t he Form 1.vb t ab. Recall t hat y ou don’t hav e t o wr it e t he shell—j ust t he cont ent s—of an event procedur e. I did insert a line cont inuat ion charact er ( _) in t he shell so y ou could see all t he code generat ed aut om at ically . Not ice a rect angle around Windows Form s Designer generat ed code. You can expand t he sect ion by click ing t he “ + ” next t o it ; click t he “ - ” next t o t he t op line of t he expanded code t o hide t he sect ion. This region cont ains code necessary for a form . For exam ple, t he code inst ant iat es an inst ance of t he Form class. The sect ion also persist s, or saves, your m anual changes t o t he form and cont r ol propert y set t ings. Visual St udio m anages t he code in t his sect ion. Don’t m ake y our changes t o t he form from t his sect ion. I nst ead, use t he Propert ies window for a select ed class, such as t he for m or any of it s cont rols, or use code in event procedures t o assign propert y set t ings at r un t im e.

N ot e The Windows For m s Designer gener at ed code region can be a conv enient way for lear ning t he synt ax and nam ing convent ions for m anipulat ing form and cont r ol pr opert ies. Make changes graphically in t he Windows Form s Designer and t he Proper t ies window. Then expand t he Windows Form s Designer gener at ed code region t o view how Visual St udio cr eat es t he set t ing pr ogr am m at ically. The Form 1_Load ev ent procedur e uses t wo lines t o set t he init ial locat ion of Form 1 on t he deskt op. These lines set t he form ’s t op left edge in 450 pix els fr om t he deskt op’s left border and down 450 pix els from t he deskt op’s t op border . The synt ax relies on t he Deskt opLocat ion propert y for For m 1 ( r efer r ed t o by it s Me keyw ord) and t he Point st ruct ur e. The For m 1_Load event procedure assigns t he st ring Capt ion for Hello World t o t he for m .

N ot e The Point st ruct ur e represent s values for a pair of xycoordinat es in a t wo- dim ensional plane. A st r uct ur e is like a

dat a pr im it ive in t hat it cont ains v alues and you can assign var iables t o t hose values ( for exam ple, My St art Point in Figure 9- 3) . Figu r e 9 - 3 . Th r e e e ve n t p roce du re s for m a n a ging t h e in it ia l displa y of a form a n d h ow a n applica t ion r esp on ds t o click s for e a ch bu t t on on t h e form .

The Click event procedure for But t on1 displays a m essage box. The MsgBox st at em ent in t he event procedur e t akes t hr ee param et ers. The first assigns a st ring for t he m essage box t o display , nam ely, Hello World. The second param et er specifies t he t ypes of but t ons t hat w ill be display ed in t he m essage box. The st at em ent designat es a single OK but t on. The closing param et er indicat es a capt ion for t he m essage box. As wit h Visual Basic 6, t he I nt elliSense feat ur e in Visual Basic .NET helps y ou specify t he MsgBox st at em ent . For exam ple, as y ou t y pe t he MsgBox st at em ent y ou can choose fr om an ar ray of but t on specificat ions for t he m essage box . The But t on2_Click ev ent procedure r eposit ions t he form on t he scr een from it s init ial point of ( 450, 450) t o a new point of ( 150, 450) . The ev ent pr ocedur e m ov es t he form 300 pix els t o t he left . You w ill find t his capabilit y useful when y ou need t o act iv ely m anage w her e for m s appear on y our deskt op. The event procedur e’s sy nt ax uses a Point st r uct ur e t o specify t he new locat ion for t he form . Howev er, t his ev ent pr ocedur e specifies t he posit ion for t he form w it h t he Locat ion pr opert y inst ead of t he Deskt opLocat ion propert y used in t he Form 1_Load ev ent procedure. I f a user docks t he Windows t askbar ( w it h t he

St art but t on) on t he t op, Deskt opLocat ion w ill y ield super ior per form ance, but t he t wo propert ies ot herw ise let y ou set a posit ion anyw here on t he deskt op. I f t he t askbar is at t he screen’s t op or left border, posit ioning a form w it h a Locat ion propert y set t ing of ( 0, 0) can obscure part of t he form . How ev er, t he Deskt opLocat ion pr oper t y set t ing assigns posit ion relat ive t o t he t askbar. Ther efor e, a Deskt opLocat ion pr opert y set t ing of ( 0, 0) posit ions a for m flush w it h t he t askbar. Aft er populat ing a form wit h cont r ols and code behind t he form , y ou w ill want t o t est your applicat ion. Ther e are a couple of way s t o do t his. First , y ou can choose St art from t he Debug m enu. This will com pile y our applicat ion and launch it s st art obj ect . I n t his case, t hat obj ect is Form 1. I f t he code com piles wit hout er ror, your applicat ion launches. You can t hen st art t o t est it . Second, you can choose t o com pile y our applicat ion wit hout at t em pt ing t o r un it im m ediat ely. You can com pile a program int o Micr osoft I nt erm ediat e Language ( MSI L) by choosing Build Solut ion fr om t he Build m enu. I n t his case, t hat process ret ur ns a file nam ed St art Wit hForm 1.ex e in t he bin folder of t he solut ion’s assem bly folder. The assem bly folder has t he nam e St art Wit hForm 1. You can r un t he solut ion’s .ex e file by double- click ing it in Windows Ex plorer or by t yping t he file’s nam e and pat h in t he Run dialog box and t hen clicking OK. The Run dialog box is available by choosing Run from t he Windows St art m enu. Deploying a solut ion can be as sim ple as copy ing t he St art Wit hForm 1. ex e t o anot her com put er r unning t he .NET Fram ew ork . You don’t need Visual St udio .NET inst alled on t he ot her com put er. The .NET Fram ework is av ailable as a separat e dow nload for com put ers w it hout Visual St udio .NET. See m sdn.m icr osoft .com / net fram ework / prodinfo/ get dot net .asp for inform at ion on how t o download t he fram ework from MSDN or order it on CD.

Ope n in g On e W in dow s For m w it h An ot h e r A form can open anot her form as a m odal form or a m odeless form . A m odal form doesn’t allow t he user t o act ivat e anot her form unt il t he m odal form is closed. A m essage box is an exam ple of a m odal form . Users hav e t o respond t o t he m essage box before t hey can proceed t o any ot her form . A m odeless form does allow users t o act ivat e anot her form befor e t hey close t he m odeless form . A t oolbar is an exam ple of a m odeless form . The Find dialog box t hat you can open by choosing Find And Replace and t hen Find from t he Edit m enu in Visual St udio is an exam ple of a m odeless form . You can search for a st r ing, sw it ch t he focus away from t he Find dialog box, and t hen t ransfer t he focus back t o t he Find dialog box t o search for anot her incidence of a st ring. A form doesn’t hav e a m odal or m odeless propert y . I nst ead, you can open a form wit h m et hods t hat expose it as eit her a m odal or a m odeless form . I nv oke t he ShowDialog m et hod for a form t o open it as a m odal form . To open a for m as a m odeless form , inv oke it s Show m et hod. Figur e 9- 4 shows a pair of form s wit h t he capt ions Form 1 and Form 2. These for m s belong t o a Windows applicat ion nam ed CallOneForm From Anot her. When a user clicks t he but t on on Form 1, t he but t on’s Click ev ent procedure inv ok es a procedur e nam ed OpenForm 2. This procedure can open Form 2 as eit her a m odal or a m odeless form . For m 1 also has a label cont rol. This label cont r ol accent uat es t he form ’s nam e beyond t he inform at ion in t he form capt ion. Form 2 cont ains t hr ee cont rols: a label, a t ext box, and a but t on. The label in Form 2 ser ves t he sam e purpose as t he one in Form 1. The t ext box in Form 2 is for display ing whet her t he form is open as a m odal or a m odeless form . The applicat ion assigns t he Text pr opert y of Tex t Box1 at r un t im e. But t on1 closes Form 2 in r esponse t o a click.

Figu r e 9 - 4 . A de sign view of a p a ir of form s u se d in t h e Ca llOn e For m From An ot h e r sa m ple.

Som e of t he form cont r ols for t he Windows applicat ion have st at ic propert y set t ings t hat don’t change at run t im e. When y ou have cont rols lik e t his, you can assign t he propert y set t ings at design t im e. For exam ple, you can change t he Text pr opert y of Label1 in eit her form in t he Propert ies w indow. When a form or it s cont rols have dy nam ic pr opert y set t ings t hat can change at run t im e in response t o user act ions, your applicat ion’s code m ak es t he propert y set t ings. The follow ing list ing cont ains t w o ev ent procedures and a sub procedur e. These procedur es, which m ak e up t he cust om code behind Form 1, m ak e dy nam ic propert y set t ings and handle int eract ion w it h t he user. The Form 1_Load ev ent procedur e m akes t hr ee dy nam ic pr opert y set t ings. First it posit ions t he form t ow ard t he upper left cor ner of t he deskt op w it h a set t ing t hat is 100 pix els dow n and 100 pix els t o t he left from t he upper left corner . Next t he procedur e widens t he widt h of But t on1 from it s default set t ing of 75 pix els t o a new set t ing of 85 pix els. This ext ra w idt h perm it s t he display of t he full Text propert y set t ing for But t on1, w hich t he pr ocedur e’s last line assigns. The But t on1_Click ev ent procedure cont ains a single line of code t hat invok es t he OpenForm 2 procedure. This st andard sub pr ocedur e present s as m any as t wo m essage box es. The first m essage box asks whet her t o open Form 2 as a m odal for m . I f t he user clicks t he Yes but t on, t he procedur e execut es a block of code t o achiev e t hat purpose. Not ice t he use of t he ShowDialog m et hod t o open t he form in t his code block. Ot herw ise, t he second m essage box appears w it h a prom pt t o open Form 2 as a m odeless form . I f t he user clicks t he OK but t on, t he applicat ion opens Form 2 as a m odeless form w it h t he Show m et hod. The user can close t he m essage box w it hout opening Form 2 by click ing t he Cancel but t on. Recall t hat a m essage box is a m odal form . Therefore, y ou m ust offer an opport unit y t o close a m essage box for an applicat ion t o proceed. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ’Position the form toward top left area of desktop, ’widen Button1’s width from its default setting of 75 pixels ’and assign a caption for Button1 as its Text property. Me.DesktopLocation = New Point(100, 100) Button1.Width = 85 Button1.Text = “Open Form 2" End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

’Invoke the OpenForm2 procedure. OpenForm2() End Sub Sub OpenForm2() ’Declare a pointer reference for Form2. Dim MyFormPointer As New Form2() ’Assign Text property for Button1 in Form2. MyFormPointer.Button1.Text = “Close" ’Open a new instance of Form2 as a modal form or a modeless form. ’When opening Form2 as a modal form, ’ 1. Assign a value to the Text property of TextBox1. ’ 2. Assign a start position 400 pixels down the page. ’ 3. Use the ShowDialog method for the object reference. ’ pointing at Form2 ’When opening Form2 as a modeless form, ’ 1. Assign a start position 200 pixels down the page ’ 2. Use the Show method for the object reference ’ pointing at Form2. ’ 3. Assign a value to the Text property of TextBox1. If MsgBox(“Open Form2 as Modal", MsgBoxStyle.YesNo) = _ MsgBoxResult.Yes Then MyFormPointer.TextBox1.Text = “I am modal." MyFormPointer.Downpix = 400 MyFormPointer.ShowDialog() ElseIf MsgBox(“OK, I am opening Form2 as a Modeless form.", _ MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then MyFormPointer.Downpix = 200 MyFormPointer.Show() MyFormPointer.TextBox1.Text = “I am modeless." End If End Sub The code blocks for opening Form 2 as a m odal or m odeless form vary in m or e way s t han j ust t he use of t he m et hod t o open t he form . For t he block t hat opens Form 2 as a m odal form , t he block st art s by set t ing t he Text pr opert y of Text Box1 on Form 2. Visual Basic doesn’t allow y ou t o dy nam ically set t he Text propert y of Text Box1 when Form 2 is open as a m odal form . Therefore, t he applicat ion m ak es t he set t ing befor e opening t he form . I n t he case of a m odeless form , t he applicat ion set s t he Tex t propert y for Text Box1 aft er t he form opens. Downpix is t he cust om Form 2 propert y t hat det erm ines how far dow n on t he desk t op Form 2 appears. By vary ing t he value of Dow npix depending on w het her Form 2 opens as a m odal or m odeless form , t he applicat ion m ak es it easier t o ident ify how Form 2 is open. Because t he Downpix propert y det erm ines w her e t he form opens on t he deskt op, you nat urally have t o specify t he propert y’s value before opening t he for m . The cust om code behind Form 2 consist s of t he t wo ev ent procedures in t he follow ing code sam ple along wit h a Public var iable declarat ion. The Public declarat ion is for t he Downpix var iable. The For m 2_Load ev ent pr ocedure uses t he value of t his var iable t o specify t he posit ion for opening Form 2 on t he deskt op. The ev ent pr ocedur e also dy nam ically set s t he Text pr opert y for But t on1. The But t on1_Click ev ent procedur e dem onst rat es t he synt ax for closing a form program m at ically wit hout using t he st andard Close but t on on form s. Using

t he Close m et hod is appropr iat e for sit uat ions in which you have t o per form som e special funct ions at t he t im e t hat a form closes.

N ot e You can suppr ess t he display of t he st andard Close but t on on a Windows for m by set t ing t he form ’s Form Bor der St y le proper t y t o None in t he Proper t ies window. Public Downpix As Integer Private Sub Form2_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ’Position the form 450 pixels from the desktop’s left border, ’and Downpix units from the desktop’s top border. Dim MyStartPoint As New Point(450, Downpix) Me.DesktopLocation = MyStartPoint Button1.Text = “Close" End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ’Close the current form (Form2). Me.Close() End Sub

Cr e a t in g a n d Usin g Cla ss Refe r e n ce s A firm grasp of class developm ent pr inciples is m or e im port ant when cr eat ing solut ions wit h Visual Basic .NET t han for pr ior v ersions of Visual Basic. I n addit ion, t hrough nam espace designat ions y ou can refer w it h a com m on sy nt ax t o bot h cust om classes and built - in .NET Fram ework classes. Built - in classes ar e m or e prom inent t han in any pr ior version. For exam ple, ev en dat a t ypes behav e as classes in t hat y ou can inst ant iat e a dat a t y pe w hen you declar e a variable based on it . Nam espaces and classes underpin one anot her , so a good grasp of eit her r equir es a work ing know ledge of bot h. This sect ion cont ains a m ix of com m ent ary on class and nam espace issues along w it h sam ples especially designed t o j um p- st art your use of classes wit h Visual Basic .NET.

Cr e a t in g a Cla ss t o Pe r for m Ca lcu la t ion s A t y pical r eason for using classes is t o ensur e t hat select ed calculat ions are always per form ed exact ly t he sam e way t hroughout an organizat ion. A calculat ion can be for t ax ing aut hor it ies, account ing r eport s, or scient ific applicat ions. Rat her t han have every applicat ion t hat needs t he calculat ions separat ely code t he expression for a calculat ion, y our applicat ions can reference a class wit h t he calculat ions accurat ely perform ed. This applicat ion for classes elim inat es possible errors by j unior pr ogram m ers w ho m ay not have t he experience t o code t he calculat ion pr oper ly. I n addit ion, t he pract ice of coding calculat ions in classes m akes for easy updat ing of t he com put at ions because t her e is a single point t o m odify when an updat e is necessary ( for exam ple, because of a new t ax rat e) .

Alt hough y ou can cr eat e a com put at ional class as part of a Windows applicat ion, you w ill t ypically der iv e m or e v alue fr om t he class by creat ing a st and- alone .dll file for it . Then any proj ect can creat e a refer ence t o t he class t hr ough it s .dll. This sect ion dem onst rat es t he overall process of creat ing a .dll for a class t hat perform s calculat ions.

N ot e To creat e a Class proj ect , choose t he Class Library t em plat e fr om t he New Proj ect dialog box ( which, as you’ll recall, opens w hen y ou click t he New Pr oj ect but t on on t he Visual St udio St art page) . Assign a nam e for your class. Visual St udio .NET creat es an assem bly folder for t he class. The .dll file in t he assem bly folder wit h t he sam e filenam e as t he proj ect for t he Class Librar y cont ains t he com piled code for t he class. To keep t he focus on t he const ruct ion of t he class .dll file, I const ruct a sim ple sam ple class nam ed Class1 wit h it s com piled code in Arit hm et icClass.dll. ( See t he follow ing code for t he class, which is available for v iew ing in Class1.vb.) Class1 exposes t wo propert ies and four m et hods. The propert ies repr esent t w o num bers t hat ser ve as input for one of four calculat ions also defined in t he class. The propert ies ar e designat ed as Wr it eOnly because applicat ions referencing t he class m er ely need t o copy values t o t he propert ies, w hich is anot her way of saying assign values t o t he propert ies. The class ret ur ns values t hr ough it s four funct ion procedur es, w hich im plem ent t he four m et hods for t he class. The funct ions, respect ively , add, subt r act , m ult iply , and div ide t he t wo num bers represent ed by t he class propert y values. Because t he class calls for Writ eOnly pr opert ies, we cannot use a Public var iable declarat ion. I nst ead, t he class uses a propert y procedur e t o enforce w rit e- only access t o each pr opert y . When you use only t he Get clause or only t he Set clause in a propert y pr ocedur e, y ou m ust also declar e t he propert y wit h eit her t he ReadOnly keyword or t he Wr it eOnly k ey word. The sam ple dem onst rat es t he synt ax for using t he Wr it eOnly keyword w it h t he Set clause for t he dblFirst and dblSecond propert ies of Class1. These propert y nam es are for ext ernal com m unicat ion wit h t he Class1 in t he Arit hm et icClass proj ect . The class int er nally m anipulat es dbl1 and dbl2, which cor respond t o dblFirst and dblSecond. The Set clauses for each pr opert y accept values t hrough t heir dblValue input argum ent . Aft er t he propert y pr ocedur es for dblFirst and dblSecond, t he list ing shows t he four funct ion procedures for im plem ent ing t he add, subt ract , m ult iply , and div ide operat ions bet w een t he t wo pr opert y values. Visual Basic .NET perm it s y ou t o ret ur n a value from a funct ion pr ocedure in eit her of t wo ways. First , you can assign a value t o t he funct ion nam e, as you did in prev ious v ersions of Visual Basic. Second, you can designat e t he funct ion’s value wit h an expression serv ing as t he argum ent for a Ret ur n st at em ent . The synt ax for bot h approaches appears in t he Add2dbls funct ion procedure. I com m ent ed out t he t radit ional approach t hat assigns a value based on t he funct ion’s nam e. The ot her t hr ee funct ion procedur es dem onst rat e use of t he t radit ional approach for specify ing a r et urn value. Public Class Class1 Private dbl1 As Double Private dbl2 As Double ’WriteOnly property named dblFirst. Public WriteOnly Property dblFirst() As Double Set(ByVal dblValue As Double) dbl1 = dblValue

End Set End Property ’WriteOnly property named dblSecond. Public WriteOnly Property dblSecond() As Double Set(ByVal dblValue As Double) dbl2 = dblValue End Set End Property ’Add dbls. Function Add2dbls() As Double ’Add2dbls = dbl1 + dbl2 Return (dbl1 + dbl2) End Function ’Subtract dbls. Function Diff2dbls() As Double Diff2dbls = dbl1 - dbl2 End Function ’Multiply dbls. Function Mult2dbls() As Double Mult2dbls = dbl1 * dbl2 End Function ’Divide dbls. Function Div2dbls() As Double Div2dbls = dbl1 / dbl2 End Function End Class

Re fe r e n cin g a Cla ss f r om a W in dow s Applica t ion The class const r uct ed in t he pr eceding sect ion has no v isual int erface. I n order t o use t he class in an int eract ive applicat ion, you need t o t eam t he class proj ect w it h anot her t ype of pr oj ect , such as a Windows applicat ion. When a Windows applicat ion r efers t o t he proj ect , you get t he best of bot h. The Windows applicat ion offers a r ich graphical env ironm ent for gat her ing input and display ing result s t o users. The class proj ect offers an env ironm ent t hat can efficient ly perform calculat ions and shar e it s com put at ional engine w it h m any client s concur r ent ly . Each client can sim ply m ake an inst ance of t he class t o gain access t o it s propert ies and m et hods. Figur e 9- 5 shows a Windows applicat ion w it h one form t hat uses Class1 in t he Arit hm et icClass proj ect . The form t hat appears in Figur e 9- 5 resides in t he Arit hm et icForm proj ect . When t he user click s one of t he four but t ons on t he form , t he Windows applicat ion t akes t he values in t he t op t wo t ext box es and passes t hem t o t he dblFirst and dblSecond pr opert ies in t he class. Then it inv ok es a class m et hod t hat cor r esponds t o t he click ed but t on. For exam ple, Figur e 9- 5 shows t hat t he + but t on was select ed last . Ther efor e, t he applicat ion invoked t he Add2dbls funct ion procedure and insert ed t he r et ur n value fr om t he procedure in t he bot t om t ext box on t he form . Figu r e 9 - 5 . A W ind ow s a pp lica t ion for u sin g t h e Arit h m e t icClass cla ss lib ra ry p roj e ct .

The cont r ols on t he for m in Figur e 9- 5 hav e pr opert y set t ings t o m ak e t he applicat ion’s user int erface v isually appealing. Many developers m ight prefer t o m ake m ore r efinem ent s while using t hose her e as a base. The but t ons, for exam ple, hav e a slight ly enlarged font over t he default size t hat appear s in boldface. Of course, t he but t on size is r educed t o accom m odat e t he side- by - side display of all four but t ons abov e t he t ext boxes. The Text Align propert y of t he t ext boxes is set t o Right . This set t ing displays t he t ext box cont ent s wit h an alignm ent t hat is t ypical for num bers as opposed t o st r ings, which is t he default Text Align pr opert y value. Because t he class in t he preceding sect ion resides in a st andalone .dll file, t he Windows applicat ion whose form appears in Figur e 9- 5 m ust have a refer ence t o t hat .dll file. This reference perm it s t he form t o int erface w it h t he class propert ies and m et hods defined in t he Visual Basic .NET code for t he class. ( See t he preceding sect ion. ) Ther e are t wo t echniques t o help you m anage a r eference t o a .dll file w it hin a Windows applicat ion. First , y ou can choose t o add a r efer ence t o t he .dll file. Aft er y ou add t he r eference, you can refine how y our applicat ion refers t o t he refer enced .dll file w it h t he I m port s st at em ent , which defines t he second t echnique. The I m port s st at em ent groups t he class elem ent s as it em s in a nam espace sim ilar t o t he way t he Syst em .Windows.Form s nam espace groups t he elem ent s under ly ing t he funct ionalit y in a Windows form . I nst ead of using t he full proj ect nam e and class nam e t o designat e a collect ion of it em s, y ou can define an alias as a m or e fam iliar nicknam e for t he class. I f t he nam es of elem ent s in y our class conflict w it h t hose in your proj ect or anot her refer enced nam espace, using t he nick nam e r esolv es conflict s.

N ot e Visual Basic doesn’t st r ict ly r equire t he use of an alias defined by an I m port s st at em ent t o resolve nam ing conflict s bet ween different nam espaces. Howev er , t he abilit y t o help resolve conflict s com bined wit h t he oppor t unit y t o define a cust om fam iliar nam e defined by t he alias is a very at t r act ive pair of feat ures. To add a r eference for a proj ect , choose Add Reference fr om t he Pr oj ect m enu in t he Code Edit or. For exam ple, if you ar e wor k ing wit h a Windows applicat ion, y ou can choose Pr oj ect and t hen Add Refer ence from t he Form 1.vb t ab. Next select t he Pr oj ect s t ab. Then click Br owse and nav igat e t o t he .dll t o w hich you want t o

for m a refer ence. For t his exam ple, y ou can nav igat e t o t he bin folder of t he Arit hm et icClass proj ect assem bly. Select t he .dll file, which has t he nam e Arit hm et icClass.dll in our exam ple, and click Open in t he Select Com ponent dialog box. Then click OK t o close t he Add Reference dialog box . Aft er creat ing a r efer ence t o t he .dll file, you m ust st ill inst ant iat e t he class in t he code behind a form before you can refer t o it s elem ent s. Use t he I m port s st at em ent ’s alias for t he class library in t he Dim st at em ent t hat inst ant iat es t he class. You m ust posit ion t he I m port s st at em ent im m ediat ely aft er t he Opt ion st at em ent , w hich is t he first st at em ent w it hin t he code m odule behind a form . The follow ing sam ple list ing shows an I m port s st at em ent t hat creat es an alias nam ed clsArit h for t he Class1 class in t he Ar it hm et icClass proj ect .

N ot e Alt hough t he Opt ion st at em ent isn’t st rict ly requir ed, it s use can help you m anage your code. For exam ple, t he Opt ion St r ict On declarat ion em braces and ex tends t he Opt ion Explicit st at em ent . Wit h an Opt ion St r ict On declarat ion, y ou m ust declare v ar iables wit h a t y pe befor e using t hem . I n addit ion, t his Opt ion st at em ent prohibit s dat a t y pe conversions t hat can lead t o dat a loss. Alt hough a r un- t im e err or accom panies t hese conv er sions, t he Opt ion St rict On declarat ion flags t hese conversions at com pile t im e. The Opt ion St rict On st at em ent also prohibit s m ost r efer ences t o t he Obj ect t ype, which ser ves as a cat chall t ype, m uch lik e t he Variant dat a t y pe in ear lier v er sions of Visual Basic. Aft er t he Dim st at em ent , t he code behind t he form r elies on four Click event procedur es and one sub procedure, PassText BoxValues, called by each ev ent procedur e. The PassTex t Box Values pr ocedure passes t he values from t he t ext boxes on t he form t o t he pr opert ies for t he clsArit h class. As t he procedur e passes t he cont ent s of t he t ext box es, it t ransform s t hem from st ring v alues int o num er ic values w it h t he CDbl funct ion. All t he event pr ocedur es for t he four but t ons hav e t he sam e general st ruct ur e. They differ by funct ion nam e and t he class m et hod inv ok ed. For exam ple, t he first ev ent procedure is for But t on1 wit h it s Text pr opert y set t o + . This ev ent pr ocedur e inv ok es t he Add2dbls m et hod for t he clsArit h class. Befor e insert ing t he r et urn v alue fr om t he m et hod int o t he bot t om t ext box on t he for m , t he pr ocedur e t ransform s t he Double dat a t ype int o a St r ing dat a t ype for com pliance w it h t he Text propert y of a t ext box .

N ot e The applicat ion in t he following list ing is a bar e- bones dem onst r at ion of how t o r efer ence a class. For exam ple, t he form fails if y ou don’t ent er values in bot h of t he t ext box es for input before clicking an arit hm et ic funct ion but t on. You can rem edy t his sit uat ion by assigning default v alues for t he input boxes or pr om pt s in t he input boxes wit h t he Load event procedure for Form 1. Option Strict On Imports clsArith = ArithmeticClass.Class1 Public Class Form1 Inherits System.Windows.Forms.Form

‘Windows Form Designer generated code goes here. ’Instantiate Class1 from ArithmeticClass for use ’with all the code behind Form1. Dim MyClass1 As New clsArith() Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ’Pass text box values to class. PassTextBoxValues() ’Convert Add2dbls function to a string in TextBox3. Me.TextBox3.Text = MyClass1.Add2dbls.ToString() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ’Pass text box values to class. PassTextBoxValues() ’Convert Diff2dbls function to a string in TextBox3. Me.TextBox3.Text = MyClass1.Diff2dbls.ToString() End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click ’Pass text box values to class. PassTextBoxValues() ’Convert Mult2dbls function to a string in TextBox3. Me.TextBox3.Text = MyClass1.Mult2dbls.ToString() End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click ’Pass text box values to class. PassTextBoxValues() ’Convert Div2dbls function to a string in TextBox3. Me.TextBox3.Text = MyClass1.Div2dbls.ToString() End Sub Sub PassTextBoxValues() ’Pass text box values to class. MyClass1.dblFirst = CDbl(Me.TextBox1.Text) MyClass1.dblSecond = CDbl(Me.TextBox2.Text) End Sub End Class

.N ET N a m e spa ce Ar ch it e ct ur e Nam espaces ar e t he m eans by w hich t he .NET Fram ework car ves up and m akes available it s fundam ent al funct ionalit y . Nam espaces corr espond t o t ypes. I n t he preceding t w o sect ions, Class1 in t he Ar it hm et icClass proj ect was a t ype. Howev er, t his t ype was a user - defined t ype. The .NET Fram ework includes it s ow n built - in nam espace archit ect ur e. This archit ect ure includes t wo general cat egor ies of t ypes. The first of t hese gener al cat egor ies is value t y pes. Value t ypes cont ain values as well as t he descript ion for a value. For exam ple, a st r ing is a value t ype. The St r ing value is a sequence of Unicode charact er codes for represent ing t he charact ers t hat m ak e up t he st ring. The cont ent s in a t ext box are repr esent ed as a St r ing value t ype. A Double value t ype is a 64- bit float ing- point num ber . Ther e is a core set of t hese .NET Fram ework value t ypes. Visual Basic .NET has dat a t ypes t hat correspond t o m any of t he .NET Fram ework dat a t y pes. The second general cat egory of t ypes is reference t ypes. A reference t ype can be your cust om class, such as Arit hm et icClass.Class1 or any of t he built - in .NET Fram ew ork nam espaces. The Syst em nam espace cont ains near ly 100 classes t hat facilit at e core .NET Fram ework funct ionalit y, such as t he garbage collect or and except ion handling. The .NET Fram ework offers except ion handling for t he processing of run- t im e er rors; I dw ell on t his m or e deeply in t his chapt er ’s last m aj or sect ion. The Syst em nam espace cont ains m any second- lev el and t hirdlevel nam espaces t hat handle im port ant funct ionalit y. For exam ple, Syst em .Windows.Form s is a t hird- lev el nam espace t hat support s t he inst ant iat ion and m anipulat ion of for m classes wit hin a Windows applicat ion. The Sy st em .Web nam espace enables t he ASP.NET infrast ruct ur e. The Syst em .Dat a nam espace perform s corr esponding funct ions for ADO.NET. Table 9- 1 list s t he m ain value t ypes for t he Syst em nam espace. This t able includes a Syst em nam espace class nam e, a m at ching Visual Basic dat a t ype when t her e is one, and a br ief descript ion of t he value t ype. One im port ant difference bet ween value t ypes and reference t ypes is t hat value t ypes r et ain values w it h t hem , but reference t y pes point t o values. This sim ple st at em ent can lead t o som e significant differences in t he behav ior of v ariables declared as value vs. r eference t ypes. An equalit y assignm ent st at em ent bet ween t wo var iables point ing t o value t ypes set s t he v alues of t he var iables equal. I f y ou subsequent ly assign a new v alue for one of t hese var iables, it ’s no longer equal t o t he ot her var iable. Wit h r efer ence t y pes, t he rules ar e different . When y ou assign t wo var iables point ing t o reference t ypes equal t o one anot her, y ou set t heir obj ect r eferences t o t he sam e obj ect . Subsequent ly, set t ing one var iable equal t o a quant it y assigns t hat quant it y t o bot h var iables. This is because t he var iables point t o t he sam e obj ect . Alt hough t he behav ior of t hese var iable r efer ences is subst ant ially differ ent , t he synt ax for m anipulat ing t hem is sim ilar. Just rem em ber, value t ypes st ore act ual values, but reference t ypes st or e point ers t o obj ect s. The refer ence t ypes don’t st ore values. I nst ead, t he refer ence t ypes der iv e value fr om t he obj ect s t o which t hey point . 7DEOH6XPPDU\RI6HOHFWHG1(7)UDPHZRUN9DOXH7\SHV 6\VWHP

9LVXDO%DVLF

1DPHVSDFH

'DWD7\SH

'HVFULSWLRQ

&ODVV1DPH Byt e

Byt e

8- bit unsigned int eger

I nt 16

Short

16- bit signed int eger

I nt 32

I nt eger

32- bit signed int eger

I nt 64

Long

64- bit signed int eger

Single

Single

32- bit float ing point num ber

Double

Double

64- bit float ing point num ber

Boolean

Boolean

A value t hat can be eit her Tr ue or False

Char

Char

Unicode charact ers w it h hexadecim al values ranging from 0x0000 t hr ough 0xFFFF

Decim al

Decim al

A signed int eger num ber wit h a m ax im um of 96 bit s of precision and up t o 28 digit s aft er t he decim al point

I nt Pt r

No built - in t ype

A signed int eger w hose size depends on t he plat form ; for exam ple, it can be a 32- bit value on a 32- bit plat form or a 64- bit value on a 64- bit plat form

Dat eTim e

Dat e

Dat es and t im es in t he r ange from 0: 00: 00 January 1, 0001, t hr ough 11: 59: 59 Decem ber 31, 9999

St r ing

St r ing

An im m ut able fix ed- lengt h sequence of Unicode charact ers

Obj ect

Obj ect

Root of t he t ype hierarchy ; all ot her classes in t he .NET Fr am ew ork der ive from t his one To highlight t his dist inct ion, I const ruct ed t he TypeTest s Windows applicat ion. I t cont ains a Windows for m wit h a but t on t hat inv ok es a procedure w hen you click it . The procedure dem onst rat es t he pot ent ial v ariable refer ences pit falls as well as a r em edy. The TypeTest s proj ect also cont ains a built - in class. This class is a reference t ype. The class definit ion includes a cust om const ruct or funct ion for init ializing t he class’s propert y v alue as w ell as a propert y procedur e wit h bot h Get and Set clauses for reading and w r it ing t o it s sole pr opert y. The follow ing Visual Basic code sam ple shows t he synt ax for t he class definit ion. Ty peRef is t he class nam e. I t s only pr opert y has t he nam e Value. The Sub New const ruct or init ializes Value t o m yI nput w henev er an applicat ion inst ant iat es a new inst ance of t he Ty peRef class. The synt ax for t he Value pr opert y procedure dem onst rat es how t o specify a read/ w r it e pr opert y for a funct ion. Public Class TypeRef Private intLocal ’Intialize Value to myInput. Public Sub New(ByVal myInput As Integer) Dim Value As Integer = myInput End Sub ’Read/Write property named Value. Public Property Value() As Integer Get Return intLocal End Get Set(ByVal Value As Integer) intLocal = Value End Set End Property End Class The next code excerpt from t he TypeTest s proj ect shows t he code for dem onst rat ing t he problem as w ell as a w ork ar ound t o t he problem . The code excerpt invok es t he ValueRefer enceTypeTest pr ocedur e w hen a user clicks But t on1. The pr ocedur e has t hr ee sect ions. Each sect ion pauses by pr esent ing a m essage box t hat show s t he result of var iable assignm ent s for eit her v alue or reference t ypes. Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click ValueReferenceTypeTest() End Sub Sub ValueReferenceTypeTest() ’Declare and assign values to a type instance (Integer). Dim val1 As New Integer() Dim val2 As Integer = val1 val2 = 123 MsgBox(“val1 = “ & CStr(val1) & vbCrLf & _ “val2 = “ & CStr(val2), _ MsgBoxStyle.DefaultButton1, _ “Type Assignments Test”) ’Declare and assign values to a class reference. Dim ref1 As New TypeRef(0) Dim ref2 As TypeRef = ref1 ref2.Value = 123 MsgBox(“ref1 = “ & CStr(ref1.Value) & vbCrLf & _ “ref2 = “ & CStr(ref2.Value), _ MsgBoxStyle.DefaultButton1, _ “Reference Assignments Test1”) ’Declare and assign values to two different class references. Dim ref3 As New TypeRef(0) Dim ref4 As New TypeRef(0) ref4.Value = 123 MsgBox(“ref3 = “ & CStr(ref3.Value) & vbCrLf & _ “ref4 = “ & CStr(ref4.Value), _ MsgBoxStyle.DefaultButton1, _ “Reference Assignments Test2”) End Sub The result s in each m essage box r ev eal t he out com e of t he assignm ent st at em ent s. Figur e 9- 6 shows each m essage box. Because t he boxes have unique t it les, y ou can m ap t he out com es t o t he differ ent synt act ic const ruct ions. Figu r e 9 - 6 . Sim ila r a ssig n m en t st a t e m e n t s ca n yie ld d iffer e n t out com e s for va lu e a n d r efe re n ce t ype va riab le d eclar at ion s.

The first m essage box in Figur e 9- 6 present s t he cont ent s of t wo variables point ing t o value r eferences. Bot h val1 and val2 point t o I nt eger dat a t ypes, which ar e value t ypes. The .NET Fram ew ork init ializes an I nt eger value t o 0. Ther efor e, t he Dim st at em ent for val1 set s t he var iable t o 0. The Dim st at em ent for val2 set s t he val2 variable equal t o t he curr ent value of val1, w hich is 0 from t he preceding st at em ent . Next t he procedure assigns t he value 123 t o v al2 and t hen pr int s t he values of val1 and val2 in a m essage box . As you can see, val1 equals 0, but val2 equals 123. This is because t he value goes w it h t he var iable for variable assignm ent s t o value t ypes. The second m essage box in Figure 9- 6 shows t he out com e for t he next block of st at em ent s. These begin by defining a new v ar iable r eference t o t he Ty peRef class. This st at em ent ex plicit ly init ializes t he var iable t o 0. ( See t he New const ruct or for TypeRef.) Next t he procedur e uses a Dim st at em ent t o set r ef2 equal t o ref1. Because r ef1 equals 0, r ef2 also equals 0 aft er t he st at em ent . Then t he procedure assigns 123 t o t he Value pr opert y of t he r ef2 var iable r eference. The st at em ent assigns 123 t o t he Value pr oper t y of r ef1 as well. This is because t he preceding st at em ent set s t he t w o var iables, r ef1 and ref2, equal t o t he sam e obj ect inst ance of t he TypeRef class. Because t her e is j ust one inst ance, it can hav e j ust one Value pr opert y . Therefor e, t he m essage box for t he second sect ion shows bot h var iables equal t o t he sam e value. This is so ev en t hough t he second Dim st at em ent has t he sam e synt ax in t he first and second sect ions. When wor k ing w it h variables point ing t o r eference t ypes, y ou m ust separat ely inst ant iat e a class obj ect for each var iable if y ou want t o m ak e dist inct assignm ent s t o each of t hem . The final sect ion in t he ValueRefer enceTy peTest procedur e dem onst rat es t he sy nt ax for achiev ing t his out com e. The final m essage box in Figur e 9- 6 confirm s t he r esult .

Con ve r t in g Be t w e e n V a lue Typ e s The value t ypes denot ed in Table 9- 1 hav e fundam ent ally differ ent ways of represent ing values w it hin a com put er. Nevert heless, applicat ions fr equent ly need t o pass values back and fort h bet ween different value t ypes. Any t ext box has a Text pr opert y, which relies on a St ring value t y pe for r epr esent ing it s cont ent s. Howev er, applicat ions will som et im es need t o gat her num er ic input or display

num er ic out put via a t ex t box . The num er ic represent at ion of a num ber in a t ext box can r equir e a t ranslat ion fr om a St r ing t ype t o anot her t ype, such as Double or I nt eger. The t ranslat ion is necessary because it is only in a num er ic represent at ion t hat a com put er can perform ar it hm et ic calculat ions w it h value t ypes. I f you don’t ex plicit ly perform t he t r anslat ion, t he .NET Fram ework w ill perform it im plicit ly. You should underst and t hat y ou m ight not always be able t o wor k perfect t ranslat ions bet ween all value t ypes. This sect ion and t he next one include som e sam ples t o acquaint y ou w it h t he kinds of issues t hat affect t ranslat ions bet w een dat a t ypes. Visual Basic offers a r ich array of funct ions for conv ert ing bet ween value t ypes. A ser ies of inline conv ersion funct ions can t r ansform any appropriat e expression int o a cor responding value t ype. For exam ple, CInt(63.4) ret ur ns an int eger equal t o 63. The CI nt funct ion r ounds fract ions. The full set of inline conversion funct ions com pr ises CBool, CByt e, CChar, CDat e, CDbl, CDec, CI nt , CLng, CObj , CShort , CSng, and CSt r . For t hese funct ions t o work pr oper ly , t heir argum ent m ust be suit able for t he value t hey ret ur n. At t em pt ing t o r et urn a value from CByte(256) raises an except ion, or r un- t im e er ror, because 256 is out side t he range of legit im at e byt e values. The CType funct ion can explicit ly conv ert an expression ( or const ant ) t o a value t ype. For ex am ple, y ou can use CType(63.4, Integer) t o conv ert a value w it h a decim al point t o one w it hout a decim al point . A conv enient approach for convert ing a num ber t o a st r ing is t o append t he ToSt r ing funct ion nam e t o t he end of t he num eric value, such as 45.ToString . Addit ional t ransform at ion funct ions in t he st y le of ToSt r ing are available for ot her value t ypes. I updat ed Form 1 in t he TypeTest s proj ect by adding anot her but t on, But t on2, and a t ext box, Text Box1. I n addit ion, t his sam ple r elies on Opt ion St rict On being t he first st at em ent in t he m odule behind Form 1 for t he pr oj ect . The code involv es t wo ev ent procedures. ( See t he follow ing list ing.) The Form 1_Load ev ent pr ocedur e labels t he but t on Add 1 because click ing it adds 1 t o t he t ext box value. The procedur e also assigns t he st ring 1 t o t he Text propert y of t he t ext box . The Click ev ent procedure for But t on2 adds 1 t o t he value in t he t ext box. Wit hout t w o conv ersions, t he ar it hm et ic for t he conv ersion will fail. First t he Click event procedur e t ransform s t he st r ing pr opert y of t he value in t he t ext box int o an int eger . Second t he expression in t he ev ent procedur e uses t he ToSt ring funct ion t o convert t he num er ic value in t he addit ion ex pression t o a st ring. Wit hout bot h of t hese conversions, y ou w ill generat e a com pilat ion err or w hen Opt ion St r ict On is t he first st at em ent in t he m odule. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ’Initialize button and text box text settings. Button2.Text = “Add 1" TextBox1.Text = “1" End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ’Transformations required by Option Strict On. TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString End Sub

Fr om Lon g t o H e x a de cim a l a nd Ba ck Aga in

Visual Basic has long had t he Hex funct ion for convert ing int eger num eric v alues t o hexadecim al st rings t hat r epresent t he num eric v alue of t he Hex funct ion argum ent . The .NET docum ent at ion explicit ly st at es t hat t he funct ion w ill w or k for Byt e, Short , I nt eger, Long, and Obj ect dat a t ypes. As it t urns out , t he m axim um value t hat t he Hex funct ion w ill convert is 9,223,372,036,854,775,807, which is t he m ax im um Long value t ype. Values abov e t his raise an except ion.

N ot e I f you ar en’t fam iliar wit h conversions bet ween hexadecim al num bers and base 10, you can use t he Windows Calculat or t o help ver ify t he operat ion of t he sam ples in t his sect ion. The follow ing pair of procedur es dem onst rat es how t o use t he Hex funct ion t o conv ert t he Long value in t he t ext box from t he preceding sam ple t o a Hex value t hat appears in a m essage box. Click ing But t on3 on t he form in t he Ty peTest s proj ect launches t he Conv ert LngToHex pr ocedure. This pr ocedur e’s list ing dem onst rat es t he synt ax for specify ing a condit ional com pilat ion, w hich includes t he # befor e k eyw ords. The value of BoundCheck is True, so t he com piler insert s t he opt ional code t hat per form s a bound check t o abort t he conv ersion if t he Hex funct ion argum ent is great er t han t he m ax im um value t hat t he built - in funct ion can convert . Condit ional com pilat ion was init ially int roduced int o Visual Basic w it h version 5. The conv ersion pr ocedur e concludes by display ing t he ret urn value of t he Hex funct ion ( unless t he procedure abort s because t he argum ent is t oo large) .

N ot e Set t ing t he BoundCheck com piler const ant t o False perm it s you t o generat e an ex cept ion for v alues gr eat er t han t he m ax im um conversion value—for exam ple, 9,223,372,036,854,775,808. Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click ’Call procedure to convert text box value ’from long to hexadecimal. ConvertLngToHex() End Sub Sub ConvertLngToHex() #Const BoundCheck = True #If BoundCheck Then ’Bound check on input; use CDec to accommodate values ’beyond bound check. If CDec(TextBox1.Text) > 9223372036854775807 Then MsgBox(“Number too large for Hex function.”) Exit Sub End If #End If ’Convert from string representation of Long number to ’hex character representation of number. MsgBox(“Hex value of text box equals:” & vbCrLf & _ Hex(CLng(TextBox1.Text)))

End Sub Going from a hexadecim al value t o a Long value is m or e com plicat ed for a couple of reasons. First , t her e is no built - in funct ion. Second, hexadecim al num bers need t o be convert ed on a charact er - by - charact er basis t hat reflect s t he char act er’s posit ion in t he hexadecim al num ber. This t ask is furt her com plicat ed by t hat fact t hat charact ers go out side t he decim al range of 0 t hrough 9 t o t he hex adecim al range of 0 t hrough F. The follow ing sam ple per for m s a check t o v er ify t hat t he hexadecim al st r ing value doesn’t exceed t he m axim um Long value. The hex represent at ion for t he m axim um Long value is 7FFFFFFFFFFFFFFF. Aft er perform ing a bound check for t he m ax im um hex adecim al value, t he Convert Hex ToLng pr ocedur e st art s a loop t hat it erat es t hr ough successiv e charact ers in t he hexadecim al num ber. St art ing at t he far r ight charact er , t he loop evaluat es each charact er. The evaluat ion m ult iplies t he hex charact er’s decim al v alue by a power of 16. The pow ers range in value fr om 0 for t he far right charact er t o up t o 15 for t he sixt eent h hex charact er ( if t her e is one) . When t he Conv ert HexToLng procedur e finishes looping t hr ough t he charact er s in t he hexadecim al num ber, t he pr ocedur e present s a m essage box w it h t he decim al value of t he hexadecim al num ber in Text Box1. Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click ’Call program to convert a hexadecimal number to ’a Long number. ConvertHexToLng() End Sub Sub ConvertHexToLng() ’Assign TextBox1 contents to hexStr. ’Dim strValue As String = TextBox1.Text Dim hexStr As String = TextBox1.Text ’If hexStr greater than 7FFFFFFFFFFFFFFF, then abort. Dim hexchars As Integer = Len(hexStr) If (hexchars = 16 And hexStr.Chars(0) > “7”) Or _ hexchars > 16 Then MsgBox(“Hex values beyond 7FFFFFFFFFFFFFFF “ & _ “generate an exception. Enter a smaller “ & _ “hex value.”) Exit Sub End If ’Variable lnghexstr stores long of hex string in TextBox1, ’and i is a loop counter value. Dim lnghexstr As Long Dim i As Integer ’Loop through characters to compute decimal equivalent ’of hex string. lnghexstr = 0 For i = 0 To hexchars - 1 Select Case Mid(UCase(hexStr), hexchars - i, 1) Case “0" lnghexstr += CLng(0 * (16 ^ i)) Case “1" lnghexstr += CLng(1 * (16 ^ i)) Case “2" lnghexstr += CLng(2 * (16 ^ i)) Case “3"

lnghexstr Case “4" lnghexstr Case “5" lnghexstr Case “6" lnghexstr Case “7" lnghexstr Case “8" lnghexstr Case “9" lnghexstr Case “A" lnghexstr Case “B " lnghexstr Case “C" lnghexstr Case “D" lnghexstr Case “E"

+= CLng(3 * (16 ^ i)) += CLng(4 * (16 ^ i)) += CLng(5 * (16 ^ i)) += CLng(6 * (16 ^ i)) += CLng(7 * (16 ^ i)) += CLng(8 * (16 ^ i)) += CLng(9 * (16 ^ i)) += CLng(10 * (16 ^ i)) += CLng(11 * (16 ^ i)) += CLng(12 * (16 ^ i)) += CLng(13 * (16 ^ i))

lnghexstr += CLng(14 * (16 ^ i)) Case “F" lnghexstr += CLng(15 * (16 ^ i)) End Select Next i ’Display long value for hex string. MsgBox(“Long value for text box equals:” & vbCrLf & _ lnghexstr.ToString) End Sub

I nh e r it in g Cla sse s Classes ar e gr eat because t hey package blocks of Visual Basic code for easy reuse. Class inherit ance m ult iplies t hat cor e benefit of classes by let t ing one class inherit t he propert ies, m et hods, and event s of anot her class. I nher it ance for cust om classes is new t o Visual Basic program m ers w it h Visual Basic .NET. This sect ion begins w it h an overv iew of design issues and keywords for im plem ent ing class inher it ance. Next I cover a couple of sam ples t hat dem onst rat e t he synt ax for im plem ent ing inher it ance w it h different key words. At t he sect ion’s close, you will discover a discussion of ov er loading. This feat ure can m ak e one m et hod or propert y w it hin a class easily accept m any different t ypes of value input s. I nst ead of building capabilit ies int o applicat ions by lay ering one class on t op of anot her or m anually coding a class t o t est m ult iple value t ypes and t hen r espond appropriat ely t o t he input value t ype, t he Over loads keyword expands t he capabilit ies of a single class. I cover t he Ov er loads keyw ord in t his sect ion because of it s r esem blance t o t he Ov er r iding keyword—one of t he k eyw ords for m anaging inher it ance—and because Ov er loads widens t he capabilit ies of a class m uch as inherit ance can.

Ov e r vie w of I nh e r it a n ce

I nher it ance is for classes. I t let s one class inher it t he pr opert ies, m et hods, and ev ent s of anot her class. My discussion of inherit ance focuses on propert ies and m et hods t o sim plify t he present at ion. ( See t he “ Program m ing Ev ent s” sect ion lat er in t his chapt er for m or e on m anaging class event s.) When Class B inher it s Class A, Class B can offer t he sam e m et hods, propert ies, and event s of Class A. I n addit ion, Class B can offer new pr opert ies and m et hods as well as m odified versions of t he pr opert ies and m et hods in Class A. Visual Basic dev elopers didn’t hav e t his capabilit y for cust om st and- alone classes wit h v ersions of Visual Basic prior t o t he . NET v ersion. Therefore, it is nat ur al t hat y ou need t o lear n som e new concept s and sy nt ax t o t ak e adv ant age of inherit ance. We can st art our new inherit ance vocabulary by r efer r ing t o t he inher it ed class as t he base class. The class t hat inher it s a base class is a derived class. When one class inher it s fr om anot her class, t he der ived class m ust cont ain a declarat ion st at ing from which class it inherit s propert ies and m et hods. Visual Basic .NET uses an I nherit s st at em ent t o m ake t he declarat ion. The I nher it s st at em ent t akes as it s argum ent t he nam e of t he base class. You can have j ust one class nam e as t he argum ent for I nher it s. Therefore, a class can inher it fr om at m ost one ot her class at a t im e. I f t he der iv ed class adds any new m et hods, it can offer t he m et hods of t he base class along w it h it s ow n new m et hods. I n addit ion t o offer ing new m et hods, t he deriv ed class can offer m odified im plem ent at ions of one or m ore m et hods from t he base class. Anot her new inherit ance t er m in Visual Basic .NET is polym or phism . I t describes t he abilit y of a der iv ed class t o change t he im plem ent at ion of a base class m em ber, such as a propert y or a m et hod. An applicat ion can inst ant iat e inst ances for a der ived class and it s base class. I n t his way, t he applicat ion can inv ok e an unm odified m et hod from a base class and an updat ed m et hod w it h t he sam e nam e from a der iv ed class. I n order for Visual Basic .NET t o m odify a base class m et hod in a der iv ed class, your class m et hods r equir e special k eyw ords. First , t he base class m ust m ark t he m et hod nam e w it h t he Over ridable key word, such as in t he following code: Class MyBaseClass Overridable Function One () As Double ’Code for returning a value. End Function End Class I n addit ion t o a keywor d in t he base class, y ou need a cor responding k eyw ord, Over rides, in t he der ived class. This k eyword m ust be applied t o a m et hod in t he der iv ed class wit h t he sam e nam e as t he one in t he base class whose im plem ent at ion y ou want t o change. For exam ple Class MyDerivedClass Inherits MyBaseClass Overrides Function One () As Double ’New code for returning a value. End Function End Class As y ou can see, im plem ent ing polym orphism requir es planning. That is, you m ust m ark t he base class t hat you want ov er r idden in der iv ed classes wit h t he Over ridable key word. You m ust also sy nchr onize m et hod nam es bet w een t he base and der iv ed classes. The m et hod nam es w it hin a class—eit her base or der iv ed—should generally be dist inct . I n general, you should also keep t he m et hod and propert y nam es dist inct bet w een base and derived classes. Using t he sam e nam e for a m et hod or a propert y in bot h base and der ived classes has a special m eaning t hat we will consider short ly . I n order for a der iv ed class t o refer back t o a m et hod or pr opert y in a base class, you need t o use t he special My Base k eyw ord. You w ill t ypically use t he My Base keyw ord wit hin a funct ion in a der iv ed class t hat ov err ides an ident ically nam ed

funct ion in a base class. You can also use t he MyBase k eyw ord t o set and get propert y values for a base class from a der iv ed class. Then you can use t he My Base k ey word t o invoke a m et hod w it h t he v alues t hat y ou passed t o t he base class. For exam ple, MyBase.One() in a deriv ed class invokes t he One m et hod in t he base class. The Shadows keyword can apply t o propert ies and m et hods in a der iv ed class. This k eyw ord essent ially blocks t he availabilit y of ident ically nam ed propert ies and m et hods in a base class. I n a sense, t he Shadows keyword for a propert y or m et hod in a derived class cast s a shadow ov er an ident ically nam ed pr opert y or m et hod in a base class. The Shadows keyword is m ore flex ible and pow er ful t han t he Ov erridable/ Ov er r ides keywords. For exam ple, t he Ov er ridable/ Ov er r ides keyw ords apply only t o m et hods im plem ent ed wit h sub procedures or funct ion procedur es. The Shadows keyw ord apples t o m et hods as well as pr opert ies. I n addit ion, you can shadow a m et hod in a base class wit h a pr opert y in a der iv ed class. The Shadows k ey word rem oves t he dependence of a der iv ed class on an ident ically nam ed obj ect in a base class. This insulat es t he derived class from any changes t o t he base class t hat could inadvert ent ly cause an er ror in t he der ived class. The Overr idable/ Ov errides k eywords don’t offer t his prot ect ion for a der ived class from changes m ade in a base class. The Ov er loads keyword isn’t st rict ly an inher it ance t opic, but t his k eyw ord pert ains t o classes, and it s nam e is sim ilar t o Ov er r ides. I n addit ion, using t he Over loads key word on a funct ion pr ocedur e, sub procedure, or propert y procedur e can alt er t he behav ior of t he pr ocedure. How ev er , t he Ov erloads keyw ord can apply t o m et hods or pr opert ies w it hin t he sam e class. A com m on use of t he Over loads k eyword is t o enable m ult iple v ersions of a funct ion procedur e t o operat e as one. Each funct ion pr ocedur e in a set of ov erloaded funct ion pr ocedur es has t he sam e nam e. Howev er , t he argum ent t ypes change for each funct ion procedur e w it hin a set . Ther efor e, one v ersion of a m et hod can accept a st ring argum ent , but anot her v ersion can accept a double dat a t ype as an ar- gum ent . The . NET Fram ework w ill aut om at ically inv ok e t he r ight funct ion procedur e based on an input ’s dat a t ype! That ’s t he power of t he Over loads keyw ord.

An I n h e r it in g a n d Ove r r iding Sa m ple Any Windows applicat ion apply ing class inher it ance w ill cont ain at least t hr ee unit s of code. You need t wo unit s of code for t he classes: one for t he base class and a second for t he derived class. A t hird unit of code is necessary t o inst ant iat e one or m or e classes and inv ok e t he m et hods or m anipulat e t he pr ocedur es in t he der iv ed class or it s base class. I n a Windows applicat ion, y ou can inst ant iat e classes and m anipulat e t he inst ances from event procedures for but t ons on a for m . One or m ore t ext boxes on a form can prov ide v ehicles for users t o specify input values as argum ent s for m et hods and propert ies. The sam ple for t his sect ion is a Windows applicat ion t hat includes a for m ( Form 1) wit h m ult iple but t ons and t ex t box es for users t o m anipulat e. The first sam ple uses But t on1 along w it h Text Box1 and Text Box 2. Click ing But t on1 launches an ev ent procedure t hat inst ant iat es a base class, Ar it hm et icClass1, and a der ived class, Class1. The procedur e m anipulat es t hese class inst ances in various way s wit h input fr om t he ent r ies in Text Box1 and Tex t Box2. I will det ail t he m anipulat ions by describing t he But t on1_Click ev ent pr ocedur e aft er discussing t he code in t he Arit hm et icClass1 and Class1 classes.

N ot e The sam ple for t his sect ion and t he nex t t wo sect ions dem onst r at ing inherit ance wit h Visual Basic .NET all use t he

sam e solut ion, I nherit ingSam ple. You can double- click I nherit ingSam ple.sln in Windows Ex plorer t o open t he solut ion in Visual St udio. To r un t he applicat ion fr om Windows Explor er, invoke t he I nherit ingSam ple1.exe file. The filenam e for t he .ex e file ret ains t he original nam e for t he solut ion. Ar it hm et icClass1 is a variat ion of t he st and- alone class in t he Ar it hm et icClass proj ect discussed in t he “ Cr eat ing and Using Class Refer ences” sect ion. This base class resides in t he I nherit ingSam ple solut ion. The code for t he base class follows. I t begins by specify ing t wo w r it e- only pr opert ies. Ar it hm et icClass1 also specifies t wo m et hods—bot h based on funct ion pr ocedur es. The Add2dbls m et hod follows dir ect ly fr om t he Arit hm et icClass present ed earlier in t his chapt er; t he m et hod adds t wo values w it h a Double value t ype. A sub procedur e im plem ent s t his m et hod. The input for t he funct ion pr ocedur e is fr om t he Writ eOnly pr opert ies, which specify t he double values t o add. A funct ion procedur e im plem ent s t he second m et hod, Add2dbls2, in Ar it hm et icClass1. Using argum ent s for t he funct ion pr ocedur e elim inat es t he need t o r ely on propert ies t o specify t he values t o add. The Ov err idable k ey word appears at t he st art of t he Add2dbls2 m et hod specificat ion. This m eans t hat anot her class inher it ing Ar it hm et icClass1 can ov er r ide t he code for t he m et hod t hat appears below. Public Class ArithmeticClass1 Private dbl1 As Double Private dbl2 As Double ’WriteOnly property named dblFirst. Public WriteOnly Property dblFirst() As Double Set(ByVal dblValue As Double) dbl1 = dblValue End Set End Property ’WriteOnly property named dblSecond. Public WriteOnly Property dblSecond() As Double Set(ByVal dblValue As Double) dbl2 = dblValue End Set End Property ’Add dbls. Function Add2dbls() As Double Return (dbl1 + dbl2) End Function ’Overridable version of Add dbls. Overridable Function Add2dbls2(ByVal MyNum1 As Double, _ ByVal MyNum2 As Double) As Double Add2dbls2 = MyNum1 + MyNum2 End Function End Class The code for Class1 has t hr ee m aj or sect ions; t he full list ing for t he class appears next . The first sect ion inher it s Ar it hm et icClass1. The I nher it s st at em ent m akes Class1 a der iv ed class wit h Ar it hm et icClass1 as it s base class. Class1 can reference all t he propert ies and m et hods of Ar it hm et icClass1 t hr ough t he My Base keyw ord.

The next sect ion in Class1 adds a new m et hod wit h t he Nt hPower funct ion. The funct ion com put es t he v alue of t he base v alue t o a power, such as 2 3 equaling 8. This funct ion accept s ar gum ent s for t he base and power var iable values. The final sect ion of code in Class1 defines a new im plem ent at ion for t he Add2dbls2 m et hod init ially specified in t he base class. ( See t he preceding code for Ar it hm et icClass1.) The Over rides k eyw ord at t he beginning of t he m et hod specificat ion in Class1 along w it h t he m at ching Over ridable key word for t he sam e m et hod nam e in Ar it hm et icClass1 perm it s t he overr ide. The new im plem ent at ion for t he Add2dbls2 m et hod doubles t he value com put ed in t he base class. The My Base k ey word facilit at es t he r efer ence back t o t he base class. The ar gum ent s passed t o t he Add2dbls2 m et hod in Class1 t ransfer t o t he base class t hrough t he argum ent s in t he expression cont aining t he My Base k ey word. Public Class Class1 ’Class1 class inherits from ArithmeticClass1. Inherits ArithmeticClass1 ’Added method to complement inherited method ’from ArithmeticClass1. Public Function NthPower(ByVal base As Double, _ ByVal power As Double) As Double NthPower = (base ^ power) End Function ’The Add2dbls2 method in Class1 overrides the ’overridable Add2dbls2 method in ArithmeticClass1. Overrides Function Add2dbls2(ByVal MyNum1 As Double, _ ByVal MyNum2 As Double) As Double ’The following code calls the original method in the base ’class, and then modifies the returned value. Add2dbls2 = MyBase.Add2dbls2(MyNum1, MyNum2) * 2 End Function End Class The Click event for But t on1, w hich appears nex t , begins by hiding som e cont rols t hat ar en’t necessary for t his use of t he form . Then t he ev ent pr ocedur e inst ant iat es Ar it hm et icClass1 as t he arclass1 variable and Class1 as t he c1 variable. The pr ocedur e uses t wo t ext boxes on t he form so t hat users can specify double values for t he m et hods in t he classes. Because t he t ext box values requir e conv ersion t o m ak e t hem Double values for t he procedures im plem ent ing t he m et hods, t he sam ple com put es t he conversion once and st ores t he result s in t w o variables w it h a Double value specificat ion. Aft er concluding t he pr eceding pr elim inary st eps, t he event procedur e st art s com put ing and display ing result s. I nit ially t he procedure passes t he Double values saved in num 1 and num 2 t o t he propert y pr ocedur es assigning values t o t he dblFirst and dblSecond propert ies in Arit hm et icClass1. Next t he procedur e inv okes t he Add2dbls m et hod w it hin t he Arit hm et icClass1 and co- nv ert s t he out com e t o a st r ing wit h t he ToSt r ing m et hod for display in a m essage box. Aft er a user clears t he m essage box from t he screen, t he event procedure invok es t he Nt hPow er m et hod in Class1. Again, t he m essage box argum ent conv ert s t he num ber t o a st r ing for display . The last pair of MsgBox funct ions in t he ev ent procedur e inv ok es t he Add2dbls2 m et hod. The first m essage box display s t he Add2dbls2 m et hod out com e from it s base class im plem ent at ion ( in Ar it hm et icClass1) . The procedure concludes by inv ok ing t he sam e m et hod from Class1. This result appearing in t he second m essage box w ill be t w ice as large as it s pr edecessor . This is because different funct ion pr ocedur es im plem ent t he m et hod in each class. ( Cont rast t he code for Add2dbls2 in t he t wo pr eceding class list ings.)

’Sample to demonstrate basic inheritance to add a ’new method or override an existing one. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ’Hide unnecessary text controls. Button2.Visible = False Button3.Visible = False ’Instantiate objects based on the ArithmeticClass1 ’and Class1 classes. Dim arclass1 As New ArithmeticClass1() Dim c1 As New Class1() ’Declare num1 and num2 variables and assign values ’to the variables based on text box entries. Dim num1 As Double Dim num2 As Double num1 = CDbl(TextBox1.Text) num2 = CDbl(TextBox2.Text) ’Set properties and invoke the Add2dbls method from ’the ArithmeticClass1 class. arclass1.dblFirst = num1 arclass1.dblSecond = num2 MsgBox(arclass1.Add2dbls.ToString, , _ “Return from Add2dbls in ArithmeticClass1”) ’Invoke the NthPower method in Class1, which is a ’new method not in ArithmeticClass1. MsgBox(c1.NthPower(num1, num2).ToString, , _ “Return from NthPower in Class1”) ’Invoke the Add2dbls2 method for the ArithmeticClass1 ’and Class1 classes; the Add2dbls2 method in Class1 ’overrides the Add2dbls2 method in ArithmeticClass1. MsgBox(arclass1.Add2dbls2(num1, num2).ToString, , _ “Return from Add2dbls2 in ArithmeticClass1”) MsgBox(c1.Add2dbls2(num1, num2).ToString, , _ “Return from Add2dbls2 in Class1”) End Sub Figur e 9- 7 sum m ar izes t he r esult s. On t he left is t he form aft er I ent er ed values in bot h t ext box es and clicked But t on1. Not ice t hat But t on2 and But t on3 ar en’t t here; t hat ’s because t he But t on1_Click ev ent procedur e m ade t hem invisible on t he form by set t ing t heir Visible pr opert y t o False. The four m essage box es on t he right display t he r esult s in t he order t hat t he But t on1_Click ev ent procedur e com put es t hem . The capt ion for each m essage box specifies t he source, including t he m et hod and t he class, for t he displayed r esult . Not ice in part icular t he last t wo m essage box es. These result s in coordinat ion w it h t he list ing for t he But t on1_Click ev ent pr ocedur e docum ent and confirm how you can overr ide a m et hod in a base class wit h a differ ent im plem ent at ion in a der iv ed class. Figu re 9 - 7 . By cr e a t in g in st a n ce s for bot h a b ase class an d a de rive d class, you ca n in vok e m et h od s for b ot h cla sse s, a n d som e of you r m e t h od r efe re n ce s in a d e rived cla ss ca n over r ide t h ose in a ba se cla ss.

A Sha dow in g Sa m ple As indicat ed in t he “ Ov erv iew of I nher it ance” sect ion, shadow ing act s sim ilar ly t o ov err iding but is m or e flex ible. The sam ple for t his sect ion dem onst rat es t he use of t he Shadows k eyw or d. You can use t he Shadows keyw ord in a der iv ed class; doing so doesn’t require any corresponding changes t o a base class. The sam ple in t he pr eceding sect ion required t he Over ridable k eyw ord in t he base class for t he Ov errides keyword in t he der iv ed class t o funct ion pr oper ly . The sam ple in t his sect ion uses t he TypeRef1 class t hat follows as t he base class. Not ice t hat t he list ing for TypeRef1 includes a propert y procedure for a propert y nam ed Value. The pr ocedur e includes bot h Get and Set clauses. This class is sim ilar t o t he TypeRef sam ple present ed ear lier in t his chapt er. The sole dist inct ion bet w een TypeRef1 and TypeRef is t hat TypeRef1 com m ent ed out t he New m et hod. Recall t hat in t he prior sam ple using TypeRef, t he New m et hod was helpful in set t ing an init ial value for a var iable inst ant iat ed on t he class. Howev er, when y ou use a class as t he base class for an I nherit s st at em ent , t he base class cannot include a m et hod nam ed New. The inabilit y t o specify a New m et hod wit hin t he class isn’t m aj or because an applicat ion can assign a value t o a variable based on t he class im m ediat ely aft er inst ant iat ing t he v ar iable. Public Class TypeRef1 Private intLocal ’Intialize Value to myInput -- not permissible in ’inherited class. ’Public Sub New(ByVal myInput As Integer) ’ Dim Value As Integer = myInput ’ MsgBox(Value.ToString, , “in new”) ’End Sub ’Read/Write property named Value. Public Property Value() As Integer Get Return intLocal

End Get Set(ByVal Value As Integer) intLocal = Value End Set End Property End Class The shadow ing sam ple also relies on a second sam ple nam ed Class2. This class inherit s TypeRef1, so Class2 is a der iv ed class wit h TypeRef1 as it s base class. Because TypeRef1 has j ust one pr opert y, Class2 m ust have a m em ber by t he sam e nam e if it is t o shadow t he pr opert y procedur e in TypeRef1. I specifically used t he t erm m em ber. This leav es open t he possibilit y of t he shadow ing elem ent being eit her a propert y or a m et hod. The only r equirem ent is t hat t he shadow ing elem ent hav e t he sam e nam e as t he m em ber t hat it shadows. Alt hough t he follow ing list ing for Class2 dem onst rat es t he use of t he Shadows k eyw ord, t he use of t his keyw ord is opt ional for im plem ent ing shadow ing. As y ou can see from t he follow ing list ing, t he shadow ing v ersion of t he pr opert y procedure for Value in Ty peRef1 adds 2 t o t he input . The or iginal v ersion of t he propert y pr ocedur e for t he Value propert y in Ty peRef m erely echoes t he input . Public Class Class2 ’Class2 inherits from TypeRef1 Inherits TypeRef1 Private intLocal ’Read/Write property named Value in Class2 ’shadows property with the same name in TypeRef1. Public Shadows Property Value() As Integer Get Return intLocal End Get ’New version adds 2 to initial input. Set(ByVal Value As Integer) intLocal = Value + 2 End Set End Property End Class Click ing But t on2 on For m 1 in t he I nherit ingSam ple solut ion launches an ev ent procedur e, w hich appears next . The pr ocedure uses But t on2 and Text Box1 ( along wit h it s label) . Therefore, t he event procedure st art s by hiding t he ot her cont rols on t he form . Next t he procedur e conv ert s and copies t he cont ent s of Text Box1 t o num 1, which t he pr ocedur e declares as an I nt eger variable. This value t ype specificat ion for num 1 is consist ent w it h t he Value pr opert y in TypeRef1 and Class2. Aft er st oring t he conv ert ed t ext box ent r y in a v ar iable for t he event procedur e, t he procedur e assigns t he value saved in num 1 t o t he Value propert y in TypeRef1 and Class2. Finally, a pair of MsgBox funct ions echoes t he quant it y in t he propert y. ’Sample to demonstrate shadowing with inheritance. Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ’Hide unnecessary text controls. TextBox2.Visible = False Label2.Visible = False Button1.Visible = False Button3.Visible = False

’Instantiate objects based on the TypeRef1 ’and Class2 classes. Dim trclass1 As New TypeRef1() Dim c2 As New Class2() ’Declare num1 variable and assign a value ’to the variable based on the text box’s entry. Dim num1 As Integer num1 = CInt(TextBox1.Text) trclass1.Value = num1 c2.Value = num1 MsgBox(trclass1.Value.ToString, , _ “Return from Value property in TypeRef1”) MsgBox(c2.Value.ToString, , _ “Return from Value property in Class2”) End Sub Figur e 9- 8 shows t he shadow ing sam ple. On t he left panel, y ou see t he t ext box and but t on for launching t he event procedure. Not ice t hat t he t ext box cont ains t he value 3. On t he r ight side, y ou see t he t w o m essage box es cont aining t he echoed Value pr opert ies fr om TypeRef1 and Class2. Alt hough t he input t o bot h propert ies was t he sam e, t he out put is differ ent because t he one expression in Class2 is dist inct from it s count erpart for a shadowed pr opert y in TypeRef1. Figu r e 9 - 8 . Sh ad ow in g m ak e s it e a sie r for a de rive d cla ss t o re t u r n a d iffer e n t r esu lt t h a n a pr op er t y w it h t h e sam e n a m e in a ba se cla ss.

An Ov e r loa din g Sa m ple Bot h ov err iding and shadow ing are about doing m or e t hings w it h t he sam e m et hods and pr opert ies. The Ov er loads k eyw or d is one m ore exam ple of a keyw ord t hat sim plifies how y ou can do m or e w it h t he code in y our sam ples. I n essence, it allows y ou t o const ruct a set of pr ocedur es all of which hav e t he sam e nam e but w it h differ ent argum ent t ype specificat ions. When a user invokes a m et hod based on t he set of procedures, t he .NET Fram ew ork aut om at ically det ect s t he specific pr ocedur e t hat m at ches t he input dat a t y pe. You don’t hav e t o use t he Ov erloads k eyword in an inherit ance cont ext , but it can work wit h

inherit ance. For sim plicit y, t his sect ion dem onst rat es t he use of t he Ov erloads keyw ord wit hout inv olv ing inherit ance.

N ot e You can also achieve overloading wit hout t he Ov erloads key word. Just m ak e sure all t he pr ocedure nam es are ident ical, wit h differ ent value t ype specificat ions for t he argum ent s in each m em ber wit hin t he set of pr ocedures. However, if you use t he Overloads keyword for at least one m em ber in t he set , you m ust use it for all m em bers. The Class3 list ing shows a sim ple over loading sam ple. The class cont ains t wo inst ances of t he TenPer cent OfI t funct ion procedur e. These inst ances collect iv ely im plem ent t he TenPercent OfI t m et hod for Class3. I f a user ent ers an ar gum ent wit h a Double v alue t ype, such as 55.5, in t he TenPercent OfI t m et hod, Class3 responds by inv ok ing t he first funct ion pr ocedur e. This m ight happen if t he user inv okes t he m et hod from a dat abase w it h a colum n of Double values. On t he ot her hand, when t he input for t he TenPercent OfI t funct ion is a st ring, such as 55.5, Class3 aut om at ically inv ok es t he second funct ion procedure. This m ight happen if an applicat ion passes a value dir ect ly from a t ext box t o t he class m et hod. By using t he Over loads k eyw ord in fr ont of bot h versions of t he funct ion, t he dev eloper can leav e it t o t he .NET Fram ework t o figur e out wit h w hich specific funct ion pr ocedur e t o im plem ent t he m et hod. As m or e pot ent ial dat a sources becom e available, it is easy t o add a new copy of t he funct ion pr ocedur e wit h different v alue t ype declarat ions for t he argum ent s. Public Class Class3 Overloads Function TenPercentOfIt(ByVal It As Double) As Double Return (It * 0.1) End Function Overloads Function TenPercentOfIt(ByVal It As String) As Double Return (CDbl(It) * 0.1) End Function End Class The follow ing Click event procedure for But t on3 dem onst rat es a t est of t he ov erloading feat ure im plem ent ed in Class3. Aft er hiding t he unnecessary cont r ols on t he form , t he applicat ion inst ant iat es c3 as an inst ance of Class3. Next it assigns a Double value of 55.5 t o num 1. The final pair of MsgBox funct ions inv okes t he TenPercent OfI t m et hod in Class3 w it h t he num 1 Double value t ype or a St r ing value t ype based on t he cont ent s of Text Box1. Because t he r et ur n fr om t he m et hod is a Double value, t he argum ent for t he MsgBox funct ions inv ok es t he ToSt r ing m et hod on t he r et urn value. The im port ant point t o not e is t hat ev en t hough t he t w o MsgBox funct ions invoke t he TenPercent OfI t m et hod w it h different v alue t ypes, t hey bot h invok e exact ly t he sam e m et hod wit h exact ly t he sam e synt ax. ‘Sample to demonstrate overloading within a class. Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click ’Hide unnecessary text controls. TextBox2.Visible = False Label2.Visible = False Button1.Visible = False Button2.Visible = False ’Instantiate Class3 with overloaded functions and declare

’a variable with a Double type for one of the functions. Dim c3 As New Class3() Dim num1 As Double ’Assign a value to the Double variable type and ’invoke one version of the overloaded function. num1 = 55.5 MsgBox(c3.TenPercentOfIt(num1).ToString, , _ “Return based on a double input”) ’Invoke another version of the overloaded function with ’string input instead of numerical input. MsgBox(c3.TenPercentOfIt(TextBox1.Text).ToString, , _ “Return based on a string input”) End Sub Figur e 9- 9 confirm s t hat you can obt ain ident ical result s from t he TenPercent OfI t ov erloaded set of funct ions based on t w o differ ent input value t ypes. The form on t he left shows 55.5 in a t ex t box. This t ext box cont ains a St r ing value. The t w o m essage box es on t he r ight show ident ical ret ur n values. How ev er , t heir capt ions confirm t hat t hey have different input value t ypes, and all our code did t o get t his result was t o use t he Over loads k eyw ord. Som et im es Microsoft can m ake life so sweet ! Figu re 9 - 9 . Ove rloa din g au t om at ica lly m a t ch e s t h e p roce du re in vok e d t o t h e d at a t yp e of t h e ar gu m en t in a st a t e m e n t ca llin g a se t of ove rloa de d p roce du re s.

Pr ogr a m m in g Even t s An ev ent is a not ificat ion t hat som et hing happened. As Visual Basic program m ers, you are well awar e of event s from built - in obj ect s, such as form s and but t ons. Many int erm ediat e and advanced program m ers r egular ly creat e cust om classes t hat generat e cust om event s wit h pr ior v ersions of Visual Basic. Adding ev ent s t o cust om classes allows obj ect s based on t he classes t o conv ey inform at ion back t o t he applicat ions t hat inst ant iat e t he obj ect s.

Visual Basic .NET ret ains t he ev ent funct ionalit y fr om earlier v ersions w hile it adds new capabilit ies as well, relat ed t o defining cust om event handlers and wor k ing w it h new sources for ev ent s. This sect ion r ev iews t he basics of ev ent program m ing t o prov ide a st andard background for m or e adv anced t opics, including t he abilit y t o dynam ically define ev ent handlers and new com ponent s t hat can raise ev ent s.

Ev e nt Pr ogr a m m in g Con ce pt s Ev en when work ing w it h built - in ev ent s for form s and t heir cont rols, it helps t o hav e a basic underst anding of ev ent program m ing concept s, but a k nowledge of t his t opic is essent ial w hen you dev elop event s for cust om classes. Happily, a few core concept s t hat ar e easy t o grasp can enable you t o declar e and m anage cust om ev ent s. Ev ent s hav e a source or a sender. This source is t he elem ent t hat sends out t he not ificat ion t hat an event happened. A class t hat you cr eat e can be a source. A for m class can be a source. For exam ple, Visual Basic raises t he Load event w hen it opens a form inst ance. Sim ilarly, when a user clicks a but t on on a for m inst ance, t his raises t he Click event for t he but t on. For a source t o raise an ev ent , t wo t hings m ust happen. First , t he ev ent m ust be declared for t he obj ect . You can declare an Ev ent st at em ent . Second, som e code inside t he class for t he obj ect inst ance m ust inv ok e t he RaiseEvent st at em ent . The RaiseEvent st at em ent t riggers an ev ent declared wit h t he Ev ent st at em ent . You can raise an ev ent only from t he class in w hich it occurs. Ther efor e, a but t on cannot raise a Load ev ent for a form on which it r esides. Sim ilarly , a der iv ed class cannot use t he RaiseEv ent st at em ent t o t rigger an ev ent declar ed in it s base class. Ev ent handlers pr ocess an ev ent . Just because a class inst ance raises an ev ent doesn’t m ean t hat an applicat ion has t o acknowledge t he ev ent . Clicking a but t on befor e y ou add a sub pr ocedur e t o process t he click has no effect . The sub procedur e is an ev ent handler. Ev ent handlers allow applicat ions t o r espond t o ev ent s r aised by class inst ances. Visual Basic can aut om at ically cr eat e em pt y ev ent handlers for t he Windows For m s and t heir cont rols. These em pt y ev ent handlers ar e called st ubs. A st ub includes t he sub procedure declarat ion w it h a nam e for t he procedur e, a list of argum ent s, and a t erm inat ing st at em ent for t he procedur e ( nam ely, End Sub) . St ubs also include a Handles clause t hat associat es t hem w it h a class inst ance and an ev ent nam e. You can det erm ine how your applicat ion r esponds t o an ev ent by placing your own code inside t he sub procedur e. When y ou w rit e ev ent handlers for cust om classes, y ou m ay need t o cr eat e y our own st ub. I f y ou use t he Wit hEv ent s k eyw ord when y ou inst ant iat e an obj ect based on a class, y ou can use t he Visual St udio dev elopm ent envir onm ent t o creat e a st ub for y ou aut om at ically. When using t he Wit hEvent s k eyw or d, you m ust inst ant iat e your obj ect at t he m odule lev el. Wit hout t he Wit hEv ent s keyw ord, ev ent s don’t propagat e from a class t o an obj ect inst ance based on it . Est ablishing an associat ion bet w een an event handler and an ev ent w it h t he Wit hEv ent s k eyw ord requir es you t o specify t he ev ent handler at design t im e. The AddHandler and Rem ov eHandler st at em ent s allow you t o dy nam ically add and rem ov e a handler for an event at r un t im e. You can also use t hese st at em ent s at design t im e. Wit h t hese t w o st at em ent s, y ou don’t have t o inst ant iat e an obj ect using t he Wit hEv ent s keyw ord in order t o process ev ent s raised by t he obj ect . I n t ur n, t his m eans t hat y ou can inst ant iat e w it hin a procedur e or at t he m odule lev el. Recall t hat t he Wit hEv ent s k eyw ord r equires inst ant iat ion at t he m odule lev el. When using t he AddHandler st at em ent t o associat e an event w it h an ev ent handler, y ou m ust wr it e y our own st ub for t he ev ent handler. I will dem onst rat e how t o do t his in a sam ple t hat illust r at es t he use of t he AddHandler st at em ent .

Usin g Bu ilt - I n For m Ev e n t s Ther e ar e a couple of w ays of m anaging built - in ev ent s wit h Windows Form s and t heir cont r ols fr om t he Windows For m s Designer. Double- clicking a for m ’s capt ion in t he Windows Form s Designer opens t he st ub for t he form ’s default ev ent , t he Load ev ent , in t he Code Edit or. This sam e t echnique works for t he cont r ols on a for m . For ex am ple, double- click ing a but t on on a form opens t he st ub for t he but t on’s default ev ent , a Click ev ent . Aft er adding one or m or e cont r ols on a for m , y ou can select any event for any cont r ol in t he Code Edit or. Choose t he cont r ol nam e from t he Class Nam e dr op- dow n list at t he upper left of t he Code Edit or , and choose t he ev ent nam e fr om t he Met hod Nam e list at t he r ight . Aft er you click an ev ent for t he cont r ol, a st ub for t he ev ent pr ocedur e appears aut om at ically. To display a nondefault event for t he form , select ( Base Class Ev ent s) from t he Class Nam e list and t hen choose a desir ed ev ent fr om t he Met hod Nam e list . I f you search t hrough t he event s for a form or any of t he cont r ols on a form , you will quickly discover an exceedingly large array of ev ent s. Alt hough t he large num ber of ev ent s is useful for fine- grained cont rol ov er t he operat ion of an applicat ion, it m ay be difficult for som e program m ers t o discern t he order of t he ev ent s so t hey can k now which one t o use. The follow ing excerpt fr om t he Code Edit or for Form 4 in t he Ev ent sSam ples solut ion dem onst rat es a st rat er gy for t racking ev ent s. Wit hin each ev ent pr ocedur e is a MsgBox funct ion indicat ing which ev ent generat ed t he cur rent m essage box in an applicat ion. For exam ple, t he m essage box for t he form Load ev ent fir es befor e Form 4 is display ed. When you click t he form ’s Close but t on, y ou w ill not ice t hat t he Closing ev ent fir es pr ior t o t he Closed ev ent . See t he follow ing not e for det ailed inst ruct ions on m aking Form 4 t he st art up obj ect for t he Ev ent sSam ples solut ion.

N ot e A Windows applicat ion st art s by default wit h For m 1, which is t he obj ect t hat Visual St udio .NET m akes aft er opening a Windows applicat ion for design. By default , t he Windows applicat ion opens t o t his obj ect when you run t he solut ion. However, you can choose anot her obj ect for a Windows applicat ion t o open when it st art s t o r un. Right - click t he solut ion’s nam e in Solut ion Explor er , and choose Propert ies t o open t he Propert y Pages dialog box for t he solut ion. Use t he St art up Obj ect drop- dow n list t o select anot her obj ect . For ex am ple, select ing For m 4 will cause t his form t o open init ially when a user chooses t o run t he solut ion. Ev ent s som et im es fire so quick ly t hat m essage boxes can pile up and m ake discover ing t heir order confusing. I n cases lik e t his, y ou can som et im es set a propert y for an obj ect on t he form —and t hus change it s appearance—t o help indicat e t he order of ev ent s. The procedures for t he MouseEnt er , MouseHov er, and MouseLeav e ev ent s fr om But t on1 dem onst rat e t his appr oach. These ev ent procedur es change t he Back Color pr opert y for But t on1. I nit ially posit ioning t he m ouse over But t on1 changes t he BackColor pr opert y from it s default set t ing t o Syst em .Drawing.Color .Cyan. Because Visual St udio aut om at ically cr eat es a reference t o t he Syst em .Draw ing nam espace w hen it init ializes a Windows applicat ion, y ou can abbreviat e t he set t ing t o Color. Cyan. Leav ing t he m ouse ov er a but t on event ually invok es t he MouseHov er ev ent , which changes t he Back Color set t ing t o Sy st em .Draw ing.Color.Red. Rem ov ing t he m ouse from ov er t he but t on rest or es t he default BackColor set t ing of

Syst em .Drawing.Syst em Colors.Cont rol. Clicking But t on1 displays a m essage box and shift s t he focus fr om For m 4 t o t he m essage box. This But t on1_Click ev ent is ort hogonal t o t he MouseEnt er and MouseHover ev ent s in t hat click ing t he but t on can int errupt t he t r ansit ion fr om t he MouseEnt er event t o t he MouseHover event . Private Sub Form4_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load MsgBox(“Just before I load.”) End Sub Private Sub Form4_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing MsgBox(“From Closing event.”) End Sub Private Sub Form4_Closed(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Closed MsgBox(“From Closed event.”) End Sub Private Sub Button1_MouseEnter(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.MouseEnter Me.Button1.BackColor = System.Drawing.Color.Cyan End Sub Private Sub Button1_MouseHover(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.MouseHover Me.Button1.BackColor = System.Drawing.Color.Red End Sub Private Sub Button1_MouseLeave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.MouseLeave Me.Button1.BackColor = System.Drawing.SystemColors.Control End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MsgBox(“You clicked Button1”) End Sub Befor e proceeding t o a second sam ple, it m ay be useful t o r ev iew t he synt ax for an ev ent pr ocedure. Not ice t hat t hey ar e sub pr ocedur es m eant for operat ion in t he cur rent m odule, as specified by t he use of t he Privat e keyw ord. Pr ivat e m arks t he ev ent procedur e for exclusive use in t he cur rent m odule. The argum ent s list can offer you a way of changing t he operat ion of t he ev ent pr ocedur e. The nex t sam ple dem onst rat es t he use of an ev ent argum ent t o cont r ol t he behavior of t he Closing event . Aft er t he argum ent list , t he Handles clause specifies t he obj ect and ev ent t hat t he sub procedur e handles. You cont rol t he operat ion of t he ev ent procedur e by placing cust om code bet ween t he Sub and End Sub st at em ent s. The next select ion of ev ent pr ocedur es shows a pair of procedures for cont rolling how a user can close a form . When a user chooses t o close a form by clicking t he for m ’s Close but t on, t he applicat ion fir es t he Closing ev ent . This event occurs befor e t he form closes. By set t ing t he Cancel ev ent argum ent t o Tr ue in t he Closing event , you can block t he Close ev ent fr om occurr ing ( nam ely , t he form will rem ain open) . The default value for t he Cancel ev ent argum ent is False. You can use t his feat ur e t o perform ot her act ions j ust before closing a for m . For exam ple, y ou can display a m essage blocking t he operat ion of t he form ’s Close but t on and inst ruct ing t he user t o click a but t on t hat launches t he ot her act ions you want done befor e invok ing t he form ’s Close m et hod. Because t he Close

m et hod raises t he Closing event , y ou m ust const ruct t he form ’s Closing ev ent procedur e t o opt ionally by pass set t ing t he Cancel argum ent t o Tr ue. The follow ing code excerpt for For m 5 dem onst r at es how t o disable a form ’s Close but t on and redirect t he user t o a but t on on t he for m . The solut ion uses t wo ev ent s. First t he Form 5_Closing event procedure blocks t he Close ev ent from occurr ing by set t ing t he Cancel ev ent argum ent t o bolDisableClose. The m odulelevel declarat ion for bolDisableClose set s t he variable’s default value t o Tr ue. The I f…Then…Else st at em ent in t he pr ocedur e displays a m essage box dir ect ing t he user t o click But t on1 t o close t he form . The Click ev ent pr ocedur e for But t on1 set s bolDisableClose t o False befor e inv ok ing t he Close m et hod for t he Me keyw ord t hat r efers back t o t he curr ent form , w hich is Form 5 in t his case. The inv ocat ion of t he Close m et hod, in t ur n, launches t he Form 5_Closing event procedur e, but t his t im e t he pr ocedur e t ak es a different pat h t hr ough it s I f…Then…Else st at em ent because of t he new value for t he bolDisableClose variable. ‘bolDisableClose controls Cancel argument. Dim bolDisableClose As Boolean = True ‘Conditionally block close of form. Private Sub Form5_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing If bolDisableClose Then e.Cancel = bolDisableClose MsgBox(“Click Button1 to close form.", , _ “After clicking Close button”) Else MsgBox(“From form’s Closing event.", , _ “After clicking Button1”) End If End Sub ‘Enable form close by setting bolDisableClose to False. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ’Perform any other necessary actions before closing Form5. bolDisableClose = False Me.Close() End Sub

Pr oce ssin g Eve n t s Using t h e W it hEv e n t s Ke yw or d The ev ent processing t echniques for t his sam ple and t he next t w o all em anat e from Form 1 in t he Event sSam ple solut ion. Figur e 9- 10 shows t his form t wo different way s. At left is t he form as it looks in t he Windows Form Designer—in design v iew. At r ight is t he form as it appears when y ou r un t he Ev ent sSam ple.ex e file. The differences bet w een t he t wo v iews of t he form ar e t he result of t he Form 1_Load ev ent pr ocedur e. ( See t he follow ing sam ple.) This procedur e adds t ext t o som e cont rols and clears it from ot her cont rols. I n addit ion, it form at s t he alignm ent for t he label and t ext cont r ols as well as resizes t he default Widt h propert y set t ing for t he but t on cont rols. This t ransfor m at ion dem onst rat es a use for t he form Load event t hat m akes it easy t o spot changes t o t he default set t ings for t he cont r ols on a form . I f you need t o duplicat e form set t ings acr oss m ult iple form s or syst em at ically change set t ings across m ult iple for m s, t his k ind of procedure can prove especially conv enient .

Figu r e 9 - 1 0 . Usin g a form Loa d e ven t pr oce d u r e t o d ocu m e n t you r form a t se t t in gs for a for m ca n h elp in docu m en t ing t h ose se t t in gs an d ap plyin g t h ose se t t in gs in a u n ifor m w a y t o m u lt iple form s in a n a pplica t ion .

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ’Set selected properties for form controls at load time. ’Set Text and Width properties for Button1. Button1.Text = “Add" Button1.Width = 90 ’Set TextAlign property for text boxes. TextBox1.TextAlign = HorizontalAlignment.Right TextBox2.TextAlign = HorizontalAlignment.Right TextBox3.TextAlign = HorizontalAlignment.Right ’Set Text property for labels. TextBox1.Text = “" TextBox2.Text = “" TextBox3.Text = “" ’Set TextAlign align property for labels. Label1.TextAlign = ContentAlignment.MiddleRight Label2.TextAlign = ContentAlignment.MiddleRight Label3.TextAlign = ContentAlignment.MiddleRight ’Set Text property for text boxes. Label1.Text = “Byte1" Label2.Text = “Byte2" Label3.Text = “Sum" ’Set Text and Width properties for Button2. Button2.Text = “Open Form2" Button2.Width = 90 ’Set Text and Width properties for Button3. Button3.Text = “Open Form3" Button3.Width = 90 ’Set Text and Width properties for Button4. Button4.Text = “Close App" Button4.Width = 90 End Sub

The for m at right in Figur e 9- 10 per m it s a user t o ent er t wo Byt e value t ype quant it ies in t he Byt e1 and Byt e2 t ext box es. When a user clicks t he Add but t on, t he form r et urns t he t ot al of t he t w o quant it ies in t he Sum t ext box . I f t he sum happens t o exceed 255, which is t he m ax im um legit im at e Byt e value, t he applicat ion displays a m essage box w it h a r em inder of t he pr oblem . Because t he applicat ion com put es t he sum as a Decim al value t ype, ret ur n values great er t han t he m ax im um don’t generat e a run- t im e er ror. Howev er, you can raise an ev ent t hat ident ifies sum s great er t han 255. I f a user ent ers a value gr eat er t han 255 in eit her t he Byt e1 or Byt e2 t ext box, Visual Basic raises an error because t he Add but t on’s Click ev ent pr ocedur e uses t he CByt e funct ion t o conv ert t he t ext box values t o Byt e dat a t ypes. Befor e rev iew ing t he code behind Form 1 t hat m anages t he operat ion of t he form , it w ill be useful t o exam ine t he code list ing for t he Byt eArit hm et ic class. Form 1 relies on t he class t o save t he values in t he t wo input t ext box es, com put e t he sum , and raise t he ev ent . The class list ing includes an ev ent declarat ion, a Privat e st at em ent for declaring t wo int ernal var iables, t wo propert y pr ocedur es, and a funct ion pr ocedur e. The Public accessibilit y qualifier for t he event declar at ion at t he t op of t he list ing m akes t he ev ent available t hr oughout t he Ev ent sSam ple solut ion assem bly. I f By t eArit hm et ic ex ist ed as a st and- alone class proj ect wit h a .dll ext ension, t he Public declarat ion would per m it t he accessibilit y of t he event in ot her pr oj ect s t hat r efer ence t he .dll file. The propert y pr ocedur es nam ed Byt e1 and Byt e2 can accept conv ert ed dat a fr om t ext boxes on Form 1. The class represent s t hese pr opert y set t ings int er nally w it h t he byt 1 and byt 2 var iables, which ar e declared direct ly below t he ev ent declarat ion. A funct ion procedur e, Add2byt e, in Byt eArit hm et ic com put es t he sum and condit ionally raises an er ror. This procedure com put es t he sum of t he t wo t ypes as a Decim al value t ype, w hich it saves in t he m ysum var iable. This design feat ur e av oids t he pot ent ial of a r un- t im e er ror fr om a sum t hat exceeds t he Byt e value lim it . How ev er, Add2byt e also checks for sum s t hat exceed 255. When it finds a sum t hat exceeds t he m ax im um Byt e value, it raises t he TooHigh ev ent and ret ur ns as an event argum ent t he m ysum v ariable value. The Add2byt e procedur e list ing concludes wit h a Ret ur n st at em ent t hat passes back t he value of m ysum t o t he procedure t hat invoked t he Byt eAr it hm et ic class inst ance. Public Class ByteArithmetic ’You need to declare an event before you can raise it. Public Event TooHigh(ByVal ReturnValue As Decimal) ’Local variables for property values. Private byt1, byt2 As Decimal ’Property procedures for Byte1 and Byte2. Public Property Byte1() As Byte Get Return byt1 End Get Set(ByVal Value As Byte) byt1 = Value End Set End Property Public Property Byte2() As Byte Get Return byt2 End Get Set(ByVal Value As Byte) byt2 = Value End Set End Property

’Function procedure for the Add2byte method. Function Add2byte() As Decimal Dim mysum As Decimal ’Compute mysum. mysum = byt1 + byt2 ’Raise event if sum is too high. If mysum > 255 Then RaiseEvent TooHigh(mysum) Return mysum End Function End Class The next code excerpt shows t he code behind Form 1 t hat works w it h t he Byt eAr it hm et ic class. The list ing st art s wit h a declarat ion of an inst ance nam ed ba for t he Byt eArit hm et ic class. There ar e t w o especially im port ant feat ur es of t his declarat ion. First , t he declarat ion includes t he Wit hEv ent s k eyw ord. This allows Form 1 t o process ev ent s raised by ba. Second, t he declarat ion occurs at t he m odule lev el. This is m andat ory when y ou use t he Wit hEvent s k eyw ord in t he declarat ion for a class inst ance. I generat ed t he st ub for t he But t on1_Click ev ent procedure by double- clicking t he cont r ol in t he Windows Form s Designer. The ev ent pr ocedur e is generat ed by t he double click on t he cont rol because Click is t he but t on’s default ev ent . Wit hin t he Click event are t wo blocks of code. First t he procedur e conv ert s t he t ex t box ent r ies wit h t he CBy t e funct ion t o Byt e value t y pes from t heir nat iv e St ring value t ypes. Second t he pr ocedur e inv ok es t he Add2byt e m et hod for t he ba class inst ance and st or es t he ret ur n value as a st r ing in Text Box 3. The second procedur e is t he event handler for t he TooHigh event from t he ba class inst ance. I n t he Code Edit or for Form 1, y ou can cr eat e t he st ub for t he ev ent procedure aut om at ically by choosing ba in t he Class Nam e box and click ing TooHigh in t he Met hod Nam e box t o it s r ight . Aft er Visual St udio cr eat ed t he st ub, I had t o add j ust one line of code, which pr esent s a m essage box r em inding t he user t hat t he sum is t oo large for a legit im at e Byt e value. The m essage box also cont ains t he value r et urned as t he sum . ‘WithEvents keyword must apply to module-level declaration; ‘the keyword permits events to pass from event source (ByteArithmetic ). Private WithEvents ba As New ByteArithmetic() Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ’Copy converted text box entries to Byte1 and ’Byte2 properties in ByteArithmetic class. ba.Byte1 = CByte(TextBox1.Text) ba.Byte2 = CByte(TextBox2.Text) ’Display result of addition in TextBox3. TextBox3.Text = (ba.Add2byte).ToString() End Sub

’Handles clause in event sub stub requires a WithEvents keyword ’in variable declaration for event source (ByteArithmetic). Private Sub ba_TooHigh(ByVal ReturnValue _

As Decimal) Handles ba.TooHigh ’Display event message. MsgBox(“Sum of “ & ReturnValue & “ too large for byte value.”) End Sub Figur e 9- 11 shows t he out com e of t ry ing t o add 4 and 252 w it h t he applicat ion det ailed in t he preceding t wo code segm ent s. Each quant it y alone is a legit im at e Byt e v alue. However, t heir sum exceeds t he m axim um Byt e value. Ther efor e, t he applicat ion displays t he m essage box shown at t he left of Figure 9- 11 befor e populat ing Text Box3 w it h t he r et urn value from t he Byt eAr it hm et ic inst ance. Form 1 appears on t he r ight side of Figur e 9- 11, wit h t he t wo input values and t heir sum . To proper ly close t he solut ion, a user m ust click t he Close App but t on. Figu r e 9 - 1 1 . You can u se a cu st om e ven t t o disp la y a m e ssa ge b ox .

Pr oce ssin g Eve n t s w it h t h e AddH a n dle r St a t e m e nt Below t he t hird t ext box in Figur e 9- 11 is t he Open Form 2 but t on. Click ing t his but t on opens a second form t hat dem onst rat es how t o use t he AddHandler st at em ent t o process a raised event . Form 2 has j ust t wo but t ons. That ’s because t his form uses t he t ext boxes on Form 1 t o display input and out put from t he inst ance of Byt eArit hm et ic t hat it declares. Ther efor e, anot her benefit of t his sam ple is t hat it r ev eals how t o pass v alues back and fort h bet ween t w o form s. The only way t hat t he applicat ion will open For m 2 is by a click t o t he Open Form 2 but t on on Form 1. The applicat ion’s logic requir es t hat t her e be num eric ent ries in t he Byt e1 and Byt e2 t ex t box es befor e t he click. Failing t o populat e t he t ext boxes w it h appropr iat e values befor e t he click will generat e a r un- t im e err or . The Click event procedur e for But t on2 on Form 1 follows. Not ice t hat t he But t on2_Click ev ent procedure com m ences by inst ant iat ing an inst ance of Form 2 and referencing it wit h t he fr m Form 2 v ariable. Wit h t he frm Form 2 var iable, t he event procedur e can t hen access elem ent s in t he code behind Form 2. As a subsequent list ing shows, t wo of t hese elem ent s ar e var iables nam ed frm 2byt e1 and frm 2byt e2. The assignm ent st at em ent s in t he Click event procedur e dem onst rat e t he synt ax for copying convert ed t ext box values from one form ( For m 1) t o

variables in t he m odule behind anot her form ( Form 2) . The event procedur e concludes by showing Form 2 and hiding Form 1. Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ’Instantiate a new instance of Form2 class. Dim frmForm2 As New Form2() ’Populate variables in module behind Form2 with ’Text property settings of text boxes in Form1. frmForm2.frm2byte1 = CByte(TextBox1.Text) frmForm2.frm2byte2 = CByte(TextBox2.Text) ’Show Form2 in a modeless window and ’hide currently open form (Form1). frmForm2.Show() Me.Hide() End Sub For ease in grasping how t he program m ing elem ent s behind Form 2 work am ong t hem selves and int erplay w it h Form 1, t he follow ing list ing includes t he whole m odule behind Form 2. The paragraphs aft er t he list ing select ively highlight different aspect s of t he code. Public Class Form2 Inherits System.Windows.Forms.Form ‘Region for “ Windows Form Designer generated code “ ’You can instantiate variables for classes within a procedure ’when you specify event handler with AddHandler statement. Howeve r, ’to make ba class instance available in two procedures, this ’sample declares ba variable at the module level. Dim ba As New ByteArithmetic() ’Declare variables. Public frm2byte1, frm2byte2 As String Private temp As Decimal Private Sub Form2_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ’Set Text and Width properties for Button1. Button1.Text = “Add" Button1.Width = 90 ’Set Text and Width properties for Button2. Button2.Text = “Open Form1" Button2.Width = 90 ’Assign values to Byte1 and Byte2 properties. ba.Byte1 = frm2byte1 ba.Byte2 = frm2byte2 End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

’Designate ba_TooHigh sub procedure to process ba.TooHigh eve nt AddHandler ba.TooHigh, AddressOf ba_TooHigh 'Assign the return value from Add2byte to temp and display th e 'method inputs and outputs if the Add2byte method return valu e 'is less than or equal to 255. temp = ba.Add2byte If temp