Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

cursor inside trigger

Posted on 2006-11-29
7
Medium Priority
?
5,992 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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

926 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