Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can .bat file execute an sql query and return a value?

How can I call a query from a .bat file? (say my query is: select version from system).

Can my .bat file save the output that this query returns? I wanna use this output in my NSIS script.

like image 702
Pia Avatar asked Jan 12 '10 09:01

Pia


People also ask

Can we do SQL in CMD?

Overview of SQL Command Line SQL Command Line (SQL*Plus) is a command-line tool for accessing Oracle Database XE. It enables you to enter and run SQL, PL/SQL, and SQL*Plus commands and statements to: Query, insert, and update data.

Can you run a SQL script from command line?

Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.


2 Answers

For Oracle:

How can I issue a single command from the command line through sql plus?

@echo select version from system; | sqlplus username/password@database 

You can either pipe the output to a file and use that, or wrap this in a for command to parse the output within your batch file.

like image 129
Patrick Cuff Avatar answered Sep 23 '22 19:09

Patrick Cuff


EDIT: This answer is for SQL Server, not Oracle. (It wasn't immediately clear when the question was posted).

You may want to check the sqlcmd utility.

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt. The following example executes a query when sqlcmd starts and then exits immediately. Multiple-semicolon-delimited queries can be executed:

sqlcmd -d AdventureWorks -Q "SELECT FirstName, LastName FROM Person.Contact WHERE LastName LIKE 'Whi%';"

To save the output of the query to a file, you can use the -o C:\< filename> option.

like image 44
Daniel Vassallo Avatar answered Sep 26 '22 19:09

Daniel Vassallo