4. /*inserting data from query into a table from product.xml*/
Use AdventureWorks
Go
create table prodxml
(productnumber xml
,quantity xml,
price xml)
insert into prodxml(productnumber,quantity, price)
Select
g.value('@PRODNUMBER', 'nvarchar(10)') as Productnumber,
g.value('@quantity', 'nvarchar(10)') as Quantity,
g.value('@price', 'nvarchar(20)') as Price
FROM( select cast (bulkcolumn as xml)as data
from openrowset(bulk 'c:tempproduct.xml', single_blob)
as g
)h
Cross apply data.nodes('/PRODUCT/PRODUCT') c(g)
Select * from prodxml
5. /*Using Value() method to reterive scalar value from an XML document */
declare @s xml
select @s = '<Student StudentID = "0989" StudentSNN="409569090" />'
select
@s.value('(Student/@StudentID)[1]', 'CHAR(8)') as STUDENTID,
@s.value('(Student/@StudentSNN)[1]','char(12)') as STUDENTSNN
STUDENTID STUDENTSNN
989 409569090
/* Using the nodes method to return row set */
declare @pid xml
select @pid ='
<PRODUCT>
<PRODNUMBER> 190 </PRODNUMBER>
<PRODNUMBER> 180 </PRODNUMBER>
<PRODNUMBER> 170 </PRODNUMBER>
<PRODNUMBER> 160 </PRODNUMBER>
<PRODNUMBER> 150 </PRODNUMBER>
<PRODNUMBER> 140 </PRODNUMBER>
<PRODNUMBER> 120 </PRODNUMBER>
</PRODUCT>'
select a.value('.', 'int') as product_number
from @pid.nodes('/PRODUCT/PRODNUMBER')b(a)
product_number
190
180
170
160
150
140
120
/* Joining XML nodews with relational tables */
6. Use AdventureWorksDW
Go
CREATE TABLE CUSTOMER1
(RECORD_ID INT IDENTITY PRIMARY KEY,
CUSTOMER_ID INT,
FIRST_NAME NVARCHAR(50),
MIDDLE_NAME NVARCHAR(50),
LAST_NAME NVARCHAR(50)
);
GO
CREATE TABLE CUSTOMER2
(RECORD_ID INT,
CUSTOMER_DATA XML
);
Alter table customer1
addgeographykey char(20);
Alter table customer1
add city nvarchar(30);
Alter table customer1
addstatenamenvarchar(50);
Alter table customer1
addstatecodenvarchar(3);
Alter table customer1
addcountycodenvarchar(3);
Alter table customer1
add POSTALCODE nvarchar(15);
Update customer1
set
INSERT INTO CUSTOMER1(CUSTOMER_ID, FIRST_NAME, MIDDLE_NAME,
LAST_NAME)
7. VALUES( (SELECT CustomerKey FROM DBO.DimCustomer WHERE CustomerKey =
11000),
(SELECT FIRSTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11000),
(SELECT MIDDLENAME FROM DBO.DimCustomer WHERE CustomerKey =
11000),
(SELECT LASTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11000)
);
INSERT INTO CUSTOMER1(CUSTOMER_ID, FIRST_NAME, MIDDLE_NAME,
LAST_NAME)
VALUES( (SELECT CustomerKey FROM DBO.DimCustomer WHERE CustomerKey =
11062),
(SELECT FIRSTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11062),
(SELECT MIDDLENAME FROM DBO.DimCustomer WHERE CustomerKey =
11062),
(SELECT LASTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11062)
);
INSERT INTO CUSTOMER1(CUSTOMER_ID, FIRST_NAME, MIDDLE_NAME,
LAST_NAME)
VALUES( (SELECT CustomerKey FROM DBO.DimCustomer WHERE CustomerKey =
11010),
(SELECT FIRSTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11010),
(SELECT MIDDLENAME FROM DBO.DimCustomer WHERE CustomerKey =
11010),
(SELECT LASTNAME FROM DBO.DimCustomer WHERE CustomerKey =
11010)
);
update dbo.CUSTOMER1
setgeographykey = (select GeographyKey from dbo.DimCustomer where CustomerKey =
11000)
wherecustomer_id = 11000
update dbo.CUSTOMER1
8. set city = 'Rockhampton'
wherecustomer_id = 11000;
go
update dbo.CUSTOMER1
setstatename = 'Queensland'
wherecustomer_id = 11000;
go
update dbo.CUSTOMER1
setstatecode = 'QLD'
wherecustomer_id = 11000;
go
update dbo.CUSTOMER1
setcountycode = 'AU'
wherecustomer_id = 11000;
go
update dbo.CUSTOMER1
setpostalcode = '4700'
wherecustomer_id = 11000;
go
update dbo.CUSTOMER1
setgeographykey = (select GeographyKey from dbo.DimCustomer where CustomerKey =
11062)
wherecustomer_id = 11062;
go
update dbo.CUSTOMER1
set city = 'Portland'
wherecustomer_id = 11062;
go
update dbo.CUSTOMER1
setstatename = 'Oregon'
wherecustomer_id = 11062;
go
update dbo.CUSTOMER1
setstatecode = 'OR'
wherecustomer_id = 11062;
9. go
update dbo.CUSTOMER1
setcountycode = 'US'
wherecustomer_id = 11062;
go
update dbo.CUSTOMER1
setpostalcode = '97205'
wherecustomer_id = 11062;
go
update dbo.CUSTOMER1
setgeographykey = (select GeographyKey from dbo.DimCustomer where CustomerKey =
11000)
wherecustomer_id = 11010
update dbo.CUSTOMER1
set city = 'East Brisbane'
wherecustomer_id = 11010;
go
update dbo.CUSTOMER1
setstatename = 'Queensland'
wherecustomer_id = 11010;
go
update dbo.CUSTOMER1
setstatecode = 'QLD'
wherecustomer_id = 11010;
go
update dbo.CUSTOMER1
setcountycode = 'AU'
wherecustomer_id = 11010;
go
update dbo.CUSTOMER1
setpostalcode = '4169'
wherecustomer_id = 11010;
go
10. insert into CUSTOMER2(Record_id, customer_data) SELECT 1,
'<customer record_Id = "1">
<customer geographykey ="26" city ="Rockhampton" statename = "Queensland" statecode
="QLD" Country = "Australia" countycode = "AU" POSTALCODE = "4700"/>
</customer >'
insert into CUSTOMER2(Record_id, customer_data) SELECT 2,
'<customer record_Id = "2">
<customer geographykey ="547" city ="Portland" statename = "Oregon" statecode ="OR"
Country = "United States" countycode = "US" POSTALCODE = "97205"/>
</customer >'
insert into CUSTOMER2(Record_id, customer_data) SELECT 3,
'<customer record_Id = "3">
<customer geographykey ="22" city ="East Brisbane" statename = "Queensland" statecode
="QLD" Country = "Australia" countycode = "AU" POSTALCODE = "4169"/>
</customer >'
useAdventureWorksDW
go
selectcustomer.FirstName, customer.LastName, customer.MiddleName, customer.CustomerKey,
customer.BirthDate, customer.Gender, customer.MaritalStatus
fromdbo.DimCustomer customer
order by NEWID()
for xml auto;
Results
<customerFirstName="Ian"LastName="Watson"MiddleName="G"CustomerK
ey="26398"BirthDate="1945-06-
10T00:00:00"Gender="M"MaritalStatus="M" />
<customerFirstName="Jordan"LastName="Green"MiddleName="L"Custome
rKey="27915"BirthDate="1974-04-
25T00:00:00"Gender="F"MaritalStatus="S" />
<customerFirstName="Gregory"LastName="Yuan"CustomerKey="21822"Bi
rthDate="1957-02-26T00:00:00"Gender="M"MaritalStatus="S" />
<customerFirstName="Jasmine"LastName="Smith"CustomerKey="28891"B
irthDate="1945-11-01T00:00:00"Gender="F"MaritalStatus="S" />
<customerFirstName="Shane"LastName="Sai"MiddleName="R"CustomerKe
y="29374"BirthDate="1965-05-
21T00:00:00"Gender="M"MaritalStatus="M" />
11. <customerFirstName="Cameron"LastName="Patterson"MiddleName="M"Cu
stomerKey="25790"BirthDate="1953-09-
10T00:00:00"Gender="M"MaritalStatus="M" />
<customerFirstName="Bradley"LastName="Rai"CustomerKey="19100"Bir
thDate="1971-06-01T00:00:00"Gender="M"MaritalStatus="M" />
<customerFirstName="Isaiah"LastName="Roberts"MiddleName="M"Custo
merKey="27211"BirthDate="1955-10-
12T00:00:00"Gender="M"MaritalStatus="M" />
<customerFirstName="Stacy"LastName="Serrano"CustomerKey="24613"B
irthDate="1970-09-23T00:00:00"Gender="F"MaritalStatus="M" />
<customerFirstName="Eugene"LastName="Zhao"CustomerKey="12328"Bir
thDate="1950-03-18T00:00:00"Gender="M"MaritalStatus="S" />
<customerFirstName="Noah"LastName="Nelson"CustomerKey="16835"Bir
thDate="1933-10-14T00:00:00"Gender="M"MaritalStatus="M" />
RESULTS (ABRIDGED)
useAdventureWorksDW
go
selectcustomer.FirstName, customer.LastName, customer.MiddleName, customer.CustomerKey,
customer.BirthDate, customer.Gender, customer.MaritalStatus,
Geography.City, Geography.StateProvinceCode as StateCode, Geography.StateProvinceName
AS StateName,
Geography.CountryRegionCode as CountryCode
fromdbo.DimCustomer customer
inner join
dbo.DimGeography Geography
on
customer.GeographyKey = Geography.GeographyKey
order by NEWID()
for xml auto;
<customerFirstName="Kellie"LastName="Dominguez"MiddleName="E"Cus
tomerKey="17244"BirthDate="1972-02-
25T00:00:00"Gender="F"MaritalStatus="M">
<GeographyCity="Findon"StateCode="SA"StateName="South
Australia"CountryCode="AU" />
</customer>
<customerFirstName="Hector"LastName="Ruiz"CustomerKey="27592"Bir
thDate="1967-11-27T00:00:00"Gender="M"MaritalStatus="S">
<GeographyCity="Newcastle"StateCode="NSW"StateName="New South
Wales"CountryCode="AU" />
</customer>
<customerFirstName="Joseph"LastName="Taylor"MiddleName="T"Custom
erKey="12289"BirthDate="1968-03-
23T00:00:00"Gender="M"MaritalStatus="S">
29. SQL>desc student
Name Null? Type
----------------------------------------- -------- -------------------
STUDENT_ID NOT NULL NUMBER(8)
SALUTATION VARCHAR2(5)
FIRST_NAME VARCHAR2(25)
LAST_NAME NOT NULL VARCHAR2(25)
STREET_ADDRESS VARCHAR2(50)
ZIP NOT NULL VARCHAR2(5)
PHONE VARCHAR2(15)
EMPLOYER VARCHAR2(50)
REGISTRATION_DATE NOT NULL DATE
CREATED_BY NOT NULL VARCHAR2(30)
CREATED_DATE NOT NULL DATE
MODIFIED_BY NOT NULL VARCHAR2(30)
MODIFIED_DATE NOT NULL DATE
SELECT XMLELEMENT("NAME", FIRST_NAME), XMLELEMENT("STUDENT_ID", STUDENT_ID),
XMLELEMENT("LAST_NAME", LAST_NAME)
FROM STUDENT
XMLELEMENT("NAME",FIRST_NAME) XMLELEMENT("STUDENT_ID",STUDENT_ID)
XMLELEMENT("LAST_NAME",LAST_NAME)
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------
-------------------------------------------------------
<NAME>Fred</NAME><STUDENT_ID>102</STUDENT_ID><LAST_NAME>Crocitto</LAST_NAME>
<NAME>J.</NAME><STUDENT_ID>103</STUDENT_ID><LAST_NAME>Landry</LAST_NAME>
<NAME>Laetia</NAME><STUDENT_ID>104</STUDENT_ID><LAST_NAME>Enison</LAST_NAME>
<NAME>Angel</NAME><STUDENT_ID>105</STUDENT_ID><LAST_NAME>Moskowitz</LAST_NAME>
SELECT XMLELEMENT("STUDENT", xmlattributes(S.STUDENT_ID AS "STUDENT_ID")
,XMLFOREST(S.FIRST_NAME ||','||S.LAST_NAME AS "STUDENT_NAME")
,XMLFOREST(S.STREET_ADDRESS AS "ADDRESS"))
FROM STUDENT S
XMLELEMENT("STUDENT",XMLATTRIBUTES(S.STUDENT_IDAS"STUDENT_ID"),XMLFOREST(S.FIRST_NAME||','||S.LAST_NAMEAS"STU
DENT_NAME"),XMLFOREST(S.STREET_ADDRESSAS"ADDRESS"))
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
30. <STUDENT STUDENT_ID="102"><STUDENT_NAME>Fred,Crocitto</STUDENT_NAME><ADDRESS>101-09 120th
St.</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="103"><STUDENT_NAME>J.,Landry</STUDENT_NAME><ADDRESS>7435 Boulevard East
#45</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="104"><STUDENT_NAME>Laetia,Enison</STUDENT_NAME><ADDRESS>144-61 87th
Ave</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="105"><STUDENT_NAME>Angel,Moskowitz</STUDENT_NAME><ADDRESS>320 John
St.</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="106"><STUDENT_NAME>Judith,Olvsade</STUDENT_NAME><ADDRESS>29 Elmwood
Ave.</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="107"><STUDENT_NAME>Catherine,Mierzwa</STUDENT_NAME><ADDRESS>22-70 41st
St.</ADDRESS></STUDENT>
<STUDENT STUDENT_ID="108"><STUDENT_NAME>Judy,Sethi</STUDENT_NAME><ADDRESS>Stratton Hall</ADDRESS></STUDENT>
Result Abbreviated
SQL> create table studentxml(student_id number , dtsys.xmltype);
Table created.
SQL> insert into studentxmlvalues(999,
2 sys.xmltype.createxml(
3 '<?xml version = "1.0"?>
4 <student>
5 <name> Michael Smith Jackson</name>
6 <student_type> Graduate </student_type>
7 <program> Information systems-Web Applications </program>
8 </student>'))
9 /
1 row created.
SQL> insert into studentxmlvalues(566,
2 sys.xmltype.createxml(
3 '<?xml version = "1.0"?>
4 <student>
5 <name> Young Andrew Jackson</name>
6 <student_type> Graduate </student_type>
7 <program> Information systems-Database Systems </program>
8 </student>'))
9 /
1 row created.
SQL> set long 8900
SQL> select * from studentxml;
31. STUDENT_ID
----------
DT
-----------------------------------------------------------------
999
<?xml version = "1.0"?>
<student>
<name> Michael Smith Jackson</name>
<student_type> Graduate </student_type>
<program> Information systems-Web Applications </program>
</student>
566
STUDENT_ID
----------
DT
-----------------------------------------------------------------
<?xml version = "1.0"?>
<student>
<name> Young Andrew Jackson</name>
<student_type> Graduate </student_type>
<program> Information systems-Database Systems </program>
</student>
SQL> select EXTRACTVALUE(S.dt,'//student/name') FROM studentxml S;
EXTRACTVALUE(S.DT,'//STUDENT/NAME')
--------------------------------------------------------------------------------
Michael Smith Jackson
Young Andrew Jackson
SQL> create table employeex
2 (ID number primary key,
3 employee XMLTYPE NOT NULL
4 );
Table created.
SQL>descemployeex
Name Null? Type
----------------------------------------- -------- ----------------
32. ID NOT NULL NUMBER
EMPLOYEE NOT NULL PUBLIC.XMLTYPE
SQL> insert into employeex
2 VALUES(908997,xmltype('<?xml version="1.0" standalone ="no"?>
3 <employee>
4 <emp>
5 <name> Richard Blue William</name>
6 </emp>
7 </employee>'));
1 row created.
SQL> insert into employeex
2 VALUES(877997,xmltype('<?xml version="1.0" standalone ="no"?>
3 <employee>
4 <emp>
5 <name>Rubby Diane Clay </name>
6 </emp>
7 </employee>'));
1 row created.
SQL> select * from employeex
2 /
ID
----------
EMPLOYEE
---------------------------------------------------------
908997
<?xml version="1.0" standalone ="no"?>
<employee>
<emp>
<name> Richard Blue William</name>
</emp>
</employee>
877997
ID
----------
EMPLOYEE
33. ---------------------------------------------------------
<?xml version="1.0" standalone ="no"?>
<employee>
<emp>
<name>Rubby Diane Clay </name>
</emp>
</employee>
SQL> update employeex
2 set employee = updatexml(employee,'/employee/emp/name/text()','Rubby Wash
ington')
3 where ID = 877997;
1 row updated.
ID
----------
EMPLOYEE
--------------------------------------------------------------------------------
877997
<?xml version="1.0" standalone='no'?><employee><emp><name>Rubby Washington</name
></emp></employee>
SQL> insert into employeex
2 VALUES(437421,xmltype('<?xml version="1.0" standalone ="yes"?>
3 <employee>
4 <emp>
5 <name> Liz Benson </name>
6 </emp>
7 </employee>'));
1 row created.
SQL> select * from employeex where id = 437421;
ID
----------
EMPLOYEE
--------------------------------------------------------------------------------
437421