Executing mysql command using a batch file - mysql

I am new to mysql. I've been trying to run a query through windows notepad batch file. The code on the file is:
USE menagerie
SELECT type FROM event
The directory I typed on mysql command line is:
C:/Users/abu/Desktop/ev.txt
The output is: ->
indicating something must be added to complete the command. Please tell me what is missing to complete the code.

You need to use the source command to execute queries from a file.
mysql> source C:/Users/abu/Desktop/ev.txt
Also, the file needs to have ; between queries. So the first line should be
USE menagerie;

Related

TCL - Reading console output (not keystrokes)

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!!

Executing sql strings from csv into mysql database

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;

Database Project - how can I access SQLCMD Variables in the Post build event command line

I've got a database project that I'm wanting to have call a batch file in the post-build event. So far that's pretty simple, but I want to be able to set some variables in the SQLCMD Variables section of the project, then pass those variables as arguments to the batch file.
When I reference any of my user-defined SQLCMD variables in my post-build event, they're just empty strings.
As an example, if I create a SQLCMD variable called $(MyVariable) and give it a default value of ABC, I try referencing it when calling my batch file with :
call <path and name of batch file> $(MyVariable)
and the parameter is empty.
Is there a way that I can do this?

Can one line mysql commands be easily stored and re-used?

I find myself constantly typing in long commands such as:
mysql> SELECT Cust_Num, Cust_Name_Full, Email FROM customers;
Is there a way to save this command as, say, "CInf" and execute it with that simple shortcut command like this:
mysql> CInf; ( = mysql> SELECT Cust_Num, Cust_Name_Full, Email FROM customers; )
I'm thinking of something analogous to a DOS batch file, where you can simply type the name of the batch file, without even needing the .bat extension.
Also, where do I store these mysql "batch files" (note: these aren't .bat files to be run from a command window, these are run from the mysql> prompt). With DOS I'd have a C:/bat folder and I'd put C:/bat in the path environment variable. How do I do a similar thing for mysql?
Mysql doesn't have "macros", but if your statements are select queries (like your example) you can create views:
create view clnf as
SELECT Cust_Num, Cust_Name_Full, Email
FROM customers;
Then to use
select * from clnf;
The other approach is to store your SQL in files, then from the mysql prompt use the source command:
mysql> source file_name
which executes the contents of the SQL file as if you had pasted it in to the window.

Run complex SQL scripts from memo (multi lines)

I'm executing sql scripts while using ADO and MSSQL server .
Under Here you will find an first example of a multi lines sql statement like :
use master;
go;
EXEC sp_detach_db
#dbname=N'DATABASENAME';
go;
I copy these lines from a Tmemo to my TADOQuery.sql.text but fail as already the go statement is not recognized and I get a keyword error by the mssql server.
Can I run the whole sript as one TQquery or do I have th split my query into several pieces, separated by the semicolon and iterate through the whole text ?
At first your code is not valid (no ; after GO) and has to be like this
USE master;
GO
EXEC sp_detach_db
#dbname=N'DATABASENAME';
GO
In fact GO is a delimiter used by MSSMS to separate the SQL-Statements.
If you want to use the same scripts as MSSMS do, you have to work on that like MSSMS.
Split the script into single parts by delimiter GO
Send every part to the Database
You have to split every statement whithout sending go.
SQL-Server is not interpreting GO, this is done by MSSMS.