Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 15264

Database Mail from Insert Trigger.

$
0
0

Hi all,

The Configuration:

SQL Server 2005, ASPX Web Application, IIS6, ASP Framework 4.0, VisualSudio 2010 VB.

The task:

Send an email from Insert trigger. The data inserted from aspx web page.

Problem:

I have no issues with sending the email, but with its content! I begin with empty table and start to add records, everything is fine DBM sends reports as expected. At some point DBM locks with last report and continue to send it on every new insert. The data in table is correct but report always the same. Untill I delete all data in table.

Here is the trigger:

USE [TMDBWeb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[NewReport]
ON [dbo].[Maintains]
FOR INSERT
AS
   declare @CustomerID int
   declare @CustomerName nvarchar(50)
   declare @Town nvarchar (50)
   declare @Phone nvarchar(50)
   declare @Mobile nvarchar(50)
   declare @Address nvarchar(50)
   declare @Date datetime
   declare @Complaint nvarchar(255)
   declare @Tech nvarchar (20)
   declare @ReportID int
   declare @Email nvarchar(50)
   declare @body nvarchar(max)
   declare @subject varchar(100)

   SELECT @ReportID = RepID, @CustomerID = CustID, @Date = RepDate, @Complaint = Complaint, @Tech = Tech
   FROM Maintains

   SELECT @Email = Email
   FROM ReportID
   WHERE TechName = @Tech
 
   SELECT @CustomerName = CustName , @Town = Town, @Phone = Phone, @Mobile = Mobile, @Address = Addr
   FROM Customer
   WHERE CustID = @CustomerID
  
            SET @body =
   '<html><body dir="rtl">' +
   '<p>Complaint: ' + @Complaint + '<p/>' +
   '</body></html>'

          SET @subject = CONVERT(varchar, @ReportID, 20) + ' - ' + @CustomerName + ', ' + @Town
         
   EXEC msdb.dbo.sp_send_dbmail 
        @profile_name = 'TMDB Report',
        @body_format = 'HTML',
        @recipients = @Email,
        @subject = @subject,
        @importance = 'High',
        @body = @body   

Any help welcomed!

Lev



Viewing all articles
Browse latest Browse all 15264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>