Solved

cursor inside trigger

Posted on 2006-11-29
7
5,984 Views
Last Modified: 2012-05-05
I have this cursor, it is fired when new records are inserted into a table through a stored procedure (multiple records at the same time).
The trigger is fired and I receive notification emails, the problem is there is no information display in the emails, the emails are blank (no subject or message body). Please help. Thanks.

CREATE TRIGGER SendEmail
ON tblADSS
FOR INSERT
AS
      DECLARE @ProjectNumber varchar(20), @SendTo varchar(50), @TestNm varchar(100), @MsgBody varchar(500)
       DECLARE Email_cursor CURSOR FOR
               SELECT ProjectNumber  FROM Inserted            
               OPEN Email_cursor
               FETCH NEXT FROM Email_cursor INTO @ProjectNumber
            
               WHILE @@FETCH_STATUS = 0
               BEGIN
                 
                 SET @SendTo = (SELECT [ProjectNumber] FROM Inserted WHERE ProjectNumber = @ProjectNumber)
                  SET @TestNm = (SELECT [ProjectLongTitle] FROM Inserted WHERE ProjectNumber = @ProjectNumber)
                  SET @MsgBody = (SELECT  'Project Name: ' + [ProjectLongTitle] + '<BR>'  
                              + 'Assigned to: ' + Assigned + '<BR><BR>'
                              +'<a href="http://www.yahoo.com">Click here to complete the form...</a>'
                  FROM Inserted WHERE ProjectNumber = @ProjectNumber)
                  
                  EXEC master.dbo.xp_smtp_sendmail
                      @server = '000.000.00.000',
                      @from = 'someone@someplace.com',
                        @to = 'someone1@someplace1.com',
                      @subject =@TestNm,
                      @type = 'text/html',
                      @message =@MsgBody
                  FETCH NEXT FROM Email_cursor INTO @ProjectNumber
               
               END
            
               CLOSE Email_cursor
               DEALLOCATE Email_cursor
0
Comment
Question by:CochiseCounty
[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
  • 2
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
MNelson831 earned 125 total points
ID: 18037855
Try this:
CREATE TRIGGER SendEmail
ON tblADSS
FOR INSERT
AS
     DECLARE @ProjectNumber varchar(20), @SendTo varchar(50), @TestNm varchar(100), @MsgBody varchar(500)
      DECLARE Email_cursor CURSOR FOR
             SELECT ProjectNumber  FROM Inserted          
             OPEN Email_cursor
             FETCH NEXT FROM Email_cursor INTO @ProjectNumber
         
             WHILE @@FETCH_STATUS = 0
             BEGIN
---Changed code starts here
Select
     @SendTo = [ProjectNumber],
     @TestNm = [ProjectLongTitle],
     @MsgBody ='Project Name: ' + isNull([ProjectLongTitle],'Its Null') + '<BR>'  
                         + 'Assigned to: ' + isNull(Assigned,'Its Null') + '<BR><BR>'
                         + '<a href="http://www.yahoo.com">Click here to complete the form...</a>'
FROM
     Inserted
WHERE
     ProjectNumber = @ProjectNumber
---Changed code ends here

  EXEC master.dbo.xp_smtp_sendmail
                   @server = '000.000.00.000',
                   @from = 'someone@someplace.com',
                    @to = 'someone1@someplace1.com',
                   @subject =@TestNm,
                   @type = 'text/html',
                   @message =@MsgBody
                FETCH NEXT FROM Email_cursor INTO @ProjectNumber
             
             END
         
             CLOSE Email_cursor
             DEALLOCATE Email_cursor
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 125 total points
ID: 18037864
Odds are one of the columns is NULL which is NULLing out the entire value.  Try this:

SET @MsgBody =  (SELECT  'Project Name: ' + ISNULL([ProjectLongTitle], '') + '<BR>'  
                         + 'Assigned to: ' + ISNULL(Assigned, '') + '<BR><BR>'
                         +'<a href="http://www.yahoo.com">Click here to complete the form...</a>'
               FROM Inserted WHERE ProjectNumber = @ProjectNumber)
               
0
 

Author Comment

by:CochiseCounty
ID: 18037940
I followed MNelson831 instructions, still not working, emails still empty. And I am sure nothing is null.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CochiseCounty
ID: 18038012
I found the bug! There is nothing wrong with the code, the Projectnumber is longer than 20 characters, I change the variable @@ProjectNumber varchar(20) to 50 characters then it works. Thanks a lot for helping anyway, I will split the points between 2 of you!
0
 
LVL 15

Expert Comment

by:MNelson831
ID: 18038019
ok, try this then:

---Changed code starts here
Select
     @SendTo = [ProjectNumber],
     @TestNm = [ProjectLongTitle],
     @MsgBody ='Message Body Text Here'
FROM
     Inserted
WHERE
     ProjectNumber = @ProjectNumber
---Changed code ends here

EXEC master.dbo.xp_smtp_sendmail
                   @server = '000.000.00.000',
                   @from = 'someone@someplace.com',
                    @to = 'someone1@someplace1.com',
                   @subject ='Subject test here',
                   @type = 'text/html',
                   @message =@MsgBody
                FETCH NEXT FROM Email_cursor INTO @ProjectNumber
             
             END
         
             CLOSE Email_cursor
             DEALLOCATE Email_cursor
0
 
LVL 15

Expert Comment

by:MNelson831
ID: 18038026
:-)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 18038276
You need to set and check the rc ... maybe there is some error occuring in the xp itself.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Instance service gone? 5 39
Related to SQL Query 5 21
T-SQL: How to extract records into a new table 7 21
calculate running total 8 15
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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