Solved

cursor inside trigger

Posted on 2006-11-29
7
5,986 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

636 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