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,028 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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