Still having trouble with mapping using SQLCMD - sqlcmd

I am trying to map MyDatabase as shown below. It works fine in SMS but my SQLCMD code always maps to 'master.
This works fine in SMS.
USE [MyDatabase]
GO
CREATE USER [TEST\MyUser] FOR LOGIN [TEST\MyUser]
GO
USE [MyDatabase]
GO
EXEC sp_addrolemember N'MyUser', N'TEST\MyUser'
How do I implement it in SQLCMD? I have tried:
SQLCMD.exe -E -S (local) -Q "USE [MyDatabase]"
SQLCMD.exe -E -S (local) -Q "CREATE USER [TEST\MyUser] FOR LOGIN \TEST\MyUser]"
SQLCMD.exe -E -S (local) -Q "USE [MyDatabase]"
SQLCMD.exe -E -S (local) -Q "EXEC sp_addrolemember N'MyUser', N'TEST\MyUser'"

Specify the database on the command line with the -d switch. The database is reset to the user's default (in this case, master) on each statement otherwise. So, you want something like:
sqlcmd -S (local) -d MyDatabase -E -Q "CREATE USER [TEXT\MyUser] FOR LOGIN [TEST\MyUser]"
sqlcmd -S (local) -d MyDatabase -E -Q "EXEC sp_addrolemember N'MyUser', N'TEST\MyUser'"

Related

Exporting views from Mysql using Docker commmand

The code below extracts views separately from the database. However, I'm trying to get this to run in a single docker run or exec command.
Right now when I try, the pipe command and in combination with trying to escape quotes gives me errors.
mysql -u username INFORMATION_SCHEMA
--skip-column-names --batch
-e "select table_name from tables where table_type = 'VIEW'
and table_schema = 'database'"
| xargs mysqldump -u username database
> views.sql
Anyone know how to achieve this within one docker command?
For example:
docker exec -i $(docker-compose ps -q mysqldb) mysql ...
Much love.
You can run both the mysql client command and the mysqldump tool from somewhere that's not "on the database server". In your case, you can run them from the host that has the MySQL server, assuming you launched the database with options like docker run -p 3306:3306. It would look something like
mysql -h 127.0.0.1 -u username INFORMATION_SCHEMA \
--skip-column-names --batch \
-e "select table_name from tables where table_type = 'VIEW' and table_schema = 'database'" \
| xargs mysqldump -h 127.0.0.1 -u username database \
> views.sql
This avoids all of the shell quoting problems trying to feed this into docker exec, and also avoids the requirement to need root-level access on the host to do an administrative task (if you can run any Docker command at all then you can use docker run to add yourself to the host's /etc/sudoers, among other things).
I also agree with #MichaelBoesl's answer, though: this is long enough that trying to make it into a one-liner isn't really worth the trouble that the various quoting and escaping will bring. I'd probably write this into a script and put the SQL query into a file.
#!/bin/sh
: ${MYSQL_HOST:=127.0.0.1}
: ${MYSQL_USER:=username}
: ${MYSQL_DATABASE:=INFORMATION_SCHEMA}
cat >/tmp/dump_views.sql <<SQL
SELECT table_name
FROM tables
WHERE table_type='VIEW' AND table_schema='database';
SQL
mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" --skip-column-names --batch \
"$MYSQL_DATABASE" </tmp/dump_views.sql \
| xargs mysqldump -h "$MYSQL_HOST" -u "$MYSQL_USER" "$MYSQL_DATABASE"
You can put all your commands into a bash script on the container and just execute the script!

Redirecting stdout to mysql in bash

I can execute mysql passing in a file as follows.
mysql -u username -p < some_file
In a bash script I have a function which echoes output which I want to pass into the same command in a bash script.
some_function() {
echo "Some SQL"
}
How can I pass the output into mysql using pipes/redirection?
I have tried the following, but it fails with no such file or directory. How can I use the output from the function here instead.
mysql -u username -p < some_function
No need to use a pipe or a redirection in this case, you can use directly -e options to execute some SQL commands:
mysql -u username -p -e "SQL/MySQL commands"
Exemple on a specific database:
mysql -u username -p -e "use database_name; SHOW tables"
mysql -u username -p -e "SHOW tables" database_name
And you can also catch the output of a command or function and passing it as argument like this:
sql_command="$(your_function)"
mysql -u username -p -e "${sql_command}" database_name;
If you really want to use a pipe or a redirection (but I think it make no sense in this case):
$ mysql -u root -p database_name < <(echo "SHOW TABLES") # redirection
$ mysql -u root -p database_name <<< "$(echo "SHOW TABLES")" # another way to use redirection
$ echo "SHOW TABLES"|mysql -u root -p database_name # pipe

Bash script with multiple queries and redirection in the same mysql session

My bash script queries a mysql database 3 times and redirects the standard out of each query to a file (3 different files in total with different columns structure ).
I want it to ask for the mysql password as it's important for me not to have the password in the script or on disk.
How can I include all queries and stdout redirection in the same mysql session in order to avoid asking for the password 3 times?
This is what I have now:
#!/bin/bash
mysql -h database.com -u user -p -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p -e "USE database; mysql query3" > file3
You could use tee and notee commands and write a single query file, say queries.sql and invoke it in a single shot:
use database
tee file1
query1
notee
tee file2
query2
notee
tee file3
query3
notee
Then invoke it:
mysql -h database.com -u user -p -e "source queries.sql" > /dev/null
Related:
What is the equivalent of the spool command in mysql
How can I run an SQL script in MySQL?
You could use bash to prompt for the password, and then supply it to each of the mysql commands:
#!/bin/bash
echo "enter the password for MySQL:"
read -s PASSWD
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query1" > file1
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query2" > file2
mysql -h database.com -u user -p$PASSWD -e "USE database; mysql query3" > file3
Here is a POSIX-compliant version of silent prompting for non-bash shells:
stty -echo
printf "enter the password for MySQL:"
read PASSWD
stty echo
printf "\n"

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.

need to "build" a mysql command and exec it in a shell script

I'm new to writing shell scripts.
I am attempting to create a database using a shell script. Here's the script:
#!/bin/bash
#create a new db
a="mysql -uuser -ppassword -e'create database $1;'"
exec $a
The command exec mysql -uuser -ppassword -e'create database databaseName;' works in a shell, but when I sh the script, I get the mysql help open...
I think the problem is in the quotes, the simple quote prevent the variable expansion.
You can simply do like this in your script:
#!/bin/bash
#create a new db
mysql -u user -p password -e "create database $1;"
Or you can try to place all your mysql commands in a file, let's say "dbname.sql".
And do this:
#!/bin/bash
#create a new db
mysql -u user -p password "$1" < "$1.sql"
if you like use exec to run commands this can be a possible solution
#!/bin/bash
#create a new db
programm="mysql"
parameter[0]="-ppassword"
parameter[1]="-uuser"
parameter[2]="-ecreate database $1;"
exec "$programm" "${parameter[#]}"
exec parameter are
exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
command is $programm and the array parameter is the argument list.
Sounds like you need to use the 'cat' command and a pipe instead of using 'exec' .
cat /path/to/my/file | mysql -h localhost -u root -padmin