i have written a c-shell script to connect to a database. This already works just fine and i now want to invoke an sql script to read and print ALL the values in a cetrain table. As of now this is how my script looks
#!/bin/csh
set MYSQL=${MYSQL_HOME}/mysql
${MYSQL} ${CLEDBUSER}
where CLEDBUSER is set as an environment variable like so - CLEADBUSER=-uusername -ppassword -Ddatabasename
i am able to run the script and connect to the database. When i runt he script it gives me the msql pront awaiting the next command. So i added to the script a variable that contains the (SELECT) statement to query the database. Now my script looks like this
#!/bin/csh
set MYSQL=${MYSQL_HOME}/mysql
set SELECTER="SELECT * FROM TB_EARTH_UI;"
${MYSQL} ${CLEDBUSER} ${SELECTER}
the problem is it doesnt return me all the rows and columsn but it returns me a listing of avaiable commands in mysql promt and default options and also vairables and boolean options. Why is my SELECT statement not getting read?
MySQL client (mysql) expects SQL instructions on its standard input (e.g. your keyboard, when invoking from the shell).
You could do: [edit: please ignore, this one is off-topic]
${MYSQL} ${CLEDBUSER} < text_file_of_sql_statements.sql
or
${MYSQL} ${CLEDBUSER} << EOF
${SELECTER}
# you can add other litteral SQL statements here, or more variables containing SQL statements
EOF
or
${MYSQL} ${CLEDBUSER} --execute="${SELECTER}"
[edit]
I totally misunderstood the OP's question. I didn't get it that he was trying to execute SQL statements from a variable. I have edited the above options (thank you outis). Here is another variation:
echo ${SELECTER} | ${MYSQL} ${CLEDBUSER}
Also, the --skip-column-names option could make it easier for you to parse the output.
Related
So I am very new to TCL commands and I have to write a simple query which would read the hash value generated by a command.
TCL query is part of a bigger script where we need to generate a hash value using tcl command.
Below is the whole scenario:
Basically, need to execute a command to generate HASH value.
For ex:
request password-hash -password <password_value>
Once above command is executed, the shell will provide a hash value. This hash value then should be provided to another command.
For ex:
set password-hash <above generated value here>
After a lot of searching I think exec command will give the hash as output, I was then planning to store it in some variable using set command, something like below:
set hash_value [exec request password-hash -password <password_value>]
& then
set password-hash $hash_value
however, I am facing error that tcl evaluation failing.
The script 'set hash_value [exec request password-hash -password <password_value>];' evaluation failed. Error: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
Is exec command correct way of doing the what I actually wanted to do?
Since evaluation is failing, I think some syntax issue is there maybe?
Any better way to read console output and provide it as input to another command?
Thanks!!
I've created a huge .csv with only one column, each column is a valid sql string update like:
UPDATE TBL SET LAST = 0 WHERE ID = 1534781;
Is there a way to execute each row as a single sql query? Also, I'm using datagrip, if anyone knows of a sort of tool I would be happy.
To execute a file against your database in DataGrip just use the context menu when observing your file in Files tool window
A CSV file that contains one column is just called a "file." :-)
The most common way of executing a series of SQL statements in a file is with the command-line mysql client:
mysql -e "source myfile.csv"
How about script:
begin
update ...
update ...
update ...
...
end;
I am trying to execute mysql queries from a shell script. Below are the two queries.The first one seems to work and gets the work done. The second statement fails. The second statement however works if I keep single query instead of two.
mysql-ib -utrial -ptrial! trial -se "SET #bh_dataformat='txt_variable'; SELECT #bh_dataformat"
$(mysql-ib -utrial -ptrial! trial -se "SET #bh_dataformat='txt_variable'; SELECT #bh_dataformat")
The error I get when I execute the second statement is
./test.sh: line 15: txt_variable: command not found
You are using a subshell with $(..). That's why the calling's shell variables like txt_variable are not defined anymore.
I want to pass SOME VARIABLES to mysql file from bash shell script.
Here is my shell script.
#!/bin/bash
echo $0 Started at $(date)
mysql -uroot -p123xyzblabla MyMYSQLDBName<mysqlfile.sql PARAM_TABLE_NAME
Please note that it is MYSQL and not SQLPLUS
My MYSQL.sql , I want to read and use passed parameter/argument (PARAM_TABLE_NAME)
select count(*) from PARAM_TABLE_NAME
Question 1: What is the correct syntax to pass variable(PARAM_TABLE_NAME) to sql file (mysqlfile.sql)?
Question 2: How can I print variable(PARAM_TABLE_NAME) in sql file (mysqlfile.sql)?
Basically, I want to make generic SQL script which can load or select data from tables based on received inputs.
Thanks
There is no such thing as passing a parameter to a SQL file. A SQL file is no more than a text file that contains a list of SQL statements. These statements are interpreted by the mysql client program exactly as if you typed them on your keyboard.
The mysql client does not provide the feature you are looking for.
But I can think of a few tricks to achieve a similar effet:
create/populate a configuration table prior to reading your SQL file. Then write your SQL file so that it takes this table contents into account:
bash> mysql -e "INSERT INTO config_table VALUES(1, 2, 3)"
bash> mysql < script.sql
prepend your SQL file with some variables declarations. Then use these variables in the rest of your script:
bash> (echo "SET #var=123;" ; cat script.sql) |mysql
[example script.sql]
SELECT * FROM mytable WHERE id = #var;
write your SQL file with some placeholders that your replace on the fly, e.g with sed:
bash> sed "s/__VAR_A__/mytable/g" script.sql |mysql
[example script.sql]
SELECT * FROM __VAR_A__ WHERE id = 123;
All the above is quite dirty. A much cleaner solution would involve stored procedures or functions. Then you would just pass your parameters as procedure parameters:
bash> PARAM1='foo'; PARAM2='bar'
bash> mysql -e "CALL MyProc($PARAM1);"
bash> mysql -e "SELECT MyFunc($PARAM2);"
note: it is not possible to parametrize a table name in SQL, so you will need to resort to dynamic SQL like this in all cases (except for the sed-based hack, which I do not recommend)
This is an old thread but I think may still be useful to some people. Something like this should work:
mysql -uroot -p123xyzblabla MyMYSQLDBName -e "set #testVar='customer_name'; source mysqlfile.sql;"
Now #testVar (customer_name) is available for you to use in mysqlfile.sql file.
HTH
The way to pass parameters has already been answered in this or other threads. However, specific to the sample in you question, I'd like to add that you can't use the variables declaration method as a placeholder for a table name, as the documentation says:
User variables are intended to provide data values. They cannot be used directly in an SQL statement as an identifier or as part of an identifier, such as in contexts where a table or database name is expected
If you want to use a table name parameter, you can still use the sed or the stored procedures or functions as answered by #RandomSeed
In addition to that, another way is using PREPARE and EXECUTE in your script. The following example allows you to create a database/schema (in case you wanted to use stored procedures you need to have them already stored in a database), like this:
[myscript.sql]
set #s=CONCAT("CREATE DATABASE ", #dbname);
PREPARE stmt FROM #s;
EXECUTE stmt;
Then use any of the proposed syntax in the other questions to set the #dbname variable:
mysql -uroot -p123xyzblabla MyMYSQLDBName -e "set #dbname='mydatabase'; source myscript.sql;"
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;