SQL Server BI Step by Step SSIS 5

來源:互聯網
上載者:User

忙了一段,終於有時間來完成這一系列了。sql server 2008正式版已經發布了,接下來的系列都將基於sql server 2008+vs.net 2008開發。

引言

在一個B2B項目中遇到這樣的一個情況,每天老闆都想看到所有的新的訂單資訊,而這個老闆很懶,不想登入系統後台,而是想通過查看郵件的方式。當然實現方式很多,這裡我們介紹一個怎麼使用SSIS包來實現這樣的一個功能。使用SQL XML Query查詢出XML結果資料,然後使用Xsl模板將xml資料轉換成html,再將html內容發送郵件。

建立SSIS包

1.和vs.net 20005中一樣的方式,建立一個Integration Service項目,並且包的名稱修改為 SendMailPackage

2.在你的本地硬碟上建立一個目錄,比如F:\SSIS_Example\SendMail用來存放查詢語句和Xsl文 件.

3.在剛才的目錄下建立一個QueryOrder.sql檔案,填充如下內容,在連線管理員中建立一個檔案串連 ,選擇建立的sql檔案:

QueryOrder.sql

/*
  實際使用時將@v_CurrentDate換成當時日期
*/


declare  @v_CurrentDate  datetime
set  @v_CurrentDate =  '2003-07-17'
 /*set  @v_CurrentDate = GETDATE()*/


if  exists  (select 1
    from  Sales.SalesOrderHeader
    where  OrderDate =  @v_CurrentDate)


begin


  select  top 10 oh.OrderDate,
    (select  round(sum(TotalDue), 2)
    from  Sales.SalesOrderHeader
    where  OrderDate =  @v_CurrentDate) as DayTotal,
    p.ProductID, p.Name,
    round(sum(oh.TotalDue), 2) as ProductSubtotal
  from  AdventureWorks.Sales.SalesOrderHeader  oh
  join  AdventureWorks.Sales.SalesOrderDetail  od
  on  od.SalesOrderID =  oh.SalesOrderId
  join  AdventureWorks.Production.Product  p
  on  p.ProductID =    od.ProductId
  where  oh.OrderDate =  @v_CurrentDate
  group
  by  oh.OrderDate, p.ProductID, p.Name
  order
  by  5 desc, p.ProductID asc
  for xml auto, elements, type, root('Order')

end

else  select cast('<NoRecords>No sales records available for this  date.</NoRecords>' as xml)

注:  在這裡我們使用一個固定的日期,實際中你可以使用系統方法GETDATE()或者DATEDIFF()來查 詢你需要的日期。

4.同樣在目錄下面建立一個Orders.xsl檔案,由於內容較多,請直接下載附件中檔案,這裡也不在詳 細介紹xsl的具體內容。 同樣也建立一個檔案串連。

5.在目錄下建立一個Orders.htm檔案,用來存放產生的html內容,同時也建立一個檔案串連。

6.建立一個Ado.net 串連你的AdventureWorks資料庫。現在你的連線管理員有如下串連:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.