How to write MySQL query in text file and run in Linux?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to write MySQL query in text file and run in Linux?
# 1  
Old 09-17-2019
How to write MySQL query in text file and run in Linux?

I would like to call a .sql file from a .sh file in linux. The .sql file will contain queries to MySQL database.

I am able to call the .sql file by running .sh file in linux, but the sql query is not working. Below is my syntax. The select statement is throwing below error.

Code:
./sample.sql: line 4: syntax error near unexpected token `from'

sample.sh file:

Code:
echo "this is shellscript calling sql"
./sample.sql

sample.sql file:

Code:
set pages 999
set lines 999
set colsep |
select * from sampletable;

expected result should be similar to a normal select * from xxx statement
# 2  
Old 09-18-2019
To call the mysql process from the shell, you need to invoke the mysql process.

Your script as SQL statements but does not invoke mysql.

For example, your script should have something like this:

Code:
mysql -u myuserid -p  mydatabase < mysqlfile.sql

(just as an example)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not able to write SQL query output in to .csv file with shell script.

I am trying to write SQL query output into a .csv file. But in the output columns are displaying in different lines instead of coming in one line. Main Code shell script: this is my code: #!/bin/bash file="db_detail.txt" . $file rm /batch/corpplan/bin/dan.csv... (6 Replies)
Discussion started by: sandeepgoli53
6 Replies

2. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. Shell Programming and Scripting

Need help to run query in loop for all acct nbr in a file..

I have a txt file with contents of acct nbr's like: 22222222222 33333333333 33445566778 I need to write a script which takes each acct nbr in the file and run the query like: select seq_nbr from event where acct_nbr='22222222222' and the query's output should be passed to a... (2 Replies)
Discussion started by: Rajesh Putnala
2 Replies

5. Shell Programming and Scripting

Need help to run query in loop for all acct nbr in a file..

I have a txt file with contents of acct nbr's like: 22222222222 33333333333 33445566778 I need to write a script which takes each acct nbr in the file and run the query like: select seq_nbr from event where acct_nbr='22222222222' and the query's output should be passed to a... (0 Replies)
Discussion started by: Rajesh Putnala
0 Replies

6. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

7. Linux

PHP is not execute the query from mysql in Linux

Hi , I have ran the php script from Linux box. It is running, But I can able to see any html content. There is no mysql data on that page. I have checked . 1) Mysql and data bases are connected successfully. Please help and guild me If I missed any think during the configuration. ... (13 Replies)
Discussion started by: Mani_apr08
13 Replies

8. Solaris

Mysql query reading a text file.

Hi, Is there any way that mysql query reads the content from a text file which has data in the below format: 1,2,3,4,5 and selects matched data from another table. ie I have a table named xyz which has same ids as in that file. I want this query to get count of the ids from xyz file by... (6 Replies)
Discussion started by: jyothi_wipro
6 Replies

9. Linux

run MySQL on linux

Hi all, I want to know that how can we Install and Configure MySQL Server on LINUX.And how can we Use it after installation.As MySQL is also came with the Linux installer but i don't know how to Configure it and Use it.Thanks in advance Regards, Amanpreet Singh (1 Reply)
Discussion started by: aman_mlt
1 Replies
Login or Register to Ask a Question