将多SELECT语句的T-SQL查询结果导入Excel的技术问询
Import Multiple T-SQL Result Sets into Excel (Matching SSMS Output)
Got it, let's walk through how to get those separate T-SQL result sets (each with different columns) into Excel exactly as they appear in SQL Server Management Studio. Since you're already starting with the Data > From Other Sources > From SQL Server workflow, here's how to tweak it to capture all your result sets intact:
Step 1: Complete the initial SQL Server connection setup
- Navigate to
Data > From Other Sources > From SQL Serverin Excel. - Enter your server name, select your authentication method (Windows or SQL Server credentials), and pick the database where your query lives. Click Next.
Step 2: Paste your full multi-SELECT query
- On the "Select Tables and Views" screen, skip picking pre-existing objects and look for the "Write a query" option (usually a button at the bottom of the window).
- Paste your entire T-SQL query (all those separate
SELECTstatements) into the query editor box. Click Next to run it.
Step 3: Load all result sets (this is the critical part!)
- Excel will automatically detect that your query returns multiple result sets, and you'll see a list of them labeled
Table0,Table1, etc. (each corresponds to one of yourSELECTstatements in order). - Instead of clicking the default "Load" button (which only imports the first result set), click "Load To..." to customize the import:
- Select all result sets in the list (hold
Ctrlto pick individual ones, orShiftto select a range). - Choose your preferred destination:
- New worksheet: Excel will create a separate tab for each result set, preserving their original order and column structure.
- Existing worksheet: Specify a starting cell for each set if you want them all in one tab (but this can get messy with different column counts—separate worksheets are usually cleaner).
- Select all result sets in the list (hold
- Click OK to start the import.
Quick Tip
If you ever accidentally click "Load" and only get the first result set, don't worry—just go back to the Queries & Connections pane (under the Data tab), find your query, right-click it, and select Load To... to pick the remaining result sets.
内容的提问来源于stack exchange,提问作者matandked
相关产品推荐
相关产品推荐

