• I tried the following script but it is not working properly. It is sending just plain attachment no result

    Ue [Tools]

    CREATE PROCEDURE [dbo].[Sp_JObStatus](@JobName SysName)

    As

    select job.Originating_Server Server

    ,job.Name JobName

    ,job.job_ID

    ,activity.run_requested_Date run_request_Date

    ,activity.stop_execution_date stop_execution_date

    ,datediff(minute, activity.run_requested_Date, getdate()) AS Elapsedmin

    FROM

    msdb.dbo.sysjobs_view job

    INNER JOIN --select top(1) * from

    msdb.dbo.sysjobactivity activity

    ON (job.job_id = activity.job_id)

    WHERE

    job.name =

    @JobName

    and

    run_Requested_date is not null

    and stop_execution_date is null

    IF EXISTS(Select

    job.Originating_Server Server

    ,job.Name JobName

    ,job.job_ID

    ,activity.run_requested_Date run_request_Date

    ,activity.stop_execution_date stop_execution_date

    ,datediff(minute, activity.run_requested_Date, getdate()) AS Elapsedmin

    FROM

    msdb.dbo.sysjobs_view job

    INNER JOIN --select top(1) * from

    msdb.dbo.sysjobactivity activity

    ON (job.job_id = activity.job_id)

    WHERE

    job.name =

    @JobName

    and

    run_Requested_date is not null

    and stop_execution_date is null)

    begin

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'TestProfile',

    @recipients = 'ramana3327@yahoo.com',

    @subject = 'JobRunStatus',

    @body = 'The Job is still running',

    @query='

    declare @JobName sysname

    Select

    job.Originating_Server Server

    ,job.Name JobName

    ,job.job_ID

    ,activity.run_requested_Date run_request_Date

    ,activity.stop_execution_date stop_execution_date

    ,datediff(minute, activity.run_requested_Date, getdate()) AS Elapsedmin

    FROM

    msdb.dbo.sysjobs_view job

    INNER JOIN --select top(1) * from

    msdb.dbo.sysjobactivity activity

    ON (job.job_id = activity.job_id)

    WHERE

    job.name = @JobName',

    @attach_query_result_as_file=1

    end

    else

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'TestProfile',

    @recipients = 'ramana3327@yahoo.com',

    @subject = 'JobRunStatus',

    @body = 'The Job run completed'

    GO