你可能想在程序當中使用xml,但是僅僅用一個簡單的查詢是不能從microsoft sql server 2000中獲取其信息的。
讓我們假設(shè)你想將一個html 表格連接到一個xml數(shù)據(jù)島,但是你十分確定要將若干表格套入母表中十分復雜。母表中的每一個表格一定是xml數(shù)據(jù)結(jié)構(gòu)中父節(jié)點下的一組節(jié)點。電話號碼目錄就是一個很好的例子。
<table datasrc="#xmlphonedata">
<tr>
<td>
<span datafld="fname"></span>
<span datafld="lname"></span>
</td>
</tr>
<tr>
<td>
<table datasrc="#xmlphonedata" datafld="phone">
<tr>
<td><span datafld="phone_type"></span>:</td>
<td><span datafld="phone_no"></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table datasrc="#xmlphonedata" datafld="other_info">
<tr>
<td><span datafld="info_type"></span>:</td>
<td><span datafld="info_data">/span></td>
</tr>
</table>
</td>
</tr>
</table>
table代表一系列信息,包括目錄中某個人的名字、所有的電話號碼和那個人的其他信息(譬如說,地址等)。
這個xml的模型如下:
<root>
<data>
<lname/>
<fname/>
<phone>
<phone_type/>
<phone_no/>
</phone>
<other_info>
<info_type/>
<info_data/>
</other_info>
</data>
</root>
盡管創(chuàng)建這個xml模型不是不可能的,我們?nèi)匀辉诒疚闹惺褂眠@個簡單的例子。
首先,我們將提供一些背景知識使其更全面。數(shù)據(jù)來源于三個方面:包含員工的名(fname)和姓(lname)的職工表;包含員工電話類型(phone_type)和電話號碼(phone_no)的電話表;包含員工其他信息(如地址等)的其他信息表(other_info)。
如果你想將信息從這些表中抓取到一個記錄集中,sql查詢可以如下:
select employee.lname, employee.fname, phone.phone_type, phone.phone_no,
other_info.info_type, other_info.info_data from employee left join phone on
phone.employee_id = employee.employee_id left join other_info on
other_info.employee_id = employee.employee_id
當你需要sql 2000中有xml的時候,你通常可以在查詢字符串的末尾輸入一個for xml auto, elements語句,你就可以得到想要的xml字符串。但是,多個節(jié)點會有一點小問題。單個節(jié)點很容易,但當你引用多個表格時,你可能不會花整天時間去嘗試獲得想要的xml輸出。
另一個方法就是關(guān)閉行計算和數(shù)據(jù)輸出。你可以通過將一個指針移入母表數(shù)據(jù),用一個指令來操作記錄,然后從帶有for xml auto、elements語句的其他表格選擇相關(guān)記錄以分流xml輸出來實現(xiàn)。
這個功能最好保存到一個存儲程序,因為指針比較慢,而一個存儲程序被事先編輯好。下面就是完成這項任務的存儲程序(transact sql)。
declare @employee_id int
declare @fname varchar(50)
declare @lname varchar(50)
declare phone_cursor cursor for select employee_id, fname, lname from employees
order by lname, fname
set nocount on
open phone_cursor
fetch next from phone_cursor into @employee_id, @fname, @lname
while @@fetch_status = 0
begin
select '<data><fname>' + @fname + '</fname><lname>' + @lname +
'</lname>'
select phone_type, phone_no from phone where employee_id =
@employee_id for xml auto, elements
select info_type, info_data from other_info where employee_id =
@employee_id for xml auto, elements
select '</data>'
fetch next from phone_cursor into @employee_id, @fname, @lname
end
close phone_cursor
deallocate phone_cursor
set nocount off
go
這幾句sql創(chuàng)建了一個用以存儲員工數(shù)據(jù)和重復執(zhí)行的指針。為了解除每一個select語句后的行計算輸出功能,nocount被設(shè)置為on。當指針中的每條記錄在被操作的時候,構(gòu)成了一個包含著xml輸出的名和姓節(jié)點字符串。電話信息和其他信息也都創(chuàng)建了xml。
然后,父節(jié)點關(guān)閉。在成功地完成了指針操作以后,關(guān)閉指針并對其再分配。這就得到了一個符合目標xml模型的xml字符串。
為了有效的使用這個xml輸出字符串,輸出必須存儲在一個ado流對象中。利用流對象中的readtext方法可以訪問數(shù)據(jù):
<xml id="xmlphonedata" name="xmlphonedata">
<%
dim adoconn, adocmd
dim adostream
set adoconn = server.createobject("adodb.connection")
set adocmd = server.createobject("adodb.command")
adoconn.connectionstring = "some connection string to ms sql 2k"
adoconn.open
set adocmd.activeconnection = adoconn
set adostream = server.createobject("adodb.stream")
adocmd.commandtype = 4 'adcmdstoredproc
adocmd.commandtext = "get_test_phone"
adostream.open
adocmd.properties("output stream") = adostream
adocmd.execute ,,1024 'adexecutestream
response.write adostream.readtext(-1)
adostream.close
set adostream = nothing
adoconn.close
set adocmd = nothing
set adoconn = nothing
%>
</xml>
以上代碼創(chuàng)建了一個ado到sql 2000的連接,執(zhí)行了一個存儲程序。結(jié)果被儲存在ado流對象(adostream)中。其數(shù)據(jù)寫入response緩沖器,流對象被關(guān)閉,然后是一些“清倉”操作。
這個范例提供了一個創(chuàng)建用戶化xml方案的一般方法。通過將html表格連接到xml數(shù)據(jù)島,你可以創(chuàng)造一些針對性的解決方法。
新聞熱點
疑難解答