Unable to copy files using xcopy sqlcmd - sqlcmd

I am copying files from one server to another through sqlcmd. I am able to get the output of the select statement, but am unable to copy. I am unable to find the syntax for copying files through sqlcmd xcopy.
H:\>sqlcmd -S cx-siscsqltest\sqlinst -Q "xcopy d:\sysdba\ e:\"
Msg 102, Level 15, State 1, Server CX-SISCSQLTEST\SQLINST, Line 1
Incorrect syntax near '\'.
I am able to get output of following:
H:\>sqlcmd -S cx-siscsqltest\sqlinst -Q "select * from sysaltfiles"
What is the xcopy sqlcmd syntax for copying files?

Related

Get Mysql query file execution result saved in a file using windows CMD

I need to get the result of mysql query execution results saved into a txt file.
The file contains over 600000 lines of insert statements.
The command that I used is:
mysql -h10.100.109.123 -f -uUserName -pPassword
--database=databaseName < AFC.sql > abc.txt
Abc.txt gets created but there is no data in it. From console, I can see there are many errors. -f is used so that it wont stop on error.
Have gone through and tried out many suggestions of similar question. But none of them worked.
MySql version 5.7

How do I pass a numeric value to a mysql script

How do I pass a parameter (numeric) from the command line to MySQL query file?
mysql --host=<hostname> --user=<username> -p --port=#### < Query.sql > Output.csv
You probably need to transform your Query.sql file, then pass it to mysql. The mysql program itself doesn't have any parameter substitution capability.
For example, in linux or freebsd.
cat 'SELECT #numericParameter := 42;' >/tmp/param$$
cat /tmp/param$$ Query.sql | mysql --host=<hostname> --user=<username> -p --port=#### > Output.csv
rm /tmp/param$$
The first line of this shell script writes one line of SQL, setting a parameter, into a temp file.
The second line sticks that one line at the beginning of your SQL file and pipes the result to the mysql client program.
The third line deletes the temp file.
This leaves your SQL file unmodified when it's done. That's probably what you want.

Executing Mass MySQL database restore from batch file using PowerShell

So what I have is about 80-90 MySQL DBs to restore and using < gives the 'saved for future use' error when used directly through PowerShell. The next step I tried was using just straight & cmd /c which also prompted the same error.
I tried the code below, but I get either Access Denied or 'system can't find the file specified' and I'm trying to figure out where it's messing up. Any help is definitely appreciated.
PowerShell
$list= gci "C:\Scripts\Migration\mysql\" -name
$mysqlbinpath="C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql"
$mysqluser="admin"
$mysqlpass=getpass-mysql
pushd "C:\Program Files\MySQL\MySQL Server 5.6\bin"
foreach($file in $list){
$dbname=$file.Substring(0, $file.LastIndexOf('.'))
$location='C:\Scripts\Migration\mysql\'+$file
& $bt $mysqluser, $mysqlpass, $dbname, $location
write-host "$file restored"
}
The batch file called by $bt
Batch
"cmd /c "mysql" -u %1 -p%2 --binary-mode=1 %3 < %4"
getpass-mysql is a function I have that pulls the MySQL root pass (our root user is 'admin') as a string.
I hope this isn't a duplicate as I checked other posts first and tried those solutions (perhaps incorrectly applied) and they didn't seem to help.
I have updated the code block with the changes I have made. I am currently receiving the error about using the right syntax near ' ■-'.

sqlcmd runs into error while parsing my .sql file

I am invoking sqlcmd as follows:
sqlcmd -S SERVERNAME\SQLEXPRESS -i "C:\Documents and Settings\user\Desktop\data\bigDB.sql"
And I get an error while the script is executing:
Sqlcmd: Error: Syntax error at line 86626 near command '%' in file 'C:\Documents
and Settings\user\Desktop\data\bigDB.sql'.
How do I tell sqlcmd to ignore the current record and keep running? I think i could edit bigDB.sql and include an :On Error Ignore directive at the top - but the bigDB.sql file is 5GB is size - i cant edit.
Or How could I go to the specific line and delete out the bad record? Thanks for helping.

write results of sql query to a file in mysql

I'm trying to write the results of a query to a file using mysql. I've seen some information on the outfile construct in a few places but it seems that this only writes the file to the machine that MySQL is running on (in this case a remote machine, i.e. the database is not on my local machine).
Alternatively, I've also tried to run the query and grab (copy/paste) the results from the mysql workbench results window. This worked for some of the smaller datasets, but the largest of the datasets seems to be too big and causing an out of memory exception/bug/crash.
Any help on this matter would be greatly appreciated.
You could try executing the query from the your local cli and redirect the output to a local file destination;
mysql -user -pass -e"select cols from table where cols not null" > /tmp/output
This is dependent on the SQL client you're using to interact with the database. For example, you could use the mysql command line interface in conjunction with the "tee" operator to output to a local file:
http://dev.mysql.com/doc/refman/5.1/en/mysql-commands.html
tee [file_name], \T [file_name]
Execute the command above before executing the SQL and the result of the query will be output to the file.
Specifically for MySQL Workbench, here's an article on Execute Query to Text Output. Although I don't see any documentation, there are indications that there should be also be an "Export" option under Query, though that is almost certainly version dependent.
You could try this, if you want to write MySQL query result in a file.
This example write the MySQL query result into a csv file with comma separated format
SELECT id,name,email FROM customers
INTO OUTFILE '/tmp/customers.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
If you are running mysql queries on the command line. Here I suppose you have the list of queries in a text file and you want the output in another text file. Then you can use this. [ test_2 is the database name ]
COMMAND 1
mysql -vv -u root -p test_2 < query.txt > /root/results.txt 2>&1
Where -vv is for the verbose output.
If you use the above statement as
COMMAND 2
mysql -vv -u root -p test_2 < query.txt 2>&1 > /root/results.txt
It will redirect STDERR to normal location (i.e on the terminal) and STDOUT to the output file which in my case is results.txt
The first command executes the query.txt until is faces an error and stops there.
That's how the redirection works. You can try
#ls key.pem asdf > /tmp/output_1 2>&1 /tmp/output_2
Here key.pm file exists and asdf doesn't exists. So when you cat the files you get the following
# cat /tmp/output_1
key.pem
#cat /tmp/output_2
ls: cannot access asdf: No such file or directory
But if you modify the previous statement with this
ls key.pem asdf > /tmp/output_1 > /tmp/output_2 2>&1
Then you get the both error and output in output_2
cat /tmp/output_2
ls: cannot access asdf: No such file or directory
key.pem
mysql -v -u -c root -p < /media/sf_Share/Solution2.sql 2>&1 > /media/sf_Share/results.txt
This worked for me. Since I wanted the comments in my script also to be reflected in the report I added a flag -c