According to the requirements, whenever details of new vendors are added to the Vendors table of the AdventureWorks database, the name of new vendors should also be added to the VList
Trang 1Instructor Inputs S e
Trang 3¤NIIT Instructor Inputs 17.3
Note
This session includes exercises of Chapter 11
Exercise 1
In the AdventureWorks database, the details of the vendors are stored in the Vendors
table In addition, the names of the vendors are saved in the VList table of the Vendor
database used by another application
According to the requirements, whenever details of new vendors are added to the Vendors
table of the AdventureWorks database, the name of new vendors should also be added to
the VList table of the Vendor database
How will you solve this problem?
For this exercise, you need to provide the Vendor database to the students For this,
you can run the Create_Vendor_Database.sql script provided in the
Datafiles_for_faculty\QMDS2005\Chapter11\Exercises folder in the TIRM CD The
vendor list is present in the VList table of the Vendor database
Solution
To solve the preceding problem, you need to perform the following tasks:
1 Create a service program
2 Create message types, contract, queues and services
3 Create a trigger on Purchasing.Vendor table
4 Verify the functionality
Solutions to Exercises
Chapter 11
Trang 4Task 1: Creating a Service Program
Before you create a Service Broker solution, you need to create a service program To create a service program, you need to perform the following steps:
1 Type the following query in the Query Editor window of the Microsoft SQL
Server Management Studio window:
USE Vendor;
GO
CREATE PROCEDURE [dbo].[OnReceiveMessage]
AS
DECLARE @message_type int
DECLARE @dialog uniqueidentifier,
@ErrorSave int,
@ErrorDesc nvarchar(100),
@message_body nvarchar(20);
WHILE (1 = 1)
BEGIN
@message_type=message_type_id, @message_body=message_body,
@dialog = conversation_handle
), TIMEOUT 3000
if (@@ROWCOUNT = 0) BEGIN
BREAK END
SET @ErrorSave = @@ERROR ;
IF (@ErrorSave <> 0) BEGIN
ROLLBACK TRANSACTION ; SET @ErrorDesc = N'An error has occurred.' ; END CONVERSATION @dialog
Trang 5¤NIIT Instructor Inputs 17.5
END
ELSE
BEGIN
END
END
2 Press the F5 key to execute the statements
Task 2: Creating Message Types, Contract, Queues and Services
Once the Service Program has been created, you need to create various objects, such as
Message Type, Contract, Queue, and Service
To create these objects, you need to perform the following steps:
1 Type the following query in the Query Editor window of the Microsoft SQL
Server Management Studio window:
USE AdventureWorks;
GO
CREATE MESSAGE TYPE SendMessage1
VALIDATION = NONE
CREATE MESSAGE TYPE AcknowledgeMessage1
VALIDATION = NONE
CREATE CONTRACT MyContract1
(SendMessage1 SENT BY INITIATOR,
AcknowledgeMessage1 SENT BY TARGET)
CREATE QUEUE AdvQueue1;
CREATE SERVICE VendorService
ON QUEUE AdvQueue1 (MyContract1)
2 Press the F5 key to execute the statements
3 Type the following query in the Query Editor window of the Microsoft SQL
Server Management Studio window:
USE Vendor
CREATE MESSAGE TYPE SendMessage
VALIDATION = NONE
Trang 6CREATE MESSAGE TYPE AcknowledgeMessage
VALIDATION = NONE
CREATE CONTRACT MyContract
(SendMessage SENT BY INITIATOR,
AcknowledgeMessage SENT BY INITIATOR)
CREATE QUEUE VendorQueue
WITH STATUS=ON,
ACTIVATION (
PROCEDURE_NAME = OnReceiveMessage,
MAX_QUEUE_READERS = 5,
CREATE SERVICE RecieveService
ON QUEUE VendorQueue (MyContract)
4 Press the F5 key to execute the statements
Task 3: Creating a Trigger on Purchasing.Vendor Table
To add the name of a vendor to the central vendor list, you need to create an insert trigger
on the Purchasing.Vendor table To create a trigger, you need to perform the following steps:
1 Type the following query in the Query Editor window of the Microsoft SQL
Server Management Studio window:
USE AdventureWorks;
GO
CREATE TRIGGER SendTrigger ON Purchasing.Vendor FOR INSERT AS
DECLARE @name AS nvarchar(40)
SELECT @name = Name FROM Inserted
DECLARE @dialog_handle uniqueidentifier;
BEGIN DIALOG CONVERSATION @dialog_handle FROM
SERVICE [VendorService] TO SERVICE 'RecieveService' ON CONTRACT [MyContract] ;
SEND ON CONVERSATION
@dialog_handle MESSAGE TYPE[SendMessage]
(@name)
Trang 7¤NIIT Instructor Inputs 17.7
Note
Task 4: Verifying the Functionality
To verify the functionality, insert a row in the Purchasing.Vendor table and check that the
name is added to the VList table of the Vendors database
For your reference, the commands that are to be executed for this solution are given in
the MR_Solution.txt file in the Datafiles_for_faculty\QMDS2005\Chapter 11\Exercises
folder in the TIRM CD