简体   繁体   中英

Using Parameters in SQL query with sub-query

I have a fairly complicated SQL query with a nested subquery. When I try to use parameters in Microsoft Query is say I can use parameters in queries that cant be represented graphically. So I need another option. I think you can place your SQL query in a cell as a string then have a Macro run it. Any ideas how I could do this?

Thanks

-Jesse

Here's what I do to work around the limitations of Microsoft Query in Excel 2007:

  1. A produce a dummy query ( SELECT NULL AS Test , for example) in Microsoft Query and insert it into the worksheet.
  2. Right-click on the table that MS Query just inserted and click Table->Edit External Data Properties.
  3. Click on the Connection Properties button, then click the Definition tab.
  4. In the Command Text section, write out or paste in the query that you want, using the usual ' ? ' convention for parameters, then click OK.
  5. Click OK to exit the External Data Properties window.
  6. Right click on the table again, and select Table->Parameters to bind the parameters in the usual way.

The idea is the bypass the GUI that MS Query provides, which has some arbitrary limitations that the underlying engine does not.

This works for many complex queries, but not all. When I encounter a query that MS Query refuses to digest at all, I either refactor the query (when feasible) or create a VIEW on the SQL server and query against that.

Unfortunately the ? doesn't work for most of my queries and a lot of them are not necessarily suited to being turned into views.

The main alternative I use is getting a macro to return the code

Dim Con As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim server, Database As String
Dim Data as Worksheet

Set data = ThisWorkBook.Worksheets("data")

'rename field here and elsewhere to your variable eg SD or StartDate
Dim field as string

server = "servername"
Database = "database"

'set connection string

If Con.State <> 1 Then
 Con.ConnectionString = "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & Database & ";Integrated Security=SSPI;"


'this is just setting the connection time out to infinite
setcono:
Con.ConnectionTimeout = 0
Con.CommandTimeout = 0


'this is making sure it set the connection time out to infinite
If Con.ConnectionTimeout > 0 Then GoTo setcono
If Con.CommandTimeout > 0 Then GoTo setcono

Con.Open

Set oRS = New ADODB.Recordset
oRS.ActiveConnection = Con

field = Range("A2").value

oRS.Source = "YOUR SQL QUERY "
oRS.Source = oRS.Source & " WHERE field = '"  & field & "'"

oRS.Open
data.Range("A2").CopyFromRecordset oRS
End If
oRS.Close
Con.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not Con Is Nothing Then Set oCon = Nothing

I would love Microsoft to fix the bug where it returns errors for the more complex queries as I find it frustrating creating macros just for the sake of returning a simple dataset

Another way to solve this is to use stored procedures

CREATE PROCEDURE [dbo].[yourprocedure] @DATEFROM DATETIME, @DATETO DATETIME
AS


SELECT Query 
where date >= @datefrom
and date <= @dateto 

then on the table properties click Connection Properties button, then click the Definition tab. In the Command Text section:

EXEC yourprocedure @DATEFROM = ?, @DATETO = ?

and direct the ? to the cells you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM