SQL Server XQuery and XML.docx

上传人:b****5 文档编号:7406973 上传时间:2023-05-11 格式:DOCX 页数:60 大小:498.37KB
下载 相关 举报
SQL Server XQuery and XML.docx_第1页
第1页 / 共60页
SQL Server XQuery and XML.docx_第2页
第2页 / 共60页
SQL Server XQuery and XML.docx_第3页
第3页 / 共60页
SQL Server XQuery and XML.docx_第4页
第4页 / 共60页
SQL Server XQuery and XML.docx_第5页
第5页 / 共60页
SQL Server XQuery and XML.docx_第6页
第6页 / 共60页
SQL Server XQuery and XML.docx_第7页
第7页 / 共60页
SQL Server XQuery and XML.docx_第8页
第8页 / 共60页
SQL Server XQuery and XML.docx_第9页
第9页 / 共60页
SQL Server XQuery and XML.docx_第10页
第10页 / 共60页
SQL Server XQuery and XML.docx_第11页
第11页 / 共60页
SQL Server XQuery and XML.docx_第12页
第12页 / 共60页
SQL Server XQuery and XML.docx_第13页
第13页 / 共60页
SQL Server XQuery and XML.docx_第14页
第14页 / 共60页
SQL Server XQuery and XML.docx_第15页
第15页 / 共60页
SQL Server XQuery and XML.docx_第16页
第16页 / 共60页
SQL Server XQuery and XML.docx_第17页
第17页 / 共60页
SQL Server XQuery and XML.docx_第18页
第18页 / 共60页
SQL Server XQuery and XML.docx_第19页
第19页 / 共60页
SQL Server XQuery and XML.docx_第20页
第20页 / 共60页
亲,该文档总共60页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

SQL Server XQuery and XML.docx

《SQL Server XQuery and XML.docx》由会员分享,可在线阅读,更多相关《SQL Server XQuery and XML.docx(60页珍藏版)》请在冰点文库上搜索。

SQL Server XQuery and XML.docx

SQLServerXQueryandXML

SQLServer2005XQueryandXML-DML

"AnOverviewofXMLSupportinSQLServer2005"

 

Eversincethemid90’s,asXMLhasevolvedtobecometheoptimumwaytoshare,transportandpersistdata,developershavesoughtefficientwaystostore,manipulateandgenerallytakeadvantageofitscapabilities.Increasinglyfastandeasy-to-useXMLparsershavebeendeveloped,transportprotocolssuchasSOAPhavebeenusedtoimplementWebServicesaroundXML,andmanyapplications,toolsandprogramminglibrariesnowincludefeaturestoimportandexporttheirdataasXML.

 

However,theoneareathathasseenlessdevelopment,particularlyintheMicrosoftworld,istheimplementationofefficientandrobusttechnologiesforpersistingXML.XMLisbasicallyjusttext,andthemostcommonpersistenceformatisasadiskfileinsomestandardtext-basedformatsuchasANSI,Unicode,etc.Thisisgenerallyfineforsingle-userapplications,butdoespromptsomeseriousquestionswhenusedinaserver-basedand/ormulti-userenvironment.

 

ThisandthetwosubsequentarticleslookathowthelatestversionofMicrosoft’senterprise-leveldatabase,SQLServer2005,nowoffersgreatsupportforandcloseintegrationwithXMLasadatapersistenceformat.Thisincludesnewwaystovalidate,storeandqueryXMLdocumentsthatarestoredwithinthedatabase.SQLServer2005providesnativesupportforXMLthatcanvastlyimproveapplicationperformance,whilesupportingrobustandsafemulti-useraccesstothedatacontainedwithintheXMLdocuments.

 

Thetopicswe’llbecoveringinthisarticleare:

 

∙AbriefoverviewofthewaythatSQLServer2005storesXMLdocumentsandschemas

∙HowSQLServer2005providessupportforqueryingandmanipulatingXMLdocuments

∙AsimpletestapplicationthatallowsyoutoexperimentwithXQuery

 

Intwosubsequentarticles,we'llseesometechniquesforimprovingtheperformanceofapplicationsthatworkwithXMLdocuments,aswellassomeexamplesofthedifferentwaysyoucanuseXQuery:

 

∙Extractingdatafromxmlcolumns,usingparameterwithXQueryandcombiningXQueryandXSL-T

∙Updatingthecontentsofxmlcolumns,andusingXQueryinamanagedcodestoredprocedure

AnOverviewofXMLSupportinSQLServer2005

SQLServer2005addsaraftofnewfeaturestosupportXMLdatastorageandmanipulation.ThesefeaturesmakeiteasiertopersistyourXMLdocumentswithinthedatabase,whileprovidingincreasedperformanceoverthetraditionaltechniques.We’llbecomparingthesetraditionaltechniques,andseeinghowyoucanimprovetheperformanceofyourapplications,inParts2and3.Forthemoment,however,we’llbrieflyexplorethenewfeaturesinSQLServer2005.Theseinclude:

 

∙AdedicateddatatypenamedxmlthatcanbeusedtostoreXMLdocumentsorfragmentsofXML

∙TheabilitytoregisterXMLschemaswithSQLServer2005,andstoreschemainformationwithinthedatabase

∙AutomaticvalidationofXMLdocumentswhenaschemaispresent;andautomaticshreddingoftheXMLdatatosupportefficientqueryingandupdatingofthecontent

∙AnimplementationofasubsetoftheW3CXQuerylanguageandXML-DMLtoprovidethisqueryingandupdatefacility

∙Supportforhostingthe.NETCommonLanguageRuntime(CLR)withinSQLServer,whichallowsstoredproceduresthatmanipulateXMLdocumentstobewritteninmanagedcode

 

You’llseehowallthesefeaturescomeintoplaythroughoutthesethreearticles,andhowtheyopenupnewtechniquesforworkingwithXMLdocumentsandXMLdata.

XMLSchemasandtheW3CInfosetModel

Inrecentyears,it’sbecomeincreasinglyobviousthatthemajorusesofXMLareasawayofstoringbothrowset(singletable)andhierarchical(multiple-table)data,ratherthanunstructuredinformationsuchasnewspaperarticles.Forexample,acommonuseofWebServicesin.NETapplicationsistoexposedatathatrepresentsaDataSetinaformatthatallowsdiscoveryandtransmissionacrossHTTPnetworkssuchastheInternet.TheDataSetmaycontainasingletable,ormultipletablesthatarerelatedthroughprimaryandforeignkeys,andtheXMLdatacanbeusedtocompletelyreconstructthatDataSetontheclient.

 

TospecifythedatatypeforanelementoranattributeinanXMLdocumentyouuseaschema.Thisindicates,forexample,whetheravaluesuchas"42"(whichisstoredasatextstringwithintheXML)representseitheracharacterstringoranumericvalue.TheclientcanthenreconstructthedatastoredintheXMLdocumentsothatitisaccessibleastheappropriatedatatypes.ThisisattheheartoftherecentmovestowardstheXMLInformationSet(Infoset)model,whicheffectivelyconsidersanXMLdocumentasoneormoretypedrowsets.

 

FordetailsoftheW3CInfosetrecommendation,seehttp:

//www.w3.org/TR/xml-infoset/

 

ThismeansthatyoumustexposeanXMLSchema(ortherelevantschemainformation)foreveryXMLdocumentorfragmentinordertotakeadvantageoftheInfosetmodelanddata-typingoftheXMLcontent.SQLServer2005makesthiseasybyprovidingaschemarepositorythatyoucanusetostoreXMLschemas,anditwillautomaticallyusetheappropriateschematovalidateandstoreXMLdata.

TheXMLSchemaRepository

SchemasareaddedtoadatabasebyexecutingtheCREATEXMLSCHEMACOLLECTIONstatement,forexample:

  

CREATEXMLSCHEMACOLLECTIONMyNewSchemaCol'

schemaxmlns="...">

  ...schemacontent...

schema>'

 

Youcanaddmultipleschemasinonegobyconcatenatingthemtogether,usetheALTERXMLSCHEMACOLLECTIONstatementtoaddorremoveindividualschemasinacollection,andremovethecollectionusingtheDROPXMLSCHEMACOLLECTIONstatement.SeetheSQLServerhelpfilesformoredetails.

 

Thenameyouassigntothecollection("MyNewSchemaCol"inthecodeabove)isusedintheALTERandDROPstatements,andisdisplayedinSQLServerManagementStudio.However,youshouldincludeatargetNamespaceattributeintheopeningelementtoidentifyeachschemainthecollection,forexample:

 

schemaxmlns:

xs="http:

//www.w3.org/2001/XMLSchema"

          targetNamespace="http:

//myns/mydemoschema">

 ...

 

ThenyoulinkyourXMLdocumentstotheappropriateschemabyspecifyingthisnamespace:

 

xmlversion="1.0"encoding="utf-8"?

>

//myns/mydemoschema">

 ...

 

YoucanuseSQLServerManagementStudiotoviewandmanageschemasandschemacollections.Forexample,Figure1showstheschemacollectionsintheAdventureWorkssampledatabasethatyoucandownloadandinstallinSQLServer2005.

 

Figure1-TheschemacollectionsintheAdventureWorkssampledatabase

TheNewxmlNativeDataType

SQLServer2005supportsanewnativedatatypenamedxmlthatyouuseinexactlythesamewayasanyotherbuilt-indatatype.Youcanuseittodefineacolumntypeforatable,asaparameterorvariableinastoredprocedure,andanywhereelseyouwouldusebuilt-intypessuchasnvarchar,int,etc.ThexmltypecanstoreeitheracompleteXMLdocument,orafragmentofXML,aslongasitiswell-formed(youcannotuseanxmltypetostoreXMLthatisnotwell-formed).

TypedandUn-typedxmlColumns

WhenyouprovideaschemafortheXMLdocumentsyouwillstore,youcancreateatypedxmlcolumninatable.Youspecifythenameoftheschemacollectionthatcontainstheschemayouwanttoapplytothatcolumn,forexample:

  

CREATETABLEMyTable(MyKeyint,MyXmlxml(MyNewSchemaCol))

 

NowthecontentoftheXMLdocumentyouinsertintothatcolumnwillbeshreddedautomaticallyintoitsindividualdataitems,andSQLServerwillstoretheseinternallyinthemostefficientandcompactwaypossible.Whenyouquerythecolumn,SQLServerautomaticallyreconstructstheXMLdocumentintoitsoriginalform.Note,however,thatthiswillnotincludethingslikecommentsthatarenotpartoftheoriginaldatacontentofthedocument.Whatyougetbackiseffectivelyaserializedrowsetthatrepresentsthedatayouoriginallystoredthere.

 

It'salsopossibletostoreyourXMLdocumentswithoutspecifyingaschema,inwhichcaseyoucreateanun-typedxmlcolumn.Inthiscase,theXMLisstoredasasimplecharacterstring,becauseSQLServerhasnowayofknowingthedatatypeofeachelementandattribute.Thisislessefficient,butdoesmaintainthecompleteoriginalcontentoftheXMLdocument(suchascomments,etc.).Butrememberthat,evenwithanun-typedcolumn,theXMLyouinsertmustbewell-formed.

 

Tocreateanun-typedcolumn,yousimplyomittheschemacollectionnamewhenyoucreatethetable:

 

CREATETABLEMyTable(MyKeyint,MyXmlxml)

 

Figure2showsSQLServerManagementStudiodisplayingthestructureoftheSales.StoretableintheAdventureWorkssampledatabase.Youcanseethexml-typedcolumnnamedDemographicsintheleft-handtreeview,andaquerythatextractstherowsfromthistableintheright-handquerywindow.Theresultsofrunningthisqueryareshowninthegridbelowthis,andwe'vesuper-imposedonthistheviewoftheXMLdocumentyougetwhenyouclickonthecontentsofoneofthecolumnsinthegrid.

 

Figure2-ThexmlcolumnintheSales.Storetable,showingoneoftheXMLdocumentsitcontains

InsertingandSelectingonanxmlColumn

Onceyouhavecreatedyourtable,youinsertanXMLdocumentintoanxmlcolumninthesamewayasyouwouldforanyotherbuilt-indatatype.Youcaninsertitasastringvalue,orusetheCASTorCONVERTfunctionstospecificallyconvertittoanxmltype:

 

INSERTINTOMyTable(MyKey,MyXml)

VALUES(1,'xml-document-string')

 

INSERTINTOMyTable(MyKey,MyXml)

VALUES(1,CAST('xml-document-string')ASxml))

 

INSERTINTOMyTable(MyKey,MyXml)

VALUES(1,CON

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2