sqlcmd runs at command prompt, not as .bat - sqlcmd

I found a few threads related to this issue, but can't seem to find an answer that works.
I am using sqlcmd to create a .csv from a SQL table. I would like this to run as a scheduled task, and have had success in the past using sqlcmd code in a .bat file.
My syntax:
sqlcmd -S SERVERNAME -d DATABASE -U sa -P PASSWORD -q "select * from TABLE" -o "C:\Export\sqlexport.csv" -s","
When I paste this code into the Command Prompt, it runs perfectly.
I then copy the code and save it with Notepad as a .bat file. When I run the batch, either from the Command Prompt or by double-click, it errors:
"Login failed for user 'sa'.
I am not understanding the reason for the different outcomes, nor have a found a solution after many hours of searching. I'm sure its simple, but I'm new to this sqlcmd process. I appreciate any advice!

Related

Setting envs or globals in bash script [NOT A duplicate]

I'm looking for a way (preferably cross-platform compatible) to set something globally accessible from a bash script.
My company is using a bash script to request access credentials to a mysql database. This returns username, password and db domain that I end up having to copy paste in my terminal to run and connect to our mysql db.
I thought i'd amend the script to set environment variables and make use of these in an alias with the credentials set in my bashrc but turns out you can't set environment variables in a bash script.
So i tried to set the mysql alias with the username password and domain pre-filled in that same script but same issue. Can't set an alias in a bash script.
I essentially want to be able to run the script that gives me the credentials and then not have to do manual copy pasting all over the place.
What I tried was (if it give more context):
#!/bin/bash
# Script gets the credentials
# Script now has username, password, endpoint variables
export MYSQL_USER=$username
export MYSQL_PASSWORD=$password
export MYSQL_ENDPOINT=$endpoint
# Script finishes
and in my bashrc:
alias mysqlenv="mysql -h $MYSQL_ENDPOINT -u $MYSQL_USER -p'$MYSQL_PASSWORD'"
I appreciate this is not working and that might not be the best solution so i'm open to other options.
PS: Forgot to mention the credentials expire every 24H which is why i want to smoothen the process
PS2: I can't source the script that gives me the credentials because it's not just exporting environment variables, it's taking params from the cli and getting me to log in to my company system on my browser, etc.
PS3: I know putting password for mysql on the command line is bad practice but this is a non-issue as that password is being printed there in the first place by the script that give me the credential (written by someone else in the company)
Since you can already parse the credentials, I'd use your awk code to output shell commands:
getMysqlCredentials() {
credential_script.sh | awk '
{parse the output}
END {
printf "local MYSQL_USER=\"%s\"\n", username
printf "local MYSQL_PASSWORD=\"%s\"\n", password
printf "local MYSQL_ENDPOINT=\"%s\"\n", endpoint
}
'
}
then, I'd have a wrapper function around mysql where you invoke that function and source the output
mysql() {
source <(getMysqlCredentials)
command mysql -h "$MYSQL_ENDPOINT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$#"
}

What is wrong with this bash script (cron + mysql)

Im using a bash script (sync.sh), used by cron, that is supposed to sync a file to a MySQL database. It works by copying a file from automatically uploaded location, parse it by calling SQL script which calls other MySQL internally stored scripts, and at the end emails a report text file as an attachment.
But, seems like something is not working as nothing happens to MySQL databases. All other commands are executed (first line and last line: copy initial file and e-mail sending).
MySQL command when run separately works perfectly.
Server is Ubuntu 16.04.
Cron job is run as root user and script is part of crontab for root user.
Here is the script:
#!/bin/bash
cp -u /home/admin/web/mydomain.com/public_html/dailyxchng/warehouse.txt /var/lib/mysql-files
mysql_pwd=syncit4321
cd /home/admin/web/mydomain.com/sync
mysql -u sync -p$mysql_pwd --database=database_name -e "call sp_sync_report();" > results.txt
echo "<h2>Report date $(date '+%d/%m/%Y %H:%M:%S')</h2><br/><br/> <strong>results.txt</strong> is an attached file which contains sync report." | mutt -e "set content_type=text/html" -s "Report date $(date '+%d/%m/%Y %H:%M:%S')" -a results.txt -- recipient#mydomain.com
cron will execute the script using a very stripped environment. you probably want to add the full path to the mysql command to the cron script
you can find the full path by
which mysql
at the prompt,
or you can add an expanded path to the cron invocation
1 2 * * * PATH=/usr/local/bin:$PATH scriptname

Running mysql command from bat file with redirection

I have the following batch file:
#ECHO on
cd "C:\Program Files\MariaDB\mariadb\bin"
mysql -u root < "C:\database_setup.sql"
When I run the command directly in the command line, it works fine. When I run this batch file I get that it's trying to execute:
mysql -u root 0<"C:\database_setup.sql"
To solve this, I tried to escape the less than sign with:
mysql -u root ^< "C:\database_setup.sql"
It appears to be correct in the console but it's dumping the mysql options instead of inserting the contents of database_setup.sql.
I'm thinking that this is because the "<" is actually be referred to as a string since I'm escaping it and not as the redirection operator.
How does one accomplish running this command in a batch file (which works fine directly in the console)?
The following workaround could help you:
mysql -u root -e "SOURCE C:\database_setup.sql"
Also the following should work:
type C:\database_setup.sql | mysql -u root

mysql won't import database dump file on Windows XP

I created a data base using mysql. I used MySQLDump to create one database backup file in text format (MySql 5.5 on Windows XP). The database is local on my machine (local host).
I am having trouble using the MySQL command to load the dump file to restore the database. I have done the following:
Research stack overflow for how to do it. I noticed there's a bug using the MySQL command to restore the data from a post. Before I run the command, I DROP the database and CREATE the database using MySQL workbench.
I type the following command in the DOS prompt to restore the database:
mysql -u root -p -h localhost -D matlab_data -o < backup.sql
backup.sql is a the backup file in text format created by MySqlDump.
I am then asked for the password which I enter. I get the DOS prompt right away with no error message. I've waited several hours for the command to run and the database is still empty.
I have tried various command formats over the last few days. If I enter incorrect data in the command line (non existen file, database, etc), I get an error message.
I feel I would not see the DOS prompt until the database is restored. If I don't DROP and CREATE the database, I get an error message. Otherwise, not.
Does anybody have any idea what the issue is? I realize that I could be making a stupid mistake.
Thank you for your help.
shell into the mysql console and run the sql file as this
If you are already running mysql, you can execute an SQL script file using the source command or . command:
mysql> source file_name
mysql> \. file_name
note that file_name must be an absolut path

How can I run multiple Stored Procedures Files & Triggers (.sql) From MySQL Workbench

I am trying to run a set of sql files with stored procedures and triggers in my windows XAMPP environment. Some suggested to me using a batch script but I do not know how to do this in windows.
Is it possible to run all these .sql files from within MySQL Workbench? How? If not, can anyone tell me how to run a batch file within windows?
Thank you.
It seems Workbench doesn't support the command "SOURCE" so the next best thing is is (at least in windows) is to run a batch job. Simply create a new .sql file and add the full path to each .sql file like so:
Create the batch file:
In windows, the batch file can be a .sql with the sql comman SOURCE which calls the other .sql files, like so:
create run.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_delete.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_insert.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_load.sql
Open Command Line and CD to MySQL Folder
Open the command line, and cd to MySQL. If you are using XAMPP, the command/location should be something like:
cd C:\xampp\mysql\bin\
Execute the Batch File by pressing ENTER
Last, simply load mysql and run the batch file using the following command:
mysql -u root -h 127.0.0.1 my_database_name -vvv < C:\xampp\htdocs\mysite\sql\procs\run.sql
The execution above means the following:
mysql -u <username> -h <host> <database> -vvv < <batch_path_file_name>
-vvv shows all the queries being executed and the rows affected for debugging.
That's it. All .sql files mentioned in the run.sql file will be executed.