Basically I want to know what SSMS is doing behind the scenes when you right click on a view -> script view as -> create to -> new query editor window/ file/ clipboard.
You can use SQL Server Profiler to intercept the SQL: create a new trace; use event selection to remove unwanted data; start and stop to inspect the SQL, i.e.:
Click New Trace
Use the template: Tuning
Click Run
—on the events selection: uncheck rpc: completed, sp:StmtCompleted
filter for your query:
click column filter
DatabaseName like "abcDatabase"
see (https://www.c-sharpcorner.com/UploadFile/raj1979/how-to-use-sql-server-profiler-and-tuning-advisor/)
In my SQL Server 2014 Management Studio I am facing indentation issue. Like if I create a stored-procedure with proper indentation like tabs, spaces etc.. and if I again check the sp then I see no indentation which I gave while creating the procedures.
Created with proper spacing etc...
What I get after rechecking-
When you execute sp_helptext in SSMS:
exec sp_helptext 'YourSpName'
try setting output type to "Results to Text" instead of "Results to Grid".
You can use the shortcut CTRL+T for this or you can enable it from menu Query/Results To/Results To Grid.
This should preserve indentation.
I am interested in saving the result of a SQL query, like a SELECT statement, as a pipe (|) delimited text file in SSMS.
I can do that using the Export wizard.
However, it seems there's a simpler method by setting a "Custom delimiter" in Tools>Options under Query Results>SQL Server>Results to Text as shown here:
Then, if I specify "Results to Text" or "Results to File", I am supposed to get the pipe delimited result.
This is also outlined here:Obtaining Pipe Delimited Results from SQL Server using SSMS
But, doing this I still get the usual output with either Results to Text or File.
I don't know what I'm missing or doing wrong.
Thanks for your help.
Thanks to #AlexYu, the answer is simply applying the "turning it off and on" trick. Closing and re-opening SSMS is required before the change in this setting takes effect.
Incidentally, disconnecting and re-connecting to the SQL server doesn't work in this case. I have done some rudimentary tests and it seems this also applies to other options here, for example under Query Results>SQL Server>Results to Grid.
I was using Microsoft SQL Server Management Studio v17.9.1, build number 14.0.17289.0.
I think "Query Results" is able to delimit the results by a custom character, only if all the datatype is varchar. I did what you wrote in the description and additionally I casted all the columns to varchar.
SELECT TOP (1000) CAST([COL_A] as varchar) as 'COL_A'
,CAST([COL_B] as varchar) as 'COL_B'
,CAST([COL_C] as varchar) as 'COL_C'
FROM TABLE_NAME;
I use Datagrip 2016.3. When I try to alter a value in a table, I can't commit that to the database (a Oracle SQL-DB).
What did I do:
- Setting a cell to /some new value
- Pressing the checkmark icon ('Commit')
- AND/OR pressing CTRL+RETURN
- Closing the tab
- Reopen the tab
- The /new value is gone, the old one still is there.
I tried this with different tables on different Schemes (and different databases). The column is nullable, the entered values are legit. I can modify the values with Oracle SQL Developer.
EDIT(20170116):
For most tests after I encountered the problem I tried to change the COUNTRY table of the standard HR schema of a local oracle installation. The same table could be altered with SQL Developer.
To commit a change to the database the following steps are needed:
Enter a row or change a value (the row is marked green)
Submit the change to database (the arrow icon with "DB")
Commit that change to the database (auto-commit or right-click for the context menu and select commit (checkmark))
There are two ways you can auto-commit change in the most recent versions of DataGrip in addition to the accepted answer. I'm on v2020.2 and these are currently more permanent options:
Change the Tx: setting on the query window from Manual to Auto.
Note: this setting will not persist after you restart DataGrip.
Change it permanently for the database / data source:
Press the Data Source Properties button (shortcut key Ctrl-Alt-Shift-S):
Options Tab -> Connection Section -> Transaction Control -> Auto/Manual
#2 is a glaring omission to the DataGrip official help document which I've posted feedback for. So hopefully they'll add it here soon: https://www.jetbrains.com/help/datagrip/submitting-and-reverting-changes.html
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 can open in excel. All good except I am missing column headers, any ideas how I get them exported?
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
Try the Export Wizard. In this example I select a whole table, but you can just as easily specify a query:
(you can also specify a query here)
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:
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".
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.
The settings which has been advised to change in #Diego's accepted answer might be good if you want to set this option permanently. Permanently means it'll apply to 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). This is again a not-so-nice experience in case there are many unsaved query session windows open and you are in the middle of debugging something.
SQL Server gives a much slicker option of changing this setting on per session basis which is very quick and convenient. I'm detailing the steps below using query options window:
Right click in query editor window. Now, click Query Options... in the context menu as shown below:
Select Grid under Results node in the left navigation pane. Now, check the Include column headers when copying or saving the results check box as shown below:
That's it. Now, your current query session will start honouring your settings with immediate effect. You don't need restarting SSMS. Also, this setting won't be propagated to any future session either. Effectively, changing this setting on a per session basis is much less noisy.
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"
Select your results by clicking in the top left corner, right click and select "Copy with Headers".
Paste in excel.
Done!
Got here when looking for a way to make SSMS properly escape CSV separators when exporting results.
Guess what? - this is actually an option, and it is unchecked by default. So by default, you get broken CSV files (and may not even realize it, esp. if your export is large and your data doesn't have commas normally) - and you have to go in and click a checkbox so that your CSVs export correctly!
To me, this seems like a monumentally stupid design choice and an apt metaphor for Microsoft's approach to software in general ("broken by default, requires meaningless ritualistic actions to make trivial functionality work").
But I will gladly donate $100 to a charity of respondent's choice if someone can give me one valid real-life reason for this option to exist (i.e., an actual scenario where it was useful).
In SQL Server 2014 Management Studio the setting is at:
Tools > Options > Query Results > SQL Server > Results to Text > Include column headers in the result set.
I also face the same issue. When I used right click in the query window and select Query Options. But header rows does not show up in output CSV file.
Then I logoff the server, login again and run the script. Then it worked.