Using MySQL in Powershell, how do I pipe the results of my script into a csv file? - mysql

In PowerShell, how do I execute my mysql script so that the results are piped into a csv file? The results of this script is just a small set of columns that I would like copied into a csv file.
I can have it go directly to the shell by doing:
mysql> source myscript.sql
And I have tried various little things like:
mysql> source myscript.sql > mysql.out
mysql> source myscript.sql > mysql.csv
in infinite variation, and I just get errors. My db connections is alright because I can do basic table queries from the command line etc... I haven't been able to find a solution on the web so far either...
Any help would be really appreciated!

You seem to not be running powershell, but the mysql command line tool (perhaps you started it in a powershell console though.)
Note also that the mysql command line tool cannot export directly to csv.
However, to redirect the output to a file just run
mysql mydb < myscript.sql >mysql.out
or e.g.
echo select * from mytable | mysql mydb >mysql.out
(and whatever arguments to mysql you need, like username, hostname)

Are you looking for SELECT INTO OUTFILE ? dev.mysql.com/doc/refman/5.1/en/select.html – Pekka 19 hours ago
Yep. Select into outfile worked! But to make sure you get column names you also need to do something like:
select *
from
(
select
a,
b,
c
)
Union ALL
(Select *
from actual)

Related

MySQL: Is there a way to return a list of functions / procedures filtered by a string found within the code?

Basically I have the unenviable position of updating our entire system to stop using a certain table and instead use another one. I've already done this for all of our code, now I need to do it for all of our functions and procedures.
I know that I can get a list of the functions / procedures in a database as such:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
I also know that I can look at the code for an individual function / procedure as such:
SHOW CREATE FUNCTION function_name
SHOW CREATE PROCEDURE procedure_name
However, I don't want to have to look through each function and procedure one by one, as we have over 200 of them.
I'm wondering if there is anything like...
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE code_column_name LIKE '%search_string%'
There doesn't seem to be any column in INFORMATION_SCHEMA.ROUTINES that contains the code, but... is there a way to do this on a different table perhaps?
I would use UNIX grep for that.
If you output the results of show create function/procedure to a flat file on disk then run grep on it. Or turn it directly thru grep.
Here is one way to do it:
echo 'show create function foo' | mysql -h*host* -u*user* -p*pass* *schema* | grep *obsolete-tablename*
or dump the entire database to disk and then grep it.
mysqldump -h*host* -u*user* -p*pass* *schema --routines > mydump.sql
grep *obsolete-tablename mydump.sql
Try:
SELECT * FROM mysql.proc WHERE body LIKE '%search_string%'
You asked about MySQL but I will just mention that I use this for stored procedures in MS SQL Server:
SELECT object_name(id)
FROM syscomments
WHERE text LIKE '%wibble%'
The INFORMATION_SCHEMA.ROUTINES table (which you mention) also contains similar stuff, but the text is cut off after 4000 chars, so you find less matches; not very helpful.

How to automate Hive query

I have created a script of hive queries mainly for features creation and scoring for cross sell project. Most of the queries are simple queries that do the data cleaning , transformation etc. I want to automate this process so that I can start with hive table as input and can output the final result into Hbase file . My question are :
What is the best way to do it ?
Can I simply create filename.sql or filename.hql and run it from shell using hive -f filename.sql
Is there something in hive like PL for SQL?
You can do it in multiple ways.
Like you can also use Hive CLI and its very ease to do such jobs.
You can write shell script in Linux or .bat in Windows.
In script you can simply go like below entries.
$HIVE_HOME/bin/hive -e 'select a.col from tab1 a';
or if you have file :
$HIVE_HOME/bin/hive -f /home/my/hive-script.sql
Make sure you have set $HIVE_HOME in your env.
Once you have tested and working fine you can put in cronjob for scheduling.
It is important to note that if you are using either of the technique, each of your queries must be separated by a semi colon i.e.
hive -e 'select * from tableA limit 10;select * from tableB limit 10'

Running mySQL queries from a script

For my database class the teacher assigned us to use Oracle SQL to design an application. Because I have more experience with mySQL he said I could use it instead.
I want to make my assignment look as simliar to his example as possible. What his example consists of of is one file run.sql that looks like this:
#start //this runs start.sql which creates the tables
DESC table_name; //do this for all tables
#insert //this runs insert.sql that creates dummy data
SELECT * FROM table_name; //do this for all tables
#query //this runs query.sql that runs our sample queries
#drop //this kills all the data
Is there a way to do something simliar?
Namely a way to write a query that calls external queries and outputs all data to an output.txt file?
Use 'source' to input the *.sql files
use 'create procedure' to generate the 'drop' function
use "into outfile '/file/path';" on your select to write out.
double redirect to append: "into outfile '>>/file/path';"
The source command for the mysql command-line client could do the job here:
source start.sql;
DESC table_name;
You can get more commands with help.

execute MySQL query from the terminal without printing results

I am performing some MySQL queries that have very large result sets. I would like to see how long they take, but I don't want all the output to be printed on my terminal because it takes up a lot of space and time. I can do this by writing a script that performs and times the query, but I was wondering if there was a way to do this directly through MySQL on the terminal. Thanks.
Change the pager in mysql like indicated here: http://www.mysqlperformanceblog.com/2013/01/21/fun-with-the-mysql-pager-command/
mysql> pager cat > /dev/null will discard the output, and mysql> pager will put it back.
Wrap your query in set #foo = (select count(*) from ( ..... ) foo)
Just run mysql console utility, then enter source file_name (where file_name contains sql commands)

redirect sql query results to a .log file

I have a c-shell scrip that connects to a mysql database database through and invokes a sql script which in turn invokes another sql script to run a query and return a report
#!/bin/csh
set MYSQL=${MYSQL_HOME}/mysql
set REPORT=${CLEADM_HOME}/Scripts/DataValidation/EOreport.sql
${MYSQL} ${CLEDBUSER} <${REPORT}
Then within the eoreport.sql I invoke another script like so
Source IERSs.sql
and finally in the IERSs.sql script i need to log the results to a log file but it is not working
SELECT *
FROM TB_EARTHORIENTATIONPARAMETER_UI
INTO OUTFILE '/vobs/tools/Scripts /results.log'
This is not working. All i see is the results of the query printed to the xterm(im using tcsh on solaris and the database is mysql client). Am i missing something?
i have even done research about the tee command that is supposed to pipe in you input and output i to the file that you specify as follows
tee /vobs/tools/Scripts/DataValidation/results.txt
SELECT * FROM TB_EARTHORIENTATIONPARAMETER_UI;
but this still outputs results to the screen and leaves my result.txt file empty. What am i missing ?
SELECT *
FROM TB_EARTHORIENTATIONPARAMETER_UI
INTO OUTFILE '/vobs/tools/Scripts /results.log'
you have a extra space between scripts and /, try this:
SELECT *
FROM TB_EARTHORIENTATIONPARAMETER_UI
INTO OUTFILE '/vobs/tools/Scripts/results.log'
Also you said :
"leaves my result.txt file empty." and you are trying to write a result.log file