title: Export to CSV using scheduled job SQL Agent Management Studio
body: <p>I'm trying to create a SQL agent job which automatically runs the below query on a daily basis and generates a CSV file which is stored on C:\test.csv and also emailed to people. </p>
  
  <p>I've tried various options online but cannot find one which will suit my query. It is data from multiple datasets put into a single file for import into another spreadsheet for reporting.</p>
  
  <p>Any assistance is appreciated. </p>
  
  <p>I've created a SQL job with the task which performs the query, and I've tried using the advanced option on the advanced page to output the file, however, the output file doesn't get updated. </p>
  
  <pre><code>use Prod_data
  declare @ReportingStart datetime = dateadd(HH,-17,convert(datetime,convert(date,getdate())))
  declare @ReportingEnd datetime = dateadd(HH,7,convert(datetime,convert(date,getdate())))
  
  
  -- Daily Production time
  declare @Production float = (select sum(dDurationSeconds/60)
  from OEEQStateData
  where tstart &gt;= @ReportingStart and tstart &lt; @ReportingEnd
  and sStateDescription = 'Production'and sWorkcellDescription ='Hoisting')
  
  -- Daily Idle time
  declare @Idle float = (select isnull(sum(dDurationSeconds/60),0)
  from OEEQStateData
  where tstart &gt;= @ReportingStart and tstart &lt; @ReportingEnd
  and sStateDescription = 'Idle Time'and sWorkcellDescription ='Hoisting')
  
  -- Daily Unplanned time
  declare @Unplanned float = (select sum(dDurationSeconds/60)
  from OEEQStateData
  where tstart &gt;= @ReportingStart and tstart &lt; @ReportingEnd
  and sStateDescription like 'Unplanned%'and sWorkcellDescription ='Hoisting')
  
  --Daily Maintenance time
  declare @Planned float = (select sum(dDurationSeconds/60)
  from OEEQStateData
  where tstart &gt;= @ReportingStart and tstart &lt; @ReportingEnd
  and sStateDescription like 'Planned%'and sWorkcellDescription ='Hoisting')
  
  --Util
  declare @Util float = @Production/(1440-@Planned-@Unplanned)
  
  --Avail
  declare @Avail float = ((@Production+@Idle)/1440)
  
  --Hoist Schedule
  declare @HoistSched int = (select round(DS_Prod+NS_Prod,-2)
  from Schedule
  where date = convert(date,@ReportingStart))
  
  
  --Hoist Schedule for tomorrow 
  declare @HoistSchedTom int = (select round(DS_Prod+NS_Prod,-2) 
  from Schedule
  where date = convert(date,@ReportingEnd))
  
  --PM for tommorrow
  declare @PM int = (select (DS_DT+NS_DT) 
  from Schedule
  where date = convert(date,dateadd(dd,1,getdate())))
  
  --Hoist Daily Production
  
  declare @Tonnes int = (select top 1
      case
          when coalesce(lead(value) over(partition by tagname order by datetime),0) - value &lt; '0' then ''
          else coalesce(lead(value) over(partition by tagname order by datetime),0) - value
      end
   from  Linked_Database
   where datetime between @ReportingStart and @ReportingEnd
   and wwResolution = (1440 * 60000)
   and tagname = 'SALV_CV005_WX1_PROD_DATA.Actual_Input'
   )
  
   --MPS 24HR
  
  declare @MPS_today float = (select sum(value)
  from  Linked_Database
   where datetime = @ReportingEnd
   and tagname like 'MPS_FI7940%.Actual_Input')
  
   declare @MPS_yest float = ( select sum(value) 
  from  Linked_Database
   where datetime = @ReportingStart
   and tagname like 'MPS_FI7940%.Actual_Input')
  
  declare @MPS_total float = (@MPS_today-@MPS_yest)
  
  --IPDW 24HR (claypit + IPDW)
  
  declare @IPDW_today float = (select isnull(sum(value),0)
  from  Linked_Database
   where datetime = @ReportingEnd
   and tagname like '%FI792%.Actual_Input')
  
   declare @Clay_today float = (select isnull(sum(value),0) 
  from  Linked_Database
   where datetime = @ReportingEnd
   and tagname like '%FI764%_TOTAL.PVAI')
  
   declare @IPDW_yest float = (select isnull(sum(value),0) 
  from  Linked_Database
   where datetime = @ReportingStart
   and tagname like '%FI792%.Actual_Input')
  
   declare @Clay_yest float = (select isnull(sum(value),0) 
  from  Linked_Database
   where datetime = @ReportingStart
   and tagname like '%FI764%_TOTAL.PVAI')
  
   declare @IPDW_total float = (@IPDW_today+@Clay_today-@IPDW_yest-@Clay_yest)
  
  --Average airflow across both vent fan
  
  declare @VF_Avg float = (select avg(value) 
  from  Linked_Database
   where datetime between @ReportingStart and @ReportingEnd
   and tagname = 'vfans_totalairflow.pv_at')
  
   --BAC wet bulb
  declare @BAC_Wet float = (select avg(value) 
  from  Linked_Database
   where datetime between @ReportingStart and @ReportingEnd
   and tagname = 'gb_bac_tt787125a._analog_PV')
  
   declare @BAC_Dry float = (select avg(value) 
  from  Linked_Database
   where datetime between @ReportingStart and @ReportingEnd
   and tagname = 'gb_bac_tt787125b._analog_PV')
  
    --Final Select Statement
   select @HoistSched as Hoist_Sched_today, @HoistSchedTom as Hoist_Sched_Tom, @PM as PM_Tom, @Tonnes as Hoist_Act, @Util as Hoist_Util, @Avail as Hoist_Avail, @MPS_total as MPS_Dewatering_Total, @IPDW_total as IPDW_Dewatering_Total,  @VF_Avg as VFan_AVG, @BAC_Dry as BAC_Dry_AVG, @BAC_Wet as BAC_Wet_AVG
  </code></pre>