Document Editing Problems

How to Reattach an SQL Data Source to a Word Document

WPS EditorWPS Editor Oct 10, 2026 870 views

Question details

The user needs to reconnect a Word document to an SQL data source to fix mail merge commands that are failing to run in certain files.

How to Reattach an SQL Data Source to a Word Document
Product
Word
Device & OS
not provided
Scenario
Executing a mail merge where the SQL command or data connection works in one document but disconnects or fails in others.
Observed behavior
The connection to the SQL data source drops or the mail merge configuration is lost, preventing the query from running successfully in the affected documents.
Before you start

Ensure you have the necessary network access and login credentials for the SQL database before attempting to reconnect the data source.

Solution 1Recommended

Reconfigure the Mail Merge Main Document

Reset the document type and reattach the SQL data source to restore mail merge functionality.

When a Word document loses its connection to an SQL database, the mail merge configuration must be explicitly reset. By redefining the document type and re-selecting the data source, you force the application to re-establish the connection parameters.

1
Open the affected document

Launch your word processor and open the specific document that is failing to run the SQL command.

2
Set the document type

Navigate to the 'Mailings' tab on the top ribbon, click 'Start Mail Merge', and select your preferred document type (such as 'Letters' or 'Email Messages').

3
Reattach the SQL data source

Click 'Select Recipients' and choose 'Use an Existing List'. Browse to your SQL database connection file (.odc) or enter your server credentials to connect directly to the database.

4
Verify the query

Once connected, click 'Edit Recipient List' to confirm that your SQL query is successfully pulling the correct data and connection settings are active.

5
Save the configuration

Save the document immediately. This binds the new mail merge configuration and SQL connection settings to the file for future use.

Reconfigure the Mail Merge Main Document
Configuration Saved: Saving the document ensures the SQL connection remains active and prevents the 'SQL command failed' prompt during future sessions.
Efficient Document Processing

Easily Manage Mail Merge and Data Sources with WPS Office

WPS Office provides a highly intuitive Mail Merge feature that makes it simple to connect external data sources, including databases and spreadsheets, to your documents without complex configuration.

  1. 1. Access Mail Merge: Open WPS Writer and navigate to the 'References' or 'Mailings' tab on the top ribbon.
  2. 2. Activate the Tool: Click on 'Mail Merge' to activate the dedicated merge toolbar.
  3. 3. Connect Data Source: Select 'Open Data Source' to quickly connect your database, query file, or spreadsheet to the current document.
  4. 4. Insert Fields and Merge: Insert the required merge fields into your document and click 'Merge to New Document' to generate your final files.
Full compatibility with Microsoft Word formats (.doc, .docx)Intuitive Mail Merge wizard for quick and stable setupEasily import external data sources without connection dropsLightweight and fast performance on all devices
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Word document lose its SQL data source connection?

Security settings in modern word processors often prompt users to confirm SQL commands upon opening a document to prevent malicious code execution. If this prompt is dismissed, or if macros and data connections are disabled in the Trust Center, the document will drop its link to the data source.

How do I fix the 'SQL command failed' error during a mail merge?

This error typically occurs if the database schema has changed, the server is offline, or permissions are restricted. Verify your SQL connection string, check your database access rights, and reattach the data source fresh using the 'Select Recipients' option.

Can I use an Excel file instead of an SQL database for mail merge?

Yes, Excel spreadsheets are highly compatible with the Mail Merge feature and are often easier to manage than SQL databases for simpler tasks. Choose 'Use an Existing List' in the Mailings tab and select your .xlsx file instead of an SQL connection file.