is there an easy way to get a list of all columns of a SQL query?
They are listed in the header of the results window of SSMS but I can't copy them.
Thanks in advance.
EDIT:
sorry, I found it myself after a little googling:
http://vidmar.net/weblog/archive/2008/06/05/save-sql-query-results-with-column-names-in-msssms.aspx
Go to Query -> Query Options and check the following box
If you change to Results To Text, then you can copy them.
To save actually executing the full query just to get the column names, you could also do:
SET FMTONLY ON;
SELECT * FROM SomeTable;
This will just return the metadata about the columns the query returns. Saves waiting around if the query is meaty.
Change the result window to 'Results to text' instead of 'Results to grid'.
This can be done by clicking of the on of the Icons above the query window. It looks somewhat like a notepad icon.
Related
I saw a demo using something like this
SHOW WAREHOUSES;
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "auto_suspend" IS NULL;
This allows to use the resultset from SHOW ... as part of a normal SQL statement.
VERY useful!
However, when I tried this in DBeaver, my LAST_QUERY_ID() always returned the result of something like "SELECT CURRENT_DATABASE(), CURRENT_SCHEMA()".
Which I discovered is precisely because DBeaver does this before any request.
Is it possible to stop this?
PS:
Of course, I can get the initial code to work by looking for an older query, like
SHOW WAREHOUSES;
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID(-2))) -- added -2
WHERE "auto_suspend" IS NULL;
But i would prefer not to have to do this...
A work around I like is to simply write the SHOW to a table. It makes it easier to use the result multiple times.
Also, if you use a transaction you don't need the -2 or Alt-X for Execute script. So
BEGIN
SHOW WAREHOUSES;
CREATE OR REPLACE TEMPORARY TABLE SHOW_WH AS
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
END;
Then
SELECT * FROM SHOW_WH WHERE "auto_suspend" IS NULL;
I just discovered that Snowflake themselves have released an extension for Visual Studio code and that does not suffer from this detail...
It does not solve the DBeaver thing... but...
And it does pretty good colour coding, intellisense, etc
How do you view ALL text from an NTEXT or NVARCHAR(max) in SQL Server Management Studio? By default, it only seems to return the first few hundred characters (255?) but sometimes I just want a quick way of viewing the whole field, without having to write a program to do it. Even SSMS 2012 still has this problem :(
I was able to get the full text (99,208 chars) out of a NVARCHAR(MAX) column by selecting (Results To Grid) just that column and then right-clicking on it and then saving the result as a CSV file. To view the result open the CSV file with a text editor (NOT Excel). Funny enough, when I tried to run the same query, but having Results to File enabled, the output was truncated using the Results to Text limit.
The work-around that #MartinSmith described as a comment to the (currently) accepted answer didn't work for me (got an error when trying to view the full XML result complaining about "The '[' character, hexadecimal value 0x5B, cannot be included in a name").
Quick trick-
SELECT CAST('<A><![CDATA[' + CAST(LogInfo as nvarchar(max)) + ']]></A>' AS xml)
FROM Logs
WHERE IDLog = 904862629
In newer versions of SSMS it can be configured in the (Query/Query Options/Results/Grid/Maximum Characters Retrieved) menu:
Old versions of SSMS
Options (Query Results/SQL Server/Results to Grid Page)
To change the options for the current queries, click Query Options on the Query menu, or right-click in the SQL Server Query window and select Query Options.
...
Maximum Characters Retrieved
Enter a number from 1 through 65535 to specify the maximum number of characters that will be displayed in each cell.
Maximum is, as you see, 64k. The default is much smaller.
BTW Results to Text has even more drastic limitation:
Maximum number of characters displayed in each column
This value defaults to 256. Increase this value to display larger result sets without truncation. The maximum value is 8,192.
I have written an add-in for SSMS and this problem is fixed there. You can use one of 2 ways:
you can use "Copy current cell 1:1" to copy original cell data to clipboard:
http://www.ssmsboost.com/Features/ssms-add-in-copy-results-grid-cell-contents-line-with-breaks
Or, alternatively, you can open cell contents in external text editor (notepad++ or notepad) using "Cell visualizers" feature: http://www.ssmsboost.com/Features/ssms-add-in-results-grid-visualizers
(feature allows to open contents of field in any external application, so if you know that it is text - you use text editor to open it. If contents is binary data with picture - you select view as picture. Sample below shows opening a picture):
Return data as XML
SELECT CONVERT(XML, [Data]) AS [Value]
FROM [dbo].[FormData]
WHERE [UID] LIKE '{my-uid}'
Make sure you set a reasonable limit in the SSMS options window, depending on the result you're expecting.
This will work if the text you're returning doesn't contain unencoded characters like & instead of & that will cause the XML conversion to fail.
Returning data using PowerShell
For this you will need the PowerShell SQL Server module installed on the machine on which you'll be running the command.
If you're all set up, configure and run the following script:
Invoke-Sqlcmd -Query "SELECT [Data] FROM [dbo].[FormData] WHERE [UID] LIKE '{my-uid}'" -ServerInstance "database-server-name" -Database "database-name" -Username "user" -Password "password" -MaxCharLength 10000000 | Out-File -filePath "C:\db_data.txt"
Make sure you set the -MaxCharLength parameter to a value that suits your needs.
I was successful with this method today. It's similar to the other answers in that it also converts the contents to XML, just using a different method. As I didn't see FOR XML PATH mentioned amongst the answers, I thought I'd add it for completeness:
SELECT [COL_NVARCHAR_MAX]
FROM [SOME_TABLE]
FOR XML PATH(''), ROOT('ROOT')
This will deliver a valid XML containing the contents of all rows, nested in an outer <ROOT></ROOT> element. The contents of the individual rows will each be contained within an element that, for this example, is called <COL_NVARCHAR_MAX>. The name of that can be changed using an alias via AS.
Special characters like &, < or > or similar will be converted to their respective entities. So you may have to convert <, > and & back to their original character, depending on what you need to do with the result.
EDIT
I just realized that CDATA can be specified using FOR XML too. I find it a bit cumbersome though. This would do it:
SELECT 1 as tag, 0 as parent, [COL_NVARCHAR_MAX] as [COL_NVARCHAR_MAX!1!!CDATA]
FROM [SOME_TABLE]
FOR XML EXPLICIT, ROOT('ROOT')
PowerShell Alternative
This is an old post and I read through the answers. Still, I found it a bit too painful to output multi-line large text fields unaltered from SSMS. I ended up writing a small C# program for my needs, but got to thinking it could probably be done using the command line. Turns out, it is fairly easy to do so with PowerShell.
Start by installing the SqlServer module from an administrative PowerShell.
Install-Module -Name SqlServer
Use Invoke-Sqlcmd to run your query:
$Rows = Invoke-Sqlcmd -Query "select BigColumn from SomeTable where Id = 123" `
-MaxCharLength 2147483647 -ConnectionString $ConnectionString
This will return an array of rows that you can output to the console as follows:
$Rows[0].BigColumn
Or output to a file as follows:
$Rows[0].BigColumn | Out-File -FilePath .\output.txt -Encoding UTF8
The result is a beautiful un-truncated text written to a file for viewing/editing. I am sure there is a similar command to save back the text to SQL Server, although that seems like a different question.
EDIT: It turns out that there was an answer by #dvlsc that described this approach as a secondary solution. I think because it was listed as a secondary answer, is the reason I missed it in the first place. I am going to leave my answer which focuses on the PowerShell approach, but wanted to at least give credit where it was due.
If you only have to view it, I've used this:
print cast(dbo.f_functiondeliveringbigformattedtext(seed) as text)
The end result is that I get line feeds and all the content in the messages window of SMSS.
Of course, it only allows for a single cell - if you want to do a single cell from a number of rows, you could do this:
declare #T varchar(max)=''
select #T=#T
+ isnull(dbo.f_functiondeliveringbigformattedtext(x.a),'NOTHINGFOUND!')
+ replicate(char(13),4)
from x -- table containing multiple rows and a value in column a
print #T
I use this to validate JSON strings generated by SQL code. Too hard to read otherwise!
Use visual studio code with sql server plugin. Super usefull for jsons
Alternative 1: Right Click to copy cell and Paste into Text Editor (hopefully with utf-8 support)
Alternative 2: Right click and export to CSV File
Alternative 3: Use SUBSTRING function to visualize parts of the column. Example:
SELECT SUBSTRING(fileXml,2200,200) FROM mytable WHERE id=123456
The easiest way to quickly view large varchar/text column:
declare #t varchar(max)
select #t = long_column from table
print #t
The question is based on SQL Server not about ssrs NEW Line
i had Already go through the stackoverflow below links related to this topic
1)New line in Sql Query
2)New line in sql server
3)New line in Sql Query
But i didn't get any answer for my situation
My problem is that i need a newline character in select query results
Example : i have a string like below
Today is Friday Yesterday is Thursday
and when I select this varchar value from my table I need it to to appear like this in my SSRS report:
Today is Friday
Yesterday is Thursday
I have tried many ways like
select 'Today is Friday' +char(13)+'Yesterday is Thursday'
but it gives me a result Today is Friday Yesterday is Thursday
but when i use print insted of select then i got the results what i want
But i cannot use print in my scenario because i need this result in aother query and that query i have used for ssrs reporting purpose
I know i can split this query and show it in two rows but in my case i can't do something like that too because the results i have used in many select querys.
i don't know it is possible in Sqlserver, if it is possible then
please help me to solve my problem
Edit : please help me if it is possible in Grid Mode rather than text mode
Since when you use print you do see the line break, it seems to me like the problem is that you execute this query in SSMS, while the result is displayed as a grid.
Try changing the display result to text and you will see the line break just like you see it when you use print instead of select.
Btw, I think you better use char(13)+char(10) as xQbert suggested in the comments.
If you are doing it in Managment Studio this works fine:
select 'Today is Friday' +char(13)+'Yesterday is Thursday'
If result to grid is selected in Managment Studio then it is printed in one line becouse this works in that way. If you copy and paste it from grid to notepad you will see it works. Try checking result to Text. It works to.
I have no experience with SSRS, but maybe this link is helpful for You:
SSRS how to add in New line
This suggest to use expression, like this:
=Replace(Field!Names.Value, ",", VbCrLf)
If you need to display the new line in an SSRS report, try using <br /> instead of CHAR(10) - and make sure to set the control that contains this text to allow HTML.
This SSMS newbie is trying to print the results of a query instead of having it directed to the grid
I followed the following steps:
Management Studio -->> Tools -->>Options -->>
Query Results -->>General -->> Results to text and c:\works as Default location for saving query
Query Results -->>SQL Server -->> Results to text -->>Include column headers when copying or saving...
yet, when query is executed, I don't see the results at all
could someone please shed the light my way as to how can I get the query results saved to a file that I can print later on?
If you send the results direct to file, you can't see them in the Results pane in SSMS.
You have three choices:
Results to Text (plain text, in the Results pane)
Results to Grid (grid view, with resizeable columns & rows similar to Excel)
Results to File (writes direct to file, results not displayed)
You can choose between these options from the Query -> Results menu, buttons on the Standard toolbar, or keyboard shortcuts (CTRL-T,CTRL-D, CTRL-SHIFT-F, in the order above). Select your output "mode", then execute the query.
With the first two options, you can right-click the results and save to a file from there. Or copy/paste elsewhere.
With Results to File, it will output the results in a file in your default location (c:\works\ in your case) but it should prompt you with the standard Windows File Save dialog.
You need to select "Results to File" not "Results to Text". When you then go back and run your query, you will not see any query results, just a prompt for the file name you want to save the results as.
You don't want Results to Text. You want Results to File. Then, when you execute the query, you'll be prompted for the file name to save under.
I have a select query producing a big output and I want to execute it in sqldeveloper, and get all the results into a file.
Sql-developer does not allow a result bigger than 5000 lines, and I have 100 000 lines to fetch...
I know i could use SQL+, but let's assume I want to do this in sqldeveloper.
Instead of using Run Script (F5), use Run Statement (Ctrl+Enter). Run Statement fetches 50 records at a time and displays them as you scroll through the results...but you can save the entire output to a file by right-clicking over the results and selecting Export Data -> csv/html/etc.
I'm a newbie SQLDeveloper user, so if there is a better way please let me know.
This question is really old, but posting this so it might help someone with a similar issue.
You can store your query in a query.sql file and and run it as a script. Here is a sample query.sql:
spool "C:\path\query_result.txt";
select * from my_table;
spool off;
In oracle sql developer you can just run this script like this and you should be able to get the result in your query_result.txt file.
#"C:\Path\to\script.sql"
Yes you can increase the size of the Worksheet by change the setting Tool-->Preferences - >Database -> Worksheet -> Max rows to print in a script(depends on you).
Mike G answer will work if you only want the output of a single statement.
However, if you want the output of a whole sql script with several statements, SQL*Plus reports, and some other output formats, you can use the spool command the same way as it is used in SQL*Plus.