SQL Server 2008 XML 첨삭 검사
create xml schema collection dbo.class_xsd as
'<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="class">
<xs:complexType>
<xs:attribute name ="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element >
<xs:element name="classes">
<xs:complexType>
<xs:sequence>
<xs:element ref="class" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element >
</xs:schema>
'
create table tblspeakers
(
speaker_id int primary key identity,
speaker_nm nvarchar(50),
speaker_country nvarchar(25),
speaker_xml xml (class_xsd) not null
)
--xml index:primary key
create primary xml index idx_1 on tblspeakers(speaker_xml)
--xml index :path
create xml index idx_a on tblspeakers(speaker_xml) using xml index idx_1 for path
-- xsd xquery 。
--
insert into tblspeakers values('Stephen Forte','USA','
<classes>
<class name="Writing Secure Code for ASP .NET"/>
<class name="Using XQuery to Query and Manipulate XML Data in SQL Server 2008"/>
<class name="SQL Server and Oracle Working Together"/>
<class name="Protecting against SQL Injection Attacks"/>
</classes>
')
insert into tblspeakers values('Richard Campbell','Canada','
<classes>
<class name="SQL Server Profiler"/>
<class name="Advanced SQL Querying Techniques"/>
<class name="SQL Server and Oracle Working Together"/>
<class name="T-SQL Error Handling in Yukon"/>
</classes>
')
insert into tblspeakers values('John Huckaby ','USA','
<classes>
<class name="Smart Client Stuff"/>
<class name="More Smart Client Stuff"/>
</classes>
')
insert into tblspeakers values('Malek Kemmou ','Morocco','
<classes>
<class name="SmartPhone 2005"/>
<class name="Office System 2003"/>
</classes>
')
insert into tblspeakers values('Goksin Bakir ','Turkey','
<classes>
<class name="SmartPhone 2007"/>
<class name="Office System 2007"/>
</classes>
')
insert into tblspeakers values('Jan Vasters ','Germany','
<classes>
<class name="SOA"/>
<class name="Biz Talk Services"/>
</classes>
')
insert into tblspeakers values('Monica L.Tripp','USA','
<classes>
<class name="SQL Server Index "/>
<class name="SQL Precon "/>
</classes>
')
insert into tblspeakers values('Bad Speaker','France','
<classes>
<class name="SQL Server Index "/>
<class name="SQL Precon "/>
</classes>
')
select * from tblspeakers
--xml.exist 1 0 null , xml
declare @xml as xml
set @xml='
<classes>
<class name="SQL Server Index "/>
<class name="SQL Precon "/>
</classes>
'
select @xml.exist('/classes')
--- check constraint
create function dbo.DesOrderXMLDataExist(@xml xml)
returns bit
as
begin
return @xml.exist('/Orders')
end
go
-- xml /Orders
create table OrdersXMLCheck
(
OrderDocID int primary key,
xOrders XML not null default '<Orders/>'
constraint xml_orderconstraint check(dbo.DesOrderXMLDataExist(xOrders)=1)
)
--
select * from tblspeakers where speaker_xml.exist('/classes/class[@name="SQL Precon "]')=1
/*
speaker_id speaker_nm speaker_country speaker_xml
----------- -------------------------------------------------- ------------------------- -------------------------------------------------------------------------------------
7 Monica L.Tripp USA <classes><class name="SQL Server Index " /><class name="SQL Precon " /></classes>
8 Bad Speaker France <classes><class name="SQL Server Index " /><class name="SQL Precon " /></classes>
(2 )
*/
--XML.VALUE xml 。。
select speaker_id,speaker_nm,speaker_country,
speaker_xml.value('/classes[1]/class[1]/@name','varchar(50)')'classname'
from tblspeakers
/*
speaker_id speaker_nm speaker_country classname
----------- -------------------------------------------------- ------------------------- ----------------------------------
1 Stephen Forte USA Writing Secure Code for ASP .NET
2 Richard Campbell Canada SQL Server Profiler
3 John Huckaby USA Smart Client Stuff
4 Malek Kemmou Morocco SmartPhone 2005
5 Goksin Bakir Turkey SmartPhone 2007
6 Jan Vasters Germany SOA
7 Monica L.Tripp USA SQL Server Index
8 Bad Speaker France SQL Server Index
(8 )
*/
--xml.query
-- xml.value , xml , 。
select speaker_id,speaker_nm,speaker_country,
speaker_xml.query('/classes[1]/class[1]' )'classname'
from tblspeakers
/*
speaker_id speaker_nm speaker_country classname
----------- -------------------------------------------------- ------------------------- --------------------------------------------------
1 Stephen Forte USA <class name="Writing Secure Code for ASP .NET" />
2 Richard Campbell Canada <class name="SQL Server Profiler" />
3 John Huckaby USA <class name="Smart Client Stuff" />
4 Malek Kemmou Morocco <class name="SmartPhone 2005" />
5 Goksin Bakir Turkey <class name="SmartPhone 2007" />
6 Jan Vasters Germany <class name="SOA" />
7 Monica L.Tripp USA <class name="SQL Server Index " />
8 Bad Speaker France <class name="SQL Server Index " />
(8 )
*/
-- xml.query
select speaker_id,speaker_nm,speaker_country,
speaker_xml.query('/classes /class ')as 'classname'
from tblspeakers
where speaker_country='Canada'
/*
for。。。
for $b in /classes/class return($b) xquery
*/
select speaker_id,speaker_nm,speaker_country,
speaker_xml.query('for $b in /classes /class return ($b)')as 'classname'
from tblspeakers
where speaker_xml.exist('/classes/class[@name="SQL Server Profiler"]')=1
---xml.query
select speaker_id,speaker_nm,speaker_country,
speaker_xml.query('
for $b in /classes/class
where $b/@name="Advanced SQL Querying Techniques"
return (<Sessions>{$b}<Speaker id="{sql:column("Speaker_ID")}">
{sql:column("Speaker_NM")}</Speaker></Sessions>)')as 'classname'
from tblspeakers
where speaker_xml.exist('/classes/class[@name="SQL Server Profiler"]')=1
/*
speaker_id speaker_nm speaker_country classname
----------- -------------------------------------------------- ------------------------- -------------------------------------------------------------------------------------------------------------------
2 Richard Campbell Canada <Sessions><class name="Advanced SQL Querying Techniques" /><Speaker id="2">Richard Campbell</Speaker></Sessions>
(1 )
*/
--XML DML
--xml.modify(insert)
update tblspeakers set speaker_xml.modify('
insert <class name="pppp"/>
into /classes[1]') where speaker_id=8
declare @newxml xml
set @newxml='<class name="Ranking and Windowing Functions in SQL Server 2008"/>'
update tblspeakers set speaker_xml.modify('insert sql:variable("@newxml") into /classes[1]' )
--xml.modify(delete)
update tblspeakers set speaker_xml.modify(
'delete /classes[1]/class[2] '
)
where speaker_id=8
select * from tblspeakers
--xml.modify(replace)
update tblspeakers set speaker_xml.modify('
replace value of classes[1]/class[1]/@name[1]
with " fuck you !!"
') where speaker_id=8
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.