Excel's interface for SQL Server queries will not let you have a custom parameters. A way around this is to create a generic Microsoft Query, then add parameters, then paste your parametorized query in the connection's properties. Here are the detailed steps for Excel 2010:
- Open Excel
- Goto Data tab
- From the From Other Sources button choose From Microsoft Query
- The "Choose Data Source" window will appear. Choose a datasource and click OK.
- The Query Qizard
- Choose Column: window will appear. The goal is to create a generic query. I recommend choosing one column from a small table.
- Filter Data: Just click Next
- Sort Order: Just click Next
- Finish: Just click Finish.
The "Import Data" window will appear:
- Click the Properties... button.
- Choose the Definition tab
- In the "Command text:" section add a WHERE clause that includes Excel parameters. It's important to add all the parameters that you want now. For example, if I want two parameters I could add this:
WHERE 1 = ? and 2 = ?
- Click OK to get back to the "Import Data" window
Choose
PivotTable Report
Click
OK
You will be prompted to enter the parameters value for each parameter.
Once you have enter the parameters you will be at your pivot table
Go batck to the
Data tab and click the connections
Properties button
- Click the Definition tab
- In the "Command text:" section, Paste in the real SQL Query that you want with the same number of parameters that you defined earlier.
- Click the Parameters... button
- enter the Prompt values for each parameter
- Click OK
Click
OK to close the properties window
Congratulations, you now have parameters.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…