Fix Power Query Running SQL Insert Scripts Multiple Times in Excel
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.

- 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.
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.
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.
In your database, set up a temporary staging table that mirrors your target table but allows duplicate records and lacks unique key constraints.
Modify your current script to point to the temporary staging table so that any duplicate executions by Power Query only populate the temporary environment.
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.
Add a final step in your stored procedure to delete the processed records from the temporary staging table to prepare for the next run.

Move Database Write Operations to VBA
Using VBA provides absolute control over when and how many times an action query is executed, bypassing Power Query entirely for write operations.
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. Download and Install: Visit the official WPS Office website to download the lightweight installer for free.
- 2. Open your Workbook: Launch WPS Spreadsheet and open your existing Excel workbook containing your data and macro scripts.
- 3. Run your Database Macros: Navigate to the Developer tab to run your VBA-based database connections smoothly without multiple background query evaluations.

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.




