共用方式為


第 11 課:將事件提交給 Weather 應用程式

在這個步驟中,您將利用 Transact-SQL 指令碼,將事件提交給 Weather 應用程式,然後檢視產生的通知。

事件資料

事件是通知應用程式用來產生通知的資料。您撰寫的通知產生查詢會聯結事件資料和訂閱資料來產生通知,例如第 5 課中的查詢。

在每一個有資料要處理的產生器配量期間,會引發產生通知的查詢。事件規則是在有一或多個事件批次抵達的任何配量中執行。排程規則是在有排程訂閱預期要處理的任何配量中執行。

如果沒有事件資料,就沒有理由執行規則。因此,不會產生通知。

WeatherData 事件

針對這個應用程式,您將使用事件提交預存程序來提交事件批次。首先,您將使用 NSEventBeginBatchWeatherData 預存程序來開啟事件批次。您將會指定 WeatherSPEventProvider 非主控的事件提供者作為事件提供者,這是您在第 6 課已定義好的。

ms170427.Caution(zh-tw,SQL.90).gif注意:
不要個別執行這些陳述式。它們會開啟和關閉事件批次,所以必須在相同交易中執行。若要執行這些查詢,請執行 AddWeatherEvents.sql 指令碼,這一課稍後會做說明。
-- Use the weather application's database.
USE TutorialWeather;
-- Start an event batch.
DECLARE @BatchID bigint;
EXEC dbo.NSEventBeginBatchWeatherData N'WeatherSPEventProvider', @BatchID OUTPUT;

建立事件批次之後,您會使用 NSEventWriteWeatherData 預存程序來加入 3 個天氣事件。您必須提供事件批次識別碼,然後提供事件欄位的值。這是一個範例事件:

-- Submit events.
EXEC dbo.NSEventWriteWeatherData
    @EventBatchId=@BatchID,
    @City=N'Seattle',
    @Date=CONVERT(DATETIME, '2005-04-20 13:00:00', 120),
    @Low = 31,
    @High = 52,
    @Forecast=N'Sunny'

使用這個預存程序提交 3 個事件之後,您將使用 NSEventFlushBatchWeatherData 來關閉事件批次及提交到應用程式資料庫。

-- Flush event batch.
EXEC dbo.NSEventFlushBatchWeatherData @BatchID;

您可以顯示事件批次識別碼,以便使用檢視和預存程序來檢視關於事件批次的資訊。

-- Display event batch ID
SELECT @BatchID 'Event Batch';

將事件提交給 Weather 應用程式

使用 AddWeatherEvents.sql 查詢,將事件加入 Weather 應用程式中。

將事件提交給 Weather 應用程式

  1. 在 [方案總管] 中,依序展開 [Weather][查詢],然後開啟 [AddWeatherEvents.sql]。

  2. 檢閱 Transact-SQL 程式碼。

  3. 按 F5 執行查詢。

    您應該接收到 2 個結果集。第一個結果集傳回的 EventCount 值為 3;第二個結果集傳回的事件批次識別碼為 1。

檢視事件資料

利用 ViewWeatherEvents.sql 查詢來檢視您剛才加入的事件。

檢視事件資料

  1. 在 [方案總管] 中,開啟 [ViewWeatherEvents.sql]。

  2. 檢閱 Transact-SQL 程式碼。

  3. 按 F5 執行查詢。

如果您提交多個事件批次,請適當地變更 EventBatchId 參數。

檢視通知

在大約 30 秒之後,Notification Services 會在 [通知] 資料夾建立 FileNotifications.htm 檔。請開啟這個檔案來檢視通知資料。您應該會收到三個類似下面的通知:

  • Notification Id: 1 Notification Class Name: WeatherNotifications Subscriber Id: stephanie Device Address: stephanie@adventure-works.com Protocol Fields: Body: Weather report for the city of Seattle [2/21/2005 2:01:00 PM]: Sunny
    預期低溫:31 F。
    預期高溫:52 F。
    如需詳細資訊,請造訪我們的網站:http://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=Seattle
    謝謝您使用 SQL Server Notification Services
  • Notification Id: 2 Notification Class Name: WeatherNotifications Subscriber Id: david Device Address: david@adventure-works.com Protocol Fields: Body: Weather report for the city of Orlando [2/22/2005 2:01:00 AM]: Partly Cloudy
    預期低溫:59 F。
    預期高溫:81 F。
    如需詳細資訊,請造訪我們的網站:http://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=Orlando
    謝謝您使用 SQL Server Notification Services
  • Notification Id: 3 Notification Class Name: WeatherNotifications Subscriber Id: richard Device Address: richard@adventure-works.com Protocol Fields: Body: Weather report for the city of Seattle [2/21/2005 2:01:00 PM]: Sunny
    預期低溫:31 F。
    預期高溫:52 F。
    如需詳細資訊,請造訪我們的網站:http://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=Seattle
    謝謝您使用 SQL Server Notification Services

因為您使用內建檔案傳遞通訊協定,所以從事件產生的這 3 個通知都在同一個檔案中。如果您提交更多事件,則更多的通知會附加至這個檔案。

也請注意,每一個通知有一個標頭。它顯示關於通知的資訊,包括訂閱者識別碼和裝置位址。通知的實際文字是從 "Body:" 之後開始。

疑難排解

如果在一分鐘內您沒有在 [通知] 資料夾內看到 FileNotifications.htm 檔,請在 Windows 事件檢視器的應用程式記錄檔中查看 Notification Services 的事件。Windows 服務可能沒有權限寫入至 [通知] 資料夾。

您可以執行 ViewNotifications.sql 查詢來檢視關於對 WeatherNotifications 通知類別產生的任何通知之資訊。

檢視通知資訊

  1. 在 [方案總管] 中,依序展開 [Weather][查詢],然後按兩下 [ViewNotifications.sql]

  2. 按 F5 執行查詢。

    查看 DeliveryStatusDescription 資料行中關於通知傳遞的資訊。如果有任何失敗,應用程式記錄檔應該會提供詳細資訊。

  3. 關閉 [ViewSubscribersAndDevices.sql]。

下一步

這個教學課程已教您如何建立 Notification Services 應用程式的簡易原型。若要繼續瞭解 Notification Services,請使用下列資源:

請參閱

概念

Notification Services 教學課程

其他資源

建立 Notification 解決方案
SQL Server Notification Services 簡介

說明及資訊

取得 SQL Server 2005 協助