• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

We use sp_send_dbmail in SQL Server 2005 within a Trigger, But there is a problem. when the appllication hits it, it just makes the application hang and the sql server datbase is locking, too.

Dear Sir/Madam
We use sp_send_dbmail in SQL Server 2005 within a  Trigger, But there is a problem.  when the appllication hits it, it just makes the application hang and the sql server datbase is locking, too.

Here is our trigger:

USE [GSBHEAT]
GO
/****** Object:  Trigger [dbo].[ExecEmailHeat]    Script Date: 11/20/2007 07:36:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



ALTER TRIGGER [dbo].[ExecEmailHeat] ON [dbo].[email_survey]
FOR INSERT
AS
DECLARE @dir varchar(5)
DECLARE @CallID varchar(20)
DECLARE @rating varchar(10)
DECLARE @knowledge varchar(10)
DECLARE @Courteous varchar(10)
DECLARE @followup varchar(10)
DECLARE @Comments varchar(250)
DECLARE @L_Status      int
Select @dir=exdir, @CallID=callid,   @rating=rating, @knowledge=knowledge, @Courteous=courteous,

@followup=followup, @Comments=Rtrim(Ltrim(comments)) From INSERTED

IF @dir='Yes'
BEGIN
      CREATE TABLE yw_texttab (c1 varchar(500), c2 varchar(500))
      INSERT yw_texttab Select  'Please Contact ' +  p.firstname + ' ' + p.lastname
                        + ' regarding HEAT call ' + c.callid
                        + ' The current phone number is ' + p.Phone  + ' The survey feedback is

listed as follows',' Rating = ' + @rating + '  Knowledgeable='  + @knowledge + ' Courteous=' +  @Courteous
                        + '  followup=' +  @followup + '  Comments=' +  @Comments
      from calllog c, profile p
      where c.custid=p.custid
      and c.callid=@Callid


--      DECLARE @cmd varchar(1000)
--      SET @cmd = 'SELECT RTRIM(c1), RTRIM(c2) FROM yw_texttab'
      EXEC msdb.dbo.sp_send_dbmail
        @recipients='yong.wu@chicagogsb.edu',
        @profile_name = 'sqlserver',  
        @query = 'SELECT COUNT(*) FROM yw_texttab',
            @query_result_header= 'TRUE',
            @Subject='Request Executive Director Follow up',
--            @set_user='dbo',
            @execute_query_database='gsbheat';
END
0
yong_wu
Asked:
yong_wu
1 Solution
 
spprivateCommented:
Actually there are few points.
First you are trying to create a table for every insert which is wrong.
Second Triggers by themselves make the application very slow in responding and over that the send email will have some overhead.
What I understand is you are trying to get some information on  survey and email it as and when entered right/
0
 
yong_wuAuthor Commented:
That's right; create table and insert data have no problem at all; the only problem is send the email which is the very last part  start with
EXEC msdb.dbo.sp_send_dbmail
       
0
 
imitchieCommented:
sp_send_dbmail should not be used inside a transaction
i don't know if a trigger counts as one.

http://www.eggheadcafe.com/forumarchives/SQLServerserver/Dec2005/post25450969.asp

may be better inserting a record into some table, and create an SQL job that will recur every minute - to check that table for emails to send?
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now