Question : SQL 2005 Trigger, send mail, join tables

I have a trigger built (below) which will email somebody when a field changes in a table. Now, I need to add data from another table in the same db, as well as data from a table in another db, into the body of the email.
 
In the same db I have a table called Suppliers.

In another db I have a table called Products.

I need to join a field (SupplierID) from the table in this trigger (Inventory) with a field in table Suppliers (SupplierID) so I can get the name of the supplier, which is in table Suppliers (SupplierName).

In the other db, table products has the name of the product (ProductName).

Bottom line, In need to display, in addition to the sku and other data already in the trigger, the supplier name and the product name.

All these tables will join on a common field (SKU).

Can this be done?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER TRIGGER [dbo].[Cost_Change]
   ON  [dbo].[Inventory] 
   AFTER UPDATE
AS 
BEGIN
	
	SET NOCOUNT ON;
	--if update(cost)
	if exists (Select deleted.cost from deleted inner join inserted on deleted.Local_sku = inserted.local_sku where deleted.cost <> inserted.cost)
    Begin
     Insert into Audit_Cost (username, "when", SKU, old, new) select SUSER_SNAME(), GetDate(), inserted.local_sku, deleted.cost, inserted.cost from inserted inner join deleted on inserted.local_sku = deleted.local_sku
	
	  DECLARE @tableHTML  NVARCHAR(MAX) ;
	  Declare @sku  nvarchar(50);
	  Declare @old  money;
	  Declare @new  money;

      Select @sku= inserted.local_sku, @old= deleted.cost, @new=inserted.cost from inserted inner join deleted on inserted.local_sku = deleted.local_sku
		
	    SET @tableHTML =
		  N'<H3>The following cost changes were made:</H3>' +
		  'SKU ' + @sku + '<br />Old cost: <b>' + cast(@old as nvarchar(20)) + '</b><br />New cost: <b>' +  cast(@new as nvarchar(20)) + '</b><br />Changed by ' + SUSER_SNAME()
	    

	EXEC msdb.dbo.sp_send_dbmail @recipients='[email protected]',
	  @subject = 'Cost Change',
	  @body = @tableHTML,
	  @body_format = 'HTML' ;
	
	end 

END

Answer : SQL 2005 Trigger, send mail, join tables

I think a KeyedCollection would work for you.l

http://msdn.microsoft.com/en-us/library/ms132438.aspx
Random Solutions  
 
programming4us programming4us