Solved

table variables in a Microsoft Query (Excel) and parameters

Posted on 2010-09-22
5
470 Views
Last Modified: 2012-05-10
Hello,

I have a MS SQL query like so

declare @qq table
( ... )

insert into @qq ( ... )
select ( ... )
from mytable
where myparameter1 = ?

I want to be able to execute this in MS Excel (2007 if ure wondering) as a Microsoft Query. Unfortunately, I cannot use any parameters in this query because it cannot be graphically represented. My actual query is way more complicated than that and has multiple table variables. Any suggestion or is this an impossibility?

Thanks in advance.
0
Comment
Question by:CodeWrangler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 33741626
Ummmm... interesting problem. The (memory) table will only exist for that session, so, kinda interesting what you are doing with it.

If there is something that needs to be updated in the Server, then normally link to the spreadsheet from the server and update via the linked server.
0
 

Author Comment

by:CodeWrangler
ID: 33742935
Well, I havent even got that far yet. Excel will execute the above code, so I know the tables are being written to and read from correctly, it's just that I cannot pass any parameters to it...
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 250 total points
ID: 33743097
Well, I have seen methods interactively when using DSN - but that is more for stored procedures...

What it looks like is a query being built more so than a parameter per se.

Normally resort to VBA code and create a little macro / routine. You can then pass your variable to the routine, or, have the routine read information from the excel page.

Have a look at : http://www.ozgrid.com/forum/showthread.php?t=41672&page=1 for a pretty good example with error checking et al... Also note it uses ADO...

0
 
LVL 13

Assisted Solution

by:MWGainesJR
MWGainesJR earned 250 total points
ID: 33743506
Would this not work?


     

Sub updateSQLTable() 
Dim conn As ADODB.Connection
Dim constr As String
Dim cmd As ADODB.Command
Dim sql As String
Dim tbl As String 
Set conn = New ADODB.Connection
constr = "Your Connection String"
tbl = InputBox("Enter Table Name.")
sql = "insert into " & tbl & " select from mytable where myparameter1 = ?" 
With conn
   .Open constr
   .Execute (sql)
End With
End Sub

Open in new window

0
 

Author Closing Comment

by:CodeWrangler
ID: 33764230
VBA was the way to do this. Used ADO for the connections. Thanks guys for the guidance.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question