1 / 20

BÁO CÁO MÔN XML

BÁO CÁO MÔN XML. Đề Tài : SỬ DỤNG XML TRONG SQL SERVER. Giáo viên HD: KS. Nguyễn Văn Phúc Nhóm thực hiện : Nguyễn Đăng Anh 0812519 Ngô Văn Hậu 0810282 Lê Quang Hoàng 0812570. 3. 3. Tổng quan. Demo. 3. 1. Trình bầy nội dung chính. 2. Nội dung báo cáo.

raven-booth
Download Presentation

BÁO CÁO MÔN XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BÁO CÁO MÔNXML Đề Tài : SỬ DỤNG XML TRONG SQL SERVER Giáoviên HD: KS. Nguyễn Văn Phúc Nhómthựchiện: NguyễnĐăngAnh 0812519 NgôVănHậu 0810282 LêQuangHoàng 0812570

  2. 3 3 Tổngquan Demo 3 1 Trìnhbầynội dung chính 2 Nội dung báocáo

  3. TổngQuanVề Xml TrongSql Server Tạisaolạisửdụngkiểudữliệu Xml trongSql Server.? • Việclưtrữdữliệu xml trongcơsởdữliệuquanhệ. Manglạilợiíchchoviệcquảnlývàxửlýdữliêu. • Dữliệu xml cóthểtươngthíchvớidữliệuquanhệ. Điềunàycónghĩadữliệu xml cóthểđượcđưavàohệthốngnhữngdữliệuphátsinhmàkhôngảnhhưởngđếnứngdụngđangchạy.

  4. NỘI DUNG CHÍNH • Kiểudữliệu Xml trong SQL SERVER • Truyvấnvàchỉnhsửdữliệu Xml trong SQL SERVER • Sửdụngmệnhđề FOR XML trongSql Server • Sửdụnghàm OPENXML() trongSql Server

  5. KiểuDữLiệu XML • Kiểudữliệu XML đượchỗtrợtừSql Server 2000. Giúpchoviệclưutrữdữliệu Xml hay cácphânđoạn Xml trongcơsởdữliệu. • Cóthểsửdụngkiểu Xml gầngiốngnhưnhữngkiểugiữliệuthôngthườngkhác. • Chúngtacóthểdùngnhưlà : • Kiểudữliệubiến DECLARE @SinhVien_Infoxml SET @SinhVien_Info = ‘<SinhVien> • <Ho>Ngo</Ho> • <Ten>Van Hau</Ten> </SinhVien>’

  6. KiểuDữLiệu XML • Dùng để tạo cột như kiểu dữ liệu thông thường khác. CREATE TABLESinhVien ( MSSV varchar(7),ThongTinSVxml ) AFTER TABLE SinhVienADDThongTinSVxml • Lưu ý khi tạo cột : • Khôngthểdùngnhưkhóachính hay khóangoại • Cóthểtạoràngbuộcnhưngkhôngthểdùngcácphươngthứccủakiểudữliệu xml đểtạo. Màphảitựđịnhnghĩamộtchứcnăngbaobọccácphươngthứccủakiểudữliệ xml.

  7. KiểuDữLiệu XML • Tạo view chứadữliệu xml. • Dùngnhưthamsốkhitạoproecdurehoặcfunction • Kiểudữliệu xml cũngcóthểlàkiểutrảvềcủahàm. • Kiểudữliệu xml cungcấpnămphươngthứcđểtacóthểtruyvấnchỉnhsửadữliệunày: Query(), Value(), Exist(), Nodes(), Modify(). Chúngtasẽtìmhiểukỹhơntrongphầntruyvấndữliệu Xml trongSql Server. • Hạnchếkhisửdụngkiểudữliệunày.

  8. TruyVấn & ChỉnhSửaDữLiệu Xml • Đểtruyvấnvàchỉnhsửadữliệu xml tatrongSql Server taquantâmđếnhaiphần: Xqueryvà XML DML • Xquery : chotakhảnăngtruyvấndữliệu xml kếthợpvớicácphươngthứcmàkiểudữliệu xml cungcấp. SELECT @sinhvienInfo.query('/DSSinhVien/SinhVien') • Các phương thưc dùng để truy vấn dữ liệu xml • Query() : Dùng khi muốn lấy về một phần hoặc một phân đoạn trong tài liệu xml (xml collumn, xml variable)

  9. TruyVấn & ChỉnhSửaDữLiệu Xml Cú pháp : Query(‘XqueryExpression’). • DECLARE @sinhvienInfo xml • SET @sinhvienInfo = • '<DSSinhVien> • <SinhVien MSSV="0810282"> • <Ho>Ngo</Ho> • <Ten>Van Hau</Ten> • <NS>15-06-1990</NS> • </SinhVien> • </DSSinhVien>' • SELECT @sinhvienInfo.query('/DSSinhVien/SinhVien‘) • XqueryExpression: là một path chỉ ra vị trí của phần dữ liệu cần lấy. • Value ( ‘XqueryExpression’, ‘SqlType’) : Dùngkhimuốnlấydữliệucủamột node hay mộtthuộctính. ‘SqlType‘làkiểudữliệucủagiatrịmuốnlấyvề (int, varchar(n)) SELECT @sinhvienInfo.value('/(DSSinhVien/SinhVien/@MSSV)[1]‘, varchar(7))

  10. TruyVấn & ChỉnhSửaDữLiệu Xml • Exist(‘XqueryExpression’) : dùngkhitacầnkiểmtramộtphầnnàođócủatàiliệu xml cótồntại hay không. • XqueryExpression: là một path chỉ ra vị trí của phần cần kiểm tra. • Nodes( ‘XqueryExpression’) : Dùngkhimuốnlấymộtthànhphầnnàođócủadữliệu xml vàchuyểnnóthànhdữliệuquanhệ. SELECT @sinhvienInfo.exist('/DSSinhVien/SinhVien[@MSSV eq xs:string("0810282")]') -- Resault = 1 SELECT DSSinhVien.SinhVien.value('(/DSSinhVien/SinhVien/Ten)[1]','varchar(50)') AS Ten, DSSinhVien.SinhVien.query('/DSSinhVien/SinhVien') AS RESULT FROM @sinhvienInfo.nodes('/DSSinhVien/SinhVien') DSSinhVien(SinhVien)

  11. TruyVấn & ChỉnhSửaDữLiệu Xml • DML(Data Modify Language) Làngônngữdùngđểchỉnhsửadữliệu xml trongSql Server. Với DML tacóthểxóa, cậpnhật, thêmmột node vàotàiliệu xml vớicácthaotáclà • Insert • Delete • Replace value of Đểcóthểchỉnhsửadữliệu xml tadùngphươngthức modify kếthợpvớibacâulệnhtrên.

  12. TruyVấn & ChỉnhSửaDữLiệu Xml • CâulệnhInsert cúphápnhưsau : • Expression1:làmộtchuỗivớicúpháp xml, cóthểlàmột node hoặcnhiều node cầnchènvàotàiliệu xml. • Expression2: LàmộtbiểuthứctruyvấnXquerydùngđểchỉravịtrícầnchènexpression1trongtàiliệu xml • INSERT Expression1 • ( • (as first | Last) into | after | before • Expression2 • ). SET @s.modify ('insert <Lop>CTK32</Lop> as last into(/DSSinhVien/SinhVien[ @MSSV = "0810282" ])[1]'); SET @s.modify ('insert attribute Sex{“Nam”} into(/DSSinhVien/SinhVien[ @MSSV = "0810282" ])[1]');

  13. TruyVấn & ChỉnhSửaDữLiệu Xml • Câulệnhdelete Cúphápnhưsau: delete expression. Epressionlàvịtrícủa node muốnxóa . • Câulệnhreplace value of SET @s.modify ('delete (/DSSinhVien/SinhVien[ @MSSV = "0810282" ]/NS)[1]'); Replace value of Expression1 With Expression2 SET @s.modify ('replace value of (/DSSinhVien/SinhVien/@Oder)[1] With “BCD”');

  14. SửDụngMệnhĐề FORXML SELECT [*,nameCollomn,…] FROM [nameTable,…] [WHERE condition] FOR XML {RAW | AUTO | EXPLICIT} [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] • ChúngtacóthểchuyểndữliệuquanhệtrongkếtquảtrảvềcủaSELECT thànhdữliệudạng xml nhờmệnhđề FOR XML

  15. SửDụngMệnhĐề FORXML

  16. Sử Dụng Hàm OPENXML() • Cho phépđọcdữliệu xml nhưlàdữliệunằmtrêncác row vàchènbàobảng. • Khisửdụng OPENXML() taquantâmđếnhaithủtụcsau : sp_xml_preparedocument()vàsp_xml_removedocument() • idoc: làmộtgiátrịthamchiếuđếntàiliệu xml EXEC sp_xml_preparedocument @idoc OUTPUT, @doc OPENXML(idocint [IN], RowPatternnvarchar [IN], [Flags byte[IN]]) [WITH (SchemaDeclaration | TableName)]

  17. SửDụngHàm OPENXML() • RowPattern: làmộtXpathchỉđếnvịtrícầnlấydữliệutrongtàiliệu xml. • Flags : Làcácgiátrịtùychọndướibảngsau

  18. SửDụngHàm OPENXML() • SchemaDeclaration: chỉtêncáccộtsẽchứadữliệu Cóthểlàtênbảngsẽchứadữliệu.

  19. DEMO

  20. CẢM ƠN THẦY & CÁC BẠN THEO DÕI Q&A

More Related