首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

使用 sp_makewebtask 將 SQL 查詢結果導出為 HTML 表格

     編輯:關於SqlServer

快速導出範例

透過系統預存程序 sp_makewebtask 可直接將查詢結果產生為 HTML 檔案,支援自訂樣板(TPL)排版。

執行腳本

EXECUTE sp_makewebtask
    @outputfile = 'D:\alcatel\20030902\check.htm',
    @query = 'SELECT * FROM Eiems_temporarydata..property',
    @templatefile = 'D:\alcatel\20030902\check.TPL',
    @dbname = 'EIEms_temporarydata',
    @rowcnt = 0,
    @whentype = 9,
    @lastupdated = 1;
GO

樣板檔 check.TPL 內容

<STYLE type="text/css">
body { font-size: 9pt; }
TH { FONT-SIZE: 12pt; }
TD { font-size: 12pt; }
</STYLE>

<CENTER>
  <H1><FONT color="#000000">check it!</FONT></H1>
  <HR>
  <H2>
    <TH><B>Size</B></TH>
    <TH><B>Date</B></TH>
    <TH><B>Name</B></TH>
    <TH><B>Exist</B></TH>
    <TH><B>Remark</B></TH>
    <TH><B>result</B></TH>
    <%begindetail%>
    <%insert_data_here%>
    <%insert_data_here%>
    <%insert_data_here%>
    <%insert_data_here%>
    <%insert_data_here%>
    <%insert_data_here%>
    <%enddetail%>
  </H2>
</CENTER>

關鍵標籤說明

  • <%begindetail%> / <%enddetail%>:定義資料列重複區塊。
  • <%insert_data_here%>:對應查詢結果的每一欄位,順序需與 SELECT 清單一致。
  • 樣板中需包含完整 HTML 結構,CSS 可內嵌或外部引用。

適用場景與限制

  • 適合定期產生靜態報表、郵件內容、簡易儀表板。
  • SQL Server 2005 起已棄用,建議改用 SSRS、PowerShell 或應用層匯出。
  • 需啟用 xp_cmdshell 相關權限,注意安全性風險。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved