How to get SqlCommand execution Result to log file ? - sql-server-2008

I am executing query from the command prompt with following command :
sqlcmd -U sa -P pal#143 -S .\SQLEXPRESS -i Employee.sql -o Employee.log
How can I get the result of execution to log file ?
I am not getting anything in the log file.
thanks

Related

How to structure a cron job and script to execute sql command

I have a MySQL database accessible through CPANEL. I want to execute a SQL command to DELETE from dbtable where eventdate = 'YYYY-MM-DD'. This is my cron job.
curl -L --max-redirs 1000 -v "https://ottawaoc.ca/test/files/delete_dates.sh" 1>/dev/null
and here is the shell script
#!/bin/bash
mysql --user = "ottawaoc_test" --password = "test ps" --database = "ottawaoc_test" --execute ="DELETE FROM `h8be5_eventregistration` WHERE `eventdate` = '2020-09-27'"
(I do insert the correct password.)
I get output mailed to me and it seems to get the shell script but nothing happens within the database.
Could someone help to give me the correct commands and/or tell me how I can get errors from MySQL.
I used to run mysql crons by putting this in the shell:
#!/bin/bash
echo "mysql statement;" | mysql -B -hHOST -uUSER -pPASS DBNAME

Using batch variable in sqlcmd query

I'm trying to create a batch to execute a few sql script and some queries. I use sqlcmd in my batch like this :
#echo off
sqlcmd -S server -U user -P password -Q query
As I will execute different queries on the same server, I'd like to replace the server statement with a variable. So I could write something like this :
#echo off
SET server=192.X.X.X
sqlcmd -S server -U user -P password -Q first query
sqlcmd -S server -U user -P password -Q second query
I found this question on SO but I'm still unable to understand how that works :
SQLCMD using batch variable in query
Does anyone have an idea ?
Thanks a lot!
Expanding on my comment a little:
#Echo off
Set "server=192.X.X.X"
Set "user=myname"
Set "password=pa55w0rd"
sqlcmd -S %server% -U %user% -P %password% -Q first query
sqlcmd -S %server% -U %user% -P %password% -Q second query
You may wish to enclose your variables with relevant quoting as necessary.
From Microsoft: "Cmd.exe provides the batch parameter expansion variables %0 through %9. When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line."
So if the number of queries you wish to execute is known just pass them in through parameters like this:
Test.bat:
#echo off
SET server=192.X.X.X
sqlcmd -S server -U user -P password -Q %1%
sqlcmd -S server -U user -P password -Q %2%
Then call Test.bat like this:
Test.bat "Select * from Test1" "Select * From Test2"
You can pass up to 9 queries this way.

Write mysql error into a log file in bash

I'm writing a bash script and I want to redirect MySQL errors to a log file.
I had success with the below (ERROR 1045 (28000): Access denied for user... is being appended to the log file)
mysql -u user -pWrongpass -sN -e "query to update db;" 2>&1 | tee -a log
however, I'm not having success with this one. The error is displayed when I run the script but I don't see it in the log file.
result=$(mysql -u user -pWrongpass -sN "query to select from db;") 2>&1 | tee -a log
What's the correct syntax to put the result of a query into a variable while printing any potential error to the log file?
Thanks in advance and let me know if I'm not clear :)
You have to put the entire pipeline inside the command substitution.
result=$(mysql -u user -pWrongpass -sN "query to select from db;" 2>&1 |
tee -a log)
Since the output of mysql is piped to tee, it is the output of tee that you need to capture in result.

using batch to detect whether database exists,if not ,create it

I am going to write a .bat file to realize this function:
to detect whether the database and the table existed,if not exist,create them.
I tried to write the bat file like this:
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password=
select * from martin.aaaperson
Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.
C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd
you need to do something like
mysql.exe -u root -p < your_sql_commands_in_this_file.sql
Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.
Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

Running mysql -vvv from a script and I get no logging

I am having a problem getting mysql -vvv to output to a log file from a script. Redhat Linux, mysql 5
I can run the following from the Linux command line and I get SQL command output to the screen just like I would expect. Work fine and tells me what I want to know.
mysql -vvv --user=username --pass=password < /path/script.sql
When I add the following to a Linux bash shell script, I do not get and output to the log file. What do I not understand?
mysql -vvv --user=username --pass=password < /path/script.sql >> /tmp/file.log
NOTE: Database connect string is inside my script.sql
MySQL verbose uses the error output, try this:
mysql -vvv --user=username --pass=password < /path/script.sql 2>&1 >> /tmp/file.log
2>&1 redirect the error to standard output