Troubleshooting “Refresh Failed” because of SQL Server Agent job conflicts

The error “Refresh Failed” occurs during an Excel workbook refresh when the target database, tables, or views are locked, modified, or temporarily unavailable due to a scheduled SQL Server Agent background job.

This error typically happens during automated morning refreshes. While your Excel query is reading data, an ETL (Extract, Transform, Load) job run by SQL Server Agent is simultaneously rebuilding indexes, truncating staging tables, or executing large batch updates. This conflict leads to lock timeouts, resource starvation, or deadlock errors.

How to Resolve SQL Server Agent Refresh Conflicts

To eliminate refresh failures caused by Agent job conflicts, coordinate the timing of your Excel refreshes so they run after ETL batch windows complete. If execution schedules must overlap, ask your DBA to enable READ_COMMITTED_SNAPSHOT (RCSI) on the database, or add the WITH (NOLOCK) query hint to your SQL statements to prevent writer transactions from blocking read queries.


Understanding Why Batch Jobs Break Excel Refreshes

SQL Server manages concurrent transactions using locks. Locks ensure that a user cannot read data that is in the middle of being modified (preventing “dirty reads”).

Conflicting Schedules:
06:00 AM ──> SQL Server Agent: Nightly ETL Job starts
             ├── Step 1: TRUNCATE TABLE Staging_Orders (Exclusive Lock: X)
             └── Step 2: INSERT 500,000 rows (Bulk Update)

06:05 AM ──> Excel Workbook: Scheduled Morning Report Refresh
             └── Executes: SELECT * FROM Orders (Shared Lock: S requested)
                 └── BLOCKED: Waiting for ETL Job to finish...
                 └── After 30-60s -> Error: "Refresh Failed" / Lock Timeout

When an Excel user attempts to refresh data while a SQL Server Agent job runs:

  1. Exclusive Table Locks (X Locks): Operations like TRUNCATE, ALTER TABLE, or heavy INSERT INTO ... WITH (TABLOCK) request exclusive access. Excel cannot place a shared read lock on the table and waits until the query times out.
  2. Deadlocks: The Excel query holds locks on Table A and requests Table B, while the SQL Agent job holds locks on Table B and requests Table A. SQL Server detects the stalemate and terminates one connection as the “deadlock victim.”
  3. Empty Result Sets: If Excel queries a staging table between a TRUNCATE command and a subsequent INSERT command, the refresh completes with zero rows, corrupting downstream formulas and charts.

Step 1: Identify Conflicting SQL Server Agent Jobs

To confirm whether a background Agent job is causing the failure, align the timestamp of your Excel refresh failure with the database execution history.

Run this query in SQL Server Management Studio (SSMS) to review jobs that ran during the failure window:

SELECT
    j.name AS JobName,
    h.step_name AS StepName,
    msdb.dbo.agent_datetime(h.run_date, h.run_time) AS ExecutionStartTime,
    h.run_duration AS DurationInSeconds,
    CASE h.run_status
        WHEN 0 THEN 'Failed'
        WHEN 1 THEN 'Succeeded'
        WHEN 2 THEN 'Retry'
        WHEN 4 THEN 'In Progress'
    END AS ExecutionStatus,
    h.message AS ExecutionMessage
FROM msdb.dbo.sysjobhistory h
JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
WHERE h.run_date = CONVERT(VARCHAR(8), GETDATE(), 112)
ORDER BY ExecutionStartTime DESC;

If the job execution times match your Excel refresh failures, you have isolated the conflict.


Step 2: Use the WITH (NOLOCK) Table Hint

If your Excel workbook connects through a native SQL query statement (e.g., in Power Query or an OLE DB connection definition), you can override shared locking behavior by adding the WITH (NOLOCK) hint.

Original SQL Query

SELECT OrderID, CustomerID, OrderDate, TotalAmount
FROM dbo.FactSales;

Modified Non-Blocking Query

SELECT OrderID, CustomerID, OrderDate, TotalAmount
FROM dbo.FactSales WITH (NOLOCK);

What WITH (NOLOCK) Does:

  • Instructs the database engine to read records immediately without waiting for other transactions to release exclusive locks.
  • Prevents the query from blocking or being blocked by SQL Server Agent jobs.
  • Operational Consideration: NOLOCK can read uncommitted data (dirty reads). If an Agent job is rolling back an incomplete batch transaction, transient data might be read into Excel. Use this hint only for analytical summary reporting, not for auditing transactions.

Step 3: Enable Read Committed Snapshot Isolation (RCSI)

The most permanent architectural solution—which eliminates read/write contention without requiring changes to individual Excel workbooks—is for your DBA to enable Read Committed Snapshot Isolation (RCSI) on the database.

Under standard SQL Server locking, writers block readers, and readers block writers. Under RCSI, SQL Server uses row versioning in TempDB:

-- Executed by Database Administrator
ALTER DATABASE [YourDatabaseName]
SET READ_COMMITTED_SNAPSHOT ON
WITH ROLLBACK IMMEDIATE;

The Benefit:

  • When an Agent job writes or updates rows, readers (such as Excel workbooks) automatically read the last committed snapshot of the data from TempDB.
  • Excel queries never wait for Agent jobs to finish.
  • Agent jobs never fail due to Excel holding shared read locks on production tables.

If enabling snapshot isolation puts heavy demand on temporary database storage, monitor your server capacity using Troubleshooting “TempDB” exhaustion on the server triggered by Excel refreshes..


Step 4: Stagger Refresh Automation Schedules

If modifying server configurations or SQL statements is not feasible, adjust your scheduling windows to prevent overlap.

Coordinated Operational Window:
02:00 AM – 04:30 AM: SQL Server Agent Maintenance & ETL Jobs Run
04:30 AM – 05:00 AM: Buffer Window (Index Maintenance & Statistics Update)
05:00 AM Onward:     Excel Power Query / Power BI Workbooks Scheduled to Refresh
  • Schedule automated workbook refreshes (via Windows Task Scheduler, Power Automate, or SharePoint background refresh) at least 30 to 45 minutes after the historical maximum completion time of the SQL Agent job.
  • Configure the SQL Server Agent job to send an alert or write a “success” flag record to a status table once complete, and configure your Excel refresh macro to check that table before pulling primary data.

Step 5: Handling Deadlock Termination

If your refresh terminates with an explicit deadlock error message, SQL Server chose your Excel connection as the deadlock victim to allow the scheduled Agent job to complete.

Microsoft OLE DB Provider for SQL Server: Transaction (Process ID 64)
was deadlocked on lock resources with another process and has been chosen as the deadlock victim.
Rerun the transaction.

To diagnose the competing process IDs and verify which resources clashed during execution, see Solving “Transaction (Process ID) was deadlocked on lock resources.” and Troubleshooting “Deadlocked” connections when Excel and SQL compete for resources..

If the workbook refresh fails without a clear deadlock notification while running in the background, check client-side update settings covered in Fixing “The background refresh of ‘Query’ failed.”. If the lock contention causes your connection to hang until the client disconnects, extend your timeout limits using Handling “Timeout Expired” errors in large-scale SQL views..