.tpl 模板檔與 HTML 結構相同,關鍵在於三個特殊標記:
<%begindetail%>:資料替換起始點<%enddetail%>:資料替換結束點<%insert_data_here%>:資料欄位插入位置,依 SELECT 欄位順序依序替換,每欄每列僅插入一次;需重複使用時,先指派給變數再引用OutPutHtml.tpl<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>show HTML</title>
<body>
<center><h2>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th><b>公司名稱</b></th>
<th><b>聯絡人</b></th>
<th><b>產品名稱</b></th>
<th><b>單價</b></th>
</tr>
<%begindetail%>
<tr>
<td><%insert_data_here%></td>
<td align="right"><%insert_data_here%></td>
<td align="right"><%insert_data_here%></td>
<td align="right"><%insert_data_here%></td>
</tr>
<%enddetail%>
</table>
</center></h2></body></html>
sp_makewebtaskEXECUTE sp_makewebtask
@outputfile = 'D:\SQL Server檔案\OutPutHtml.htm',
@query = 'SELECT Suppliers.CompanyName, Suppliers.ContactName, Products.ProductName, Products.UnitPrice
FROM Suppliers INNER JOIN Products ON Suppliers.SupplierID = Products.SupplierID
WHERE Suppliers.SupplierID = 1',
@templatefile = 'D:\SQL Server檔案\OutPutHtml.tpl',
@dbname = 'NorthWind',
@rowcnt = 0,
@whentype = 9,
@lastupdated = 1
GO
| 參數 | 用途 |
|---|---|
| @outputfile | 輸出 HTML 檔案完整路徑,支援 UNC 路徑 |
| @query | 要執行的 SQL 查詢,可多條 SELECT 產生多張表格 |
| @templatefile | 模板檔路徑,含格式與插入標記 |
| @dbname | 執行查詢的資料庫名稱 |
| @rowcnt | 最大輸出列數,0 為全部 |
| @whentype | 執行時機,9 = 立即建立/重建 |
| @lastupdated | 1 = 顯示「上次更新時間」時間戳 |
sp_configure 'show advanced options', 1; RECONFIGURE;
sp_configure 'Web Assistant Procedures', 1; RECONFIGURE;
執行後產生 OutPutHtml.htm,模板標記已被實際資料取代,直接以瀏覽器開啟即可查看表格化報表。