Saving results with headers in Sql Server Management Studio

后端 未结 10 2239
长发绾君心
长发绾君心 2020-12-13 01:35

I am using SQL Server Management Studio.

I wish to save the results of a query to an excel file.

I choose \"save as\" and then save to CSV file which I ca

相关标签:
10条回答
  • 2020-12-13 01:39

    Tools > Options > Query Results > SQL Server > Results to Text (or Grid if you want) > Include columns headers in the result set

    You might need to close and reopen SSMS after changing this option.

    On the SQL Editor Toolbar you can select save to file without having to restart SSMS

    0 讨论(0)
  • 2020-12-13 01:41

    Try the Export Wizard. In this example I select a whole table, but you can just as easily specify a query:

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    (you can also specify a query here)

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    0 讨论(0)
  • 2020-12-13 01:42

    Another possibility is to use the clipboard to copy and paste the results directly into Excel. Just be careful with General type Excel columns, as they can sometimes have unpredictable results, depending on your data. CTL-A anywhere in the result grid, and then right-click:

    enter image description here

    If you have trouble with Excel's General format doing undesired conversions, select the blank columns in Excel before you paste and change the format to "text".

    0 讨论(0)
  • 2020-12-13 01:42

    At least in SQL Server 2012, you can right click in the query window and select Query Options. From there you can select Include Headers for grid and/or text and have the Save As work the way you want it without restarting SSMS.

    You'll still need to change it in Tools->Options in the menu bar to have new query windows use those settings by default.

    0 讨论(0)
  • 2020-12-13 01:46

    The settings which has been advised to change in @Diego's accepted answer might be good if you want to set this option permanently for all future query sessions that you open within SQL Server Management Studio(SSMS). This is usually not the case. Also, changing this setting requires restarting SQL Server Management Studio (SSMS) application. This is again a 'not-so-nice' experience if you've many unsaved open query session windows and you are in the middle of some debugging.

    SQL Server gives a much slick option of changing it on per session basis which is very quick, handy and convenient. I'm detailing the steps below using query options window:

    1. Right click in query editor window > Click Query Options... at the bottom of the context menu as shown below:

    1. Select Results > Grid in the left navigation pane. Check the Include column headers when copying or saving the results check box in right pane as shown below:

    That's it. Your current session will honour your settings with immediate effect without restarting SSMS. Also, this setting won't be propagated to any future session. Effectively changing this setting on a per session basis is much less noisy.

    0 讨论(0)
  • 2020-12-13 01:50

    The same problem exists in Visual Studio, here's how to fix it there:

    Go to:

    Tools > Options > SQL Server Tools > Transact-SQL Editor > Query Results > Results To Grid
    

    Now click the check box to true: "Include column headers when copying or saving the results"

    0 讨论(0)
提交回复
热议问题