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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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