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,039 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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