Data Import & Export

Use a Worksheet Cell as SQL Command Text in Excel 2016

Adam DavisAdam Davis Nov 13, 2026 868 views

Question details

Use one complete SQL query stored in a worksheet cell as the source for approximately two dozen Excel database connections, including queries that execute CREATE TABLE, INSERT, and SELECT statements consecutively.

Use a Worksheet Cell as SQL Command Text in Excel 2016
Product
Microsoft Excel 2016
Device & OS
not provided
Scenario
Maintain the SQL text in one location so that multiple database queries can reuse it when refreshed.
Observed behavior
The existing database connection and query work, but the standard connection Command text box does not directly bind an entire SQL statement to a worksheet cell.
Before you start

Confirm the database type, server name, database name, and permissions required by the SQL statements; the Power Query example below targets SQL Server. Test the batch in an approved test database, because CREATE TABLE and INSERT change database state and may run again on refresh.

Solution 1Recommended

Read a named worksheet cell with Power Query and Sql.Database

Use this approach for SQL Server when you want multiple Power Query queries to read the same SQL text from a worksheet cell.

The standard Excel connection Command text box is not a formula field: entering a cell reference there does not substitute the cell's contents. Power Query can explicitly read a named cell and pass its text to Sql.Database.

This creates Power Query queries rather than automatically changing your existing legacy connections. Each query can read the same named cell, but each refresh executes against its own configured database.

1
Name the SQL cell

Enter the complete SQL text in one worksheet cell. Select that cell, click the Name Box to the left of the formula bar, type SqlText, and press Enter. Use a workbook-scoped name so other queries can find it.

2
Create a blank Power Query query

Open Data > New Query > From Other Sources > Blank Query in Excel 2016. In builds with the newer ribbon labels, use Data > Get Data > From Other Sources > Blank Query.

3
Read the cell and submit its SQL text

In the Power Query Editor, select Home > Advanced Editor and replace the query with: let SqlText = Text.From(Excel.CurrentWorkbook(){[Name="SqlText"]}[Content]{0}[Column1]), Source = Sql.Database("YOUR_SERVER", "YOUR_DATABASE", [Query=SqlText]) in Source. Replace YOUR_SERVER and YOUR_DATABASE with your SQL Server connection details, then select Done.

4
Authorize and load the result

When prompted, select the appropriate database authentication method and approve the native query only after reviewing its SQL. Set accurate privacy levels if requested, then choose Home > Close & Load to load the returned table into Excel.

5
Reuse the named cell across queries

In Data > Show Queries, right-click the query and choose Duplicate for each additional target. Open each duplicate's Advanced Editor and change its server or database as needed while keeping the SqlText cell reference. Edit the worksheet cell once, then use Data > Refresh All to rerun the queries.

Read a named worksheet cell with Power Query and Sql.Database
Multi-statement SQL requires testing: For SQL Server, submit CREATE TABLE, INSERT, and the final SELECT as one batch, using semicolons where appropriate—not GO, which is a client-side batch separator. A local temporary table such as #Results must be created and read within the same execution scope. Sql.Database returns only the first result set when multiple result sets are produced; keep the intended output as the only result-producing SELECT. SET NOCOUNT ON can suppress row-count messages but does not remove extra result sets.
Free Microsoft Office alternative

Use WPS Spreadsheet to Review Exported SQL Results

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Excel format compatibility for an easy transition. Use WPS Spreadsheet to review and analyze exported database results; it does not resolve Excel 2016 connection settings or guarantee support for Power Query and ADODB workflows.

Open and edit Microsoft Excel .xlsx and .xls files.Review SQL results exported to CSV or Excel workbooks.Use familiar spreadsheet formulas, sorting, and filtering to analyze returned data.Move everyday spreadsheet work to a lightweight, easy-to-use Office alternative.
microsoft office alternative - wps office

Frequently Asked Questions

Can I put a cell reference directly in Excel's connection Command text box?

Not as a built-in substitution for a complete SQL statement. Data > Connections > Properties > Definition shows literal command text, not a worksheet formula. Use Power Query to read a named cell or VBA to assign and execute its contents.

Can one cell supply SQL text to approximately two dozen connections?

Yes, if each query is configured to read that cell. In Power Query's Advanced Editor, use Excel.CurrentWorkbook() to access the workbook-scoped name SqlText, then pass the value to Sql.Database. Existing legacy connections must be adapted or replaced; naming the cell alone does not link them.

What delimiter should I use between CREATE TABLE, INSERT, and SELECT?

For SQL Server, use valid T-SQL statement syntax with semicolons where appropriate. Do not include GO in the text submitted through Sql.Database or ADODB: GO is interpreted by tools such as SQL Server Management Studio, not by the SQL Server engine. Other database providers may impose different multi-statement restrictions.

Why does a temporary table disappear before the SELECT runs?

A local SQL Server temporary table belongs to a session and execution scope. Creating it in one connection and selecting from it in another will not work. Submit the operations in one supported batch or use the same open ADODB connection for the commands.

Will changing the SQL cell immediately update every result table?

No. Change the cell, then use Data > Refresh All or refresh the relevant query individually. Each query reads the new text during its execution. Review data-changing statements first, since a refresh may repeat inserts or attempt to recreate tables.