資源描述:
《sqlserver操作xml整理》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、SQL操作XML整理=====================================================================SQL操作XML,實(shí)現(xiàn)導(dǎo)入功能----讀取XML---------declare@xmlvarchar(8000)set@xml=(SELECT*FROMOPENROWSET(BULK'D:peo.xml',SINGLE_CLOB)asx)select@xmlUnicode格式的文件用SINGLE_NCLOB.讀取XMLalterPROCEDUREusp_InsertShoppingCartOrder(@xmlnv
2、archar(4000))ASBEGINDECLARE@PointerINTEXECUTEsp_xml_preparedocument@PointerOUTPUT,@xmlINSERTINTOWebSales(ProductID,Price,SaleDate,SaleBatchID,CustomerID)SELECTProductID,Price,SaleDate,SaleBatchID,CustomerIDFROMOPENXML(@Pointer,'/ShoppingCart/Purchase')WITH(ProductIDINT,PriceMONEY,SaleDate
3、SMALLDATETIME,SaleBatchIDINT,CustomerIDINT)EXECsp_xml_removedocument@PointerEND生成XMLselect*fromusersFORXMLRAWselect*fromusersFORXMLautoselect*fromusersFORXMLXPLICITselect*fromusersFORXMLauto,ELEMENTS模式說(shuō)明RAW行集合的每個(gè)記錄都轉(zhuǎn)換成叫做行的XML元素。元素將包含一個(gè)屬性,用來(lái)表示所檢索的列。AUTO行集合記錄可以轉(zhuǎn)換成以FROM子句中的表命名的嵌套XML元素。所
4、檢索每一列都將表示為一個(gè)屬性EXPLICIT為格式化XML提供許多控制。不過(guò),EXPLICIT模式的使用語(yǔ)法要復(fù)雜得多。XSLT是一個(gè)比較常用的XML轉(zhuǎn)換方法。結(jié)合net:語(yǔ)言privatestringGetFORXML(stringsFile,stringsSQL){//Create和opentheconnectiontoNorthwindusing(SqlConnectionoCn=newSqlConnection(this.sCnNW)){oCn.Open();//CreatetheSQLcommandtoexecuteSqlCommandoCmd=newSqlCo
5、mmand(sSQL,oCn);DataSetoDs=newDataSet();//ExecutetheSQLstatementandreturnthedatatoanXmlReader.//ThenreadtheschemaandthefragmentXmlReaderoXml=oCmd.ExecuteXmlReader();oDs.ReadXmlSchema(oXml);oDs.ReadXml(oXml,XmlReadMode.Fragment);oCn.Close();oDs.DataSetName="root";oDs.WriteXml(this.sPath+sF
6、ile);return"WroteXMLtofile"+sFile;}}圖片字段轉(zhuǎn)換成BASE64讀?。篠ELECTEmployeeID,FirstName,LastName,PhotoFROMEmployeesWHERELastNameLIKE'D%'FORXMLRAW,BINARYBASE64不支持聚合:可以將聚合后的內(nèi)容插入表變量中,對(duì)表變量處理XMLDECLARE@TempTableTABLE(OrderIDINT,TotalMONEY)處理SELECTOrderID,TotalFROM@TempTableASOrderDetailsFORXMLAUTO使用FOR
7、XML時(shí)值得注意的最后一個(gè)問(wèn)題是任何專用的XML字符都將通過(guò)使用XML編碼來(lái)轉(zhuǎn)換。正如HTML可以轉(zhuǎn)換URL中的專用字符一樣,適當(dāng)形式的XML可以編碼專用字符。例如,如果<字符包含在數(shù)據(jù)中,則它將轉(zhuǎn)換成“<”。通過(guò)OPENXML插入CustomerCREATEPROCEDUREprInsertCustomerFromXML(@sXMLNTEXT)ASDECLARE@iDocINTEXECsp_xml_preparedocument@iDocOUTPUT,@sXMLINSERTINTOCustomer(CustomerID