Multiple MySql queries in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple MySql queries in shell script?
# 1  
Old 04-16-2009
Multiple MySql queries in shell script?

Hi guys,

i know how to run a single query using mysql embedded in a shell script as follows:

Code:
`mysql -umyuser -pmypass --host myhost database<<SQL
${query};
quit
SQL`

However, how would i be able to run several queries within the same connection?

The reason for this is i am creating TEMPORARY tables and MySql drops them when you close a connection, so i cant create one, close a connection and re-use it later...Smilie
# 2  
Old 04-16-2009
If I understand correctly you can use something like this:

Code:
mysql -user<user> -p<pass> -h<host> database -e'
  statement one;
  statement two;
  ...
  statment n;
  '

Or, of course, put more than one statement in the here document in the example in your original post.
# 3  
Old 04-16-2009
Quote:
Originally Posted by radoulov
If I understand correctly you can use something like this:

Code:
mysql -user<user> -p<pass> -h<host> database -e'
  statement one;
  statement two;
  ...
  statment n;
  '

Or, of course, put more than one statement in the here document in the example in your original post.
Thanks,

Can i do the following?

Code:
mysql -user<user> -p<pass> -h<host> database -e'
  statement one;\n
  statement two;\n
  ...
  statment n;
  '

Or, of course, put more than one statement in the here document in the example in your original post.[/QUOTE]

Notice the newlines? if i have tried it the way you say but the statements get wrapped around rather than going onto the next line...i.e.:

Code:
mysql -user<user> -p<pass> -h<host> database -e'
  statement one; statement two; statment n;
  '

Or, of course, put more than one statement in the here document in the example in your original post.[/QUOTE]
# 4  
Old 04-16-2009
You need the newlines only for legibility, mysql accepts more than one statement on the same line
(the statements should be separated by semicolons).
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run multiple Queries in a ksh Script?

How to run multiple Queries in a ksh Script I have a KSH script that has one SQL Query and generates and emails output of the query in HTML format. I want to change the script so that it has three SQL queries and the last query generates and emails the HTML output page of just that query. So far... (5 Replies)
Discussion started by: JolietJake
5 Replies

2. Linux

How to store count of multiple queries in variables in a shell script?

how to store the count of queries in variables inside a filein shell script my output : filename ------- variable1=result from 1st query variable2=result from 2nd query . . . . (3 Replies)
Discussion started by: sanvel
3 Replies

3. Shell Programming and Scripting

Executing Multiple Queries in parallel in Shell

I have n number of SQL queries needs to executed in Shell. Result of this query need to assign in a variable. Once all the queries are executed script needs to exit. Sample Query: SQL 1: Select Count(*) from TABLE GROUP BY COL1,COL2 SQL 2: Select Count(*) from TABLE GROUP BY COL1,COL2 ... (2 Replies)
Discussion started by: Niranjancse
2 Replies

4. Shell Programming and Scripting

Executing set of sql queries from shell script

Hi All, I tried executing set of queries from shell script but not able to capture the input query in the log file. The code looks something similar to below sqlplus user/pwd@dbname << EOF > output.log $(<inputfile.txt) EOF The above code is capturing the output of queries into... (9 Replies)
Discussion started by: loggedin.ksh
9 Replies

5. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

6. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

7. Shell Programming and Scripting

How to extract queries using UNIX shell script?

Hi, I have an input file which have many lines,from which i need to extract only the complete sql statements and write this alone to an output file. please help us in this. Regards Meva (7 Replies)
Discussion started by: meva
7 Replies

8. UNIX for Dummies Questions & Answers

shell script for sql queries

Hi All, I have written 4 sql queries . Now I want to write one SHELL SCRIPTING program for all these queries... i.e 1.select * from head; 2. select * from detail; 3. delete from head; 4. delete from detail; Please let me know how to write a shell script... Thank you (1 Reply)
Discussion started by: user71408
1 Replies

9. Shell Programming and Scripting

shell script queries: $home; broadcast ping

Dear all, This is the Bionic Fysh again. I have two quick questions: 1- when writing shell scripts, how does one allow the tilda ~ into the script ? e.g ls ~; ls ~me; user=you; ls ~$user (N.B I think that for this one you need: ls `~$user`) 2- In FreeBSD 4.0, I would like for a... (6 Replies)
Discussion started by: bionicfysh
6 Replies
Login or Register to Ask a Question