Solved

cursor inside trigger

Posted on 2006-11-29
7
5,978 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 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:ScottPletcher
ScottPletcher 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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:ScottPletcher
ID: 18038276
You need to set and check the rc ... maybe there is some error occuring in the xp itself.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql server query? 6 28
SQL Script to find duplicates 16 20
Safely Uninstall SQL Server 2008 R2 Express 3 26
Merge Statement 3 9
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now