Hi all,
I have created one SP for sending mail with formatting the HTML code inside script whenever i am individually declaring it and printing its expected but the problem at time of executing SP its giving error like this
Msg 132, Level 15, State 1, Line 47
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure.
what is the possibilities to overcome this problem follwing is my stored procedure code
ALTER PROCEDURE [dbo].[USP_DataLoadMailsend_essRules] AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; BEGIN TRY ---BEGIN TRANSACTION T1 DECLARE @packagelogid INT DECLARE @batchlogid INT DECLARE @packagestatus CHAR(2) select @batchlogid =19870 --print(@batchlogid) DECLARE @script VARCHAR(MAX) DECLARE @tableHTML VARCHAR(MAX) DECLARE @mailheader VARCHAR(50) DECLARE @count INT DECLARE @recipients1 VARCHAR(50) DECLARE @subject1 VARCHAR(200) DECLARE @sql VARCHAR(MAX) Declare @UserId varchar(Max) Declare @Information varchar(max) Declare @TableHTML1 varchar(max) Declare @TableHTML2 varchar(max) SET @mailheader = '' SET @mailheader = (select case WHEN FileUpload = 'F' THEN 'BussinessRules is Aborted' WHEN (InRule = 'S') AND ( SELECT sum(isnull(ErrorRecords, 0)) tot FROM abc.FileUploadSummary z WHERE z.BatchId = bts.BatchId GROUP BY BatchId ) > 0 THEN 'BussinessRules is Processed with Errors' WHEN InRule = 'F' THEN 'BussinessRules is Failed' WHEN (InRule = 'S') AND ( SELECT sum(isnull(ErrorRecords, 0)) tot FROM abc.FileUploadSummary z WHERE z.BatchId = bts.BatchId GROUP BY BatchId ) = 0 THEN 'BussinessRules is Succeeded' end from abc..BatchStatus bts where BatchId=@batchlogid) /* Selecting Person Mail as Recipient */ SELECT TOP 1 @recipients1 = EmailId FROM abc.PersonEmail WHERE PersonId = ( SELECT TOP 1 personid FROM abc.FileUploadSummary WHERE BatchId = @batchlogid )AND EmailTypeId = 1 /* Selecting UserId*/ select top 1 @UserId=loginid from abc.FUS where BatchId=@batchlogid /*Selecting Information about the Status */ Set @Information= (select case WHEN FileUpload = 'F' THEN 'BussinessRules is Aborted' WHEN (InRule = 'S') AND ( SELECT sum(isnull(ErrorRecords, 0)) tot FROM abc.FileUploadSummary z WHERE z.BatchId = bts.BatchId GROUP BY BatchId ) > 0 THEN 'BussinessRules is Processed with Errors' WHEN InRule = 'F' THEN 'BussinessRules is Failed' WHEN (InRule = 'S') AND ( SELECT sum(isnull(ErrorRecords, 0)) tot FROM abc.FileUploadSummary z WHERE z.BatchId = bts.BatchId GROUP BY BatchId ) = 0 THEN 'BussinessRules is Succeeded' end + N' <br> <B>BatchId= '+ convert(varchar(250),(select @batchlogid)) +'</B>' from abc..BatchStatus bts where BatchId=@batchlogid ) /*Selecting the Error Reason*/ if exists (select 1 from BatchStatus where BatchId=@batchlogid and ( InRule='f')) begin set @TableHTML1 = '<table border=1><tr><th>Sr.No.</th><th><P>Error Reason :</th></tr>'+ cast((select td= ROW_NUMBER()over (order by (select 1)),'', td=isnull(e.ErrorDescription, '') from abc.x.tbPackageErrorLog e --50594 join abc.x.tbPackageLog p -- 10223 on p.PackageLogID=e.PackageLogID where p.BatchLogID= @batchlogid FOR XML PATH('tr'), TYPE ) as NVarchar(max)) +'</table>' -- print @tableHTML if not exists (select 1 from BatchStatus where BatchId=@batchlogid and ( InRule='f')) set @TableHTML2 = 'Error Reason :N/A' end -- insert into #tmp values ( @TableHTML1) --select * from #tmp Set @tableHTML= 'Hello '+@UserId+', <br>Information:'+isnull(@Information,'') + '<Table Border=1><Tr><th>Sr No</th><th>Uploaded files </th>:'+ CAST ((select td= ROW_NUMBER()over(order by f.FileUploadId), '', td = f.TableName , '' from abc.FileUploadSummary F where BatchId=@batchlogid FOR XML PATH('tr'), TYPE ) AS NVARCHAR(max) ) +'</table>'+ 'Error Reason :'+isnull(isnull(@TableHTML1,'')+ isnull(@TableHTML2,''),'N/A')+ --concat (isnull(@TableHTML1,''),isnull(@TableHTML2,'') '<br> Please login to Your Account for further Details..!'+'<br>@Note: This is system generated message, Do not reply to this mail. <br>Regards,<br>'+ 'Admin' print @tableHTML SET @sql = ' EXEC msdb.dbo.sp_send_dbmail @profile_name = ''DBA_mail_test'' ,@recipients = ''' + isnull(@recipients1,'''') + ''',@subject = ''' + isnull(@mailheader,'''') + ''', @body = ''' +isnull(@tableHTML,'''')+ ''', @body_format = ''HTML''' Exec(@sql) END TRY BEGIN CATCH PRINT error_message() -- Test whether the transaction is uncommittable. -- IF (XACT_STATE()) = - 1 -- ROLLBACK TRANSACTION --Comment it if SP contains only select statement DECLARE @ErrorFromProc VARCHAR(500) DECLARE @ErrorMessage VARCHAR(1000) DECLARE @SeverityLevel INT SELECT @ErrorFromProc = ERROR_PROCEDURE() ,@ErrorMessage = ERROR_MESSAGE() ,@SeverityLevel = ERROR_SEVERITY() --INSERT INTO dbo.ErrorLogForUSP ( -- ErrorFromProc -- ,ErrorMessage -- ,SeverityLevel -- ,DateTimeStamp -- ) --VALUES ( -- @ErrorFromProc -- ,@ErrorMessage -- ,@SeverityLevel -- ,GETDATE() -- ) END CATCH END
please help me to solve this problem
Niraj Sevalkar