Power Query Problems

Fix Power Query Running SQL Insert Scripts Multiple Times in Excel

Ayan MasoodAyan Masood Nov 3, 2026 869 views

Question details

The user needs a way to prevent Power Query from evaluating and executing ODBC SQL insert scripts multiple times to avoid duplicate records.

Fix Power Query Running SQL Insert Scripts Multiple Times in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Executing Pervasive SQL insert statements to write data back to a database using Power Query and an ODBC connection within an Excel application.
Observed behavior
Power Query evaluates the script repeatedly, creating duplicate database records. Adding a unique key to the table prevents the duplicates but triggers duplicate-key errors in the application.
Before you start

Ensure you have backed up your target database tables before modifying your SQL insert scripts. Verify that you have the necessary database permissions to create temporary staging tables and stored procedures.

Solution 1Recommended

Use a Stored Procedure with a Temporary Staging Table

This is the recommended approach to bypass Power Query's multiple-evaluation limitation by staging data before committing distinct rows to the target table.

Power Query is designed primarily for reading and transforming data, not for writing data via action queries (like INSERT, UPDATE, or DELETE). Because its engine can evaluate expressions multiple times during a background refresh or schema check, direct SQL insert statements will execute repeatedly. Offloading this logic to a stored procedure resolves the problem safely.

1
Create a staging table

In your database, set up a temporary staging table that mirrors your target table but allows duplicate records and lacks unique key constraints.

2
Insert raw data into the staging table

Modify your current script to point to the temporary staging table so that any duplicate executions by Power Query only populate the temporary environment.

3
Create a stored procedure for distinct insertion

Write a stored procedure in your database that selects only distinct (unique) records from the temporary staging table and inserts them into your actual target table.

4
Clear the staging table

Add a final step in your stored procedure to delete the processed records from the temporary staging table to prepare for the next run.

Use a Stored Procedure with a Temporary Staging Table
Best Practice: Avoid using 'On Error Resume Next' in VBA to simply hide the duplicate-key errors. Always handle database transactions explicitly through staging tables or UPSERT logic.
Free Microsoft Office alternative

Try WPS Office for Advanced Spreadsheet Data Processing

If you frequently manage complex database connections and require precise macro execution without the unpredictable background refreshes of Power Query, WPS Office provides a highly compatible and robust alternative. With full support for VBA macros, it empowers you to handle database scripts accurately.

  1. 1. Download and Install: Visit the official WPS Office website to download the lightweight installer for free.
  2. 2. Open your Workbook: Launch WPS Spreadsheet and open your existing Excel workbook containing your data and macro scripts.
  3. 3. Run your Database Macros: Navigate to the Developer tab to run your VBA-based database connections smoothly without multiple background query evaluations.
Seamless compatibility with Microsoft Excel (.xlsx, .xls, .csv) formats.Full support for VBA (WPS Macros) to execute precise ADO/ODBC database connections and write operations.Lightweight architecture ensuring fast loading times for large datasets.Completely free to download with a familiar interface and zero learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Power Query execute my SQL query multiple times?

Power Query's engine is built for data retrieval and transformation, not writing data. During background analysis, schema evaluation, and data fetching, the engine may evaluate queries and their underlying M code multiple times, causing action queries like INSERT to trigger repeatedly.

Can I force Power Query to run a script only once?

No, you cannot strictly control the internal evaluation frequency of the Power Query engine. Because of this, it is highly recommended to separate data reading (using Power Query) from data writing (using VBA or stored procedures).

Is it safe to use 'On Error Resume Next' to ignore duplicate key errors?

It is generally discouraged. Suppressing errors with 'On Error Resume Next' hides underlying logic flaws and can lead to silent data corruption or missing records. It is much safer to stage the data or write an UPSERT statement.

Does WPS Office support ODBC database connections via VBA?

Yes, WPS Office fully supports VBA (WPS Macros) which allows you to utilize ADO and ODBC connections to read from and write to external SQL databases securely and efficiently.