Need help printing query results to a file/text - sql-server

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.

Related

how to display incomplete display the “DDL” of tengine’ table?

I am using tdengine. I met a problem that when I check the DDL of a table.Using the command below:
Show create table question;
While my query result displayed by taos shell is incomplete. Just like this picture.
Does anyone know how to fix this?
Do you mean complete the query result while displayed by taos shell. Due to the limited display width of taos shell in the terminal, it is possible that a relatively long column is not displayed completely.
The workaround can be by modifying the setting option "maxBinaryDisplayWidth" in the taos.cfg file, or directly entering the command set max_binary_display_width 100. Or, use the \G parameter at the end of the command to adjust how the results are displayed.

Export query result in Pervasive to txt / csv file

I'm using Pervasive 10 with PCC (Pervasive Control Center) and I need to export a lot of results (over 100 000) to a TXT file.I know it's possible "Execute in Text" but this feature does not work for me because after exporting about 20 000 records the program stops. I have also changed the settings in PCC (Windows->Preferences->Text Output-> Maximun number of rows to display = 500,000).
Anyone know a way to export my query result to a txt file?
You should be able to use the Export Data function. Right click on the table name in the PCC and select Export Data. From there, you can either execute the standard "select * from " or make a more complex query to pull only the data you need. You can set the delimiter to Comma, Tab, or Colon.
Nice answer mirtheil, was wondering about this my self as well.
To add something to the answer.
It does not matter which table you right click and choose "Export Data" on, Because your query will override the default table query.

Microsoft word Database quick part - How to use a mergefield as a filter for the database query

I am using mail merge to input data from an excel sheet. Everthing works great and I can access my variables using «MyMergefield»
Now I need for each letter generated to look into another excel file and do a query that will take the «MyMergefield» as a query filter SELECT FROM x WHERE field1 = «MyMergefield»
The way I am proceeding is "inserting a quick part" => "Field" in my word document.
In the quickpart dialog, I choose "DataBase", then I choose my excel file.
once the data source is chosen, There an option to change the request parameters, I click on it and I get the filter configuration popup where I can choose the field (from the excel sheet), the operator ("equals" in this case). Then there's the compare with field. In my case its not as simple as comparing to as string. Its comparing to a mail merge field.
I tried the following syntax:
«Myfield»
MERGEFIELD Myfield
MERGEFIELD "Myfield"
{MergeField Myfield}
{ MERGEFIELD Myfield}
None worked, it complained that it did not find any match so it did not insert the database (Of course it will not find any match to the syntax if I don't run mail merge)
I did look directly in the openxml file of an existing example (because I can't edit existing quickpart - Correct me if Im wrong) and the database query looked like:
FROM `Candidates$` WHERE ((`column` = '</w:instrText>
...
<w:instrText xml:space="preserve"> MERGEFIELD Myfield</w:instrText>
</w:r>
Any ideas? Thank you!

How to list all columns of a given sql query

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.

Script output to file when using SQL-Developer

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.

Resources