Saving Results to File
You may also want to dump the output into a file rather than display the results on screen. Different database management systems us different syntax. To mention a few:
While using the command line (SQL*plus) In Oracle, all you need to do is type:
SELECT col1, col2, col3 FROM table;
This will save the results in the fame named filename.
You can achieve the same task using MySQL with the OUTFILE extension. The syntax is as it is in the following query:
SELECT col1, col2, col3 INTO OUTFILE 'folder/filename.ext'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM table;
You can see there is a lot going on here which is used for formatting the output file.
In SQL Server this can be done with the following query:
SELECT col1, col2, col3 FROM table QUERYOUT "c:\filename.txt" -c
The -c flag here is used to ensure the formating is kept.
All these queries listed here above otput a CSV file format so the filename does not matter as long as you keep that in mind. The reason why each of these use a different syntax is because standard SQL does not have a native way of sending the output to file and therefore has to be implemented by the vendors.