Excel Spreadsheet Automation with the QUERY Formula



Pull data, automate spreadsheets, and more with the QUERY formula.

The CData Excel Add-In for SQL Server provides formulas that can edit, save, and delete SQL Server data. The following three steps show how you can automate the following task: Search SQL Server data for a user-specified value and then organize the results into an Excel spreadsheet.

The syntax of the CDATAQUERY formula is the following: =CDATAQUERY(Query, [Connection], [Parameters], [ResultLocation]);

This formula requires three inputs:

  • Query: The declaration of the SQL Server data records you want to retrieve or the modifications to be made, written in standard SQL.
  • Connection: Either the connection name, such as SQLConnection1, or a connection string. The connection string consists of the required properties for connecting to SQL Server data, separated by semicolons.

    Connecting to Microsoft SQL Server

    Connect to Microsoft SQL Server using the following properties:

    • Server: The name of the server running SQL Server.
    • User: The username provided for authentication with SQL Server.
    • Password: The password associated with the authenticating user.
    • Database: The name of the SQL Server database.

    Connecting to Azure SQL Server and Azure Data Warehouse

    You can authenticate to Azure SQL Server or Azure Data Warehouse by setting the following connection properties:

    • Server: The server running Azure. You can find this by logging into the Azure portal and navigating to "SQL databases" (or "SQL data warehouses") -> "Select your database" -> "Overview" -> "Server name."
    • User: The name of the user authenticating to Azure.
    • Password: The password associated with the authenticating user.
    • Database: The name of the database, as seen in the Azure portal on the SQL databases (or SQL warehouses) page.
  • ResultLocation: The cell that the output of results should start from.

Pass Spreadsheet Cells as Inputs to the Query

The procedure below results in a spreadsheet that organizes all the formula inputs in the first column.

  1. Define cells for the formula inputs. In addition to the connection inputs, add another input to define a criterion for a filter to be used to search SQL Server data, such as ShipCountry.
  2. In another cell, write the formula, referencing the cell values from the user input cells defined above. Single quotes are used to enclose values such as addresses that may contain spaces.
  3. =CDATAQUERY("SELECT * FROM Orders WHERE ShipCountry = '"&B6&"'","User="&B1&";Password="&B2&";Database="&B3&";Server="&B4&";Port="&B5&";Provider=SQL",B7)
  4. Change the filter to change the data.

Ready to get started?

Download a free trial of the Excel Add-In for SQL Server to get started:

 Download Now

Learn more:

SQL Server Icon Excel Add-In for SQL Server

The SQL Excel Add-In is a powerful tool that allows you to connect with live data from SQL Server databases, directly from Microsoft Excel.

Use Excel to read, write, and update SQL Tables. Perfect for mass imports / exports / updates, data cleansing & de-duplication, Excel based data analysis, and more!