Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.06 KB | None | 0 0
  1. use northwind
  2.  
  3. select * from Customers where TerritoryID is not null
  4. select * from Employees
  5. select * from Territories
  6. select * from EmployeeTerritories
  7. select * from Orders where ShippedDate is null
  8. /*
  9. Problem Problem
  10. We develop a system which couples the trading database of the Northwind Co. running on the PRIM server
  11. instances to the HR database running on the THIRD server.
  12. In the Northwind database, the territories for which an agent (i.e. an employee) is responsible are listed
  13. in the EmployeeTerritories table, which acts as a linking table between the Employees and Territories
  14. tables. A territory is assigned to an employee by inserting a new record in the linking table.
  15. The expected minimum functionality, for 70% of the points is that when a new territory is assigned to an
  16. employee in the trading database, a notification should be sent from the HR database ASAP, to those
  17. customers who are located on the territory and have at least one active (not yet shipped) order. The note
  18. should include the name and phone number of the customer as well as the contact name and company
  19. name of the customer. You can simulate the sending of the notification by printing a message like this:
  20. “Dear Maria Anders at Alfreds Futterkiste, please not that as of today a new Northwind agent, Robert
  21. King, phone (71) 555-5598, is a your service”
  22. The expected full functionality is that you process not only new entries in the EmployeeTerritories table
  23. but also changed records, when an existing record is changed. In this case, send a message to the
  24. customers of the new territory as well as the customers of the unassigned territory, the latter something
  25. like “…we regret to inform you that Mr. XY will no longer be at your service etc”.
  26. */
  27.  
  28. /*Stored procedure*/
  29. alter procedure send_message_to_customers @territory_id nvarchar(20), @employee_id int
  30. as
  31. set nocount on
  32. declare @customer_name varchar(50),  @company_name varchar(50), @i int, @str varchar(150), @res_no int
  33. select @res_no=count(*)  from Employees where EmployeeID = @employee_id
  34. if @res_no=0 print 'No matching record.'
  35. else if @res_no>1 print 'More than one matching record.'
  36. else begin --if we have a valid employee
  37. select @str = (FirstName + ' ' + LastName + ' phone ' + HomePhone) from Employees where EmployeeID = @employee_id
  38. begin tran
  39. begin try
  40.     declare customer_cursor cursor for
  41.     select distinct(Customers.ContactName), Customers.CompanyName from Customers join Orders on Customers.CustomerID = Orders.CustomerID where TerritoryID like @territory_id and ShippedDate is null
  42.     set @i=1
  43.     open customer_cursor
  44.     fetch next from customer_cursor into @customer_name, @company_name
  45.     while @@fetch_status = 0
  46.     begin
  47.     print 'Dear ' + @customer_name + ' at ' + @company_name +  ', please not that as of today a new Northwind agent, ' + @str +  ', is a your service!'
  48.      set @i=@i+1
  49.      fetch next from customer_cursor into @customer_name, @company_name
  50.     end
  51.     close customer_cursor
  52.     deallocate customer_cursor
  53.     commit tran
  54. end try
  55.     begin catch
  56.     print 'OTHER ERROR: '+ ERROR_MESSAGE() + ' (' + cast(ERROR_NUMBER() as varchar(20)) +
  57.     ')'
  58.     print 'Rolling back transaction'
  59.     rollback tran
  60.     end catch
  61. end
  62.  
  63.  
  64. /*Test stored procedure*/
  65. exec send_message_to_customers '01581', 1
  66.  
  67. /*Triggers*/
  68.  
  69. /*Insert trigger*/
  70. create trigger new_agent_inserted on EmployeeTerritories for insert as
  71. declare @employee_id int, @territory_id nvarchar(20)
  72. select @employee_id=EmployeeID, @territory_id = TerritoryID from inserted
  73. exec send_message_to_customers @territory_id, @employee_id
  74. go
  75.  
  76. /*Test insert trigger*/
  77. delete from EmployeeTerritories where TerritoryID = '01581'
  78. insert into EmployeeTerritories (EmployeeID, TerritoryID) values(9, '01581')
  79.  
  80. /*Update trigger*/
  81.  
  82. alter trigger agent_updated on EmployeeTerritories for update as
  83. declare @employee_id int, @territory_id nvarchar(20)
  84. select @employee_id=EmployeeID, @territory_id = TerritoryID from inserted
  85. exec send_message_to_customers @territory_id, @employee_id
  86. go
  87.  
  88. update EmployeeTerritories set TerritoryID = '01581' where EmployeeID = 9 and TerritoryID = '03049'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement