How to store multiline sql query in a shell variable? - mysql

How can i hold multiline sql query in a shell variable?
SQL='Lets get CREATE TRIGGER STATEMENT'
How to hold it?

This worked well for me
extract_sql="SELECT *
FROM TABLE"
To run SQLs with command line tools like wxsubmit or sqlplus
commandlinesqltool -options << EOF > /dev/null 2>&1
SET HEADER OFF
UPDATE TABLE
SET A=1;
EOF

Declare #sql nvarchar(max);
SET #sql='SELECT *'+char(13) +'FROM table'
Print(#sql);
result: SELECT *
FROM table
In above query char(13) will hepls to write in next line.
Now #sql variable has two lines.

Related

Finding name of column(s) based on a value stored MYSQL [duplicate]

I am working on a MySQL database that is huge (about 120 tables). I am trying to make some sense of it and it will help a great deal if I can search all 120 tables + columns for a string I am looking for.
Is that possible to do on a MySQL DB?
There is one solution, which might not be what you want. If you dumped the table into a file (mysqldump) with the data, then you would be able to grep any information you wanted out of it.
It would remove the need for time consuming search queries, and is the most efficient way I can think of.
This will help you to find a string in entire database
DELIMITER ##
CREATE PROCEDURE sp_search1(IN searchstring INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE output TEXT;
DECLARE table_name TEXT;
DECLARE column_name TEXT;
DECLARE s TEXT;
DECLARE searchcursor CURSOR FOR
SELECT table_name,column_name FROM information_schema.columns AS column
ORDER BY table_name,ordinal_position;
OPEN searchcursor;
PREPARE stmt2 FROM 'select * from ? where ? = ?' ;
search_loop : LOOP
IF done THEN
LEAVE search_loop;
END IF;
FETCH searchcursor INTO table_name,column_name;
IF( EXECUTE stmt2 USING table_name, column_name,searchstring) THEN
INSERT INTO `table_names`(`table_name`) VALUES(#table_name);
END IF;
END LOOP;
END;
Just wanted to add on to Omnipresent's answer, which is the de facto way to search a db.
Unfortunately, 99% of the time, my db is huge and an average dump has few newlines, meaning grepping for the string I want returns the vast majority of the sql file.
I now prefer to use the --tab switch which makes a tab delimited txt file per table in a db.
This means not only do I get one record per line, but I can quickly get the table my search term is in.
Try this:
mysqldump -u user_name -p database_name --tab=tmp
Where tmp is an empty directory you've created.
An ls of tmp would give you something like this:
users.sql
users.txt
orders.sql
orders.txt
where the sql files contain the create table syntax, and the txt contain the data.
Note that the tab option utilizes mysql's SELECT INTO OUTFILE which means this trick cannot be done anywhere but localhost.
In unix machines, if the database is not too big:
mysqldump -u <username> -p <password> <database_name> --extended=FALSE | grep <String to search> | less -S
You could just iterate each table:
mysql="mysql -uUSER -pPASS -hHOST --protocol=tcp dbname -e"
for table in `$mysql "show tables;"`
do
echo $table
$mysql "select * from $table;" | grep STRING_TO_SEARCH_FOR
done

How can I drop all MySQL Databases with a certain prefix?

I need to drop hundreds of mysql databases that all share a common prefix, but have random id's as the rest of their name ( eg. database_0123456789, database_9876543210 ). All these databases are on the same server. There are other databases on that same server that I don't want to drop.
This is what I'd like to do:
DROP DATABASE `database_*`;
How can I drop these efficiently? Is there a MySQL query I can run? Maybe a shell script?
The syntax of the DROP DATABASE statement supports only a single database name. You will need to execute a separate DROP DATABASE statement for each database.
You can run a query to return a list of database names, or maybe more helpful, to generate the actual statements you need to run. If you want to drop all databases that start with the literal string database_ (including the underscore character), then:
SELECT CONCAT('DROP DATABASE `',schema_name,'` ;') AS `-- stmt`
FROM information_schema.schemata
WHERE schema_name LIKE 'database\_%' ESCAPE '\\'
ORDER BY schema_name
Copy the results from that query, and you've got yourself a SQL script.
(Save the results as plain text file (e.g. dropdbs.sql), review with your favorite text editor to remove any goofy header and footer lines, make sure the script looks right, save it, and then from the mysql command line tool, mysql> source dropdbs.sql.)
Obviously, you could get more sophisticated than that, but for a one-time shot, this is probably the most efficient.)
Don't need of an external script file. A stored procedure using prepare statements might do the trick:
CREATE PROCEDURE kill_em_all()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE dbname VARCHAR(255);
DECLARE cur CURSOR FOR SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE 'database\_%' ESCAPE '\\'
ORDER BY schema_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO dbname;
IF done THEN
LEAVE read_loop;
END IF;
SET #query = CONCAT('DROP DATABASE ',dbname);
PREPARE stmt FROM #query;
EXECUTE stmt;
END LOOP;
END;
Once you have that procedure, you just have to:
CALL kill_em_all();
When done:
DROP PROCEDURE kill_em_all
This question lacks an answer without creating a file first.
Our build server automatically creates a database for every topic branch while running unit tests. After information_schema queries get really slow which causes our tests to fail.
I created a batch file which runs every day. I did not want to deal with temporary files. So here is my solution.
#ECHO OFF
REM drops all databases excluding defaults
SET user=user
SET pass=pass
mysql ^
-u %user% ^
-p%pass% ^
-NBe "SELECT CONCAT('drop database `', schema_name, '`;') from information_schema.schemata where schema_name NOT IN ('mysql', 'test', 'performance_schema', 'information_schema')" | mysql -u %user% -p%pass%
Modifying spencer7593 answer
Here is the command to find desired results and save it in file where prefix is database prefix
SELECT CONCAT('DROP DATABASE ',schema_name,' ;') AS stmt
FROM information_schema.schemata
WHERE schema_name LIKE 'prefix\_%' ESCAPE '\\'
ORDER BY schema_name into outfile '/var/www/pardeep/file.txt';
if you get permission denied then change folder permission to 777 or change folder group to mysql using this
chown -R mysql /var/www/pardeep/
then run this query
source /var/www/pardeep/file.txt;

How to append data from SQL to an existing file

SQL has the option to dump data into a file, using the INTO OUTFILE option, for exmaple
SELECT * from FIshReport INTO OUTFILE './FishyFile'
The problem is, this command is only allowed if the file didn't exist before it. It creates the file and then enters the data.
So, is there any way to append data to a file this way?
As the MySQL page on SELECT syntax suggests:
http://dev.mysql.com/doc/refman/5.0/en/select.html
the alternative to this is to issue the SELECT from the MySQL client:
However, if the MySQL client software is installed on the remote machine,
you can instead use a client command such as mysql -e "SELECT ..." > file_name
to generate the file on the client host.
which, in your case, would be modified to be:
mysql -e "SELECT * from FishReport" >> file_name
so that you simply append to the file.
From your Tcl script, you could simply issue this as an exec command:
http://www.tcl.tk/man/tcl/tutorial/Tcl26.html
I think MySQL does not allow appending data to an existing file or overwriting an existing file for security reasons.
A work around could be to save resuts in seperate files and then append the using file IO.
You could always append the output from your SQL script to a file using >>
For example (for Sybase):
isql < script.sql >> outputfile.out
I can't tell you what the equivalent is for MySQL but the principle should be the same.
Of course output will all go to one file so if your SQL script is outputting various SQL selects to different output files then you'd need to split the script up.
You could just add it to a variable. Then use a SELECT with UNION.
declare t varchar(100);
set #myvar = concat('
select * INTO OUTFILE \'',file,'\'
from (
select \'',t,'\'
union all
SELECT col from tbl where x
) a'
);
PREPARE stmt1 FROM #myvar;
EXECUTE stmt1;
Deallocate prepare stmt1;

way to search entire database for a string in MySQL

I am working on a MySQL database that is huge (about 120 tables). I am trying to make some sense of it and it will help a great deal if I can search all 120 tables + columns for a string I am looking for.
Is that possible to do on a MySQL DB?
There is one solution, which might not be what you want. If you dumped the table into a file (mysqldump) with the data, then you would be able to grep any information you wanted out of it.
It would remove the need for time consuming search queries, and is the most efficient way I can think of.
This will help you to find a string in entire database
DELIMITER ##
CREATE PROCEDURE sp_search1(IN searchstring INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE output TEXT;
DECLARE table_name TEXT;
DECLARE column_name TEXT;
DECLARE s TEXT;
DECLARE searchcursor CURSOR FOR
SELECT table_name,column_name FROM information_schema.columns AS column
ORDER BY table_name,ordinal_position;
OPEN searchcursor;
PREPARE stmt2 FROM 'select * from ? where ? = ?' ;
search_loop : LOOP
IF done THEN
LEAVE search_loop;
END IF;
FETCH searchcursor INTO table_name,column_name;
IF( EXECUTE stmt2 USING table_name, column_name,searchstring) THEN
INSERT INTO `table_names`(`table_name`) VALUES(#table_name);
END IF;
END LOOP;
END;
Just wanted to add on to Omnipresent's answer, which is the de facto way to search a db.
Unfortunately, 99% of the time, my db is huge and an average dump has few newlines, meaning grepping for the string I want returns the vast majority of the sql file.
I now prefer to use the --tab switch which makes a tab delimited txt file per table in a db.
This means not only do I get one record per line, but I can quickly get the table my search term is in.
Try this:
mysqldump -u user_name -p database_name --tab=tmp
Where tmp is an empty directory you've created.
An ls of tmp would give you something like this:
users.sql
users.txt
orders.sql
orders.txt
where the sql files contain the create table syntax, and the txt contain the data.
Note that the tab option utilizes mysql's SELECT INTO OUTFILE which means this trick cannot be done anywhere but localhost.
In unix machines, if the database is not too big:
mysqldump -u <username> -p <password> <database_name> --extended=FALSE | grep <String to search> | less -S
You could just iterate each table:
mysql="mysql -uUSER -pPASS -hHOST --protocol=tcp dbname -e"
for table in `$mysql "show tables;"`
do
echo $table
$mysql "select * from $table;" | grep STRING_TO_SEARCH_FOR
done

How do I pass a variable to a mysql script?

I know that with mysql you can write SQL statements into a .sql file and run the file from the mysql command line like this:
mysql> source script.sql
How do I pass a variable to the script? For example, if I want to run a script that retrieves all the employees in a department, I want to be able to pass in the number of the department as a variable.
I am not trying to run queries through a shell script. There are simple queries I run from the mysql command line. I'm tired of retyping them all the time, and writing a shell script for them would be overkill.
#!/bin/bash
#verify the passed params
echo 1 cmd arg : $1
echo 2 cmd arg : $2
export db=$1
export tbl=$2
#set the params ... Note the quotes ( needed for non-numeric values )
mysql -uroot -pMySecretPaassword \
-e "set #db='${db}';set #tbl='${tbl}';source run.sql ;" ;
#usage: bash run.sh my_db my_table
#
#eof file: run.sh
--file:run.sql
SET #query = CONCAT('Select * FROM ', #db , '.' , #tbl ) ;
SELECT 'RUNNING THE FOLLOWING query : ' , #query ;
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
--eof file: run.sql
you can re-use the whole concept from from the following project
Like this:
set #department := 'Engineering';
Then, reference #department wherever you need to in script.sql:
update employee set salary = salary + 10000 where department = #department;
you really should be looking at a more appropriate way of doing this. i'm going to guess that you're trying to run mysql queries via a shell script. you should instead be using something like PERL or PHP.