0

In a .bat file, I am trying to get the count from a database table and trying to assign it to a variable. But echo of the variable shows that the variable is getting assigned to the statement string value instead of the count. When I execute only the statement in command prompt I am able to see the count, which means the statement is correct. Need help in assigning this count to a variable.

Code Snippet:
SET x='mysql -uroot -pmysql -N testdatabase -e"select count(*) from    test"'
echo "x :" %x%

1 Answer 1

1

In a batch file it's not that simple I'm afraid. You will need to call this strange for syntax:

for /f "delims=" %%a in ('mysql -uroot -pmysql -N testdatabase -e"select count(*) from    test"') do @set x=%%a
echo "x :" %x%

Or you may try Powershell script file which looks much nicer:

$x = & mysql -uroot -pmysql -N testdatabase -e"select count(*) from    test"
"x : $x"
Sign up to request clarification or add additional context in comments.

2 Comments

I agree the batch file looks strange, hence I used cygwin utility to execute shell script in my windows machine using your second suggestion and it worked. Thanks :)
@DJones I'm glad it worked. If you are happy with the response please mark it as an answer. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.