Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Trigger is not working on multi row insert

$
0
0

Hello Guys,

I have created below trigger, it is working fine if we insert 1 row but the  problem is if we insert multiple rows into the table it is processing only one's.

What is the correct way to implement the insert trigger in order to process ALL rows that are inserted by one insert statement?

CREATE TRIGGER [dbo].[tr_Solution_Failure_Email_Notification] ON [dbo].[T_EDM_SOL_FAILURE] 

AFTER INSERT AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRY   
    
    DECLARE @sol_runid int
    DECLARE @sol_parent_runid int
    DECLARE @edmEnvironment varchar(256)
    DECLARE @serverName varchar(256)
    DECLARE @subjectPrefix varchar(256)
    DECLARE @subject varchar(256)
    DECLARE @solutionName varchar(256)
    DECLARE @email_body varchar(max)
    
    DECLARE @solution_runid varchar(256)
DECLARE @launched_by varchar(256)
DECLARE @failed_component varchar(256)
DECLARE @message varchar(256) 
DECLARE @log_time varchar(256)
DECLARE @process_type varchar(256)
DECLARE @process_name varchar(256)   
    
    SELECT @edmEnvironment = text_val
    FROM t_edm_control
    WHERE TYPE = 2
    
    SELECT @serverName = text_val
    FROM t_edm_control
    WHERE TYPE = 3    
     
    IF @edmEnvironment = 'TEST'
       BEGIN
          SELECT @subjectPrefix = 'TEDMF:'
       END
    ELSE IF @edmEnvironment = 'QA'
       BEGIN
          SELECT @subjectPrefix = 'QEDMF:'
       END
    ELSE IF @edmEnvironment = 'PROD'
       BEGIN
          SELECT @subjectPrefix = 'PEDMF:'
       END  


    SELECT @sol_runid = inserted.[sol_runid] from inserted -- 50318

    SELECT @sol_parent_runid = parentrunid, @launched_by = LAUNCHEDBY, @solutionName = name 
    FROM CADIS.vwSolutionHistory
    WHERE RUNID = @sol_runid
    
    SELECT @subject = @subjectPrefix + ' ' + @solutionName
    
    --- CURSOR TO RETRIEVE ALL RELATED LOG MESSAGES ---
    DECLARE error_cursor CURSOR FOR
    
    SELECT
vph.processname,
vpl.logmessage, 
vpl.logtime,
vpl.processtype,
vpl.processname
FROM
CADIS.vwSolutionHistory vsh
inner join
CADIS.vwProcessHistory vph
ON 
vph.PARENTRUNID = vsh.runid
inner join
CADIS.vwProcessLog vpl
ON
vpl.runid = vph.runid
WHERE
vsh.RUNID = @sol_runid
and
vsh.RUNSUCCESSFUL = 0
and
vph.RUNSUCCESSFUL = 0
order by vph.runid 
---------------------------------------------------

SELECT @email_body = 'An EDM ' + @edmEnvironment + ' solution has failed on application server ' + @serverName + '.'

SELECT @email_body = @email_body + '<TABLE>'
SELECT @email_body = @email_body + '<TR>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Solution Runid: ' + '<B>' + CONVERT(VARCHAR(256),@sol_runid) + '   '
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Solution Parent Runid: ' +  '<B>' + CONVERT(VARCHAR(256),@sol_parent_runid) + '   '
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Solution Name: ' + '<B>' + @solutionName
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Launched By: ' + '<B>' + @launched_by 
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '</TR>'
SELECT @email_body = @email_body + '</TABLE>'

SELECT @email_body = @email_body + '<TABLE border = "1">'
SELECT @email_body = @email_body + '<TR>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Failed Component'
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Message'
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'When Logged'
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Process Type'
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body = @email_body + '<TD>'
SELECT @email_body = @email_body + 'Process Name'
SELECT @email_body = @email_body + '</TD>'
SELECT @email_body + @email_body + '</TR>'


OPEN error_cursor

FETCH NEXT FROM error_cursor
INTO @failed_component, @message, @log_time, @process_type, @process_name 

WHILE @@FETCH_STATUS = 0
 BEGIN
     
     SELECT @email_body = @email_body + '<TR>'
     SELECT @email_body = @email_body + '<TD>'
     SELECT @email_body = @email_body + @failed_component
     SELECT @email_body = @email_body + '</TD>'
     SELECT @email_body = @email_body + '<TD>'
     SELECT @email_body = @email_body + @message
     SELECT @email_body = @email_body + '</TD>'
     SELECT @email_body = @email_body + '<TD>'
     SELECT @email_body = @email_body + @log_time
     SELECT @email_body = @email_body + '</TD>'
     SELECT @email_body = @email_body + '<TD>'
     SELECT @email_body = @email_body + @process_type
     SELECT @email_body = @email_body + '</TD>'
     SELECT @email_body = @email_body + '<TD>'
     SELECT @email_body = @email_body + @process_name
     SELECT @email_body = @email_body + '</TD>'
     SELECT @email_body + @email_body + '</TR>'
    
  FETCH NEXT FROM error_cursor
  INTO @failed_component, @message, @log_time, @process_type, @process_name

  END 
    
CLOSE error_cursor  
DEALLOCATE error_cursor 
  
  
SELECT @email_body = @email_body + '</TABLE>'
          
    EXECUTE msdb.dbo.sp_send_dbmail
    @profile_name='EDM_Solution_Failure_Notification_recipient_Profile',
    --@recipients='mlitchfield@incomeresearch.com;EDM@incomeresearch.com',
    @recipients='mlitchfield@incomeresearch.com',
    @subject = @subject,
    @body_format='HTML',
    @body= @email_body

    END TRY


   BEGIN CATCH

      IF @@TRANCOUNT > 0
           BEGIN
               ROLLBACK TRANSACTION;
           END

   END CATCH

END



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>