sqlcmd output file remove header and footer - sql-server-2008

Using SQLCMD, I can output the query result to a text file, now, the text file has header:
And footer:
How do I remove them during query?
My query, inside a sql file:
SELECT internal_no, item_no, dspl_descr, rtl_prc FROM PLU
My SQLCMD command:
SQLCMD -S SQLSERVER01 -U AdminUser -P au5584 -i C:\text.sql -o C:\out.txt

Add
SET NOCOUNT ON
To the top of your query
SET NOCOUNT ON
SELECT internal_no, item_no, dspl_descr, rtl_prc FROM PLU
And add "-h-1" your SQLCMD command to:
SQLCMD -h-1 -S SQLSERVER01 -U AdminUser -P au5584 -i C:\text.sql -o C:\out.txt

Related

Bash: file not created with '>' command

I'm writing a script to create backups of a MySQL database running in a docker container. The database is correctly up and running.
My current code is
#!/bin/bash
PATH=/usr/bin:/usr/local/bin:/root/.local/bin:$PATH
docker-compose exec -T db mkdir -p /opt/booking-backup
docker_backup_path="/opt/booking-backup/dump_prod_$(date +%F_%R).sql"
copy_backup_path="/root/backup_scripts/booking_prod/dump_prod_$(date +%F_%R).sql"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "$docker_backup_path"
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > "/opt/booking-backup/dump_prod.sql"
[ -d ./backup ] || mkdir ./backup
docker cp $(docker-compose ps -q db):$docker_backup_path $copy_backup_path
However, when I execute it it throws this error:
Error: No such container:path: f0baa241becd20d2690bb901fb257a4bbec8cac17e6f1ce6d50adb9532bbae03:/opt/booking-backup/dump_prod_2019-05-28_14:23.sql
What makes this weirder is that I have the exact same code (but with booking switched out for abc, and with PSQL instead of MySQL) that works correctly.
It appears that this line
docker-compose exec db mysqldump --add-drop-database --add-drop-table --user=root --password="pw" booking > $docker_backup_path
does not create the output file, but when I use tee I can see the contents of the dump and they are correct.
What's going wrong here?
The shell redirections
docker-compose exec db mysqldump ... > "$docker_backup_path"
docker-compose exec db mysqldump ... > "/opt/booking-backup/dump_prod.sql"
# -----------------------------------^ here
... will be expanded by your local shell, not inside the container. Meaning the files are written to your local filesystem not to the container's filesystem.

Removing White Spaces from SQLCMD output

USing SQLCMD on SQL version 2016...to remove white spaces from my output, I've tried the -W -s switches...and I still get white spaces....Here's my code
SQLCMD -S myserver -d mydb -b -Q "exec dbo.GetiBottaItemFile"
-o "myfile" -W -s ","
-h -1 -m17
The output takes up as many spaces as the size of the field I'm selecting inthe stored procedure...I'd prefer not to do a trimr in the stored procedure on all my character fields...Any thoughts?
USing SQLCMD on SQL version 2016 and I want to remove white spaces from my output. I've tried the -W -s switches and I still get white spaces
Here's my code
SQLCMD -S myserver -d mydb -b -Q "exec dbo.GetiBottaItemFile"
-o "myfile" -W -s ","
-h -1 -m17
The output takes up as many spaces as the size of the field I'm selecting in the stored procedure.I'd prefer not to do a rtrim in the stored procedure on all my character fields. Any thoughts?

Automate MYSQL CREATE DATABASE on terminal

I'm running a script I have created that downloads and installs WordPress. To save more time I wanted to open MAMP MYSQL and create a new database like so:
/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
CREATE DATABASE $1; // passed into the function as argument
exit; // to get out of MYSQL
The rest of my function:
wordpress() {
cd /Volumes/example/example/Web/dev; mkdir $1;
cd $1;
curl -O https://wordpress.org/latest.tar.gz;
tar -xvzf latest.tar.gz;
mv wordpress/* .;
rmdir wordpress/;
rm latest.tar.gz;
cp wp-config-sample.php wp-config.php;
vim -s /Volumes/example/example/Web/dev/db_overwrite.txt wp-config.php;
curl -O https://downloads.wordpress.org/plugin/wp-super-cache.latest-stable.zip;
curl -O https://downloads.wordpress.org/plugin/wp-migrate-db.latest-stable.zip;
curl -O https://downloads.wordpress.org/plugin/contact-form-7.latest-stable.zip;
curl -O https://downloads.wordpress.org/plugin/wordpress-seo.latest-stable.zip
mv wp-super-cache.latest-stable.zip wp-migrate-db.latest-stable.zip contact-form-7.latest-stable.zip wordpress-seo.latest-stable.zip wp-content/plugins;
cd wp-content/plugins;
cp /Volumes/example/example/Web/Plugins/advanced-custom-fields-pro.zip .
unzip advanced-custom-fields-pro.zip;
unzip wp-super-cache.latest-stable.zip;
unzip wp-migrate-db.latest-stable.zip;
unzip contact-form-7.latest-stable.zip;
unzip wordpress-seo.latest-stable.zip;
rm wp-super-cache.latest-stable.zip wp-migrate-db.latest-stable.zip contact-form-7.latest-stable.zip wordpress-seo.latest-stable.zip;
cd ../themes;
git clone https://github.com/example/my-template;
}
How would I add the MYSQL lines to this function and make it work as intended?
Thanks
Why not use:
/Applications/MAMP/Library/bin/mysqladmin -uroot -proot create mydatabase
mysqladmin is a command-line tool that is part of the standard MySQL client software. It should be present everywhere that the mysql tool is present.
You might like to read https://dev.mysql.com/doc/refman/5.7/en/mysqladmin.html
Another alternative is to pass the CREATE DATABASE as an argument to mysql:
/Applications/MAMP/Library/bin/mysql ... -e "CREATE DATABASE mydatabase"

How to escape mysqldump quotes in bash?

I need to take a backup of my database using mysqldump tool.
My test table has a TIMESTAMP column that I have to use as filter.
I'm using a bash script with the following code:
#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
echo $cmd
$cmd
I'm printing the command that I assume should work. The above script produces the output:
mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where="timestamp_c>='2016-12-03 00:00:00'" --tables "test_table"
If I copy the printed command on the terminal, it works;
however, the command on the script print the error:
mysqldump: Couldn't find table: "00:00:00'""
Is there something that I'm not understanding about quotes escape?
Variables hold data, not code. Define a function instead.
cmd () {
mysqldump --verbose --opt --extended-insert \
--result-file /home/backups/mysql/test/test_table.20161205.bak test \
--where="timestamp_c>='2016-12-03 00:00:00'" \
--tables "test_table"
}
Try executing your command using 'eval'. Example:
#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
eval $cmd

Linux Bash file use Directory name

What I have is a few script files that are used for crons for different buildings in my company, but what I'm running into is I'm having to go into each file and change the OAK3 to a different building id, as well as the oak3(lowercase). The files are all located in there respectives warehouses folder ex: Desktop/CRON/OAK3. What I would like it to do, is if it's OAK3 use OAK3, and oak3(lowercase) instead of having to go into each file everytime we create a new db for a warehouse.
I am new to the linux world so I'm not sure if there is a way, and haven't found anything on google.
Example.sh
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1
Desired effect is possible
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/$WAREHOUSE_ID/$warehouse_id_count_portal.txt --ignore-lines=1
If i get what you want, which I´m not sure, this will help to do all new databases
databases=`mysql -B -r -u ${user} --skip-column-names -p${pass} --execute='show databases'`
for db in $databases; do
## now loop through the above array
echo $db # current DB
mysqldump -u $user --password=$pass $db > "$db.sql" #dump db to file
done
Using a combination of dirname and basename with the Bash special variable $0, you can get all of what you need.
The running script's filename is $0. Meanwhile dirname $0 will give you the directory path of the executing file. But you don't want the full path, just the last part, which basename will provide. realpath is used to expand the directory so . is not used.
Getting just the last directory name:
$ ls
tmp.sh # Ok, there's our file
$ dirname tmp.sh
. # The . is current directory
$ dirname $(realpath tmp.sh)
/home/mjb/OAK3 # so we expand it with realpath
$ basename $(dirname $(realpath tmp.sh))
OAK3 # then take only the last one with basename
So here's how it will work for you:
# Get the directory name
warehouse=$(basename $(dirname $(realpath $0)))
# And lowercase it with `tr` into a new variable
warehouse_lcase=$(echo $warehouse | tr '[:upper:]' '[:lower:]')
# Substitute the variables
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/${warehouse}/${warehouse_lcase}_count_portal.txt --ignore-lines=1
See also: Can a Bash script tell which directory it's stored in?
There is lot easier way to figure out the basename of the current-working-directory: pwd -PL | sed sg.\*/ggg
[san#alarmp OAK3]$ pwd; pwd -PL | sed sg.\*/ggg
/opt/local/OAK3
OAK3
So, if I understand your requirement correctly, if you don't wanna change the script(s) manually by hand, you can do this whilst inside that particular directory:
$ cat example.sh
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/OAK3/oak3_count_portal.txt --ignore-lines=1
#
$ this_dir=$(pwd -PL | sed sg.\*/ggg)
#
$ sed -e "s/${this_dir}/\${WAREHOUSE_ID}/g" example.sh
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/${WAREHOUSE_ID}/oak3_count_portal.txt --ignore-lines=1
#
$ sed -e "s/$(echo $this_dir | tr '[:upper:]' '[:lower:]')/\${warehouse_id}/g" example.sh
/usr/bin/mysqlimport --host=localhost -u root -ppassword --local --verbose -C --delete test \
/workplace/gwwallen/ETLdump/OAK3/${warehouse_id}_count_portal.txt --ignore-lines=1
Use -i option to make the change permanent in-file (without creating a new one) like this:
sed -ie "s/${this_dir}/\${WAREHOUSE_ID}/g" example.sh