Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cursor inside trigger

Posted on 2006-11-29
7
Medium Priority
?
5,987 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 500 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 500 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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 70

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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