Editor's Note: Share your SQL Server discoveries, comments, problems, solutions, and experiences with products and reach out to other SQL Server Magazine readers. Email your contributions (400 words or less) to r2r@sqlmag.com. Please include your phone number. We edit submissions for style, grammar, and length. If we print your submission, you'll get $50.

SQL.REQUEST: Excel's Sleeping Giant Function
Microsoft Excel has a powerful yet poorly documented and little-known function that lets users run ad hoc queries against a SQL Server database through a Data Source Name (DSN) connection and have the results returned to a specific cell in the Excel spreadsheet or an array. To work correctly, this codeless solution requires Excel 97 or later.

The SQL.REQUEST function differs from Excel's Query Wizard in that the SQL.REQUEST queries can be more complex. The Query Wizard performs only basic SELECTs with a simple WHERE clause. But SQL.REQUEST lets you devise queries that are limited in complexity only by your SQL skill level, then edit those queries directly in a cell. You can also use a cell reference within the query to point to a value or condition elsewhere on the spreadsheet. This functionality lets you change the query dynamically as the referenced cell value changes.

SQL.REQUEST doesn't currently support statements that modify data directly (i.e., UPDATE, CREATE, DELETE, and INSERT), but you can launch some stored procedures. Even with that limitation, you can set up a template formula that uses SQL.REQUEST from within Excel; for example, you can use it to eliminate some Data Transformation Services (DTS) packages and technical support overhead involved in performing ad hoc queries. Many other applications and possibilities exist for combining Excel's SQL.REQUEST function with the power of SQL Server.

To use SQL.REQUEST, you must have appropriate rights on the SQL Server machine, configure a DSN connection to the server through ODBC, install the Excel ODBC add-in (XLODBC.XLA), and enable Macros within the spreadsheet. For whatever reason, the only example in the Microsoft documentation connects to a dBase4 source. The required syntax for connecting to SQL Server is different from that for connecting to dBase4.

Following are examples that demonstrate the minimum required syntax to make the function work with a SQL Server data source. Note that the password in the examples is blank and that Excel 97 is case sensitive. The following formula defines the entire query within the SQL.REQUEST function:

=SQL.REQUEST("DSN=mytest;UID=sa;
   PWD=;Database=PUBS",,,"select * from
	authors where lname = 'Jackson'")

Figure 1 shows the results of this query. The next example references a spreadsheet cell to complete the query:

=SQL.REQUEST("DSN=mytest;UID=sa;
  PWD=;Database=PUBS",,,"select * from
  authors where lname =  
" & B2)

Figure 2 shows the results of using the dynamic cell reference formula. You can also reference a spreadsheet cell for the entire query:

=SQL.REQUEST("DSN=mytest;UID=sa;
   PWD=;Database=PUBS",,,"" & B2)

You can find additional documentation for this powerful query function in the Excel Help file under the topic SQL.REQUEST.

End of Article




You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

In order to reference a string type, you have to include quotes in the reference cell. e.g B2 = 'Jackson' The quotes can be placed in the sql.request function instead, e.g =SQLRequest("DSN=Pubs;UID=sa; PWD=;Database=Pubs","select au_lname, phone from authors where au_fname = '" & B2 & "'")

Quotes are not needed in referencing cell consisting a number.

Subroto

TRIED OUT THIS GREAT FUNCTION BUT WHAT I FOUND WAS THAT WHEN REFERENCING A CELL THAT CONTAINS A TEXT STRING E.G.=SQL.REQUEST("DSN=FMCSparta;Database=MACIntegratedDB",,, "Select dummy2 from fmc_rv_portfolio_extensions WHERE portfolio_code=" & A5, FALSE) THE TEXT STRING IN CELL a5 WOULD NEED TO BE QUOTED AND IN ADDITION HAVE A LEADING SPACE SO THE CELL CONTAINS " 'test2'" and not "'test2'". I'm running win 2k sp2 and office 2000 pro sr1. Any ideas?

ANDY MAC

How can I show the results as a matrix. I just get the first item of the results in the cell of the formula.

Thomas Luh

Could it be possible that the amount of data returned from an Oracle DB is limited when using SQL.REQUEST? Or is there any configuration parameter on the driver or so? When I run a query that returns a high amount of data, then I only get the first x-rows in excel filled with data from the query, the remaining rows hold #n/a. Thanks for any suggestions.

Wolfgang Nolze

I am having problems with SQL.REQUEST, and this article has addressed some of them. Not all, but some.

Anonymous User

Article Rating 4 out of 5

Cannot use a query over 250 characters. EG.=SQL.REQUEST("DSN=test;SERVER=test1;UID=test3;PWD=test4",,,"select sum(isnull(amounttowin,0))+ sum(isnull(amounttoplace,0)) from v_allbets b inner join v_allevents e on b.eventid = e.eventid inner join tblclients c on b.clientid = c.clientid where internet=0 and (settleddate >= '"&B1&"' and settleddate < '"&B2&"') and (valid = 1) and (bettype >0 and bettype <100) and (clienttype &16 =0)") Works Ok if I shorten the query

Anonymous User