Solved

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.

Posted on 2007-11-20
3
1,029 Views
Last Modified: 2008-02-01
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
Comment
Question by:yong_wu
3 Comments
 
LVL 15

Accepted Solution

by:
spprivate earned 500 total points
ID: 20319919
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
 

Author Comment

by:yong_wu
ID: 20319955
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
 
LVL 25

Expert Comment

by:imitchie
ID: 20324248
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now